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::is_regular_file(boost::filesystem::path const& path)
49 return boost::filesystem::is_regular_file(dcp::filesystem::fix_long_path(path));
54 dcp::filesystem::create_directory(boost::filesystem::path const& path)
56 return boost::filesystem::create_directory(dcp::filesystem::fix_long_path(path));
61 dcp::filesystem::copy(boost::filesystem::path const& from, boost::filesystem::path const& to)
63 boost::filesystem::copy(dcp::filesystem::fix_long_path(from), dcp::filesystem::fix_long_path(to));
68 dcp::filesystem::copy_file(boost::filesystem::path const& from, boost::filesystem::path const& to)
70 boost::filesystem::copy_file(dcp::filesystem::fix_long_path(from), dcp::filesystem::fix_long_path(to));
75 dcp::filesystem::create_directories(boost::filesystem::path const& path)
77 return boost::filesystem::create_directories(dcp::filesystem::fix_long_path(path));
81 boost::filesystem::path
82 dcp::filesystem::absolute(boost::filesystem::path const& path)
84 return dcp::filesystem::unfix_long_path(boost::filesystem::absolute(dcp::filesystem::fix_long_path(path)));
88 boost::filesystem::path
89 dcp::filesystem::canonical(boost::filesystem::path const& path)
91 return dcp::filesystem::unfix_long_path(boost::filesystem::canonical(dcp::filesystem::fix_long_path(path)));
96 dcp::filesystem::remove_all(boost::filesystem::path const& path)
98 return boost::filesystem::remove_all(dcp::filesystem::fix_long_path(path));
103 dcp::filesystem::file_size(boost::filesystem::path const& path)
105 return boost::filesystem::file_size(dcp::filesystem::fix_long_path(path));
109 boost::filesystem::path
110 dcp::filesystem::current_path()
112 return dcp::filesystem::unfix_long_path(boost::filesystem::current_path());
117 dcp::filesystem::current_path(boost::filesystem::path const& path)
119 boost::filesystem::current_path(dcp::filesystem::fix_long_path(path));
124 dcp::filesystem::create_hard_link(boost::filesystem::path const& from, boost::filesystem::path const& to)
126 boost::filesystem::create_hard_link(dcp::filesystem::fix_long_path(from), dcp::filesystem::fix_long_path(to));
130 #ifdef DCPOMATIC_WINDOWS
132 dcp::filesystem::directory_iterator::directory_iterator(boost::filesystem::path const& path)
133 : _wrapped(dcp::filesystem::fix_long_path(path))
139 boost::filesystem::path
140 dcp::filesystem::directory_entry::path() const
142 return dcp::filesystem::unfix_long_path(_path);
146 dcp::filesystem::directory_entry::operator boost::filesystem::path const &() const
148 return dcp::filesystem::unfix_long_path(_path);
152 dcp::filesystem::recursive_directory_iterator::recursive_directory_iterator(boost::filesystem::path const& path)
153 : _wrapped(dcp::filesystem::fix_long_path(path))
160 dcp::filesystem::directory_iterator::directory_iterator(boost::filesystem::path const& path)
167 boost::filesystem::path
168 dcp::filesystem::directory_entry::path() const
174 dcp::filesystem::directory_entry::operator boost::filesystem::path const &() const
180 dcp::filesystem::recursive_directory_iterator::recursive_directory_iterator(boost::filesystem::path const& path)
189 dcp::filesystem::directory_entry::directory_entry(boost::filesystem::path const& path)
196 dcp::filesystem::directory_iterator&
197 dcp::filesystem::directory_iterator::operator++()
204 dcp::filesystem::directory_entry
205 dcp::filesystem::directory_iterator::operator*() const
207 return dcp::filesystem::directory_entry(*_wrapped);
212 dcp::filesystem::directory_iterator::operator!=(dcp::filesystem::directory_iterator const& other) const
214 return _wrapped != other._wrapped;
218 dcp::filesystem::directory_iterator const&
219 dcp::filesystem::begin(dcp::filesystem::directory_iterator const& iter)
225 dcp::filesystem::directory_iterator
226 dcp::filesystem::end(dcp::filesystem::directory_iterator const&)
228 return dcp::filesystem::directory_iterator();
232 dcp::filesystem::recursive_directory_iterator&
233 dcp::filesystem::recursive_directory_iterator::operator++()
241 dcp::filesystem::recursive_directory_iterator::operator!=(dcp::filesystem::recursive_directory_iterator const& other) const
243 return _wrapped != other._wrapped;
247 dcp::filesystem::directory_entry
248 dcp::filesystem::recursive_directory_iterator::operator*() const
250 _entry = dcp::filesystem::directory_entry(_wrapped->path());
255 dcp::filesystem::directory_entry*
256 dcp::filesystem::recursive_directory_iterator::operator->() const
258 _entry = dcp::filesystem::directory_entry(_wrapped->path());
263 dcp::filesystem::recursive_directory_iterator const&
264 dcp::filesystem::begin(dcp::filesystem::recursive_directory_iterator const& iter)
270 dcp::filesystem::recursive_directory_iterator
271 dcp::filesystem::end(dcp::filesystem::recursive_directory_iterator const&)
273 return dcp::filesystem::recursive_directory_iterator();
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)
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.
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.
291 boost::filesystem::path
292 dcp::filesystem::fix_long_path(boost::filesystem::path long_path)
294 #ifdef LIBDCP_WINDOWS
295 using namespace boost::filesystem;
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.
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).
307 path fixed = "\\\\?\\";
308 if (long_path.is_absolute()) {
309 fixed += long_path.make_preferred();
311 fixed += filesystem::current_path() / long_path.make_preferred();
320 boost::filesystem::path
321 dcp::filesystem::unfix_long_path(boost::filesystem::path long_path)
323 #ifdef LIBDCP_WINDOWS
324 if (boost::algorithm::starts_with(long_path.string(), "\\\\?\\")) {
325 return long_path.string().substr(4);