Merge.
[dcpomatic.git] / src / lib / check_hashes_job.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <fstream>
21 #include <boost/lexical_cast.hpp>
22 #include <boost/filesystem.hpp>
23 #include "check_hashes_job.h"
24 #include "film_state.h"
25 #include "options.h"
26 #include "log.h"
27 #include "job_manager.h"
28 #include "ab_transcode_job.h"
29 #include "transcode_job.h"
30
31 using namespace std;
32 using namespace boost;
33
34 CheckHashesJob::CheckHashesJob (shared_ptr<const FilmState> s, shared_ptr<const Options> o, Log* l)
35         : Job (s, o, l)
36         , _bad (0)
37 {
38
39 }
40
41 string
42 CheckHashesJob::name () const
43 {
44         return String::compose ("Check hashes of %1", _fs->name);
45 }
46
47 void
48 CheckHashesJob::run ()
49 {
50         _bad = 0;
51         
52         for (int i = 0; i < _fs->length; ++i) {
53                 string const j2k_file = _opt->frame_out_path (i, false);
54                 string const hash_file = j2k_file + ".md5";
55
56                 ifstream ref (hash_file.c_str ());
57                 string hash;
58                 ref >> hash;
59
60                 if (hash != md5_digest (j2k_file)) {
61                         _log->log ("Frame " + lexical_cast<string> (i) + " has wrong hash; deleting.");
62                         filesystem::remove (j2k_file);
63                         filesystem::remove (hash_file);
64                         ++_bad;
65                 }
66
67                 set_progress (float (i) / _fs->length);
68         }
69
70         if (_bad) {
71                 shared_ptr<Job> tc;
72
73                 if (_fs->dcp_ab) {
74                         tc.reset (new ABTranscodeJob (_fs, _opt, _log));
75                 } else {
76                         tc.reset (new TranscodeJob (_fs, _opt, _log));
77                 }
78                 
79                 JobManager::instance()->add_after (shared_from_this(), tc);
80                 JobManager::instance()->add_after (tc, shared_ptr<Job> (new CheckHashesJob (_fs, _opt, _log)));
81         }
82                 
83         set_progress (1);
84         set_state (FINISHED_OK);
85 }
86
87 string
88 CheckHashesJob::status () const
89 {
90         stringstream s;
91         s << Job::status ();
92         if (overall_progress() > 0) {
93                 if (_bad == 0) {
94                         s << "; no bad frames found";
95                 } else if (_bad == 1) {
96                         s << "; 1 bad frame found";
97                 } else {
98                         s << "; " << _bad << " bad frames found";
99                 }
100         }
101         return s.str ();
102 }