fs/io/FileOutputStream: use C++ exceptions in Commit()

This commit is contained in:
Max Kellermann 2015-12-16 00:24:41 +01:00
parent 24b2198668
commit 7eae3bc8c5
8 changed files with 61 additions and 44 deletions

View File

@ -239,7 +239,11 @@ SavePlaylistFile(const PlaylistFileContents &contents, const char *utf8path,
for (const auto &uri_utf8 : contents)
playlist_print_uri(bos, uri_utf8.c_str());
return bos.Flush(error) && fos.Commit(error);
if (!bos.Flush(error))
return false;
fos.Commit();
return true;
}
PlaylistFileContents
@ -411,9 +415,11 @@ spl_append_song(const char *utf8path, const DetachedSong &song, Error &error)
playlist_print_song(bos, song);
if (!bos.Flush(error) || !fos.Commit(error))
if (!bos.Flush(error))
return false;
fos.Commit();
idle_add(IDLE_STORED_PLAYLIST);
return true;
}

View File

@ -86,9 +86,11 @@ spl_save_queue(const char *name_utf8, const Queue &queue, Error &error)
for (unsigned i = 0; i < queue.GetLength(); i++)
playlist_print_song(bos, queue.Get(i));
if (!bos.Flush(error) || !fos.Commit(error))
if (!bos.Flush(error))
return false;
fos.Commit();
idle_add(IDLE_STORED_PLAYLIST);
return true;
}

View File

@ -92,10 +92,12 @@ StateFile::Write()
try {
Error error;
FileOutputStream fos(path);
if (!Write(fos, error) || !fos.Commit(error)) {
if (!Write(fos, error)) {
LogError(error);
return;
}
fos.Commit();
} catch (const std::exception &e) {
LogError(e);
}

View File

@ -411,8 +411,7 @@ SimpleDatabase::Save(Error &error)
}
#endif
if (!fos.Commit(error))
return false;
fos.Commit();
FileInfo fi;
if (GetFileInfo(path, fi))

View File

@ -72,13 +72,12 @@ BaseFileOutputStream::Write(const void *data, size_t size, Error &error)
return true;
}
bool
FileOutputStream::Commit(gcc_unused Error &error)
void
FileOutputStream::Commit()
{
assert(IsDefined());
Close();
return true;
}
void
@ -162,8 +161,8 @@ BaseFileOutputStream::Write(const void *data, size_t size, Error &error)
return true;
}
bool
FileOutputStream::Commit(Error &error)
void
FileOutputStream::Commit()
{
assert(IsDefined());
@ -176,20 +175,20 @@ FileOutputStream::Commit(Error &error)
snprintf(fd_path, sizeof(fd_path), "/proc/self/fd/%d",
GetFD().Get());
if (linkat(AT_FDCWD, fd_path, AT_FDCWD, GetPath().c_str(),
AT_SYMLINK_FOLLOW) < 0) {
error.FormatErrno("Failed to commit %s",
AT_SYMLINK_FOLLOW) < 0)
throw FormatErrno("Failed to commit %s",
GetPath().c_str());
Close();
return false;
}
}
#endif
bool success = Close();
if (!success)
error.FormatErrno("Failed to commit %s", GetPath().c_str());
return success;
if (!Close()) {
#ifdef WIN32
throw FormatLastError("Failed to commit %s",
GetPath().ToUTF8().c_str());
#else
throw FormatErrno("Failed to commit %s", GetPath().c_str());
#endif
}
}
void
@ -233,18 +232,17 @@ AppendFileOutputStream::AppendFileOutputStream(Path _path)
#endif
}
bool
AppendFileOutputStream::Commit(gcc_unused Error &error)
void
AppendFileOutputStream::Commit()
{
assert(IsDefined());
if (!Close()) {
#ifdef WIN32
return Close();
throw FormatLastError("Failed to commit %s",
GetPath().ToUTF8().c_str());
#else
bool success = Close();
if (!success)
error.FormatErrno("Failed to commit %s", GetPath().c_str());
return success;
throw FormatErrno("Failed to commit %s", GetPath().c_str());
#endif
}
}

View File

@ -139,7 +139,7 @@ public:
Cancel();
}
bool Commit(Error &error);
void Commit();
void Cancel();
};
@ -152,7 +152,7 @@ public:
Close();
}
bool Commit(Error &error);
void Commit();
};
#endif

View File

@ -244,8 +244,14 @@ RecorderOutput::Commit(Error &error)
encoder->Close();
if (success && !file->Commit(error))
success = false;
if (success) {
try {
file->Commit();
} catch (...) {
delete file;
throw;
}
}
delete file;
@ -263,9 +269,13 @@ RecorderOutput::Close()
return;
}
Error error;
if (!Commit(error))
LogError(error);
try {
Error error;
if (!Commit(error))
LogError(error);
} catch (const std::exception &e) {
LogError(e);
}
if (HasDynamicPath()) {
assert(!path.IsNull());
@ -281,9 +291,13 @@ RecorderOutput::FinishFormat()
if (file == nullptr)
return;
Error error;
if (!Commit(error))
LogError(error);
try {
Error error;
if (!Commit(error))
LogError(error);
} catch (const std::exception &e) {
LogError(e);
}
file = nullptr;
path.SetNull();

View File

@ -61,16 +61,12 @@ main(int argc, char **argv)
const Path path = Path::FromFS(argv[1]);
try {
Error error;
FileOutputStream fos(path);
if (!Copy(fos, STDIN_FILENO))
return EXIT_FAILURE;
if (!fos.Commit(error)) {
fprintf(stderr, "%s\n", error.GetMessage());
return EXIT_FAILURE;
}
fos.Commit();
return EXIT_SUCCESS;
} catch (const std::exception &e) {