comparison src/core/filesystem.cc @ 391:c3f717b7321b

filesystem: only iterate over the list once when erasing deleted files :)
author Paper <paper@tflc.us>
date Fri, 07 Nov 2025 07:09:31 -0500
parents 0265e125f680
children 963047512d34
comparison
equal deleted inserted replaced
390:2d3e10319112 391:c3f717b7321b
77 EventHandler handler_; 77 EventHandler handler_;
78 void *opaque_; 78 void *opaque_;
79 }; 79 };
80 80
81 /* ------------------------------------------------------------------------ */ 81 /* ------------------------------------------------------------------------ */
82 /* Non-recursive filesystem watcher. 82 /* Filesystem watcher.
83 * This is the portable version for non-Windows */ 83 * This is the portable version for non-Windows (of which the Windows
84 * specific version hasn't been written yet... TODO) */
84 85
85 template<typename T> 86 template<typename T>
86 class StdFilesystemWatcher : public Watcher { 87 class StdFilesystemWatcher : public Watcher {
87 public: 88 public:
88 StdFilesystemWatcher(void *opaque, const std::filesystem::path &path, EventHandler handler) 89 StdFilesystemWatcher(void *opaque, const std::filesystem::path &path, EventHandler handler)
113 114
114 DeleteUntoggledPaths(); 115 DeleteUntoggledPaths();
115 } 116 }
116 117
117 protected: 118 protected:
118 bool FindAndTogglePath(const std::filesystem::path &p) { 119 bool FindAndTogglePath(const std::filesystem::path &p)
120 {
119 for (auto &pp : paths_) { 121 for (auto &pp : paths_) {
120 if (pp.path == p) { 122 if (pp.path == p) {
121 pp.found = true; 123 pp.found = true;
122 return true; 124 return true;
123 } 125 }
132 pp.found = false; 134 pp.found = false;
133 } 135 }
134 136
135 void DeleteUntoggledPaths() 137 void DeleteUntoggledPaths()
136 { 138 {
137 for (const auto &path : paths_)
138 if (!path.found)
139 handler_(opaque_, path.path, Event::Deleted);
140
141 auto it = paths_.begin(); 139 auto it = paths_.begin();
142 140
143 while (it != paths_.end()) { 141 while (it != paths_.end()) {
144 if (!it->found) { 142 if (!it->found) {
143 handler_(opaque_, it->path, Event::Deleted);
145 it = paths_.erase(it); 144 it = paths_.erase(it);
146 } else { 145 } else {
147 it++; 146 it++;
148 } 147 }
149 } 148 }