2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
19 * Author: Helge Norberg, helge.norberg@svt.se
24 #include "polling_filesystem_monitor.h"
31 #include <boost/asio.hpp>
32 #include <boost/thread.hpp>
33 #include <boost/filesystem/fstream.hpp>
34 #include <boost/filesystem/convenience.hpp>
36 #include <tbb/atomic.h>
37 #include <tbb/concurrent_queue.h>
44 class exception_protected_handler
46 filesystem_monitor_handler handler_;
48 exception_protected_handler(const filesystem_monitor_handler& handler)
53 void operator()(filesystem_event event, const boost::filesystem::path& file)
57 handler_(event, file);
61 CASPAR_LOG_CURRENT_EXCEPTION();
66 class directory_monitor
68 bool report_already_existing_;
69 boost::filesystem::path folder_;
70 filesystem_event events_mask_;
71 filesystem_monitor_handler handler_;
72 initial_files_handler initial_files_handler_;
73 bool first_scan_ = true;
74 std::map<boost::filesystem::path, std::time_t> files_;
75 std::map<boost::filesystem::path, uintmax_t> being_written_sizes_;
78 bool report_already_existing,
79 const boost::filesystem::path& folder,
80 filesystem_event events_mask,
81 const filesystem_monitor_handler& handler,
82 const initial_files_handler& initial_files_handler)
83 : report_already_existing_(report_already_existing)
85 , events_mask_(events_mask)
86 , handler_(exception_protected_handler(handler))
87 , initial_files_handler_(initial_files_handler)
91 void reemmit_all(const tbb::atomic<bool>& running)
93 if (static_cast<int>(events_mask_ & filesystem_event::MODIFIED) == 0)
96 for (auto& file : files_)
101 handler_(filesystem_event::MODIFIED, file.first);
105 void reemmit(const boost::filesystem::path& file)
107 if (static_cast<int>(events_mask_ & filesystem_event::MODIFIED) == 0)
110 if (files_.find(file) != files_.end() && boost::filesystem::exists(file))
111 handler_(filesystem_event::MODIFIED, file);
114 void scan(const boost::function<bool ()>& should_abort)
116 static const std::time_t NO_LONGER_WRITING_AGE = 3; // Assume std::time_t is expressed in seconds
117 using namespace boost::filesystem;
119 bool interested_in_removed = static_cast<int>(events_mask_ & filesystem_event::REMOVED) > 0;
120 bool interested_in_created = static_cast<int>(events_mask_ & filesystem_event::CREATED) > 0;
121 bool interested_in_modified = static_cast<int>(events_mask_ & filesystem_event::MODIFIED) > 0;
123 auto filenames = cpplinq::from(files_).select(keys());
124 std::set<path> removed_files(filenames.begin(), filenames.end());
125 std::set<path> initial_files;
127 for (boost::filesystem::wrecursive_directory_iterator iter(folder_); iter != boost::filesystem::wrecursive_directory_iterator(); ++iter)
132 auto& path = iter->path();
134 if (is_directory(path))
137 auto now = std::time(nullptr);
138 std::time_t current_mtime;
142 current_mtime = last_write_time(path);
146 // Probably removed, will be captured the next round.
150 auto time_since_written_to = now - current_mtime;
151 bool no_longer_being_written_to = time_since_written_to >= NO_LONGER_WRITING_AGE;
152 auto previous_it = files_.find(path);
153 bool already_known = previous_it != files_.end();
155 if (already_known && no_longer_being_written_to)
157 bool modified = previous_it->second != current_mtime;
159 if (modified && can_read_file(path))
161 if (interested_in_modified)
162 handler_(filesystem_event::MODIFIED, path);
164 files_[path] = current_mtime;
165 being_written_sizes_.erase(path);
168 else if (no_longer_being_written_to && can_read_file(path))
170 if (interested_in_created && (report_already_existing_ || !first_scan_))
171 handler_(filesystem_event::CREATED, path);
174 initial_files.insert(path);
176 files_.insert(std::make_pair(path, current_mtime));
177 being_written_sizes_.erase(path);
180 removed_files.erase(path);
183 for (auto& path : removed_files)
186 being_written_sizes_.erase(path);
188 if (interested_in_removed)
189 handler_(filesystem_event::REMOVED, path);
193 initial_files_handler_(initial_files);
198 bool can_read_file(const boost::filesystem::path& file)
200 boost::filesystem::wifstream stream(file);
202 return stream.is_open();
206 class polling_filesystem_monitor
207 : public filesystem_monitor
208 , public spl::enable_shared_from_this<polling_filesystem_monitor>
210 tbb::atomic<bool> running_;
211 std::shared_ptr<boost::asio::io_service> scheduler_;
212 directory_monitor root_monitor_;
213 boost::asio::deadline_timer timer_;
214 int scan_interval_millis_;
215 std::promise<void> initial_scan_completion_;
216 tbb::concurrent_queue<boost::filesystem::path> to_reemmit_;
217 tbb::atomic<bool> reemmit_all_;
220 polling_filesystem_monitor(
221 const boost::filesystem::path& folder_to_watch,
222 filesystem_event events_of_interest_mask,
223 bool report_already_existing,
224 int scan_interval_millis,
225 std::shared_ptr<boost::asio::io_service> scheduler,
226 const filesystem_monitor_handler& handler,
227 const initial_files_handler& initial_files_handler)
228 : scheduler_(std::move(scheduler))
229 , root_monitor_(report_already_existing, folder_to_watch, events_of_interest_mask, handler, initial_files_handler)
230 , timer_(*scheduler_)
231 , scan_interval_millis_(scan_interval_millis)
232 , executor_(L"polling_filesystem_monitor")
235 reemmit_all_ = false;
240 executor_.begin_invoke([this]
243 initial_scan_completion_.set_value();
248 ~polling_filesystem_monitor()
251 boost::system::error_code e;
252 timer_.cancel(e); // Can still have be queued for execution by asio, therefore the task has a weak_ptr to this
255 std::future<void> initial_files_processed() override
257 return initial_scan_completion_.get_future();
260 void reemmit_all() override
265 void reemmit(const boost::filesystem::path& file) override
267 to_reemmit_.push(file);
275 std::weak_ptr<polling_filesystem_monitor> weak_self = shared_from_this();
277 timer_.expires_from_now(boost::posix_time::milliseconds(scan_interval_millis_));
278 timer_.async_wait([weak_self](const boost::system::error_code& e)
280 auto strong_self = weak_self.lock();
283 strong_self->begin_scan();
292 executor_.begin_invoke([this]()
306 if (reemmit_all_.fetch_and_store(false))
307 root_monitor_.reemmit_all(running_);
310 boost::filesystem::path file;
312 while (to_reemmit_.try_pop(file))
313 root_monitor_.reemmit(file);
316 root_monitor_.scan([=] { return !running_; });
320 CASPAR_LOG_CURRENT_EXCEPTION();
325 struct polling_filesystem_monitor_factory::impl
327 std::shared_ptr<boost::asio::io_service> scheduler_;
328 int scan_interval_millis;
331 std::shared_ptr<boost::asio::io_service> scheduler,
332 int scan_interval_millis)
333 : scheduler_(std::move(scheduler))
334 , scan_interval_millis(scan_interval_millis)
339 polling_filesystem_monitor_factory::polling_filesystem_monitor_factory(
340 std::shared_ptr<boost::asio::io_service> scheduler,
341 int scan_interval_millis)
342 : impl_(new impl(std::move(scheduler), scan_interval_millis))
346 polling_filesystem_monitor_factory::~polling_filesystem_monitor_factory()
350 filesystem_monitor::ptr polling_filesystem_monitor_factory::create(
351 const boost::filesystem::path& folder_to_watch,
352 filesystem_event events_of_interest_mask,
353 bool report_already_existing,
354 const filesystem_monitor_handler& handler,
355 const initial_files_handler& initial_files_handler)
357 auto monitor = spl::make_shared<polling_filesystem_monitor>(
359 events_of_interest_mask,
360 report_already_existing,
361 impl_->scan_interval_millis,
364 initial_files_handler);