openMSX
FilePool.hh
Go to the documentation of this file.
1 #ifndef FILEPOOL_HH
2 #define FILEPOOL_HH
3 
4 #include "FileOperations.hh"
5 #include "StringSetting.hh"
6 #include "Observer.hh"
7 #include "EventListener.hh"
8 #include "MemBuffer.hh"
9 #include "sha1.hh"
10 #include <cassert>
11 #include <cstdint>
12 #include <ctime>
13 #include <memory>
14 #include <string>
15 #include <vector>
16 
17 namespace openmsx {
18 
19 class CommandController;
20 class Reactor;
21 class File;
22 class Sha1SumCommand;
23 
24 class FilePool final : private Observer<Setting>, private EventListener
25 {
26 public:
27  FilePool(CommandController& controller, Reactor& reactor);
28  ~FilePool();
29 
30  enum FileType {
31  SYSTEM_ROM = 1, ROM = 2, DISK = 4, TAPE = 8
32  };
33 
38  File getFile(FileType fileType, const Sha1Sum& sha1sum);
39 
44  Sha1Sum getSha1Sum(File& file);
45 
46 private:
47  struct ScanProgress {
48  uint64_t lastTime;
49  unsigned amountScanned;
50  };
51  struct Entry {
52  std::string path;
53  int types;
54  };
55  using Directories = std::vector<Entry>;
56 
57  struct PoolEntry {
58  PoolEntry(const Sha1Sum& s, time_t t, const char* f)
59  : filename(f), time(t), sum(s)
60  {
61  assert(time != time_t(-1));
62  }
63  PoolEntry(const Sha1Sum& s, const char* t, const char* f)
64  : filename(f), timeStr(t), sum(s)
65  {
66  assert(timeStr != nullptr);
67  }
68 
69  time_t getTime();
70  void setTime(time_t t);
71 
72  // - At least one of 'timeStr' or 'time' is valid.
73  // - 'filename' and 'timeStr' are non-owning pointers.
74  const char* filename;
75  const char* timeStr = nullptr; // might be nullptr
76  time_t time = time_t(-1); // might be -1
77  Sha1Sum sum;
78  };
79  struct ComparePool { // PoolEntry sorted on 'sum'
80  bool operator()(const PoolEntry& x, const PoolEntry& y) const {
81  return x.sum < y.sum;
82  }
83  bool operator()(const PoolEntry& x, const Sha1Sum& y) const {
84  return x.sum < y;
85  }
86  bool operator()(const Sha1Sum& x, const PoolEntry& y) const {
87  return x < y.sum;
88  }
89  };
90  using Pool = std::vector<PoolEntry>; // sorted with 'ComparePool'
91 
92  void insert(const Sha1Sum& sum, time_t time, const std::string& filename);
93  void remove(Pool::iterator it);
94  bool adjust(Pool::iterator it, const Sha1Sum& newSum);
95 
96  void readSha1sums();
97  void writeSha1sums();
98 
99  File getFromPool(const Sha1Sum& sha1sum);
100  File scanDirectory(const Sha1Sum& sha1sum,
101  const std::string& directory,
102  const std::string& poolPath,
103  ScanProgress& progress);
104  File scanFile(const Sha1Sum& sha1sum,
105  const std::string& filename,
106  const FileOperations::Stat& st,
107  const std::string& poolPath,
108  ScanProgress& progress);
109  Pool::iterator findInDatabase(const std::string& filename);
110 
111  Directories getDirectories() const;
112 
113  // Observer<Setting>
114  void update(const Setting& setting) override;
115 
116  // EventListener
117  int signalEvent(const std::shared_ptr<const Event>& event) override;
118 
119 
120  StringSetting filePoolSetting;
121  Reactor& reactor;
122  std::unique_ptr<Sha1SumCommand> sha1SumCommand;
123  MemBuffer<char> fileMem; // content of initial .filecache
124  std::vector<std::string> stringBuffer; // owns strings that are not in 'fileMem'
125 
126  Pool pool;
127  bool quit;
128  bool needWrite;
129 };
130 
131 } // namespace openmsx
132 
133 #endif
Contains the main loop of openMSX.
Definition: Reactor.hh:66
auto sum(InputRange &&range)
Definition: stl.hh:288
File getFile(FileType fileType, const Sha1Sum &sha1sum)
Search file with the given sha1sum.
Definition: FilePool.cc:302
This class represents the result of a sha1 calculation (a 160-bit value).
Definition: sha1.hh:19
constexpr const char *const filename
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
FilePool(CommandController &controller, Reactor &reactor)
Definition: FilePool.cc:55
Sha1Sum getSha1Sum(File &file)
Calculate sha1sum for the given File object.
Definition: FilePool.cc:545
constexpr KeyMatrixPosition x
Keyboard bindings.
Definition: Keyboard.cc:1377
Generic Gang-of-Four Observer class, templatized edition.
Definition: Observer.hh:9
TclObject t
constexpr AdjustTables adjust
Definition: Y8950.cc:204