Index: runtime/bin/file_macos.cc |
diff --git a/runtime/bin/file_macos.cc b/runtime/bin/file_macos.cc |
index 4556b6bbd2c0dda71cbcc1311242ccafed573ca7..767908ec6066cbec29160ca1b459cd55f8ae3f34 100644 |
--- a/runtime/bin/file_macos.cc |
+++ b/runtime/bin/file_macos.cc |
@@ -268,65 +268,79 @@ bool File::CreateLink(const char* name, const char* target) { |
} |
-bool File::Delete(const char* name) { |
- File::Type type = File::GetType(name, true); |
- if (type == kIsFile) { |
- return NO_RETRY_EXPECTED(unlink(name)) == 0; |
- } else if (type == kIsDirectory) { |
- errno = EISDIR; |
+File::Type File::GetType(const char* pathname, bool follow_links) { |
+ struct stat entry_info; |
+ int stat_success; |
+ if (follow_links) { |
+ stat_success = NO_RETRY_EXPECTED(stat(pathname, &entry_info)); |
} else { |
- errno = ENOENT; |
+ stat_success = NO_RETRY_EXPECTED(lstat(pathname, &entry_info)); |
} |
- return false; |
+ if (stat_success == -1) { |
+ return File::kDoesNotExist; |
+ } |
+ if (S_ISDIR(entry_info.st_mode)) { |
+ return File::kIsDirectory; |
+ } |
+ if (S_ISREG(entry_info.st_mode)) { |
+ return File::kIsFile; |
+ } |
+ if (S_ISLNK(entry_info.st_mode)) { |
+ return File::kIsLink; |
+ } |
+ return File::kDoesNotExist; |
} |
-bool File::DeleteLink(const char* name) { |
- File::Type type = File::GetType(name, false); |
- if (type == kIsLink) { |
- return NO_RETRY_EXPECTED(unlink(name)) == 0; |
+static bool CheckTypeAndSetErrno(const char* name, |
+ File::Type expected, |
+ bool follow_links) { |
+ File::Type actual = File::GetType(name, follow_links); |
+ if (actual == expected) { |
+ return true; |
+ } |
+ switch (actual) { |
+ case File::kIsDirectory: |
+ errno = EISDIR; |
+ break; |
+ case File::kDoesNotExist: |
+ errno = ENOENT; |
+ break; |
+ default: |
+ errno = EINVAL; |
+ break; |
} |
- errno = EINVAL; |
return false; |
} |
+bool File::Delete(const char* name) { |
+ return CheckTypeAndSetErrno(name, kIsFile, true) && |
+ (NO_RETRY_EXPECTED(unlink(name)) == 0); |
+} |
+ |
+ |
+bool File::DeleteLink(const char* name) { |
+ return CheckTypeAndSetErrno(name, kIsLink, false) && |
+ (NO_RETRY_EXPECTED(unlink(name)) == 0); |
+} |
+ |
+ |
bool File::Rename(const char* old_path, const char* new_path) { |
- File::Type type = File::GetType(old_path, true); |
- if (type == kIsFile) { |
- return NO_RETRY_EXPECTED(rename(old_path, new_path)) == 0; |
- } else if (type == kIsDirectory) { |
- errno = EISDIR; |
- } else { |
- errno = ENOENT; |
- } |
- return false; |
+ return CheckTypeAndSetErrno(old_path, kIsFile, true) && |
+ (NO_RETRY_EXPECTED(rename(old_path, new_path)) == 0); |
} |
bool File::RenameLink(const char* old_path, const char* new_path) { |
- File::Type type = File::GetType(old_path, false); |
- if (type == kIsLink) { |
- return NO_RETRY_EXPECTED(rename(old_path, new_path)) == 0; |
- } else if (type == kIsDirectory) { |
- errno = EISDIR; |
- } else { |
- errno = EINVAL; |
- } |
- return false; |
+ return CheckTypeAndSetErrno(old_path, kIsLink, false) && |
+ (NO_RETRY_EXPECTED(rename(old_path, new_path)) == 0); |
} |
bool File::Copy(const char* old_path, const char* new_path) { |
- File::Type type = File::GetType(old_path, true); |
- if (type == kIsFile) { |
- return copyfile(old_path, new_path, NULL, COPYFILE_ALL) == 0; |
- } else if (type == kIsDirectory) { |
- errno = EISDIR; |
- } else { |
- errno = ENOENT; |
- } |
- return false; |
+ return CheckTypeAndSetErrno(old_path, kIsFile, true) && |
+ (copyfile(old_path, new_path, NULL, COPYFILE_ALL) == 0); |
} |
@@ -477,30 +491,6 @@ File::StdioHandleType File::GetStdioHandleType(int fd) { |
} |
-File::Type File::GetType(const char* pathname, bool follow_links) { |
- struct stat entry_info; |
- int stat_success; |
- if (follow_links) { |
- stat_success = NO_RETRY_EXPECTED(stat(pathname, &entry_info)); |
- } else { |
- stat_success = NO_RETRY_EXPECTED(lstat(pathname, &entry_info)); |
- } |
- if (stat_success == -1) { |
- return File::kDoesNotExist; |
- } |
- if (S_ISDIR(entry_info.st_mode)) { |
- return File::kIsDirectory; |
- } |
- if (S_ISREG(entry_info.st_mode)) { |
- return File::kIsFile; |
- } |
- if (S_ISLNK(entry_info.st_mode)) { |
- return File::kIsLink; |
- } |
- return File::kDoesNotExist; |
-} |
- |
- |
File::Identical File::AreIdentical(const char* file_1, const char* file_2) { |
struct stat file_1_info; |
struct stat file_2_info; |