From 4cf7024db7c89dab73c8668031263eb8fd991d8f Mon Sep 17 00:00:00 2001 From: Dimitry Ivanov Date: Thu, 11 Aug 2016 11:11:52 -0700 Subject: [PATCH] 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 --- linker/linker.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/linker/linker.cpp b/linker/linker.cpp index 7d3482329..d2a6e7dc3 100644 --- a/linker/linker.cpp +++ b/linker/linker.cpp @@ -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 ---