2
1
Fork 0
mirror of https://github.com/yuzu-emu/yuzu.git synced 2024-07-04 23:31:19 +01:00

common: fs: Amend IsFile check in FileOpen / (Write/Append)StringToFile

This check was preventing files with the Write or Append file access modes from being created, as per the documented behavior in FileAccessMode.
This amends the check to test for the existence of a filesystem object prior to checking whether it is a regular file.
Thanks to liushuyu for pointing out that removing the check altogether would not guard against attempting to open non-regular files such as directories, symlinks, FIFO (pipes), sockets, block devices, or character devices.
The documentation has also been updated for these functions to clarify that a file refers to a regular file.
This commit is contained in:
Morph 2021-06-19 03:49:11 -04:00
parent cf0b9d1de2
commit 76b2313b25
4 changed files with 12 additions and 9 deletions

View file

@ -172,7 +172,7 @@ std::string ReadStringFromFile(const std::filesystem::path& path, FileType type)
size_t WriteStringToFile(const std::filesystem::path& path, FileType type, size_t WriteStringToFile(const std::filesystem::path& path, FileType type,
std::string_view string) { std::string_view string) {
if (!IsFile(path)) { if (Exists(path) && !IsFile(path)) {
return 0; return 0;
} }
@ -183,7 +183,7 @@ size_t WriteStringToFile(const std::filesystem::path& path, FileType type,
size_t AppendStringToFile(const std::filesystem::path& path, FileType type, size_t AppendStringToFile(const std::filesystem::path& path, FileType type,
std::string_view string) { std::string_view string) {
if (!IsFile(path)) { if (Exists(path) && !IsFile(path)) {
return 0; return 0;
} }

View file

@ -49,7 +49,7 @@ void OpenFileStream(FileStream& file_stream, const Path& path, std::ios_base::op
/** /**
* Reads an entire file at path and returns a string of the contents read from the file. * Reads an entire file at path and returns a string of the contents read from the file.
* If the filesystem object at path is not a file, this function returns an empty string. * If the filesystem object at path is not a regular file, this function returns an empty string.
* *
* @param path Filesystem path * @param path Filesystem path
* @param type File type * @param type File type
@ -72,7 +72,8 @@ template <typename Path>
/** /**
* Writes a string to a file at path and returns the number of characters successfully written. * 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 already exists at path, its contents will be erased.
* If the filesystem object at path is not a file, this function returns 0. * If a file does not exist at path, it creates and opens a new empty file for writing.
* If the filesystem object at path exists and is not a regular file, this function returns 0.
* *
* @param path Filesystem path * @param path Filesystem path
* @param type File type * @param type File type
@ -95,7 +96,8 @@ template <typename Path>
/** /**
* Appends a string to a file at path and returns the number of characters successfully written. * Appends a string to a file at path and returns the number of characters successfully written.
* If the filesystem object at path is not a file, this function returns 0. * If a file does not exist at path, it creates and opens a new empty file for appending.
* If the filesystem object at path exists and is not a regular file, this function returns 0.
* *
* @param path Filesystem path * @param path Filesystem path
* @param type File type * @param type File type

View file

@ -135,8 +135,9 @@ std::shared_ptr<IOFile> FileOpen(const fs::path& path, FileAccessMode mode, File
return nullptr; return nullptr;
} }
if (!IsFile(path)) { if (Exists(path) && !IsFile(path)) {
LOG_ERROR(Common_Filesystem, "Filesystem object at path={} is not a file", LOG_ERROR(Common_Filesystem,
"Filesystem object at path={} exists and is not a regular file",
PathToUTF8String(path)); PathToUTF8String(path));
return nullptr; return nullptr;
} }

View file

@ -110,8 +110,8 @@ template <typename Path1, typename Path2>
* *
* Failures occur when: * Failures occur when:
* - Input path is not valid * - Input path is not valid
* - Filesystem object at path is not a file * - Filesystem object at path exists and is not a regular file
* - The file is not opened * - The file is not open
* *
* @param path Filesystem path * @param path Filesystem path
* @param mode File access mode * @param mode File access mode