fs/io/Reader: use C++ exceptions instead of class Error

This commit is contained in:
Max Kellermann
2015-12-16 11:05:33 +01:00
parent fe60c52c70
commit e6e7d6dbd6
33 changed files with 192 additions and 334 deletions

@@ -61,14 +61,10 @@ InputStream *
OpenFileInputStream(Path path,
Mutex &mutex, Cond &cond,
Error &error)
{
FileReader reader(path, error);
if (!reader.IsDefined())
return nullptr;
try {
FileReader reader(path);
FileInfo info;
if (!reader.GetFileInfo(info, error))
return nullptr;
const FileInfo info = reader.GetFileInfo();
if (!info.IsRegular()) {
error.Format(file_domain, "Not a regular file: %s",
@@ -84,6 +80,9 @@ OpenFileInputStream(Path path,
return new FileInputStream(path.ToUTF8().c_str(),
std::move(reader), info.GetSize(),
mutex, cond);
} catch (const std::exception &e) {
error.Set(e);
return nullptr;
}
static InputStream *
@@ -98,23 +97,24 @@ input_file_open(gcc_unused const char *filename,
bool
FileInputStream::Seek(offset_type new_offset, Error &error)
{
if (!reader.Seek((off_t)new_offset, error))
return false;
try {
reader.Seek((off_t)new_offset);
offset = new_offset;
return true;
} catch (const std::exception &e) {
error.Set(e);
return false;
}
size_t
FileInputStream::Read(void *ptr, size_t read_size, Error &error)
{
ssize_t nbytes = reader.Read(ptr, read_size, error);
if (nbytes < 0)
return 0;
try {
size_t nbytes = reader.Read(ptr, read_size);
offset += nbytes;
return (size_t)nbytes;
return nbytes;
} catch (const std::exception &e) {
error.Set(e);
return 0;
}
const InputPlugin input_plugin_file = {