OSDN Git Service

linker: use stat(2) to check file existence
authorDimitry Ivanov <dimitry@google.com>
Thu, 11 Aug 2016 18:11:52 +0000 (11:11 -0700)
committerDimitry Ivanov <dimitry@google.com>
Thu, 11 Aug 2016 18:11:52 +0000 (11:11 -0700)
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

linker/linker.cpp

index 7d34823..d2a6e7d 100644 (file)
@@ -131,13 +131,13 @@ static bool is_system_library(const std::string& realpath) {
 
 // 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 ---