0dd89228eade553360eac89a7c562ab344634d99
[libdcp.git] / src / filesystem.cc
1 /*
2     Copyright (C) 2012-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
35 #include "filesystem.h"
36 #include <boost/algorithm/string.hpp>
37
38
39 bool
40 dcp::filesystem::exists(boost::filesystem::path const& path)
41 {
42         return boost::filesystem::exists(dcp::filesystem::fix_long_path(path));
43 }
44
45
46 bool
47 dcp::filesystem::is_regular_file(boost::filesystem::path const& path)
48 {
49         return boost::filesystem::is_regular_file(dcp::filesystem::fix_long_path(path));
50 }
51
52
53 bool
54 dcp::filesystem::create_directory(boost::filesystem::path const& path)
55 {
56         return boost::filesystem::create_directory(dcp::filesystem::fix_long_path(path));
57 }
58
59
60 void
61 dcp::filesystem::copy(boost::filesystem::path const& from, boost::filesystem::path const& to)
62 {
63         boost::filesystem::copy(dcp::filesystem::fix_long_path(from), dcp::filesystem::fix_long_path(to));
64 }
65
66
67 void
68 dcp::filesystem::copy_file(boost::filesystem::path const& from, boost::filesystem::path const& to)
69 {
70         boost::filesystem::copy_file(dcp::filesystem::fix_long_path(from), dcp::filesystem::fix_long_path(to));
71 }
72
73
74 bool
75 dcp::filesystem::create_directories(boost::filesystem::path const& path)
76 {
77         return boost::filesystem::create_directories(dcp::filesystem::fix_long_path(path));
78 }
79
80
81 boost::filesystem::path
82 dcp::filesystem::absolute(boost::filesystem::path const& path)
83 {
84         return dcp::filesystem::unfix_long_path(boost::filesystem::absolute(dcp::filesystem::fix_long_path(path)));
85 }
86
87
88 boost::filesystem::path
89 dcp::filesystem::canonical(boost::filesystem::path const& path)
90 {
91         return dcp::filesystem::unfix_long_path(boost::filesystem::canonical(dcp::filesystem::fix_long_path(path)));
92 }
93
94
95 uintmax_t
96 dcp::filesystem::remove_all(boost::filesystem::path const& path)
97 {
98         return boost::filesystem::remove_all(dcp::filesystem::fix_long_path(path));
99 }
100
101
102 uintmax_t
103 dcp::filesystem::file_size(boost::filesystem::path const& path)
104 {
105         return boost::filesystem::file_size(dcp::filesystem::fix_long_path(path));
106 }
107
108
109 boost::filesystem::path
110 dcp::filesystem::current_path()
111 {
112         return dcp::filesystem::unfix_long_path(boost::filesystem::current_path());
113 }
114
115
116 void
117 dcp::filesystem::current_path(boost::filesystem::path const& path)
118 {
119         boost::filesystem::current_path(dcp::filesystem::fix_long_path(path));
120 }
121
122
123 void
124 dcp::filesystem::create_hard_link(boost::filesystem::path const& from, boost::filesystem::path const& to)
125 {
126         boost::filesystem::create_hard_link(dcp::filesystem::fix_long_path(from), dcp::filesystem::fix_long_path(to));
127 }
128
129
130 #ifdef DCPOMATIC_WINDOWS
131
132 dcp::filesystem::directory_iterator::directory_iterator(boost::filesystem::path const& path)
133         : _wrapped(dcp::filesystem::fix_long_path(path))
134 {
135
136 }
137
138
139 boost::filesystem::path
140 dcp::filesystem::directory_entry::path() const
141 {
142         return dcp::filesystem::unfix_long_path(_path);
143 }
144
145
146 dcp::filesystem::directory_entry::operator boost::filesystem::path const &() const
147 {
148         return dcp::filesystem::unfix_long_path(_path);
149 }
150
151
152 dcp::filesystem::recursive_directory_iterator::recursive_directory_iterator(boost::filesystem::path const& path)
153         : _wrapped(dcp::filesystem::fix_long_path(path))
154 {
155
156 }
157
158 #else
159
160 dcp::filesystem::directory_iterator::directory_iterator(boost::filesystem::path const& path)
161         : _wrapped(path)
162 {
163
164 }
165
166
167 boost::filesystem::path
168 dcp::filesystem::directory_entry::path() const
169 {
170         return _path;
171 }
172
173
174 dcp::filesystem::directory_entry::operator boost::filesystem::path const &() const
175 {
176         return _path;
177 }
178
179
180 dcp::filesystem::recursive_directory_iterator::recursive_directory_iterator(boost::filesystem::path const& path)
181         : _wrapped(path)
182 {
183
184 }
185
186 #endif
187
188
189 dcp::filesystem::directory_entry::directory_entry(boost::filesystem::path const& path)
190         : _path(path)
191 {
192
193 }
194
195
196 dcp::filesystem::directory_iterator&
197 dcp::filesystem::directory_iterator::operator++()
198 {
199         ++_wrapped;
200         return *this;
201 }
202
203
204 dcp::filesystem::directory_entry
205 dcp::filesystem::directory_iterator::operator*() const
206 {
207         return dcp::filesystem::directory_entry(*_wrapped);
208 }
209
210
211 bool
212 dcp::filesystem::directory_iterator::operator!=(dcp::filesystem::directory_iterator const& other) const
213 {
214         return _wrapped != other._wrapped;
215 }
216
217
218 dcp::filesystem::directory_iterator const&
219 dcp::filesystem::begin(dcp::filesystem::directory_iterator const& iter)
220 {
221         return iter;
222 }
223
224
225 dcp::filesystem::directory_iterator
226 dcp::filesystem::end(dcp::filesystem::directory_iterator const&)
227 {
228         return dcp::filesystem::directory_iterator();
229 }
230
231
232 dcp::filesystem::recursive_directory_iterator&
233 dcp::filesystem::recursive_directory_iterator::operator++()
234 {
235         ++_wrapped;
236         return *this;
237 }
238
239
240 bool
241 dcp::filesystem::recursive_directory_iterator::operator!=(dcp::filesystem::recursive_directory_iterator const& other) const
242 {
243         return _wrapped != other._wrapped;
244 }
245
246
247 dcp::filesystem::directory_entry
248 dcp::filesystem::recursive_directory_iterator::operator*() const
249 {
250         _entry = dcp::filesystem::directory_entry(_wrapped->path());
251         return _entry;
252 }
253
254
255 dcp::filesystem::directory_entry*
256 dcp::filesystem::recursive_directory_iterator::operator->() const
257 {
258         _entry = dcp::filesystem::directory_entry(_wrapped->path());
259         return &_entry;
260 }
261
262
263 dcp::filesystem::recursive_directory_iterator const&
264 dcp::filesystem::begin(dcp::filesystem::recursive_directory_iterator const& iter)
265 {
266         return iter;
267 }
268
269
270 dcp::filesystem::recursive_directory_iterator
271 dcp::filesystem::end(dcp::filesystem::recursive_directory_iterator const&)
272 {
273         return dcp::filesystem::recursive_directory_iterator();
274 }
275
276
277 /** Windows can't "by default" cope with paths longer than 260 characters, so if you pass such a path to
278  *  any boost::filesystem method it will fail.  There is a "fix" for this, which is to prepend
279  *  the string \\?\ to the path.  This will make it work, so long as:
280  *  - the path is absolute.
281  *  - the path contains no .. parts.
282  *  - the path only uses backslashes.
283  *  - individual path components are "short enough" (probably less than 255 characters)
284  *
285  *  See https://www.boost.org/doc/libs/1_57_0/libs/filesystem/doc/reference.html under
286  *  "Warning: Long paths on Windows" for some details.
287  *
288  *  Our fopen_boost uses this method to get this fix, but any other calls to boost::filesystem
289  *  will not unless this method is explicitly called to pre-process the pathname.
290  */
291 boost::filesystem::path
292 dcp::filesystem::fix_long_path(boost::filesystem::path long_path)
293 {
294 #ifdef LIBDCP_WINDOWS
295         using namespace boost::filesystem;
296
297         if (boost::algorithm::starts_with(long_path.string(), "\\\\")) {
298                 /* This could mean it starts with \\ (i.e. a SMB path) or \\?\ (a long path)
299                  * or a variety of other things... anyway, we'll leave it alone.
300                  */
301                 return long_path;
302         }
303
304         /* We have to make the path canonical but we can't call canonical() on the long path
305          * as it will fail.  So we'll sort of do it ourselves (possibly badly).
306          */
307         path fixed = "\\\\?\\";
308         if (long_path.is_absolute()) {
309                 fixed += long_path.make_preferred();
310         } else {
311                 fixed += filesystem::current_path() / long_path.make_preferred();
312         }
313         return fixed;
314 #else
315         return long_path;
316 #endif
317 }
318
319
320 boost::filesystem::path
321 dcp::filesystem::unfix_long_path(boost::filesystem::path long_path)
322 {
323 #ifdef LIBDCP_WINDOWS
324         if (boost::algorithm::starts_with(long_path.string(), "\\\\?\\")) {
325                 return long_path.string().substr(4);
326         }
327 #endif
328         return long_path;
329 }