From ea0e6d98248a5bc1a85e7707f3d69d0555011068 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Mon, 15 Aug 2016 22:25:15 +0200 Subject: [PATCH] fs/FileSystem: RemoveFile() throws exception on error --- src/PlaylistFile.cxx | 11 +++++++++-- src/event/ServerSocket.cxx | 2 +- src/fs/FileSystem.cxx | 12 ++++++++++++ src/fs/FileSystem.hxx | 14 ++++---------- src/fs/io/FileOutputStream.cxx | 16 +++++++++++++--- src/output/plugins/FifoOutputPlugin.cxx | 8 ++++---- src/unix/Daemon.cxx | 5 ++++- src/unix/PidFile.hxx | 2 +- 8 files changed, 48 insertions(+), 22 deletions(-) diff --git a/src/PlaylistFile.cxx b/src/PlaylistFile.cxx index 0afebba32..9c73a6030 100644 --- a/src/PlaylistFile.cxx +++ b/src/PlaylistFile.cxx @@ -321,8 +321,15 @@ spl_delete(const char *name_utf8) const auto path_fs = spl_map_to_fs(name_utf8); assert(!path_fs.IsNull()); - if (!RemoveFile(path_fs)) - ThrowPlaylistErrno(); + try { + RemoveFile(path_fs); + } catch (const std::system_error &e) { + if (IsFileNotFound(e)) + throw PlaylistError(PlaylistResult::NO_SUCH_LIST, + "No such playlist"); + else + throw; + } idle_add(IDLE_STORED_PLAYLIST); } diff --git a/src/event/ServerSocket.cxx b/src/event/ServerSocket.cxx index 608ee65e2..3f34a5736 100644 --- a/src/event/ServerSocket.cxx +++ b/src/event/ServerSocket.cxx @@ -426,7 +426,7 @@ ServerSocket::AddPath(AllocatedPath &&path, Error &error) #ifdef HAVE_UN (void)error; - RemoveFile(path); + unlink(path.c_str()); AllocatedSocketAddress address; address.SetLocal(path.c_str()); diff --git a/src/fs/FileSystem.cxx b/src/fs/FileSystem.cxx index b2d4c69da..49a1072d7 100644 --- a/src/fs/FileSystem.cxx +++ b/src/fs/FileSystem.cxx @@ -66,3 +66,15 @@ TruncateFile(Path path) close(fd); #endif } + +void +RemoveFile(Path path) +{ +#ifdef WIN32 + if (!DeleteFile(path.c_str())) + throw FormatLastError("Failed to delete %s", path.c_str()); +#else + if (unlink(path.c_str()) < 0) + throw FormatErrno("Failed to delete %s", path.c_str()); +#endif +} diff --git a/src/fs/FileSystem.hxx b/src/fs/FileSystem.hxx index 76f8bc40a..83261a8ec 100644 --- a/src/fs/FileSystem.hxx +++ b/src/fs/FileSystem.hxx @@ -100,17 +100,11 @@ void TruncateFile(Path path); /** - * Wrapper for unlink() that uses #Path names. + * Wrapper for unlink() that uses #Path names. Throws + * std::system_error on error. */ -static inline bool -RemoveFile(Path file) -{ -#ifdef WIN32 - return _tunlink(file.c_str()) == 0; -#else - return unlink(file.c_str()) == 0; -#endif -} +void +RemoveFile(Path path); /** * Wrapper for readlink() that uses #Path names. diff --git a/src/fs/io/FileOutputStream.cxx b/src/fs/io/FileOutputStream.cxx index 852984d40..fafde30da 100644 --- a/src/fs/io/FileOutputStream.cxx +++ b/src/fs/io/FileOutputStream.cxx @@ -76,7 +76,11 @@ FileOutputStream::Cancel() assert(IsDefined()); Close(); - RemoveFile(GetPath()); + + try { + RemoveFile(GetPath()); + } catch (std::runtime_error) { + } } #else @@ -153,7 +157,10 @@ FileOutputStream::Commit() #if HAVE_LINKAT if (is_tmpfile) { - RemoveFile(GetPath()); + try { + RemoveFile(GetPath()); + } catch (std::runtime_error) { + } /* hard-link the temporary file to the final path */ char fd_path[64]; @@ -186,7 +193,10 @@ FileOutputStream::Cancel() #ifdef HAVE_LINKAT if (!is_tmpfile) #endif - RemoveFile(GetPath()); + try { + RemoveFile(GetPath()); + } catch (std::runtime_error) { + } } #endif diff --git a/src/output/plugins/FifoOutputPlugin.cxx b/src/output/plugins/FifoOutputPlugin.cxx index ccd1d6176..79e9f2349 100644 --- a/src/output/plugins/FifoOutputPlugin.cxx +++ b/src/output/plugins/FifoOutputPlugin.cxx @@ -87,10 +87,10 @@ FifoOutput::Delete() FormatDebug(fifo_output_domain, "Removing FIFO \"%s\"", path_utf8.c_str()); - if (!RemoveFile(path)) { - FormatErrno(fifo_output_domain, - "Could not remove FIFO \"%s\"", - path_utf8.c_str()); + try { + RemoveFile(path); + } catch (const std::runtime_error &e) { + LogError(e, "Could not remove FIFO"); return; } diff --git a/src/unix/Daemon.cxx b/src/unix/Daemon.cxx index 598446c3d..a2960dd1f 100644 --- a/src/unix/Daemon.cxx +++ b/src/unix/Daemon.cxx @@ -22,7 +22,10 @@ #include "system/FatalError.hxx" #include "fs/AllocatedPath.hxx" #include "fs/FileSystem.hxx" + +#ifndef WIN32 #include "PidFile.hxx" +#endif #include #include @@ -247,7 +250,7 @@ void daemonize_finish(void) { if (!pidfile.IsNull()) { - RemoveFile(pidfile); + unlink(pidfile.c_str()); pidfile = AllocatedPath::Null(); } diff --git a/src/unix/PidFile.hxx b/src/unix/PidFile.hxx index d1d109fab..850b407b2 100644 --- a/src/unix/PidFile.hxx +++ b/src/unix/PidFile.hxx @@ -64,7 +64,7 @@ public: assert(!path.IsNull()); close(fd); - RemoveFile(path); + unlink(path.c_str()); } void Write(pid_t pid) {