summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Morph2021-06-19 03:49:11 -0400
committerGravatar Morph2021-06-22 15:06:58 -0400
commit76b2313b25e2fd33a508f63137d5113e1ca85150 (patch)
treeda79f542733f16ca27b834da5805a75503b1019a
parentcommon: fs: file: Remove [[nodiscard]] attribute from Flush (diff)
downloadyuzu-76b2313b25e2fd33a508f63137d5113e1ca85150.tar.gz
yuzu-76b2313b25e2fd33a508f63137d5113e1ca85150.tar.xz
yuzu-76b2313b25e2fd33a508f63137d5113e1ca85150.zip
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.
-rw-r--r--src/common/fs/file.cpp4
-rw-r--r--src/common/fs/file.h8
-rw-r--r--src/common/fs/fs.cpp5
-rw-r--r--src/common/fs/fs.h4
4 files changed, 12 insertions, 9 deletions
diff --git a/src/common/fs/file.cpp b/src/common/fs/file.cpp
index 710e88b39..077f34995 100644
--- a/src/common/fs/file.cpp
+++ b/src/common/fs/file.cpp
@@ -172,7 +172,7 @@ std::string ReadStringFromFile(const std::filesystem::path& path, FileType type)
172 172
173size_t WriteStringToFile(const std::filesystem::path& path, FileType type, 173size_t WriteStringToFile(const std::filesystem::path& path, FileType type,
174 std::string_view string) { 174 std::string_view string) {
175 if (!IsFile(path)) { 175 if (Exists(path) && !IsFile(path)) {
176 return 0; 176 return 0;
177 } 177 }
178 178
@@ -183,7 +183,7 @@ size_t WriteStringToFile(const std::filesystem::path& path, FileType type,
183 183
184size_t AppendStringToFile(const std::filesystem::path& path, FileType type, 184size_t AppendStringToFile(const std::filesystem::path& path, FileType type,
185 std::string_view string) { 185 std::string_view string) {
186 if (!IsFile(path)) { 186 if (Exists(path) && !IsFile(path)) {
187 return 0; 187 return 0;
188 } 188 }
189 189
diff --git a/src/common/fs/file.h b/src/common/fs/file.h
index 087b2993a..588fe619d 100644
--- a/src/common/fs/file.h
+++ b/src/common/fs/file.h
@@ -49,7 +49,7 @@ void OpenFileStream(FileStream& file_stream, const Path& path, std::ios_base::op
49 49
50/** 50/**
51 * Reads an entire file at path and returns a string of the contents read from the file. 51 * Reads an entire file at path and returns a string of the contents read from the file.
52 * If the filesystem object at path is not a file, this function returns an empty string. 52 * If the filesystem object at path is not a regular file, this function returns an empty string.
53 * 53 *
54 * @param path Filesystem path 54 * @param path Filesystem path
55 * @param type File type 55 * @param type File type
@@ -72,7 +72,8 @@ template <typename Path>
72/** 72/**
73 * Writes a string to a file at path and returns the number of characters successfully written. 73 * Writes a string to a file at path and returns the number of characters successfully written.
74 * If a file already exists at path, its contents will be erased. 74 * If a file already exists at path, its contents will be erased.
75 * If the filesystem object at path is not a file, this function returns 0. 75 * If a file does not exist at path, it creates and opens a new empty file for writing.
76 * If the filesystem object at path exists and is not a regular file, this function returns 0.
76 * 77 *
77 * @param path Filesystem path 78 * @param path Filesystem path
78 * @param type File type 79 * @param type File type
@@ -95,7 +96,8 @@ template <typename Path>
95 96
96/** 97/**
97 * Appends a string to a file at path and returns the number of characters successfully written. 98 * Appends a string to a file at path and returns the number of characters successfully written.
98 * If the filesystem object at path is not a file, this function returns 0. 99 * If a file does not exist at path, it creates and opens a new empty file for appending.
100 * If the filesystem object at path exists and is not a regular file, this function returns 0.
99 * 101 *
100 * @param path Filesystem path 102 * @param path Filesystem path
101 * @param type File type 103 * @param type File type
diff --git a/src/common/fs/fs.cpp b/src/common/fs/fs.cpp
index d3159e908..9089cad67 100644
--- a/src/common/fs/fs.cpp
+++ b/src/common/fs/fs.cpp
@@ -135,8 +135,9 @@ std::shared_ptr<IOFile> FileOpen(const fs::path& path, FileAccessMode mode, File
135 return nullptr; 135 return nullptr;
136 } 136 }
137 137
138 if (!IsFile(path)) { 138 if (Exists(path) && !IsFile(path)) {
139 LOG_ERROR(Common_Filesystem, "Filesystem object at path={} is not a file", 139 LOG_ERROR(Common_Filesystem,
140 "Filesystem object at path={} exists and is not a regular file",
140 PathToUTF8String(path)); 141 PathToUTF8String(path));
141 return nullptr; 142 return nullptr;
142 } 143 }
diff --git a/src/common/fs/fs.h b/src/common/fs/fs.h
index cf7dfffcc..a6c993962 100644
--- a/src/common/fs/fs.h
+++ b/src/common/fs/fs.h
@@ -110,8 +110,8 @@ template <typename Path1, typename Path2>
110 * 110 *
111 * Failures occur when: 111 * Failures occur when:
112 * - Input path is not valid 112 * - Input path is not valid
113 * - Filesystem object at path is not a file 113 * - Filesystem object at path exists and is not a regular file
114 * - The file is not opened 114 * - The file is not open
115 * 115 *
116 * @param path Filesystem path 116 * @param path Filesystem path
117 * @param mode File access mode 117 * @param mode File access mode