1 // Copyright (C) 2019 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef IORAP_SRC_INODE2FILENAME_DATA_SOURCE_H_
16 #define IORAP_SRC_INODE2FILENAME_DATA_SOURCE_H_
17 
18 #include "inode2filename/inode_result.h"
19 #include "inode2filename/system_call.h"
20 
21 #include <rxcpp/rx.hpp>
22 
23 #include <memory>
24 #include <optional>
25 #include <string>
26 #include <vector>
27 namespace iorap::inode2filename {
28 
29 enum class DataSourceKind {
30   kDiskScan,
31   kTextCache,
32   kBpf,
33 };
34 
35 std::vector<std::string> ToArgs(DataSourceKind data_source_kind);
36 
37 struct DataSourceDependencies {
38   DataSourceKind data_source = DataSourceKind::kDiskScan;
39   borrowed<SystemCall*> system_call = nullptr;
40 
41   // kDiskScan-specific options. Other data sources ignore these fields.
42   std::vector<std::string> root_directories;
43   // kTextCache-specific options. Other data sources ignore these fields.
44   std::optional<std::string> text_cache_filename;
45 };
46 
47 std::vector<std::string> ToArgs(const DataSourceDependencies& deps);
48 
49 class DataSource : std::enable_shared_from_this<DataSource> {
50  public:
51   static std::shared_ptr<DataSource> Create(DataSourceDependencies dependencies);
52 
53   virtual void StartRecording();  // XX: feels like this should be BPF-specific.
54   virtual void StopRecording();
55 
56   // Emit all inode->filename mappings (i.e. an infinite lazy list).
57   // The specific order is determined by the extra dependency options.
58   //
59   // The work must terminate if all subscriptions are removed.
60   virtual rxcpp::observable<InodeResult> EmitInodes() const = 0;
61 
62   // Does the InodeResult include a valid device number?
63   // If returning false, the InodeResolver fills in the missing device number with stat(2).
ResultIncludesDeviceNumber()64   virtual bool ResultIncludesDeviceNumber() const { return true; };
65 
66  protected:
~DataSource()67   virtual ~DataSource() {}
68   DataSource(DataSourceDependencies dependencies);
69   DataSourceDependencies dependencies_;
70 };
71 
72 }  // namespace iorap::inode2filename
73 
74 #endif  // IORAP_SRC_INODE2FILENAME_DATA_SOURCE_H_
75