libdcp
asset.cc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2021 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 
40 #include "asset.h"
41 #include "asset_map.h"
42 #include "compose.hpp"
43 #include "dcp_assert.h"
44 #include "equality_options.h"
45 #include "exceptions.h"
46 #include "filesystem.h"
47 #include "pkl.h"
48 #include "raw_convert.h"
49 #include "util.h"
50 #include "warnings.h"
51 LIBDCP_DISABLE_WARNINGS
52 #include <libxml++/libxml++.h>
53 LIBDCP_ENABLE_WARNINGS
54 #include <boost/algorithm/string.hpp>
55 
56 
57 using std::string;
58 using boost::function;
59 using std::shared_ptr;
60 using boost::optional;
61 using namespace dcp;
62 
63 
65 {
66 
67 }
68 
69 
70 Asset::Asset(boost::filesystem::path file)
71  : _file (file)
72 {
73 
74 }
75 
76 
77 Asset::Asset(string id, boost::filesystem::path file)
78  : Object (id)
79  , _file (file)
80 {
81 
82 }
83 
84 
85 void
86 Asset::add_to_pkl(shared_ptr<PKL> pkl, boost::filesystem::path root) const
87 {
88  DCP_ASSERT (_file);
89 
90  auto path = relative_to_root (
91  filesystem::canonical(root),
92  filesystem::canonical(_file.get())
93  );
94 
95  if (!path) {
96  /* The path of this asset is not within our DCP, so we assume it's an external
97  (referenced) one.
98  */
99  return;
100  }
101 
102  pkl->add_asset(_id, _id, hash(), file_size(_file.get()), pkl_type(pkl->standard()), _file->filename().string());
103 }
104 
105 
106 void
107 Asset::add_to_assetmap(AssetMap& asset_map, boost::filesystem::path root) const
108 {
109  DCP_ASSERT (_file);
110  add_file_to_assetmap (asset_map, root, _file.get(), _id);
111 }
112 
113 
114 void
115 Asset::add_file_to_assetmap(AssetMap& asset_map, boost::filesystem::path root, boost::filesystem::path file, string id)
116 {
117  auto path = relative_to_root (
118  filesystem::canonical(root),
119  filesystem::canonical(file)
120  );
121 
122  if (!path) {
123  /* The path of this asset is not within our DCP, so we assume it's an external
124  (referenced) one.
125  */
126  return;
127  }
128 
129  asset_map.add_asset(id, file, false);
130 }
131 
132 
133 string
134 Asset::hash(function<void (int64_t, int64_t)> progress) const
135 {
136  DCP_ASSERT (_file);
137 
138  if (!_hash) {
139  _hash = make_digest (_file.get(), progress);
140  }
141 
142  return _hash.get();
143 }
144 
145 
146 bool
147 Asset::equals(std::shared_ptr<const Asset> other, EqualityOptions const& opt, NoteHandler note) const
148 {
149  if (_hash != other->_hash) {
150  if (!opt.asset_hashes_can_differ) {
151  note(NoteType::ERROR, "Asset: hashes differ");
152  return false;
153  } else {
154  note(NoteType::NOTE, "Asset: hashes differ");
155  }
156  }
157 
158  return true;
159 }
160 
161 
162 void
163 Asset::set_file(boost::filesystem::path file) const
164 {
165  _file = filesystem::absolute(file);
166  _hash = optional<string>();
167 }
168 
169 
170 void
171 Asset::set_file_preserving_hash(boost::filesystem::path file) const
172 {
173  _file = filesystem::absolute(file);
174 }
175 
176 
177 void
178 Asset::rename_file(boost::filesystem::path file)
179 {
180  _file = filesystem::absolute(file);
181 }
182 
183 
184 void
185 Asset::set_hash (string hash)
186 {
187  _hash = hash;
188 }
189 
190 
191 void
192 Asset::unset_hash()
193 {
194  _hash = optional<string>();
195 }
196 
Asset class.
virtual std::string pkl_type(Standard standard) const =0
boost::optional< boost::filesystem::path > file() const
Definition: asset.h:100
boost::optional< boost::filesystem::path > _file
Definition: asset.h:143
void set_file_preserving_hash(boost::filesystem::path file) const
Definition: asset.cc:171
boost::optional< std::string > _hash
Definition: asset.h:154
std::string hash(boost::function< void(int64_t, int64_t)> progress={}) const
Asset()
Definition: asset.cc:64
void set_file(boost::filesystem::path file) const
Definition: asset.cc:163
void rename_file(boost::filesystem::path file)
Definition: asset.cc:178
A class to describe what "equality" means for a particular test.
Some part of a DCP that has a UUID.
Definition: object.h:63
DCP_ASSERT macro.
Class to describe what equality means when calling Asset::equals().
Exceptions thrown by libdcp.
Namespace for everything in libdcp.
Definition: array_data.h:50
std::string make_digest(boost::filesystem::path filename, boost::function< void(int64_t, int64_t)>)
PKL class.
Methods for conversion to/from string.
Utility methods and classes.