2 Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
4 This file is part of libdcp.
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.
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.
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/>.
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
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.
35 #include "filesystem.h"
36 #include <boost/algorithm/string.hpp>
40 dcp::filesystem::exists(boost::filesystem::path const& path)
42 return boost::filesystem::exists(dcp::filesystem::fix_long_path(path));
47 dcp::filesystem::exists(boost::filesystem::path const& path, boost::system::error_code& ec)
49 return boost::filesystem::exists(dcp::filesystem::fix_long_path(path), ec);
54 dcp::filesystem::is_directory(boost::filesystem::path const& path)
56 return boost::filesystem::is_directory(dcp::filesystem::fix_long_path(path));
61 dcp::filesystem::is_empty(boost::filesystem::path const& path)
63 return boost::filesystem::is_empty(dcp::filesystem::fix_long_path(path));
68 dcp::filesystem::is_regular_file(boost::filesystem::path const& path)
70 return boost::filesystem::is_regular_file(dcp::filesystem::fix_long_path(path));
75 dcp::filesystem::create_directory(boost::filesystem::path const& path)
77 return boost::filesystem::create_directory(dcp::filesystem::fix_long_path(path));
82 dcp::filesystem::create_directory(boost::filesystem::path const& path, boost::system::error_code& ec)
84 return boost::filesystem::create_directory(dcp::filesystem::fix_long_path(path), ec);
89 dcp::filesystem::copy(boost::filesystem::path const& from, boost::filesystem::path const& to)
91 boost::filesystem::copy(dcp::filesystem::fix_long_path(from), dcp::filesystem::fix_long_path(to));
96 dcp::filesystem::copy_file(boost::filesystem::path const& from, boost::filesystem::path const& to)
98 boost::filesystem::copy_file(dcp::filesystem::fix_long_path(from), dcp::filesystem::fix_long_path(to));
103 dcp::filesystem::copy_file(boost::filesystem::path const& from, boost::filesystem::path const& to, boost::system::error_code& ec)
105 boost::filesystem::copy_file(dcp::filesystem::fix_long_path(from), dcp::filesystem::fix_long_path(to), ec);
110 dcp::filesystem::copy_file(boost::filesystem::path const& from, boost::filesystem::path const& to, boost::filesystem::copy_option option)
112 boost::filesystem::copy_file(dcp::filesystem::fix_long_path(from), dcp::filesystem::fix_long_path(to), option);
117 dcp::filesystem::create_directories(boost::filesystem::path const& path)
119 return boost::filesystem::create_directories(dcp::filesystem::fix_long_path(path));
124 dcp::filesystem::create_directories(boost::filesystem::path const& path, boost::system::error_code& ec)
126 return boost::filesystem::create_directories(dcp::filesystem::fix_long_path(path), ec);
130 boost::filesystem::path
131 dcp::filesystem::absolute(boost::filesystem::path const& path)
133 return dcp::filesystem::unfix_long_path(boost::filesystem::absolute(dcp::filesystem::fix_long_path(path)));
137 boost::filesystem::path
138 dcp::filesystem::canonical(boost::filesystem::path const& path)
140 return dcp::filesystem::unfix_long_path(boost::filesystem::canonical(dcp::filesystem::fix_long_path(path)));
144 boost::filesystem::path
145 dcp::filesystem::weakly_canonical(boost::filesystem::path const& path)
147 #ifdef DCPOMATIC_HAVE_WEAKLY_CANONICAL
148 return dcp::filesystem::unfix_long_path(boost::filesystem::weakly_canonical(dcp::filesystem::fix_long_path(path)));
150 boost::filesystem::path complete(boost::filesystem::system_complete(dcp::filesystem::fix_long_path(path)));
151 boost::filesystem::path result;
152 for (auto part: complete) {
154 boost::system::error_code ec;
155 if (boost::filesystem::is_symlink(result, ec) || result.filename() == "..") {
158 result = result.parent_path();
160 } else if (part != ".") {
165 return dcp::filesystem::unfix_long_path(result.make_preferred());
171 dcp::filesystem::remove(boost::filesystem::path const& path)
173 return boost::filesystem::remove(dcp::filesystem::fix_long_path(path));
178 dcp::filesystem::remove(boost::filesystem::path const& path, boost::system::error_code& ec)
180 return boost::filesystem::remove(dcp::filesystem::fix_long_path(path), ec);
185 dcp::filesystem::remove_all(boost::filesystem::path const& path)
187 return boost::filesystem::remove_all(dcp::filesystem::fix_long_path(path));
192 dcp::filesystem::remove_all(boost::filesystem::path const& path, boost::system::error_code& ec)
194 return boost::filesystem::remove_all(dcp::filesystem::fix_long_path(path), ec);
199 dcp::filesystem::file_size(boost::filesystem::path const& path)
201 return boost::filesystem::file_size(dcp::filesystem::fix_long_path(path));
206 dcp::filesystem::file_size(boost::filesystem::path const& path, boost::system::error_code& ec)
208 return boost::filesystem::file_size(dcp::filesystem::fix_long_path(path), ec);
212 boost::filesystem::path
213 dcp::filesystem::current_path()
215 return dcp::filesystem::unfix_long_path(boost::filesystem::current_path());
220 dcp::filesystem::current_path(boost::filesystem::path const& path)
222 boost::filesystem::current_path(dcp::filesystem::fix_long_path(path));
227 dcp::filesystem::create_hard_link(boost::filesystem::path const& from, boost::filesystem::path const& to)
229 boost::filesystem::create_hard_link(dcp::filesystem::fix_long_path(from), dcp::filesystem::fix_long_path(to));
234 dcp::filesystem::create_hard_link(boost::filesystem::path const& from, boost::filesystem::path const& to, boost::system::error_code& ec)
236 boost::filesystem::create_hard_link(dcp::filesystem::fix_long_path(from), dcp::filesystem::fix_long_path(to), ec);
241 dcp::filesystem::create_symlink(boost::filesystem::path const& from, boost::filesystem::path const& to, boost::system::error_code& ec)
243 boost::filesystem::create_symlink(dcp::filesystem::fix_long_path(from), dcp::filesystem::fix_long_path(to), ec);
248 dcp::filesystem::extension(boost::filesystem::path const& path)
250 return boost::filesystem::extension(dcp::filesystem::fix_long_path(path));
254 boost::filesystem::space_info
255 dcp::filesystem::space(boost::filesystem::path const& path)
257 return boost::filesystem::space(dcp::filesystem::fix_long_path(path));
262 dcp::filesystem::last_write_time(boost::filesystem::path const& path)
264 return boost::filesystem::last_write_time(dcp::filesystem::fix_long_path(path));
269 dcp::filesystem::last_write_time(boost::filesystem::path const& path, boost::system::error_code& ec)
271 return boost::filesystem::last_write_time(dcp::filesystem::fix_long_path(path), ec);
276 dcp::filesystem::hard_link_count(boost::filesystem::path const& path)
278 return boost::filesystem::hard_link_count(dcp::filesystem::fix_long_path(path));
283 dcp::filesystem::rename(boost::filesystem::path const& old_path, boost::filesystem::path const& new_path)
285 boost::filesystem::rename(dcp::filesystem::fix_long_path(old_path), dcp::filesystem::fix_long_path(new_path));
290 dcp::filesystem::rename(boost::filesystem::path const& old_path, boost::filesystem::path const& new_path, boost::system::error_code& ec)
292 boost::filesystem::rename(dcp::filesystem::fix_long_path(old_path), dcp::filesystem::fix_long_path(new_path), ec);
296 /* We don't really need this but let's add it for completeness */
297 boost::filesystem::path
298 dcp::filesystem::change_extension(boost::filesystem::path const& path, std::string const& new_extension)
300 return boost::filesystem::change_extension(path, new_extension);
304 #ifdef DCPOMATIC_WINDOWS
306 dcp::filesystem::directory_iterator::directory_iterator(boost::filesystem::path const& path)
307 : _wrapped(dcp::filesystem::fix_long_path(path))
313 dcp::filesystem::directory_iterator::directory_iterator(boost::filesystem::path const& path, boost::system::error_code& ec)
314 : _wrapped(dcp::filesystem::fix_long_path(path), ec)
320 boost::filesystem::path
321 dcp::filesystem::directory_entry::path() const
323 return dcp::filesystem::unfix_long_path(_path);
327 dcp::filesystem::directory_entry::operator boost::filesystem::path const &() const
329 return dcp::filesystem::unfix_long_path(_path);
333 dcp::filesystem::recursive_directory_iterator::recursive_directory_iterator(boost::filesystem::path const& path)
334 : _wrapped(dcp::filesystem::fix_long_path(path))
341 dcp::filesystem::directory_iterator::directory_iterator(boost::filesystem::path const& path)
348 dcp::filesystem::directory_iterator::directory_iterator(boost::filesystem::path const& path, boost::system::error_code& ec)
355 boost::filesystem::path
356 dcp::filesystem::directory_entry::path() const
362 dcp::filesystem::directory_entry::operator boost::filesystem::path const &() const
368 dcp::filesystem::recursive_directory_iterator::recursive_directory_iterator(boost::filesystem::path const& path)
377 dcp::filesystem::directory_entry::directory_entry(boost::filesystem::path const& path)
384 dcp::filesystem::directory_iterator&
385 dcp::filesystem::directory_iterator::operator++()
392 dcp::filesystem::directory_entry
393 dcp::filesystem::directory_iterator::operator*() const
395 _entry = dcp::filesystem::directory_entry(*_wrapped);
400 dcp::filesystem::directory_entry*
401 dcp::filesystem::directory_iterator::operator->() const
403 _entry = dcp::filesystem::directory_entry(_wrapped->path());
408 dcp::filesystem::directory_iterator::operator!=(dcp::filesystem::directory_iterator const& other) const
410 return _wrapped != other._wrapped;
414 dcp::filesystem::directory_iterator const&
415 dcp::filesystem::begin(dcp::filesystem::directory_iterator const& iter)
421 dcp::filesystem::directory_iterator
422 dcp::filesystem::end(dcp::filesystem::directory_iterator const&)
424 return dcp::filesystem::directory_iterator();
428 dcp::filesystem::recursive_directory_iterator&
429 dcp::filesystem::recursive_directory_iterator::operator++()
437 dcp::filesystem::recursive_directory_iterator::operator!=(dcp::filesystem::recursive_directory_iterator const& other) const
439 return _wrapped != other._wrapped;
443 dcp::filesystem::directory_entry
444 dcp::filesystem::recursive_directory_iterator::operator*() const
446 _entry = dcp::filesystem::directory_entry(_wrapped->path());
451 dcp::filesystem::directory_entry*
452 dcp::filesystem::recursive_directory_iterator::operator->() const
454 _entry = dcp::filesystem::directory_entry(_wrapped->path());
459 dcp::filesystem::recursive_directory_iterator const&
460 dcp::filesystem::begin(dcp::filesystem::recursive_directory_iterator const& iter)
466 dcp::filesystem::recursive_directory_iterator
467 dcp::filesystem::end(dcp::filesystem::recursive_directory_iterator const&)
469 return dcp::filesystem::recursive_directory_iterator();
473 /** Windows can't "by default" cope with paths longer than 260 characters, so if you pass such a path to
474 * any boost::filesystem method it will fail. There is a "fix" for this, which is to prepend
475 * the string \\?\ to the path. This will make it work, so long as:
476 * - the path is absolute.
477 * - the path contains no .. parts.
478 * - the path only uses backslashes.
479 * - individual path components are "short enough" (probably less than 255 characters)
481 * See https://www.boost.org/doc/libs/1_57_0/libs/filesystem/doc/reference.html under
482 * "Warning: Long paths on Windows" for some details.
484 * Our fopen_boost uses this method to get this fix, but any other calls to boost::filesystem
485 * will not unless this method is explicitly called to pre-process the pathname.
487 boost::filesystem::path
488 dcp::filesystem::fix_long_path(boost::filesystem::path long_path)
490 #ifdef LIBDCP_WINDOWS
491 using namespace boost::filesystem;
493 if (boost::algorithm::starts_with(long_path.string(), "\\\\")) {
494 /* This could mean it starts with \\ (i.e. a SMB path) or \\?\ (a long path)
495 * or a variety of other things... anyway, we'll leave it alone.
500 /* We have to make the path canonical but we can't call canonical() on the long path
501 * as it will fail. So we'll sort of do it ourselves (possibly badly).
503 path fixed = "\\\\?\\";
504 if (long_path.is_absolute()) {
505 fixed += long_path.make_preferred();
507 fixed += filesystem::current_path() / long_path.make_preferred();
516 boost::filesystem::path
517 dcp::filesystem::unfix_long_path(boost::filesystem::path long_path)
519 #ifdef LIBDCP_WINDOWS
520 if (boost::algorithm::starts_with(long_path.string(), "\\\\?\\")) {
521 return long_path.string().substr(4);