Compare commits

...

2 Commits

Author SHA1 Message Date
ameerj
3d9d0cbd26 common/fs/file: Update documentation for {Append/Write}StringToFile 2021-06-19 08:55:19 -04:00
ameerj
371720818e common/fs: Fix file write/append access when file does not exist.
This fixes errors when a file is being opened for write/append access, but the file does not exist initially.

These functions were not respecting the FileAccessMode which specify that the file will be created if it does not exist.
2021-06-18 19:23:18 -04:00
3 changed files with 9 additions and 13 deletions

View File

@@ -172,23 +172,13 @@ std::string ReadStringFromFile(const std::filesystem::path& path, FileType type)
size_t WriteStringToFile(const std::filesystem::path& path, FileType type,
std::string_view string) {
if (!IsFile(path)) {
return 0;
}
IOFile io_file{path, FileAccessMode::Write, type};
return io_file.WriteString(string);
}
size_t AppendStringToFile(const std::filesystem::path& path, FileType type,
std::string_view string) {
if (!IsFile(path)) {
return 0;
}
IOFile io_file{path, FileAccessMode::Append, type};
return io_file.WriteString(string);
}

View File

@@ -72,6 +72,7 @@ template <typename Path>
/**
* Writes a string to a file at path and returns the number of characters successfully written.
* If a file already exists at path, its contents will be erased.
* If a file does not exist at path, it creates and opens a new empty file for writing.
* If the filesystem object at path is not a file, this function returns 0.
*
* @param path Filesystem path
@@ -95,6 +96,7 @@ template <typename Path>
/**
* Appends a string to a file at path and returns the number of characters successfully written.
* If a file does not exist at path, it creates and opens a new empty file for appending.
* If the filesystem object at path is not a file, this function returns 0.
*
* @param path Filesystem path

View File

@@ -135,9 +135,13 @@ std::shared_ptr<IOFile> FileOpen(const fs::path& path, FileAccessMode mode, File
return nullptr;
}
if (!IsFile(path)) {
LOG_ERROR(Common_Filesystem, "Filesystem object at path={} is not a file",
PathToUTF8String(path));
const bool needs_existing_file =
mode == FileAccessMode::Read || mode == FileAccessMode::ReadWrite;
if (needs_existing_file && !IsFile(path)) {
LOG_ERROR(Common_Filesystem,
"Filesystem object at path={} is not a file."
"FileAccessMode={} requires a file to exist in order to be accessed",
PathToUTF8String(path), mode);
return nullptr;
}