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