From aac0f40d2cc37a2d5d3f3dec558d3e66f7fee308 Mon Sep 17 00:00:00 2001 From: Thierry Crozat Date: Mon, 4 Oct 2021 21:12:01 +0100 Subject: [PATCH] BACKENDS: Add overloadable removeFile in DefaultSaveFileManager This allows using the DefaultSaveFileManager on system where remove() does not exist. See bug #12975. --- backends/saves/default/default-saves.cpp | 28 +++++++++++++----------- backends/saves/default/default-saves.h | 6 +++++ 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/backends/saves/default/default-saves.cpp b/backends/saves/default/default-saves.cpp index d6129a224cb..e84015c7f78 100644 --- a/backends/saves/default/default-saves.cpp +++ b/backends/saves/default/default-saves.cpp @@ -205,23 +205,25 @@ bool DefaultSaveFileManager::removeSavefile(const Common::String &filename) { _saveFileCache.erase(file); file = _saveFileCache.end(); - // FIXME: remove does not exist on all systems. If your port fails to - // compile because of this, please let us know (scummvm-devel). - // There is a nicely portable workaround, too: Make this method overloadable. - if (remove(fileNode.getPath().c_str()) != 0) { - if (errno == EACCES) - setError(Common::kWritePermissionDenied, "Search or write permission denied: "+fileNode.getName()); - - if (errno == ENOENT) - setError(Common::kPathDoesNotExist, "removeSavefile: '"+fileNode.getName()+"' does not exist or path is invalid"); - - return false; - } else { + Common::ErrorCode result = removeFile(fileNode.getPath()); + if (result == Common::kNoError) return true; - } + Common::Error error(result); + setError(error, "Failed to remove savefile '" + fileNode.getName() + "': " + error.getDesc()); + return false; } } +Common::ErrorCode DefaultSaveFileManager::removeFile(const Common::String &filepath) { + if (remove(filepath.c_str()) == 0) + return Common::kNoError; + if (errno == EACCES) + return Common::kWritePermissionDenied; + if (errno == ENOENT) + return Common::kPathDoesNotExist; + return Common::kUnknownError; +} + bool DefaultSaveFileManager::exists(const Common::String &filename) { // Assure the savefile name cache is up-to-date. assureCached(getSavePath()); diff --git a/backends/saves/default/default-saves.h b/backends/saves/default/default-saves.h index 0557ffdc383..0b3ec657ceb 100644 --- a/backends/saves/default/default-saves.h +++ b/backends/saves/default/default-saves.h @@ -71,6 +71,12 @@ protected: */ virtual void checkPath(const Common::FSNode &dir); + /** + * Removes the given file. + * This is called from removeSavefile() with the full file path. + */ + virtual Common::ErrorCode removeFile(const Common::String &filepath); + /** * Assure that the given save path is cached. *