• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef SRC_TRACED_PROBES_FILESYSTEM_FILE_SCANNER_H_
18 #define SRC_TRACED_PROBES_FILESYSTEM_FILE_SCANNER_H_
19 
20 #include <string>
21 #include <vector>
22 
23 #include "perfetto/base/scoped_file.h"
24 #include "perfetto/base/task_runner.h"
25 #include "perfetto/base/weak_ptr.h"
26 #include "perfetto/traced/data_source_types.h"
27 
28 namespace perfetto {
29 
30 class FileScanner {
31  public:
32   class Delegate {
33    public:
34     virtual bool OnInodeFound(BlockDeviceID,
35                               Inode,
36                               const std::string&,
37                               protos::pbzero::InodeFileMap_Entry_Type) = 0;
38     virtual void OnInodeScanDone() = 0;
39     virtual ~Delegate();
40   };
41 
42   FileScanner(std::vector<std::string> root_directories,
43               Delegate* delegate,
44               uint32_t scan_interval_ms,
45               uint32_t scan_steps);
46 
47   // Ctor when only the blocking version of Scan is used.
48   FileScanner(std::vector<std::string> root_directories, Delegate* delegate);
49 
50   FileScanner(const FileScanner&) = delete;
51   FileScanner& operator=(const FileScanner&) = delete;
52 
53   void Scan(base::TaskRunner* task_runner);
54   void Scan();
55 
56  private:
57   void NextDirectory();
58   void Step();
59   void Steps(uint32_t n);
60   bool Done();
61 
62   Delegate* delegate_;
63   const uint32_t scan_interval_ms_;
64   const uint32_t scan_steps_;
65 
66   std::vector<std::string> queue_;
67   base::ScopedDir current_dir_handle_;
68   std::string current_directory_;
69   BlockDeviceID current_block_device_id_;
70   base::WeakPtrFactory<FileScanner> weak_factory_;  // Keep last.
71 };
72 
73 }  // namespace perfetto
74 
75 #endif  // SRC_TRACED_PROBES_FILESYSTEM_FILE_SCANNER_H_
76