linker: use stat(2) to check file existence
open(2) can be used to open directories; use stat to
check that the file exists and is a regular file.
Addresses review comments for 5aa67675f853af9588ac9274ecf86d7858695ce2
Bug: http://b/30320104
Change-Id: Ia944db2f2f779a87ea01dd41dcd171e59c9bef01
diff --git a/linker/linker.cpp b/linker/linker.cpp
index 7d34823..d2a6e7d 100644
--- a/linker/linker.cpp
+++ b/linker/linker.cpp
@@ -131,13 +131,13 @@
// Checks if the file exists and not a directory.
static bool file_exists(const char* path) {
- int fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC));
- if (fd == -1) {
+ struct stat s;
+
+ if (stat(path, &s) != 0) {
return false;
- } else {
- close(fd);
- return true;
}
+
+ return S_ISREG(s.st_mode);
}
// TODO(dimitry): The grey-list is a workaround for http://b/26394120 ---