Pass options only to jobs that need them.
[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, shared_ptr<Job> req)
35         : Job (s, l, req)
36         , _opt (o)
37         , _bad (0)
38 {
39
40 }
41
42 string
43 CheckHashesJob::name () const
44 {
45         return String::compose ("Check hashes of %1", _fs->name());
46 }
47
48 void
49 CheckHashesJob::run ()
50 {
51         _bad = 0;
52
53         int const N = _fs->dcp_length ();
54         
55         for (int i = 0; i < N; ++i) {
56                 string const j2k_file = _opt->frame_out_path (i, false);
57                 string const hash_file = j2k_file + ".md5";
58
59                 if (!filesystem::exists (j2k_file)) {
60                         _log->log (String::compose ("Frame %1 has a missing J2K file.", i));
61                         filesystem::remove (hash_file);
62                         ++_bad;
63                 } else if (!filesystem::exists (hash_file)) {
64                         _log->log (String::compose ("Frame %1 has a missing hash file.", i));
65                         filesystem::remove (j2k_file);
66                         ++_bad;
67                 } else {
68                         ifstream ref (hash_file.c_str ());
69                         string hash;
70                         ref >> hash;
71                         if (hash != md5_digest (j2k_file)) {
72                                 _log->log (String::compose ("Frame %1 has wrong hash; deleting.", i));
73                                 filesystem::remove (j2k_file);
74                                 filesystem::remove (hash_file);
75                                 ++_bad;
76                         }
77                 }
78
79                 set_progress (float (i) / _fs->length());
80         }
81
82         if (_bad) {
83                 shared_ptr<Job> tc;
84
85                 if (_fs->dcp_ab()) {
86                         tc.reset (new ABTranscodeJob (_fs, _opt, _log, shared_from_this()));
87                 } else {
88                         tc.reset (new TranscodeJob (_fs, _opt, _log, shared_from_this()));
89                 }
90                 
91                 JobManager::instance()->add_after (shared_from_this(), tc);
92                 JobManager::instance()->add_after (tc, shared_ptr<Job> (new CheckHashesJob (_fs, _opt, _log, tc)));
93         }
94                 
95         set_progress (1);
96         set_state (FINISHED_OK);
97 }
98
99 string
100 CheckHashesJob::status () const
101 {
102         stringstream s;
103         s << Job::status ();
104         if (overall_progress() > 0) {
105                 if (_bad == 0) {
106                         s << "; no bad frames found";
107                 } else if (_bad == 1) {
108                         s << "; 1 bad frame found";
109                 } else {
110                         s << "; " << _bad << " bad frames found";
111                 }
112         }
113         return s.str ();
114 }