Add --no-asset-hash-check and --asset-hash-check-maximum-size (DoM #2444).
[libdcp.git] / tools / dcpverify.cc
1 /*
2     Copyright (C) 2020 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
6     libdcp 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 2 of the License, or
9     (at your option) any later version.
10
11     libdcp 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.
15
16     You should have received a copy of the GNU General Public License
17     along with libdcp.  If not, see <http://www.gnu.org/licenses/>.
18
19     In addition, as a special exception, the copyright holders give
20     permission to link the code of portions of this program with the
21     OpenSSL library under certain conditions as described in each
22     individual source file, and distribute linked combinations
23     including the two.
24
25     You must obey the GNU General Public License in all respects
26     for all of the code used other than OpenSSL.  If you modify
27     file(s) with this exception, you may extend this exception to your
28     version of the file(s), but you are not obligated to do so.  If you
29     do not wish to do so, delete this exception statement from your
30     version.  If you delete this exception statement from all source
31     files in the program, then also delete it here.
32 */
33
34 #include "verify.h"
35 #include "compose.hpp"
36 #include "common.h"
37 #include "raw_convert.h"
38 #include <boost/bind.hpp>
39 #include <boost/optional.hpp>
40 #include <boost/filesystem.hpp>
41 #include <getopt.h>
42 #include <iostream>
43 #include <cstdlib>
44
45 using std::cout;
46 using std::cerr;
47 using std::string;
48 using std::vector;
49 using std::list;
50 using boost::bind;
51 using boost::optional;
52
53 static void
54 help (string n)
55 {
56         cerr << "Syntax: " << n << " [OPTION] <DCP>\n"
57              << "  -V, --version                                show libdcp version\n"
58              << "  -h, --help                                   show this help\n"
59              << "  --ignore-missing-assets                      don't give errors about missing assets\n"
60              << "  --ignore-bv21-smpte                          don't give the SMPTE Bv2.1 error about a DCP not being SMPTE\n"
61              << "  --no-asset-hash-check                        don't check asset hashes\n"
62              << "  --asset-hash-check-maximum-size <size-in-MB> only check hashes for assets smaller than this size (in MB)\n"
63              << "  -q, --quiet                                  don't report progress\n";
64 }
65
66
67 int
68 main (int argc, char* argv[])
69 {
70         dcp::init ();
71
72         bool ignore_missing_assets = false;
73         bool ignore_bv21_smpte = false;
74         bool quiet = false;
75
76         dcp::VerificationOptions verification_options;
77
78         int option_index = 0;
79         while (true) {
80                 static struct option long_options[] = {
81                         { "version", no_argument, 0, 'V' },
82                         { "help", no_argument, 0, 'h' },
83                         { "ignore-missing-assets", no_argument, 0, 'A' },
84                         { "ignore-bv21-smpte", no_argument, 0, 'B' },
85                         { "no-asset-hash-check", no_argument, 0, 'C' },
86                         { "asset-hash-check-maximum-size", required_argument, 0, 'D' },
87                         { "quiet", no_argument, 0, 'q' },
88                         { 0, 0, 0, 0 }
89                 };
90
91                 int c = getopt_long (argc, argv, "VhABCD:q", long_options, &option_index);
92
93                 if (c == -1) {
94                         break;
95                 } else if (c == '?' || c == ':') {
96                         exit(EXIT_FAILURE);
97                 }
98
99                 switch (c) {
100                 case 'V':
101                         cout << "dcpverify version " << LIBDCP_VERSION << "\n";
102                         exit (EXIT_SUCCESS);
103                 case 'h':
104                         help (argv[0]);
105                         exit (EXIT_SUCCESS);
106                 case 'A':
107                         ignore_missing_assets = true;
108                         break;
109                 case 'B':
110                         ignore_bv21_smpte = true;
111                         break;
112                 case 'C':
113                         verification_options.check_asset_hashes = false;
114                         break;
115                 case 'D':
116                         verification_options.maximum_asset_size_for_hash_check = dcp::raw_convert<int>(optarg) * 1000000LL;
117                         break;
118                 case 'q':
119                         quiet = true;
120                         break;
121                 }
122         }
123
124         if (argc <= optind) {
125                 help (argv[0]);
126                 exit (EXIT_FAILURE);
127         }
128
129         if (!boost::filesystem::exists (argv[optind])) {
130                 cerr << argv[0] << ": DCP " << argv[optind] << " not found.\n";
131                 exit (EXIT_FAILURE);
132         }
133
134         auto stage = [quiet](string s, optional<boost::filesystem::path> path) {
135                 if (quiet) {
136                         return;
137                 }
138
139                 if (path) {
140                         cout << s << ": " << path->string() << "\n";
141                 } else {
142                         cout << s << "\n";
143                 }
144         };
145
146         auto progress = [quiet](float amount) {
147                 if (quiet) {
148                         return;
149                 }
150                 int const width = 60;
151                 int const index = std::rint(amount * width);
152                 cout << "[";
153                 for (int i = 0; i < width; ++i) {
154                         if (i < index) {
155                                 std::cout << "=";
156                         } else if (i == index) {
157                                 std::cout << ">";
158                         } else {
159                                 std::cout << " ";
160                         }
161                 }
162                 cout << "] " << std::rint(amount * 100) << "%\r";
163                 cout.flush();
164         };
165
166         vector<boost::filesystem::path> directories;
167         directories.push_back (argv[optind]);
168         auto notes = dcp::verify(directories, stage, progress, verification_options);
169         dcp::filter_notes (notes, ignore_missing_assets);
170
171         if (!quiet) {
172                 cout << "\n";
173         }
174
175         bool failed = false;
176         bool bv21_failed = false;
177         bool warned = false;
178         for (auto i: notes) {
179                 if (ignore_bv21_smpte && i.code() == dcp::VerificationNote::Code::INVALID_STANDARD) {
180                         continue;
181                 }
182                 switch (i.type()) {
183                 case dcp::VerificationNote::Type::ERROR:
184                         cout << "Error: " << note_to_string(i) << "\n";
185                         failed = true;
186                         break;
187                 case dcp::VerificationNote::Type::BV21_ERROR:
188                         cout << "Bv2.1 error: " << note_to_string(i) << "\n";
189                         bv21_failed = true;
190                         break;
191                 case dcp::VerificationNote::Type::WARNING:
192                         cout << "Warning: " << note_to_string(i) << "\n";
193                         warned = true;
194                         break;
195                 }
196         }
197
198         if (!failed && !quiet) {
199                 if (bv21_failed || warned) {
200                         cout << "\n";
201                 }
202                 cout << "DCP verified OK.\n";
203         }
204
205         exit (failed ? EXIT_FAILURE : EXIT_SUCCESS);
206 }