summaryrefslogtreecommitdiffstats
path: root/base/file.cpp
diff options
context:
space:
mode:
authorliwugang <liwugang@xiaomi.com>2018-07-11 13:24:49 +0800
committerElliott Hughes <enh@google.com>2018-07-12 17:35:17 -0700
commitc63cb070633864fe5424d8a2b6e44f0fd19ef08f (patch)
tree97067b15cdb73ef22653c45befa40e9222e2cf16 /base/file.cpp
parent097381382bb49547b847c90e0471694d84ce68f6 (diff)
downloadsystem_core-c63cb070633864fe5424d8a2b6e44f0fd19ef08f.tar.gz
system_core-c63cb070633864fe5424d8a2b6e44f0fd19ef08f.tar.bz2
system_core-c63cb070633864fe5424d8a2b6e44f0fd19ef08f.zip
libbase: return different result depend on the errno
In the RemoveFileIfExists it always return true even if error appeared when using stat function. It should distinguish different error. Such as ENOENT and ENOTDIR we exactly know the file does not exist. But EACCES(current user has not all search permission in the file path) and other errors appeared we can't know whether file exits. So we should return false indicate there are some error appeared. Test: ran unit tests Change-Id: I75788bf0621040812413d52596b5effb628fd0b1 Signed-off-by: liwugang <liwugang@xiaomi.com>
Diffstat (limited to 'base/file.cpp')
-rw-r--r--base/file.cpp10
1 files changed, 8 insertions, 2 deletions
diff --git a/base/file.cpp b/base/file.cpp
index 2f697a1cc..d6fe753d1 100644
--- a/base/file.cpp
+++ b/base/file.cpp
@@ -199,17 +199,23 @@ bool WriteFully(int fd, const void* data, size_t byte_count) {
bool RemoveFileIfExists(const std::string& path, std::string* err) {
struct stat st;
#if defined(_WIN32)
- //TODO: Windows version can't handle symbol link correctly.
+ // TODO: Windows version can't handle symbolic links correctly.
int result = stat(path.c_str(), &st);
bool file_type_removable = (result == 0 && S_ISREG(st.st_mode));
#else
int result = lstat(path.c_str(), &st);
bool file_type_removable = (result == 0 && (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)));
#endif
+ if (result == -1) {
+ if (errno == ENOENT || errno == ENOTDIR) return true;
+ if (err != nullptr) *err = strerror(errno);
+ return false;
+ }
+
if (result == 0) {
if (!file_type_removable) {
if (err != nullptr) {
- *err = "is not a regular or symbol link file";
+ *err = "is not a regular file or symbolic link";
}
return false;
}