summaryrefslogtreecommitdiff
path: root/src/filesystem.cc
blob: 4cbbc3d4c513e7711a83bfc3bd78ec2a0d5cc39a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/*
    Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>

    This file is part of libdcp.

    libdcp is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    libdcp is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with libdcp.  If not, see <http://www.gnu.org/licenses/>.

    In addition, as a special exception, the copyright holders give
    permission to link the code of portions of this program with the
    OpenSSL library under certain conditions as described in each
    individual source file, and distribute linked combinations
    including the two.

    You must obey the GNU General Public License in all respects
    for all of the code used other than OpenSSL.  If you modify
    file(s) with this exception, you may extend this exception to your
    version of the file(s), but you are not obligated to do so.  If you
    do not wish to do so, delete this exception statement from your
    version.  If you delete this exception statement from all source
    files in the program, then also delete it here.
*/


#include "filesystem.h"
#include <boost/algorithm/string.hpp>


bool
dcp::filesystem::exists(boost::filesystem::path const& path)
{
	return boost::filesystem::exists(dcp::filesystem::fix_long_path(path));
}


bool
dcp::filesystem::exists(boost::filesystem::path const& path, boost::system::error_code& ec)
{
	return boost::filesystem::exists(dcp::filesystem::fix_long_path(path), ec);
}


bool
dcp::filesystem::is_directory(boost::filesystem::path const& path)
{
	return boost::filesystem::is_directory(dcp::filesystem::fix_long_path(path));
}


bool
dcp::filesystem::is_empty(boost::filesystem::path const& path)
{
	return boost::filesystem::is_empty(dcp::filesystem::fix_long_path(path));
}


bool
dcp::filesystem::is_regular_file(boost::filesystem::path const& path)
{
	return boost::filesystem::is_regular_file(dcp::filesystem::fix_long_path(path));
}


bool
dcp::filesystem::create_directory(boost::filesystem::path const& path)
{
	return boost::filesystem::create_directory(dcp::filesystem::fix_long_path(path));
}


bool
dcp::filesystem::create_directory(boost::filesystem::path const& path, boost::system::error_code& ec)
{
	return boost::filesystem::create_directory(dcp::filesystem::fix_long_path(path), ec);
}


void
dcp::filesystem::copy(boost::filesystem::path const& from, boost::filesystem::path const& to)
{
	boost::filesystem::copy(dcp::filesystem::fix_long_path(from), dcp::filesystem::fix_long_path(to));
}


void
dcp::filesystem::copy_file(boost::filesystem::path const& from, boost::filesystem::path const& to)
{
	boost::filesystem::copy_file(dcp::filesystem::fix_long_path(from), dcp::filesystem::fix_long_path(to));
}


void
dcp::filesystem::copy_file(boost::filesystem::path const& from, boost::filesystem::path const& to, boost::system::error_code& ec)
{
	boost::filesystem::copy_file(dcp::filesystem::fix_long_path(from), dcp::filesystem::fix_long_path(to), ec);
}


void
dcp::filesystem::copy_file(boost::filesystem::path const& from, boost::filesystem::path const& to, CopyOptions option)
{
#ifdef LIBDCP_HAVE_COPY_OPTIONS
	auto const options = option == CopyOptions::OVERWRITE_EXISTING ? boost::filesystem::copy_options::overwrite_existing : boost::filesystem::copy_options::none;
#else
	auto const options = option == CopyOptions::OVERWRITE_EXISTING ? boost::filesystem::copy_option::overwrite_if_exists : boost::filesystem::copy_option::none;
#endif

	boost::filesystem::copy_file(dcp::filesystem::fix_long_path(from), dcp::filesystem::fix_long_path(to), options);
}


bool
dcp::filesystem::create_directories(boost::filesystem::path const& path)
{
	return boost::filesystem::create_directories(dcp::filesystem::fix_long_path(path));
}


bool
dcp::filesystem::create_directories(boost::filesystem::path const& path, boost::system::error_code& ec)
{
	return boost::filesystem::create_directories(dcp::filesystem::fix_long_path(path), ec);
}


boost::filesystem::path
dcp::filesystem::absolute(boost::filesystem::path const& path)
{
	return dcp::filesystem::unfix_long_path(boost::filesystem::absolute(dcp::filesystem::fix_long_path(path)));
}


boost::filesystem::path
dcp::filesystem::canonical(boost::filesystem::path const& path)
{
	return dcp::filesystem::unfix_long_path(boost::filesystem::canonical(dcp::filesystem::fix_long_path(path)));
}


boost::filesystem::path
dcp::filesystem::weakly_canonical(boost::filesystem::path const& path)
{
#ifdef DCPOMATIC_HAVE_WEAKLY_CANONICAL
	return dcp::filesystem::unfix_long_path(boost::filesystem::weakly_canonical(dcp::filesystem::fix_long_path(path)));
#else
	boost::filesystem::path complete(boost::filesystem::system_complete(dcp::filesystem::fix_long_path(path)));
	boost::filesystem::path result;
	for (auto part: complete) {
		if (part == "..") {
			boost::system::error_code ec;
			if (boost::filesystem::is_symlink(result, ec) || result.filename() == "..") {
				result /= part;
			} else {
				result = result.parent_path();
			}
		} else if (part != ".") {
			result /= part;
		}
	}

	return dcp::filesystem::unfix_long_path(result.make_preferred());
#endif
}


bool
dcp::filesystem::remove(boost::filesystem::path const& path)
{
	return boost::filesystem::remove(dcp::filesystem::fix_long_path(path));
}


bool
dcp::filesystem::remove(boost::filesystem::path const& path, boost::system::error_code& ec)
{
	return boost::filesystem::remove(dcp::filesystem::fix_long_path(path), ec);
}


uintmax_t
dcp::filesystem::remove_all(boost::filesystem::path const& path)
{
	return boost::filesystem::remove_all(dcp::filesystem::fix_long_path(path));
}


uintmax_t
dcp::filesystem::remove_all(boost::filesystem::path const& path, boost::system::error_code& ec)
{
	return boost::filesystem::remove_all(dcp::filesystem::fix_long_path(path), ec);
}


uintmax_t
dcp::filesystem::file_size(boost::filesystem::path const& path)
{
	return boost::filesystem::file_size(dcp::filesystem::fix_long_path(path));
}


uintmax_t
dcp::filesystem::file_size(boost::filesystem::path const& path, boost::system::error_code& ec)
{
	return boost::filesystem::file_size(dcp::filesystem::fix_long_path(path), ec);
}


boost::filesystem::path
dcp::filesystem::current_path()
{
	return dcp::filesystem::unfix_long_path(boost::filesystem::current_path());
}


void
dcp::filesystem::current_path(boost::filesystem::path const& path)
{
	boost::filesystem::current_path(dcp::filesystem::fix_long_path(path));
}


void
dcp::filesystem::create_hard_link(boost::filesystem::path const& from, boost::filesystem::path const& to)
{
	boost::filesystem::create_hard_link(dcp::filesystem::fix_long_path(from), dcp::filesystem::fix_long_path(to));
}


void
dcp::filesystem::create_hard_link(boost::filesystem::path const& from, boost::filesystem::path const& to, boost::system::error_code& ec)
{
	boost::filesystem::create_hard_link(dcp::filesystem::fix_long_path(from), dcp::filesystem::fix_long_path(to), ec);
}


void
dcp::filesystem::create_symlink(boost::filesystem::path const& from, boost::filesystem::path const& to, boost::system::error_code& ec)
{
	boost::filesystem::create_symlink(dcp::filesystem::fix_long_path(from), dcp::filesystem::fix_long_path(to), ec);
}


std::string
dcp::filesystem::extension(boost::filesystem::path const& path)
{
	return dcp::filesystem::fix_long_path(path).extension().string();
}


boost::filesystem::space_info
dcp::filesystem::space(boost::filesystem::path const& path)
{
	return boost::filesystem::space(dcp::filesystem::fix_long_path(path));
}


std::time_t
dcp::filesystem::last_write_time(boost::filesystem::path const& path)
{
	return boost::filesystem::last_write_time(dcp::filesystem::fix_long_path(path));
}


std::time_t
dcp::filesystem::last_write_time(boost::filesystem::path const& path, boost::system::error_code& ec)
{
	return boost::filesystem::last_write_time(dcp::filesystem::fix_long_path(path), ec);
}


uintmax_t
dcp::filesystem::hard_link_count(boost::filesystem::path const& path)
{
	return boost::filesystem::hard_link_count(dcp::filesystem::fix_long_path(path));
}


void
dcp::filesystem::rename(boost::filesystem::path const& old_path, boost::filesystem::path const& new_path)
{
	boost::filesystem::rename(dcp::filesystem::fix_long_path(old_path), dcp::filesystem::fix_long_path(new_path));
}


void
dcp::filesystem::rename(boost::filesystem::path const& old_path, boost::filesystem::path const& new_path, boost::system::error_code& ec)
{
	boost::filesystem::rename(dcp::filesystem::fix_long_path(old_path), dcp::filesystem::fix_long_path(new_path), ec);
}


/* We don't really need this but let's add it for completeness */
boost::filesystem::path
dcp::filesystem::change_extension(boost::filesystem::path const& path, std::string const& new_extension)
{
#ifdef LIBDCP_HAVE_REPLACE_EXTENSION
	auto copy = path;
	copy.replace_extension(new_extension);
	return copy;
#else
	return boost::filesystem::change_extension(path, new_extension);
#endif
}


#ifdef DCPOMATIC_WINDOWS

dcp::filesystem::directory_iterator::directory_iterator(boost::filesystem::path const& path)
	: _wrapped(dcp::filesystem::fix_long_path(path))
{

}


dcp::filesystem::directory_iterator::directory_iterator(boost::filesystem::path const& path, boost::system::error_code& ec)
	: _wrapped(dcp::filesystem::fix_long_path(path), ec)
{

}


boost::filesystem::path
dcp::filesystem::directory_entry::path() const
{
	return dcp::filesystem::unfix_long_path(_path);
}


dcp::filesystem::directory_entry::operator boost::filesystem::path const &() const
{
	return dcp::filesystem::unfix_long_path(_path);
}


dcp::filesystem::recursive_directory_iterator::recursive_directory_iterator(boost::filesystem::path const& path)
	: _wrapped(dcp::filesystem::fix_long_path(path))
{

}

#else

dcp::filesystem::directory_iterator::directory_iterator(boost::filesystem::path const& path)
	: _wrapped(path)
{

}


dcp::filesystem::directory_iterator::directory_iterator(boost::filesystem::path const& path, boost::system::error_code& ec)
	: _wrapped(path, ec)
{

}


boost::filesystem::path
dcp::filesystem::directory_entry::path() const
{
	return _path;
}


dcp::filesystem::directory_entry::operator boost::filesystem::path const &() const
{
	return _path;
}


dcp::filesystem::recursive_directory_iterator::recursive_directory_iterator(boost::filesystem::path const& path)
	: _wrapped(path)
{

}

#endif


dcp::filesystem::directory_entry::directory_entry(boost::filesystem::path const& path)
	: _path(path)
{

}


dcp::filesystem::directory_iterator&
dcp::filesystem::directory_iterator::operator++()
{
	++_wrapped;
	return *this;
}


dcp::filesystem::directory_entry
dcp::filesystem::directory_iterator::operator*() const
{
	_entry = dcp::filesystem::directory_entry(*_wrapped);
	return _entry;
}


dcp::filesystem::directory_entry*
dcp::filesystem::directory_iterator::operator->() const
{
	_entry = dcp::filesystem::directory_entry(_wrapped->path());
	return &_entry;
}

bool
dcp::filesystem::directory_iterator::operator!=(dcp::filesystem::directory_iterator const& other) const
{
	return _wrapped != other._wrapped;
}


dcp::filesystem::directory_iterator const&
dcp::filesystem::begin(dcp::filesystem::directory_iterator const& iter)
{
	return iter;
}


dcp::filesystem::directory_iterator
dcp::filesystem::end(dcp::filesystem::directory_iterator const&)
{
	return dcp::filesystem::directory_iterator();
}


dcp::filesystem::recursive_directory_iterator&
dcp::filesystem::recursive_directory_iterator::operator++()
{
	++_wrapped;
	return *this;
}


bool
dcp::filesystem::recursive_directory_iterator::operator!=(dcp::filesystem::recursive_directory_iterator const& other) const
{
	return _wrapped != other._wrapped;
}


dcp::filesystem::directory_entry
dcp::filesystem::recursive_directory_iterator::operator*() const
{
	_entry = dcp::filesystem::directory_entry(_wrapped->path());
	return _entry;
}


dcp::filesystem::directory_entry*
dcp::filesystem::recursive_directory_iterator::operator->() const
{
	_entry = dcp::filesystem::directory_entry(_wrapped->path());
	return &_entry;
}


dcp::filesystem::recursive_directory_iterator const&
dcp::filesystem::begin(dcp::filesystem::recursive_directory_iterator const& iter)
{
	return iter;
}


dcp::filesystem::recursive_directory_iterator
dcp::filesystem::end(dcp::filesystem::recursive_directory_iterator const&)
{
	return dcp::filesystem::recursive_directory_iterator();
}


/** Windows can't "by default" cope with paths longer than 260 characters, so if you pass such a path to
 *  any boost::filesystem method it will fail.  There is a "fix" for this, which is to prepend
 *  the string \\?\ to the path.  This will make it work, so long as:
 *  - the path is absolute.
 *  - the path contains no .. parts.
 *  - the path only uses backslashes.
 *  - individual path components are "short enough" (probably less than 255 characters)
 *
 *  See https://www.boost.org/doc/libs/1_57_0/libs/filesystem/doc/reference.html under
 *  "Warning: Long paths on Windows" for some details.
 *
 *  Our fopen_boost uses this method to get this fix, but any other calls to boost::filesystem
 *  will not unless this method is explicitly called to pre-process the pathname.
 */
boost::filesystem::path
dcp::filesystem::fix_long_path(boost::filesystem::path long_path)
{
#ifdef LIBDCP_WINDOWS
	using namespace boost::filesystem;

	if (boost::algorithm::starts_with(long_path.string(), "\\\\")) {
		/* This could mean it starts with \\ (i.e. a SMB path) or \\?\ (a long path)
		 * or a variety of other things... anyway, we'll leave it alone.
		 */
		return long_path;
	}

	/* We have to make the path canonical but we can't call canonical() on the long path
	 * as it will fail.  So we'll sort of do it ourselves (possibly badly).
	 */
	path fixed = "\\\\?\\";
	if (long_path.is_absolute()) {
		fixed += long_path.make_preferred();
	} else {
		fixed += filesystem::current_path() / long_path.make_preferred();
	}
	return fixed;
#else
	return long_path;
#endif
}


boost::filesystem::path
dcp::filesystem::unfix_long_path(boost::filesystem::path long_path)
{
#ifdef LIBDCP_WINDOWS
	if (boost::algorithm::starts_with(long_path.string(), "\\\\?\\")) {
		return long_path.string().substr(4);
	}
#endif
	return long_path;
}