Add warning flags to File deletion functions

Adds a flag to File::Delete and File::DeleteDir functions to control
whether a console warning is emitted when the file or directory doesn't
exist. The flag is optional and true by default to match current behavior.
This commit is contained in:
Dentomologist 2020-12-03 13:13:16 -08:00
parent c434eefe94
commit 4a55511e18
2 changed files with 20 additions and 6 deletions

View File

@ -135,7 +135,7 @@ bool IsFile(const std::string& path)
// Deletes a given filename, return true on success // Deletes a given filename, return true on success
// Doesn't supports deleting a directory // Doesn't supports deleting a directory
bool Delete(const std::string& filename) bool Delete(const std::string& filename, IfAbsentBehavior behavior)
{ {
INFO_LOG_FMT(COMMON, "Delete: file {}", filename); INFO_LOG_FMT(COMMON, "Delete: file {}", filename);
@ -154,7 +154,10 @@ bool Delete(const std::string& filename)
// Return true because we care about the file not being there, not the actual delete. // Return true because we care about the file not being there, not the actual delete.
if (!file_info.Exists()) if (!file_info.Exists())
{ {
WARN_LOG_FMT(COMMON, "Delete: {} does not exist", filename); if (behavior == IfAbsentBehavior::ConsoleWarning)
{
WARN_LOG_FMT(COMMON, "Delete: {} does not exist", filename);
}
return true; return true;
} }
@ -253,14 +256,17 @@ bool CreateFullPath(const std::string& fullPath)
} }
// Deletes a directory filename, returns true on success // Deletes a directory filename, returns true on success
bool DeleteDir(const std::string& filename) bool DeleteDir(const std::string& filename, IfAbsentBehavior behavior)
{ {
INFO_LOG_FMT(COMMON, "DeleteDir: directory {}", filename); INFO_LOG_FMT(COMMON, "DeleteDir: directory {}", filename);
// Return true because we care about the directory not being there, not the actual delete. // Return true because we care about the directory not being there, not the actual delete.
if (!File::Exists(filename)) if (!File::Exists(filename))
{ {
WARN_LOG_FMT(COMMON, "DeleteDir: {} does not exist", filename); if (behavior == IfAbsentBehavior::ConsoleWarning)
{
WARN_LOG_FMT(COMMON, "DeleteDir: {} does not exist", filename);
}
return true; return true;
} }

View File

@ -136,12 +136,20 @@ bool CreateDir(const std::string& filename);
// Creates the full path of fullPath returns true on success // Creates the full path of fullPath returns true on success
bool CreateFullPath(const std::string& fullPath); bool CreateFullPath(const std::string& fullPath);
enum class IfAbsentBehavior
{
ConsoleWarning,
NoConsoleWarning
};
// Deletes a given filename, return true on success // Deletes a given filename, return true on success
// Doesn't supports deleting a directory // Doesn't supports deleting a directory
bool Delete(const std::string& filename); bool Delete(const std::string& filename,
IfAbsentBehavior behavior = IfAbsentBehavior::ConsoleWarning);
// Deletes a directory filename, returns true on success // Deletes a directory filename, returns true on success
bool DeleteDir(const std::string& filename); bool DeleteDir(const std::string& filename,
IfAbsentBehavior behavior = IfAbsentBehavior::ConsoleWarning);
// renames file srcFilename to destFilename, returns true on success // renames file srcFilename to destFilename, returns true on success
bool Rename(const std::string& srcFilename, const std::string& destFilename); bool Rename(const std::string& srcFilename, const std::string& destFilename);