Check content for changes on loading a project.
authorCarl Hetherington <cth@carlh.net>
Tue, 21 Aug 2018 21:36:24 +0000 (22:36 +0100)
committerCarl Hetherington <cth@carlh.net>
Tue, 21 Aug 2018 21:36:24 +0000 (22:36 +0100)
src/lib/check_content_change_job.cc [new file with mode: 0644]
src/lib/check_content_change_job.h [new file with mode: 0644]
src/lib/content.cc
src/lib/content.h
src/lib/wscript
src/tools/dcpomatic.cc

diff --git a/src/lib/check_content_change_job.cc b/src/lib/check_content_change_job.cc
new file mode 100644 (file)
index 0000000..0862ab0
--- /dev/null
@@ -0,0 +1,87 @@
+/*
+    Copyright (C) 2018 Carl Hetherington <cth@carlh.net>
+
+    This file is part of DCP-o-matic.
+
+    DCP-o-matic is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    DCP-o-matic is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#include "check_content_change_job.h"
+#include "job_manager.h"
+#include "examine_content_job.h"
+#include "content.h"
+#include "film.h"
+#include <boost/foreach.hpp>
+#include <iostream>
+
+#include "i18n.h"
+
+using std::string;
+using std::list;
+using std::cout;
+using boost::shared_ptr;
+
+CheckContentChangeJob::CheckContentChangeJob (shared_ptr<const Film> film)
+       : Job (film)
+{
+
+}
+
+string
+CheckContentChangeJob::name () const
+{
+       return _("Check content for changes");
+}
+
+string
+CheckContentChangeJob::json_name () const
+{
+       return N_("check_content_change");
+}
+
+void
+CheckContentChangeJob::run ()
+{
+       set_progress_unknown ();
+
+       list<shared_ptr<Content> > changed;
+
+       BOOST_FOREACH (shared_ptr<Content> i, _film->content()) {
+               bool ic = false;
+               for (size_t j = 0; j < i->number_of_paths(); ++j) {
+                       cout << boost::filesystem::last_write_time(i->path(j)) << " " << i->last_write_time(j) << "\n";
+                       if (boost::filesystem::last_write_time(i->path(j)) != i->last_write_time(j)) {
+                               cout << "last write differs.\n";
+                               ic = true;
+                               break;
+                       }
+               }
+               if (!ic && i->calculate_digest() != i->digest()) {
+                       cout << "digest differs.\n";
+                       ic = true;
+               }
+               if (ic) {
+                       cout << i->path(0) << " changed.\n";
+                       changed.push_back (i);
+               }
+       }
+
+       BOOST_FOREACH (shared_ptr<Content> i, changed) {
+               JobManager::instance()->add(shared_ptr<Job>(new ExamineContentJob(_film, i)));
+       }
+
+       set_progress (1);
+       set_state (FINISHED_OK);
+}
diff --git a/src/lib/check_content_change_job.h b/src/lib/check_content_change_job.h
new file mode 100644 (file)
index 0000000..4ae3ed1
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+    Copyright (C) 2018 Carl Hetherington <cth@carlh.net>
+
+    This file is part of DCP-o-matic.
+
+    DCP-o-matic is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    DCP-o-matic is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+#include "job.h"
+
+/** @class CheckContentChangeJob
+ *  @brief A job to check whether content has changed since it was added to the film.
+ */
+
+class CheckContentChangeJob : public Job
+{
+public:
+       CheckContentChangeJob (boost::shared_ptr<const Film>);
+
+       std::string name () const;
+       std::string json_name () const;
+       void run ();
+};
index 4e77831ac2ba0faaf409c033c01c245ba2bae607..69dcbc1395e2a125032e686d68f80ae1a7e5a740 100644 (file)
@@ -97,7 +97,7 @@ Content::Content (shared_ptr<const Film> film, cxml::ConstNodePtr node)
        list<cxml::NodePtr> path_children = node->node_children ("Path");
        BOOST_FOREACH (cxml::NodePtr i, path_children) {
                _paths.push_back (i->content());
-               optional<time_t> const mod = i->optional_number_child<time_t>("mtime");
+               optional<time_t> const mod = i->optional_number_attribute<time_t>("mtime");
                if (mod) {
                        _last_write_times.push_back (*mod);
                } else if (boost::filesystem::exists(i->content())) {
@@ -169,13 +169,9 @@ Content::as_xml (xmlpp::Node* node, bool with_paths) const
        }
 }
 
-void
-Content::examine (shared_ptr<Job> job)
+string
+Content::calculate_digest () const
 {
-       if (job) {
-               job->sub (_("Computing digest"));
-       }
-
        boost::mutex::scoped_lock lm (_mutex);
        vector<boost::filesystem::path> p = _paths;
        lm.unlock ();
@@ -184,9 +180,19 @@ Content::examine (shared_ptr<Job> job)
           digest here: a digest of the first and last 1e6 bytes with the
           size of the first file tacked on the end as a string.
        */
-       string const d = digest_head_tail (p, 1000000) + raw_convert<string> (boost::filesystem::file_size (p.front ()));
+       return digest_head_tail(p, 1000000) + raw_convert<string>(boost::filesystem::file_size(p.front()));
+}
 
-       lm.lock ();
+void
+Content::examine (shared_ptr<Job> job)
+{
+       if (job) {
+               job->sub (_("Computing digest"));
+       }
+
+       string const d = calculate_digest ();
+
+       boost::mutex::scoped_lock lm (_mutex);
        _digest = d;
 }
 
index 3ab272c168114dc3073b89348b59c787c6f4e00e..2eaa738d2c72e3e88a9388cd7bfa28f2eb88d5b6 100644 (file)
@@ -118,6 +118,11 @@ public:
                return _paths[i];
        }
 
+       std::time_t last_write_time (size_t i) const {
+               boost::mutex::scoped_lock lm (_mutex);
+               return _last_write_times[i];
+       }
+
        bool paths_valid () const;
 
        /** @return Digest of the content's file(s).  Note: this is
@@ -178,6 +183,8 @@ public:
 
        std::list<UserProperty> user_properties () const;
 
+       std::string calculate_digest () const;
+
        /* CHANGE_PENDING and CHANGE_CANCELLED may be emitted from any thread; CHANGE_DONE always from GUI thread */
        boost::signals2::signal<void (ChangeType, boost::weak_ptr<Content>, int, bool)> Change;
 
index 0cc3d782361fd3648b13bed645f69eb115e67c60..a85ce1f261337aafccd2a18567d55a793d655ff1 100644 (file)
@@ -41,6 +41,7 @@ sources = """
           text_content.cc
           text_decoder.cc
           case_insensitive_sorter.cc
+          check_content_change_job.cc
           cinema.cc
           cinema_kdms.cc
           cinema_sound_processor.cc
index 7ae6b1c72a707aeb335fc0a504537d0c83984354..0af65bd2611cafa0bedee24e6241563a2db6f5b4 100644 (file)
@@ -71,6 +71,7 @@
 #include "lib/transcode_job.h"
 #include "lib/dkdm_wrapper.h"
 #include "lib/audio_content.h"
+#include "lib/check_content_change_job.h"
 #include "lib/text_content.h"
 #include <dcp/exceptions.h>
 #include <dcp/raw_convert.h>
@@ -414,6 +415,8 @@ public:
                }
 
                set_film (film);
+
+               JobManager::instance()->add(shared_ptr<Job>(new CheckContentChangeJob(film)));
        }
        catch (std::exception& e) {
                wxString p = std_to_wx (file.string ());