Merge "tracing: Work around an LLVM bug on Windows"
diff --git a/include/perfetto/ext/base/file_utils.h b/include/perfetto/ext/base/file_utils.h
index 289e5c1..17271da 100644
--- a/include/perfetto/ext/base/file_utils.h
+++ b/include/perfetto/ext/base/file_utils.h
@@ -34,8 +34,6 @@
 
 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
 using FileOpenMode = int;
-
-int CloseFindHandle(PlatformHandle handle);
 #else
 using FileOpenMode = mode_t;
 #endif
diff --git a/src/base/file_utils.cc b/src/base/file_utils.cc
index 9275bfa..4c06980 100644
--- a/src/base/file_utils.cc
+++ b/src/base/file_utils.cc
@@ -44,23 +44,8 @@
 namespace base {
 namespace {
 constexpr size_t kBufSize = 2048;
-
-#if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
-using ScopedFindHandle = ScopedResource<HANDLE,
-                                        &CloseFindHandle,
-                                        /*InvalidValue=*/INVALID_HANDLE_VALUE,
-                                        /*CheckClose=*/true>;
-#endif
 }  // namespace
 
-#if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
-int CloseFindHandle(HANDLE handle) {
-  // FindClose returns non-zero on success, but CloseFunction of ScopedResource
-  // expects 0 on success.
-  return FindClose(handle) != 0 ? 0 : 1;
-}
-#endif
-
 ssize_t Read(int fd, void* dst, size_t dst_size) {
 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
   return _read(fd, dst, static_cast<unsigned>(dst_size));
@@ -252,12 +237,17 @@
     if (glob_path.length() + 1 > MAX_PATH)
       return base::ErrStatus("Directory path %s is too long", dir_path.c_str());
     WIN32_FIND_DATAA ffd;
-    ScopedFindHandle hFind(FindFirstFileA(glob_path.c_str(), &ffd));
-    if (!hFind)
+    // We do not use a ScopedResource for the HANDLE from FindFirstFile because
+    // the invalid value INVALID_HANDLE_VALUE is not a constexpr under some
+    // compile configurations, and thus cannot be used as a template argument.
+    HANDLE hFind = FindFirstFileA(glob_path.c_str(), &ffd);
+    if (hFind == INVALID_HANDLE_VALUE) {
       // For empty directories, there should be at least one entry '.'.
       // If FindFirstFileA returns INVALID_HANDLE_VALUE, this means directory
       // couldn't be accessed.
+      FindClose(hFind);
       return base::ErrStatus("Failed to open directory %s", cur_dir.c_str());
+    }
     do {
       if (strcmp(ffd.cFileName, ".") == 0 || strcmp(ffd.cFileName, "..") == 0)
         continue;
@@ -269,7 +259,8 @@
         PERFETTO_CHECK(full_path.length() > root_dir_path.length());
         output.push_back(full_path.substr(root_dir_path.length()));
       }
-    } while (FindNextFileA(hFind.get(), &ffd));
+    } while (FindNextFileA(hFind, &ffd));
+    FindClose(hFind);
 #else
     ScopedDir dir = ScopedDir(opendir(cur_dir.c_str()));
     if (!dir) {