87eb40d144a0a4926155b48b7aeab869d7f767a8
[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
28 using namespace std;
29 using namespace boost;
30
31 CheckHashesJob::CheckHashesJob (shared_ptr<const FilmState> s, shared_ptr<const Options> o, Log* l)
32         : Job (s, o, l)
33         , _bad (0)
34 {
35
36 }
37
38 string
39 CheckHashesJob::name () const
40 {
41         stringstream s;
42         s << "Check hashes of " << _fs->name;
43         return s.str ();
44 }
45
46 void
47 CheckHashesJob::run ()
48 {
49         _bad = 0;
50         
51         for (int i = 0; i < _fs->length; ++i) {
52                 string const j2k_file = _opt->frame_out_path (i, false);
53                 string const hash_file = j2k_file + ".md5";
54
55                 ifstream ref (hash_file.c_str ());
56                 string hash;
57                 ref >> hash;
58
59                 if (hash != md5_digest (j2k_file)) {
60                         _log->log ("Frame " + lexical_cast<string> (i) + " has wrong hash; deleting.");
61                         filesystem::remove (j2k_file);
62                         filesystem::remove (hash_file);
63                         ++_bad;
64                 }
65
66                 set_progress (float (i) / _fs->length);
67         }
68
69         set_progress (1);
70         set_state (FINISHED_OK);
71 }
72
73 string
74 CheckHashesJob::status () const
75 {
76         stringstream s;
77         s << Job::status () << "; " << _bad << " bad frames found";
78         return s.str ();
79 }