Bump libcxml for comment-in-node-data fix.
[libdcp.git] / src / asset.cc
1 /*
2     Copyright (C) 2014-2015 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 /** @file  src/asset.cc
35  *  @brief Asset class.
36  */
37
38 #include "raw_convert.h"
39 #include "asset.h"
40 #include "util.h"
41 #include "exceptions.h"
42 #include "dcp_assert.h"
43 #include "compose.hpp"
44 #include <libxml++/libxml++.h>
45 #include <boost/algorithm/string.hpp>
46
47 using std::string;
48 using boost::function;
49 using boost::optional;
50 using namespace dcp;
51
52 /** Create an Asset with a randomly-generated ID */
53 Asset::Asset ()
54 {
55
56 }
57
58 /** Create an Asset from a given file.
59  *  @param file File name.
60  */
61 Asset::Asset (boost::filesystem::path file)
62         : _file (file)
63 {
64
65 }
66
67 /** Create an Asset from a given file with a known ID.
68  *  @param file File name.
69  *  @param id ID.
70  */
71 Asset::Asset (string id, boost::filesystem::path file)
72         : Object (id)
73         , _file (file)
74 {
75
76 }
77
78 void
79 Asset::write_to_pkl (xmlpp::Node* node, boost::filesystem::path root, Standard standard) const
80 {
81         DCP_ASSERT (_file);
82
83         optional<boost::filesystem::path> path = relative_to_root (
84                 boost::filesystem::canonical (root),
85                 boost::filesystem::canonical (_file.get())
86                 );
87
88         if (!path) {
89                 /* The path of this asset is not within our DCP, so we assume it's an external
90                    (referenced) one.
91                 */
92                 return;
93         }
94
95         xmlpp::Node* asset = node->add_child ("Asset");
96         asset->add_child("Id")->add_child_text ("urn:uuid:" + _id);
97         asset->add_child("AnnotationText")->add_child_text (_id);
98         asset->add_child("Hash")->add_child_text (hash ());
99         asset->add_child("Size")->add_child_text (raw_convert<string> (boost::filesystem::file_size (_file.get())));
100         asset->add_child("Type")->add_child_text (pkl_type (standard));
101 }
102
103 void
104 Asset::write_to_assetmap (xmlpp::Node* node, boost::filesystem::path root) const
105 {
106         DCP_ASSERT (_file);
107
108         optional<boost::filesystem::path> path = relative_to_root (
109                 boost::filesystem::canonical (root),
110                 boost::filesystem::canonical (_file.get())
111                 );
112
113         if (!path) {
114                 /* The path of this asset is not within our DCP, so we assume it's an external
115                    (referenced) one.
116                 */
117                 return;
118         }
119
120         xmlpp::Node* asset = node->add_child ("Asset");
121         asset->add_child("Id")->add_child_text ("urn:uuid:" + _id);
122         xmlpp::Node* chunk_list = asset->add_child ("ChunkList");
123         xmlpp::Node* chunk = chunk_list->add_child ("Chunk");
124
125         chunk->add_child("Path")->add_child_text (path.get().generic_string());
126         chunk->add_child("VolumeIndex")->add_child_text ("1");
127         chunk->add_child("Offset")->add_child_text ("0");
128         chunk->add_child("Length")->add_child_text (raw_convert<string> (boost::filesystem::file_size (_file.get())));
129 }
130
131 string
132 Asset::hash (function<void (float)> progress) const
133 {
134         DCP_ASSERT (_file);
135
136         if (!_hash) {
137                 _hash = make_digest (_file.get(), progress);
138         }
139
140         return _hash.get();
141 }
142
143 bool
144 Asset::equals (boost::shared_ptr<const Asset> other, EqualityOptions, NoteHandler note) const
145 {
146         if (_hash != other->_hash) {
147                 note (DCP_ERROR, "Asset: hashes differ");
148                 return false;
149         }
150
151         return true;
152 }
153
154 /** Set the file that holds this asset on disk.  Calling this function
155  *  clears this object's store of its hash, so you should call ::hash
156  *  after this.
157  *
158  *  @param file New file's path.
159  */
160 void
161 Asset::set_file (boost::filesystem::path file) const
162 {
163         _file = boost::filesystem::absolute (file);
164         _hash = optional<string> ();
165 }
166
167 void
168 Asset::set_hash (string hash)
169 {
170         _hash = hash;
171 }