filesystem: Migrate from probes_producer and add mount points

Bug: 73625480
Change-Id: I83146cfa6589af0bcf3301397e6f79b02f405fdc
diff --git a/src/traced/probes/probes_producer.cc b/src/traced/probes/probes_producer.cc
index 0c1025d..27064fd 100644
--- a/src/traced/probes/probes_producer.cc
+++ b/src/traced/probes/probes_producer.cc
@@ -28,6 +28,7 @@
 #include "perfetto/tracing/core/ftrace_config.h"
 #include "perfetto/tracing/core/trace_config.h"
 #include "perfetto/tracing/core/trace_packet.h"
+#include "src/traced/probes/filesystem/inode_file_data_source.h"
 
 #include "perfetto/trace/filesystem/inode_file_map.pbzero.h"
 #include "perfetto/trace/ftrace/ftrace_event_bundle.pbzero.h"
@@ -93,7 +94,7 @@
   if (source_config.name() == kFtraceSourceName) {
     CreateFtraceDataSourceInstance(id, source_config);
   } else if (source_config.name() == kInodeFileMapSourceName) {
-    CreateInodeFileMapDataSourceInstance(id, source_config);
+    CreateInodeFileDataSourceInstance(id, source_config);
   } else if (source_config.name() == kProcessStatsSourceName) {
     CreateProcessStatsDataSourceInstance(id, source_config);
   } else {
@@ -152,16 +153,17 @@
   AddWatchdogsTimer(id, source_config);
 }
 
-void ProbesProducer::CreateInodeFileMapDataSourceInstance(
+void ProbesProducer::CreateInodeFileDataSourceInstance(
     DataSourceInstanceID id,
     const DataSourceConfig& source_config) {
   PERFETTO_LOG("Inode file map start (id=%" PRIu64 ", target_buf=%" PRIu32 ")",
                id, source_config.target_buffer());
   auto trace_writer = endpoint_->CreateTraceWriter(
       static_cast<BufferID>(source_config.target_buffer()));
-  CreateDeviceToInodeMap("/system/", &system_inodes_);
-  auto file_map_source = std::unique_ptr<InodeFileMapDataSource>(
-      new InodeFileMapDataSource(&system_inodes_, std::move(trace_writer)));
+  if (system_inodes_.empty())
+    CreateDeviceToInodeMap("/system/", &system_inodes_);
+  auto file_map_source = std::unique_ptr<InodeFileDataSource>(
+      new InodeFileDataSource(&system_inodes_, std::move(trace_writer)));
   file_map_sources_.emplace(id, std::move(file_map_source));
   AddWatchdogsTimer(id, source_config);
 }
@@ -179,50 +181,6 @@
   it_and_inserted.first->second->WriteAllProcesses();
 }
 
-// static
-void ProbesProducer::CreateDeviceToInodeMap(
-    const std::string& root_directory,
-    std::map<uint32_t, InodeMap>* block_device_map) {
-  // Return immediately if we've already filled in the system map
-  if (!block_device_map->empty())
-    return;
-  std::queue<std::string> queue;
-  queue.push(root_directory);
-  while (!queue.empty()) {
-    struct dirent* entry;
-    std::string filepath = queue.front();
-    queue.pop();
-    DIR* dir = opendir(filepath.c_str());
-    filepath += "/";
-    if (dir == nullptr)
-      continue;
-    while ((entry = readdir(dir)) != nullptr) {
-      std::string filename = entry->d_name;
-      if (filename == "." || filename == "..")
-        continue;
-      uint64_t inode_number = entry->d_ino;
-      struct stat buf;
-      if (lstat(filepath.c_str(), &buf) != 0)
-        continue;
-      uint32_t block_device_id = buf.st_dev;
-      InodeMap& inode_map = (*block_device_map)[block_device_id];
-      // Default
-      Type type = protos::pbzero::InodeFileMap_Entry_Type_UNKNOWN;
-      // Readdir and stat not guaranteed to have directory info for all systems
-      if (entry->d_type == DT_DIR || S_ISDIR(buf.st_mode)) {
-        // Continue iterating through files if current entry is a directory
-        queue.push(filepath + filename);
-        type = protos::pbzero::InodeFileMap_Entry_Type_DIRECTORY;
-      } else if (entry->d_type == DT_REG || S_ISREG(buf.st_mode)) {
-        type = protos::pbzero::InodeFileMap_Entry_Type_FILE;
-      }
-      inode_map[inode_number].first = type;
-      inode_map[inode_number].second.emplace(filepath + filename);
-    }
-    closedir(dir);
-  }
-}
-
 void ProbesProducer::TearDownDataSourceInstance(DataSourceInstanceID id) {
   PERFETTO_LOG("Producer stop (id=%" PRIu64 ")", id);
   // |id| could be the id of any of the datasources we handle:
@@ -292,39 +250,8 @@
 }
 
 void ProbesProducer::SinkDelegate::OnInodes(
-    const std::vector<std::pair<uint64_t, uint32_t>>& inodes) {
-  PERFETTO_DLOG("Saw FtraceBundle with %zu inodes.", inodes.size());
-}
-
-ProbesProducer::InodeFileMapDataSource::InodeFileMapDataSource(
-    std::map<uint32_t, InodeMap>* file_system_inodes,
-    std::unique_ptr<TraceWriter> writer)
-    : file_system_inodes_(file_system_inodes), writer_(std::move(writer)) {}
-
-ProbesProducer::InodeFileMapDataSource::~InodeFileMapDataSource() = default;
-
-void ProbesProducer::InodeFileMapDataSource::WriteInodes(
-    const FtraceMetadata& metadata) {
-  auto trace_packet = writer_->NewTracePacket();
-  auto inode_file_map = trace_packet->set_inode_file_map();
-  // TODO(azappone): Get mount_points & add to the proto
-  auto inodes = metadata.inodes;
-  for (const auto& inode : inodes) {
-    uint32_t block_device_id = inode.first;
-    uint64_t inode_number = inode.second;
-    auto* entry = inode_file_map->add_entries();
-    entry->set_inode_number(inode_number);
-    auto block_device_map = file_system_inodes_->find(block_device_id);
-    if (block_device_map != file_system_inodes_->end()) {
-      auto inode_map = block_device_map->second.find(inode_number);
-      if (inode_map != block_device_map->second.end()) {
-        entry->set_type(inode_map->second.first);
-        for (const auto& path : inode_map->second.second)
-          entry->add_paths(path.c_str());
-      }
-    }
-  }
-  trace_packet->Finalize();
+    const std::vector<std::pair<Inode, uint32_t>>& inodes) {
+  PERFETTO_LOG("Saw FtraceBundle with %zu inodes.", inodes.size());
 }
 
 }  // namespace perfetto