fs/io/FileOutputStream: use C++ exceptions in Commit()
This commit is contained in:
		| @@ -239,7 +239,11 @@ SavePlaylistFile(const PlaylistFileContents &contents, const char *utf8path, | |||||||
| 	for (const auto &uri_utf8 : contents) | 	for (const auto &uri_utf8 : contents) | ||||||
| 		playlist_print_uri(bos, uri_utf8.c_str()); | 		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 | PlaylistFileContents | ||||||
| @@ -411,9 +415,11 @@ spl_append_song(const char *utf8path, const DetachedSong &song, Error &error) | |||||||
|  |  | ||||||
| 	playlist_print_song(bos, song); | 	playlist_print_song(bos, song); | ||||||
|  |  | ||||||
| 	if (!bos.Flush(error) || !fos.Commit(error)) | 	if (!bos.Flush(error)) | ||||||
| 		return false; | 		return false; | ||||||
|  |  | ||||||
|  | 	fos.Commit(); | ||||||
|  |  | ||||||
| 	idle_add(IDLE_STORED_PLAYLIST); | 	idle_add(IDLE_STORED_PLAYLIST); | ||||||
| 	return true; | 	return true; | ||||||
| } | } | ||||||
|   | |||||||
| @@ -86,9 +86,11 @@ spl_save_queue(const char *name_utf8, const Queue &queue, Error &error) | |||||||
| 	for (unsigned i = 0; i < queue.GetLength(); i++) | 	for (unsigned i = 0; i < queue.GetLength(); i++) | ||||||
| 		playlist_print_song(bos, queue.Get(i)); | 		playlist_print_song(bos, queue.Get(i)); | ||||||
|  |  | ||||||
| 	if (!bos.Flush(error) || !fos.Commit(error)) | 	if (!bos.Flush(error)) | ||||||
| 		return false; | 		return false; | ||||||
|  |  | ||||||
|  | 	fos.Commit(); | ||||||
|  |  | ||||||
| 	idle_add(IDLE_STORED_PLAYLIST); | 	idle_add(IDLE_STORED_PLAYLIST); | ||||||
| 	return true; | 	return true; | ||||||
| } | } | ||||||
|   | |||||||
| @@ -92,10 +92,12 @@ StateFile::Write() | |||||||
| 	try { | 	try { | ||||||
| 		Error error; | 		Error error; | ||||||
| 		FileOutputStream fos(path); | 		FileOutputStream fos(path); | ||||||
| 		if (!Write(fos, error) || !fos.Commit(error)) { | 		if (!Write(fos, error)) { | ||||||
| 			LogError(error); | 			LogError(error); | ||||||
| 			return; | 			return; | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
|  | 		fos.Commit(); | ||||||
| 	} catch (const std::exception &e) { | 	} catch (const std::exception &e) { | ||||||
| 		LogError(e); | 		LogError(e); | ||||||
| 	} | 	} | ||||||
|   | |||||||
| @@ -411,8 +411,7 @@ SimpleDatabase::Save(Error &error) | |||||||
| 	} | 	} | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
| 	if (!fos.Commit(error)) | 	fos.Commit(); | ||||||
| 		return false; |  | ||||||
|  |  | ||||||
| 	FileInfo fi; | 	FileInfo fi; | ||||||
| 	if (GetFileInfo(path, fi)) | 	if (GetFileInfo(path, fi)) | ||||||
|   | |||||||
| @@ -72,13 +72,12 @@ BaseFileOutputStream::Write(const void *data, size_t size, Error &error) | |||||||
| 	return true; | 	return true; | ||||||
| } | } | ||||||
|  |  | ||||||
| bool | void | ||||||
| FileOutputStream::Commit(gcc_unused Error &error) | FileOutputStream::Commit() | ||||||
| { | { | ||||||
| 	assert(IsDefined()); | 	assert(IsDefined()); | ||||||
|  |  | ||||||
| 	Close(); | 	Close(); | ||||||
| 	return true; |  | ||||||
| } | } | ||||||
|  |  | ||||||
| void | void | ||||||
| @@ -162,8 +161,8 @@ BaseFileOutputStream::Write(const void *data, size_t size, Error &error) | |||||||
| 	return true; | 	return true; | ||||||
| } | } | ||||||
|  |  | ||||||
| bool | void | ||||||
| FileOutputStream::Commit(Error &error) | FileOutputStream::Commit() | ||||||
| { | { | ||||||
| 	assert(IsDefined()); | 	assert(IsDefined()); | ||||||
|  |  | ||||||
| @@ -176,20 +175,20 @@ FileOutputStream::Commit(Error &error) | |||||||
| 		snprintf(fd_path, sizeof(fd_path), "/proc/self/fd/%d", | 		snprintf(fd_path, sizeof(fd_path), "/proc/self/fd/%d", | ||||||
| 			 GetFD().Get()); | 			 GetFD().Get()); | ||||||
| 		if (linkat(AT_FDCWD, fd_path, AT_FDCWD, GetPath().c_str(), | 		if (linkat(AT_FDCWD, fd_path, AT_FDCWD, GetPath().c_str(), | ||||||
| 			   AT_SYMLINK_FOLLOW) < 0) { | 			   AT_SYMLINK_FOLLOW) < 0) | ||||||
| 			error.FormatErrno("Failed to commit %s", | 			throw FormatErrno("Failed to commit %s", | ||||||
| 					  GetPath().c_str()); | 					  GetPath().c_str()); | ||||||
| 			Close(); |  | ||||||
| 			return false; |  | ||||||
| 		} |  | ||||||
| 	} | 	} | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
| 	bool success = Close(); | 	if (!Close()) { | ||||||
| 	if (!success) | #ifdef WIN32 | ||||||
| 		error.FormatErrno("Failed to commit %s", GetPath().c_str()); | 		throw FormatLastError("Failed to commit %s", | ||||||
|  | 				      GetPath().ToUTF8().c_str()); | ||||||
| 	return success; | #else | ||||||
|  | 		throw FormatErrno("Failed to commit %s", GetPath().c_str()); | ||||||
|  | #endif | ||||||
|  | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
| void | void | ||||||
| @@ -233,18 +232,17 @@ AppendFileOutputStream::AppendFileOutputStream(Path _path) | |||||||
| #endif | #endif | ||||||
| } | } | ||||||
|  |  | ||||||
| bool | void | ||||||
| AppendFileOutputStream::Commit(gcc_unused Error &error) | AppendFileOutputStream::Commit() | ||||||
| { | { | ||||||
| 	assert(IsDefined()); | 	assert(IsDefined()); | ||||||
|  |  | ||||||
|  | 	if (!Close()) { | ||||||
| #ifdef WIN32 | #ifdef WIN32 | ||||||
| 	return Close(); | 		throw FormatLastError("Failed to commit %s", | ||||||
|  | 				      GetPath().ToUTF8().c_str()); | ||||||
| #else | #else | ||||||
| 	bool success = Close(); | 		throw FormatErrno("Failed to commit %s", GetPath().c_str()); | ||||||
| 	if (!success) |  | ||||||
| 		error.FormatErrno("Failed to commit %s", GetPath().c_str()); |  | ||||||
|  |  | ||||||
| 	return success; |  | ||||||
| #endif | #endif | ||||||
| 	} | 	} | ||||||
|  | } | ||||||
|   | |||||||
| @@ -139,7 +139,7 @@ public: | |||||||
| 			Cancel(); | 			Cancel(); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	bool Commit(Error &error); | 	void Commit(); | ||||||
| 	void Cancel(); | 	void Cancel(); | ||||||
| }; | }; | ||||||
|  |  | ||||||
| @@ -152,7 +152,7 @@ public: | |||||||
| 			Close(); | 			Close(); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	bool Commit(Error &error); | 	void Commit(); | ||||||
| }; | }; | ||||||
|  |  | ||||||
| #endif | #endif | ||||||
|   | |||||||
| @@ -244,8 +244,14 @@ RecorderOutput::Commit(Error &error) | |||||||
|  |  | ||||||
| 	encoder->Close(); | 	encoder->Close(); | ||||||
|  |  | ||||||
| 	if (success && !file->Commit(error)) | 	if (success) { | ||||||
| 		success = false; | 		try { | ||||||
|  | 			file->Commit(); | ||||||
|  | 		} catch (...) { | ||||||
|  | 			delete file; | ||||||
|  | 			throw; | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	delete file; | 	delete file; | ||||||
|  |  | ||||||
| @@ -263,9 +269,13 @@ RecorderOutput::Close() | |||||||
| 		return; | 		return; | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	try { | ||||||
| 		Error error; | 		Error error; | ||||||
| 		if (!Commit(error)) | 		if (!Commit(error)) | ||||||
| 			LogError(error); | 			LogError(error); | ||||||
|  | 	} catch (const std::exception &e) { | ||||||
|  | 		LogError(e); | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	if (HasDynamicPath()) { | 	if (HasDynamicPath()) { | ||||||
| 		assert(!path.IsNull()); | 		assert(!path.IsNull()); | ||||||
| @@ -281,9 +291,13 @@ RecorderOutput::FinishFormat() | |||||||
| 	if (file == nullptr) | 	if (file == nullptr) | ||||||
| 		return; | 		return; | ||||||
|  |  | ||||||
|  | 	try { | ||||||
| 		Error error; | 		Error error; | ||||||
| 		if (!Commit(error)) | 		if (!Commit(error)) | ||||||
| 			LogError(error); | 			LogError(error); | ||||||
|  | 	} catch (const std::exception &e) { | ||||||
|  | 		LogError(e); | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	file = nullptr; | 	file = nullptr; | ||||||
| 	path.SetNull(); | 	path.SetNull(); | ||||||
|   | |||||||
| @@ -61,16 +61,12 @@ main(int argc, char **argv) | |||||||
| 	const Path path = Path::FromFS(argv[1]); | 	const Path path = Path::FromFS(argv[1]); | ||||||
|  |  | ||||||
| 	try { | 	try { | ||||||
| 		Error error; |  | ||||||
| 		FileOutputStream fos(path); | 		FileOutputStream fos(path); | ||||||
|  |  | ||||||
| 		if (!Copy(fos, STDIN_FILENO)) | 		if (!Copy(fos, STDIN_FILENO)) | ||||||
| 			return EXIT_FAILURE; | 			return EXIT_FAILURE; | ||||||
|  |  | ||||||
| 		if (!fos.Commit(error)) { | 		fos.Commit(); | ||||||
| 			fprintf(stderr, "%s\n", error.GetMessage()); |  | ||||||
| 			return EXIT_FAILURE; |  | ||||||
| 		} |  | ||||||
|  |  | ||||||
| 		return EXIT_SUCCESS; | 		return EXIT_SUCCESS; | ||||||
| 	} catch (const std::exception &e) { | 	} catch (const std::exception &e) { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Max Kellermann
					Max Kellermann