Use fstatat() in pal::readdir() instead of building a std::string
authorLeandro Pereira <leandro.pereira@microsoft.com>
Thu, 13 Jun 2019 17:44:46 +0000 (10:44 -0700)
committerLeandro A. F. Pereira <leandro@hardinfo.org>
Fri, 19 Jul 2019 18:05:43 +0000 (11:05 -0700)
fstatat(2) is equivalent to stat(2), but it takes a file descriptor for
a directory, so that lookups are always relative to that file
descriptor.  There's no need to construct a temporary std::string.

Commit migrated from https://github.com/dotnet/core-setup/commit/6a6266a1ab4ba99b3c42b16cc8dc822db35fb45f

src/installer/corehost/common/pal.unix.cpp

index 5c149af..5ca7ae7 100644 (file)
@@ -786,14 +786,9 @@ static void readdir(const pal::string_t& path, const pal::string_t& pattern, boo
             case DT_LNK:
             case DT_UNKNOWN:
                 {
-                    std::string fullFilename;
-
-                    fullFilename.append(path);
-                    fullFilename.push_back(DIR_SEPARATOR);
-                    fullFilename.append(entry->d_name);
-
                     struct stat sb;
-                    if (stat(fullFilename.c_str(), &sb) == -1)
+
+                    if (fstatat(dirfd(dir), entry->d_name, &sb, 0) == -1)
                     {
                         continue;
                     }