Allow to stop ScanFilesDFS early.

This will be important when looking for specific inodes.

Bug: 74584014
Change-Id: I0a74d30c6d91d6007c98e6ef78a73d7c344aaec1
diff --git a/src/traced/probes/filesystem/inode_file_data_source.cc b/src/traced/probes/filesystem/inode_file_data_source.cc
index 6c324f2..bcb4bf7 100644
--- a/src/traced/probes/filesystem/inode_file_data_source.cc
+++ b/src/traced/probes/filesystem/inode_file_data_source.cc
@@ -23,6 +23,7 @@
 #include <queue>
 
 #include "perfetto/base/logging.h"
+#include "perfetto/base/scoped_file.h"
 #include "perfetto/tracing/core/trace_packet.h"
 #include "perfetto/tracing/core/trace_writer.h"
 
@@ -32,7 +33,7 @@
 
 void ScanFilesDFS(
     const std::string& root_directory,
-    const std::function<void(BlockDeviceID block_device_id,
+    const std::function<bool(BlockDeviceID block_device_id,
                              Inode inode_number,
                              const std::string& path,
                              protos::pbzero::InodeFileMap_Entry_Type type)>&
@@ -42,11 +43,11 @@
     struct dirent* entry;
     std::string directory = queue.back();
     queue.pop_back();
-    DIR* dir = opendir(directory.c_str());
+    base::ScopedDir dir(opendir(directory.c_str()));
     directory += "/";
-    if (dir == nullptr)
+    if (!dir)
       continue;
-    while ((entry = readdir(dir)) != nullptr) {
+    while ((entry = readdir(dir.get())) != nullptr) {
       std::string filename = entry->d_name;
       if (filename == "." || filename == "..")
         continue;
@@ -75,9 +76,9 @@
         type = protos::pbzero::InodeFileMap_Entry_Type_FILE;
       }
 
-      fn(block_device_id, inode_number, filepath, type);
+      if (!fn(block_device_id, inode_number, filepath, type))
+        return;
     }
-    closedir(dir);
   }
 }
 
@@ -93,6 +94,7 @@
             (*block_device_map)[block_device_id];
         inode_map[inode_number].SetType(type);
         inode_map[inode_number].AddPath(path);
+        return true;
       });
 }