1// boost/filesystem/operations.hpp ---------------------------------------------------//
2
3// Copyright Beman Dawes 2002-2009
4// Copyright Jan Langer 2002
5// Copyright Dietmar Kuehl 2001
6// Copyright Vladimir Prus 2002
7// Copyright Andrey Semashev 2020-2024
8
9// Distributed under the Boost Software License, Version 1.0.
10// See http://www.boost.org/LICENSE_1_0.txt
11
12// Library home page: http://www.boost.org/libs/filesystem
13
14//--------------------------------------------------------------------------------------//
15
16#ifndef BOOST_FILESYSTEM_OPERATIONS_HPP
17#define BOOST_FILESYSTEM_OPERATIONS_HPP
18
19#include <boost/filesystem/config.hpp>
20#include <boost/filesystem/path.hpp>
21#include <boost/filesystem/file_status.hpp>
22
23#include <boost/detail/bitmask.hpp>
24#include <boost/system/error_code.hpp>
25#include <boost/cstdint.hpp>
26#include <ctime>
27#include <string>
28
29#include <boost/filesystem/detail/header.hpp> // must be the last #include
30
31//--------------------------------------------------------------------------------------//
32
33namespace boost {
34namespace filesystem {
35
36struct space_info
37{
38 // all values are byte counts
39 boost::uintmax_t capacity;
40 boost::uintmax_t free; // <= capacity
41 boost::uintmax_t available; // <= free
42};
43
44enum class copy_options : unsigned int
45{
46 none = 0u, // Default. For copy_file: error if the target file exists. For copy: do not recurse, follow symlinks, copy file contents.
47
48 // copy_file options:
49 skip_existing = 1u, // Don't overwrite the existing target file, don't report an error
50 overwrite_existing = 1u << 1u, // Overwrite existing file
51 update_existing = 1u << 2u, // Overwrite existing file if its last write time is older than the replacement file
52 synchronize_data = 1u << 3u, // Flush all buffered data written to the target file to permanent storage
53 synchronize = 1u << 4u, // Flush all buffered data and attributes written to the target file to permanent storage
54 ignore_attribute_errors = 1u << 5u, // Ignore errors of copying file attributes
55
56 // copy options:
57 recursive = 1u << 8u, // Recurse into sub-directories
58 copy_symlinks = 1u << 9u, // Copy symlinks as symlinks instead of copying the referenced file
59 skip_symlinks = 1u << 10u, // Don't copy symlinks
60 directories_only = 1u << 11u, // Only copy directory structure, do not copy non-directory files
61 create_symlinks = 1u << 12u, // Create symlinks instead of copying files
62 create_hard_links = 1u << 13u, // Create hard links instead of copying files
63 _detail_recursing = 1u << 14u // Internal use only, do not use
64};
65
66BOOST_BITMASK(copy_options)
67
68//--------------------------------------------------------------------------------------//
69// implementation details //
70//--------------------------------------------------------------------------------------//
71
72namespace detail {
73
74BOOST_FILESYSTEM_DECL
75path absolute_v3(path const& p, path const& base, system::error_code* ec = nullptr);
76BOOST_FILESYSTEM_DECL
77path absolute_v4(path const& p, path const& base, system::error_code* ec = nullptr);
78BOOST_FILESYSTEM_DECL
79file_status status(path const& p, system::error_code* ec = nullptr);
80BOOST_FILESYSTEM_DECL
81file_status symlink_status(path const& p, system::error_code* ec = nullptr);
82BOOST_FILESYSTEM_DECL
83bool is_empty(path const& p, system::error_code* ec = nullptr);
84BOOST_FILESYSTEM_DECL
85path initial_path(system::error_code* ec = nullptr);
86BOOST_FILESYSTEM_DECL
87path canonical_v3(path const& p, path const& base, system::error_code* ec = nullptr);
88BOOST_FILESYSTEM_DECL
89path canonical_v4(path const& p, path const& base, system::error_code* ec = nullptr);
90BOOST_FILESYSTEM_DECL
91void copy(path const& from, path const& to, copy_options options, system::error_code* ec = nullptr);
92BOOST_FILESYSTEM_DECL
93bool copy_file(path const& from, path const& to, copy_options options, system::error_code* ec = nullptr);
94BOOST_FILESYSTEM_DECL
95void copy_symlink(path const& existing_symlink, path const& new_symlink, system::error_code* ec = nullptr);
96BOOST_FILESYSTEM_DECL
97bool create_directories(path const& p, system::error_code* ec = nullptr);
98BOOST_FILESYSTEM_DECL
99bool create_directory(path const& p, const path* existing, system::error_code* ec = nullptr);
100BOOST_FILESYSTEM_DECL
101void create_directory_symlink(path const& to, path const& from, system::error_code* ec = nullptr);
102BOOST_FILESYSTEM_DECL
103void create_hard_link(path const& to, path const& from, system::error_code* ec = nullptr);
104BOOST_FILESYSTEM_DECL
105void create_symlink(path const& to, path const& from, system::error_code* ec = nullptr);
106BOOST_FILESYSTEM_DECL
107path current_path(system::error_code* ec = nullptr);
108BOOST_FILESYSTEM_DECL
109void current_path(path const& p, system::error_code* ec = nullptr);
110BOOST_FILESYSTEM_DECL
111bool equivalent_v3(path const& p1, path const& p2, system::error_code* ec = nullptr);
112BOOST_FILESYSTEM_DECL
113bool equivalent_v4(path const& p1, path const& p2, system::error_code* ec = nullptr);
114BOOST_FILESYSTEM_DECL
115boost::uintmax_t file_size(path const& p, system::error_code* ec = nullptr);
116BOOST_FILESYSTEM_DECL
117boost::uintmax_t hard_link_count(path const& p, system::error_code* ec = nullptr);
118BOOST_FILESYSTEM_DECL
119std::time_t creation_time(path const& p, system::error_code* ec = nullptr);
120BOOST_FILESYSTEM_DECL
121std::time_t last_write_time(path const& p, system::error_code* ec = nullptr);
122BOOST_FILESYSTEM_DECL
123void last_write_time(path const& p, const std::time_t new_time, system::error_code* ec = nullptr);
124BOOST_FILESYSTEM_DECL
125void permissions(path const& p, perms prms, system::error_code* ec = nullptr);
126BOOST_FILESYSTEM_DECL
127path read_symlink(path const& p, system::error_code* ec = nullptr);
128BOOST_FILESYSTEM_DECL
129path relative(path const& p, path const& base, system::error_code* ec = nullptr);
130BOOST_FILESYSTEM_DECL
131bool remove(path const& p, system::error_code* ec = nullptr);
132BOOST_FILESYSTEM_DECL
133boost::uintmax_t remove_all(path const& p, system::error_code* ec = nullptr);
134BOOST_FILESYSTEM_DECL
135void rename(path const& old_p, path const& new_p, system::error_code* ec = nullptr);
136BOOST_FILESYSTEM_DECL
137void resize_file(path const& p, uintmax_t size, system::error_code* ec = nullptr);
138BOOST_FILESYSTEM_DECL
139space_info space(path const& p, system::error_code* ec = nullptr);
140BOOST_FILESYSTEM_DECL
141path system_complete(path const& p, system::error_code* ec = nullptr);
142BOOST_FILESYSTEM_DECL
143path temp_directory_path(system::error_code* ec = nullptr);
144BOOST_FILESYSTEM_DECL
145path unique_path(path const& p, system::error_code* ec = nullptr);
146BOOST_FILESYSTEM_DECL
147path weakly_canonical_v3(path const& p, path const& base, system::error_code* ec = nullptr);
148BOOST_FILESYSTEM_DECL
149path weakly_canonical_v4(path const& p, path const& base, system::error_code* ec = nullptr);
150
151} // namespace detail
152
153//--------------------------------------------------------------------------------------//
154// //
155// status query functions //
156// //
157//--------------------------------------------------------------------------------------//
158
159inline file_status status(path const& p)
160{
161 return detail::status(p);
162}
163
164inline file_status status(path const& p, system::error_code& ec) noexcept
165{
166 return detail::status(p, ec: &ec);
167}
168
169inline file_status symlink_status(path const& p)
170{
171 return detail::symlink_status(p);
172}
173
174inline file_status symlink_status(path const& p, system::error_code& ec) noexcept
175{
176 return detail::symlink_status(p, ec: &ec);
177}
178
179inline bool exists(path const& p)
180{
181 return filesystem::exists(f: detail::status(p));
182}
183
184inline bool exists(path const& p, system::error_code& ec) noexcept
185{
186 return filesystem::exists(f: detail::status(p, ec: &ec));
187}
188
189inline bool is_regular_file(path const& p)
190{
191 return filesystem::is_regular_file(f: detail::status(p));
192}
193
194inline bool is_regular_file(path const& p, system::error_code& ec) noexcept
195{
196 return filesystem::is_regular_file(f: detail::status(p, ec: &ec));
197}
198
199inline bool is_directory(path const& p)
200{
201 return filesystem::is_directory(f: detail::status(p));
202}
203
204inline bool is_directory(path const& p, system::error_code& ec) noexcept
205{
206 return filesystem::is_directory(f: detail::status(p, ec: &ec));
207}
208
209inline bool is_symlink(path const& p)
210{
211 return filesystem::is_symlink(f: detail::symlink_status(p));
212}
213
214inline bool is_symlink(path const& p, system::error_code& ec) noexcept
215{
216 return filesystem::is_symlink(f: detail::symlink_status(p, ec: &ec));
217}
218
219inline bool is_block_file(path const& p)
220{
221 return filesystem::is_block_file(f: detail::status(p));
222}
223
224inline bool is_block_file(path const& p, system::error_code& ec) noexcept
225{
226 return filesystem::is_block_file(f: detail::status(p, ec: &ec));
227}
228
229inline bool is_character_file(path const& p)
230{
231 return filesystem::is_character_file(f: detail::status(p));
232}
233
234inline bool is_character_file(path const& p, system::error_code& ec) noexcept
235{
236 return filesystem::is_character_file(f: detail::status(p, ec: &ec));
237}
238
239inline bool is_fifo(path const& p)
240{
241 return filesystem::is_fifo(f: detail::status(p));
242}
243
244inline bool is_fifo(path const& p, system::error_code& ec) noexcept
245{
246 return filesystem::is_fifo(f: detail::status(p, ec: &ec));
247}
248
249inline bool is_socket(path const& p)
250{
251 return filesystem::is_socket(f: detail::status(p));
252}
253
254inline bool is_socket(path const& p, system::error_code& ec) noexcept
255{
256 return filesystem::is_socket(f: detail::status(p, ec: &ec));
257}
258
259inline bool is_reparse_file(path const& p)
260{
261 return filesystem::is_reparse_file(f: detail::symlink_status(p));
262}
263
264inline bool is_reparse_file(path const& p, system::error_code& ec) noexcept
265{
266 return filesystem::is_reparse_file(f: detail::symlink_status(p, ec: &ec));
267}
268
269inline bool is_other(path const& p)
270{
271 return filesystem::is_other(f: detail::status(p));
272}
273
274inline bool is_other(path const& p, system::error_code& ec) noexcept
275{
276 return filesystem::is_other(f: detail::status(p, ec: &ec));
277}
278
279inline bool is_empty(path const& p)
280{
281 return detail::is_empty(p);
282}
283
284inline bool is_empty(path const& p, system::error_code& ec)
285{
286 return detail::is_empty(p, ec: &ec);
287}
288
289//--------------------------------------------------------------------------------------//
290// //
291// operational functions //
292// //
293//--------------------------------------------------------------------------------------//
294
295inline path initial_path()
296{
297 return detail::initial_path();
298}
299
300inline path initial_path(system::error_code& ec)
301{
302 return detail::initial_path(ec: &ec);
303}
304
305template< class Path >
306path initial_path()
307{
308 return initial_path();
309}
310template< class Path >
311path initial_path(system::error_code& ec)
312{
313 return detail::initial_path(ec: &ec);
314}
315
316inline path current_path()
317{
318 return detail::current_path();
319}
320
321inline path current_path(system::error_code& ec)
322{
323 return detail::current_path(ec: &ec);
324}
325
326inline void current_path(path const& p)
327{
328 detail::current_path(p);
329}
330
331inline void current_path(path const& p, system::error_code& ec) noexcept
332{
333 detail::current_path(p, ec: &ec);
334}
335
336inline void copy(path const& from, path const& to)
337{
338 detail::copy(from, to, options: copy_options::none);
339}
340
341inline void copy(path const& from, path const& to, system::error_code& ec) noexcept
342{
343 detail::copy(from, to, options: copy_options::none, ec: &ec);
344}
345
346inline void copy(path const& from, path const& to, copy_options options)
347{
348 detail::copy(from, to, options);
349}
350
351inline void copy(path const& from, path const& to, copy_options options, system::error_code& ec) noexcept
352{
353 detail::copy(from, to, options, ec: &ec);
354}
355
356inline bool copy_file(path const& from, path const& to)
357{
358 return detail::copy_file(from, to, options: copy_options::none);
359}
360
361inline bool copy_file(path const& from, path const& to, system::error_code& ec) noexcept
362{
363 return detail::copy_file(from, to, options: copy_options::none, ec: &ec);
364}
365
366inline bool copy_file(path const& from, path const& to, copy_options options)
367{
368 return detail::copy_file(from, to, options);
369}
370
371inline bool copy_file(path const& from, path const& to, copy_options options, system::error_code& ec) noexcept
372{
373 return detail::copy_file(from, to, options, ec: &ec);
374}
375
376inline void copy_symlink(path const& existing_symlink, path const& new_symlink)
377{
378 detail::copy_symlink(existing_symlink, new_symlink);
379}
380
381inline void copy_symlink(path const& existing_symlink, path const& new_symlink, system::error_code& ec) noexcept
382{
383 detail::copy_symlink(existing_symlink, new_symlink, ec: &ec);
384}
385
386inline bool create_directories(path const& p)
387{
388 return detail::create_directories(p);
389}
390
391inline bool create_directories(path const& p, system::error_code& ec) noexcept
392{
393 return detail::create_directories(p, ec: &ec);
394}
395
396inline bool create_directory(path const& p)
397{
398 return detail::create_directory(p, existing: nullptr);
399}
400
401inline bool create_directory(path const& p, system::error_code& ec) noexcept
402{
403 return detail::create_directory(p, existing: nullptr, ec: &ec);
404}
405
406inline bool create_directory(path const& p, path const& existing)
407{
408 return detail::create_directory(p, existing: &existing);
409}
410
411inline bool create_directory(path const& p, path const& existing, system::error_code& ec) noexcept
412{
413 return detail::create_directory(p, existing: &existing, ec: &ec);
414}
415
416inline void create_directory_symlink(path const& to, path const& from)
417{
418 detail::create_directory_symlink(to, from);
419}
420
421inline void create_directory_symlink(path const& to, path const& from, system::error_code& ec) noexcept
422{
423 detail::create_directory_symlink(to, from, ec: &ec);
424}
425
426inline void create_hard_link(path const& to, path const& new_hard_link)
427{
428 detail::create_hard_link(to, from: new_hard_link);
429}
430
431inline void create_hard_link(path const& to, path const& new_hard_link, system::error_code& ec) noexcept
432{
433 detail::create_hard_link(to, from: new_hard_link, ec: &ec);
434}
435
436inline void create_symlink(path const& to, path const& new_symlink)
437{
438 detail::create_symlink(to, from: new_symlink);
439}
440
441inline void create_symlink(path const& to, path const& new_symlink, system::error_code& ec) noexcept
442{
443 detail::create_symlink(to, from: new_symlink, ec: &ec);
444}
445
446inline boost::uintmax_t file_size(path const& p)
447{
448 return detail::file_size(p);
449}
450
451inline boost::uintmax_t file_size(path const& p, system::error_code& ec) noexcept
452{
453 return detail::file_size(p, ec: &ec);
454}
455
456inline boost::uintmax_t hard_link_count(path const& p)
457{
458 return detail::hard_link_count(p);
459}
460
461inline boost::uintmax_t hard_link_count(path const& p, system::error_code& ec) noexcept
462{
463 return detail::hard_link_count(p, ec: &ec);
464}
465
466inline std::time_t creation_time(path const& p)
467{
468 return detail::creation_time(p);
469}
470
471inline std::time_t creation_time(path const& p, system::error_code& ec) noexcept
472{
473 return detail::creation_time(p, ec: &ec);
474}
475
476inline std::time_t last_write_time(path const& p)
477{
478 return detail::last_write_time(p);
479}
480
481inline std::time_t last_write_time(path const& p, system::error_code& ec) noexcept
482{
483 return detail::last_write_time(p, ec: &ec);
484}
485
486inline void last_write_time(path const& p, const std::time_t new_time)
487{
488 detail::last_write_time(p, new_time);
489}
490
491inline void last_write_time(path const& p, const std::time_t new_time, system::error_code& ec) noexcept
492{
493 detail::last_write_time(p, new_time, ec: &ec);
494}
495
496inline void permissions(path const& p, perms prms)
497{
498 detail::permissions(p, prms);
499}
500
501inline void permissions(path const& p, perms prms, system::error_code& ec) noexcept
502{
503 detail::permissions(p, prms, ec: &ec);
504}
505
506inline path read_symlink(path const& p)
507{
508 return detail::read_symlink(p);
509}
510
511inline path read_symlink(path const& p, system::error_code& ec)
512{
513 return detail::read_symlink(p, ec: &ec);
514}
515
516inline bool remove(path const& p)
517{
518 return detail::remove(p);
519}
520
521inline bool remove(path const& p, system::error_code& ec) noexcept
522{
523 return detail::remove(p, ec: &ec);
524}
525
526inline boost::uintmax_t remove_all(path const& p)
527{
528 return detail::remove_all(p);
529}
530
531inline boost::uintmax_t remove_all(path const& p, system::error_code& ec) noexcept
532{
533 return detail::remove_all(p, ec: &ec);
534}
535
536inline void rename(path const& old_p, path const& new_p)
537{
538 detail::rename(old_p, new_p);
539}
540
541inline void rename(path const& old_p, path const& new_p, system::error_code& ec) noexcept
542{
543 detail::rename(old_p, new_p, ec: &ec);
544}
545
546// name suggested by Scott McMurray
547inline void resize_file(path const& p, uintmax_t size)
548{
549 detail::resize_file(p, size);
550}
551
552inline void resize_file(path const& p, uintmax_t size, system::error_code& ec) noexcept
553{
554 detail::resize_file(p, size, ec: &ec);
555}
556
557inline path relative(path const& p, path const& base = current_path())
558{
559 return detail::relative(p, base);
560}
561
562inline path relative(path const& p, system::error_code& ec)
563{
564 path base = current_path(ec);
565 if (ec)
566 return path();
567 return detail::relative(p, base, ec: &ec);
568}
569
570inline path relative(path const& p, path const& base, system::error_code& ec)
571{
572 return detail::relative(p, base, ec: &ec);
573}
574
575inline space_info space(path const& p)
576{
577 return detail::space(p);
578}
579
580inline space_info space(path const& p, system::error_code& ec) noexcept
581{
582 return detail::space(p, ec: &ec);
583}
584
585inline path system_complete(path const& p)
586{
587 return detail::system_complete(p);
588}
589
590inline path system_complete(path const& p, system::error_code& ec)
591{
592 return detail::system_complete(p, ec: &ec);
593}
594
595inline path temp_directory_path()
596{
597 return detail::temp_directory_path();
598}
599
600inline path temp_directory_path(system::error_code& ec)
601{
602 return detail::temp_directory_path(ec: &ec);
603}
604
605inline path unique_path(path const& p =
606#if defined(BOOST_WINDOWS_API)
607 L"%%%%-%%%%-%%%%-%%%%"
608#else
609 "%%%%-%%%%-%%%%-%%%%"
610#endif
611)
612{
613 return detail::unique_path(p);
614}
615
616inline path unique_path(system::error_code& ec)
617{
618 return detail::unique_path
619 (
620#if defined(BOOST_WINDOWS_API)
621 L"%%%%-%%%%-%%%%-%%%%",
622#else
623 p: "%%%%-%%%%-%%%%-%%%%",
624#endif
625 ec: &ec
626 );
627}
628
629inline path unique_path(path const& p, system::error_code& ec)
630{
631 return detail::unique_path(p, ec: &ec);
632}
633
634namespace BOOST_FILESYSTEM_VERSION_NAMESPACE {
635
636inline path absolute(path const& p, path const& base = current_path())
637{
638 return BOOST_FILESYSTEM_VERSIONED_SYM(detail::absolute)(p, base);
639}
640
641inline path absolute(path const& p, system::error_code& ec)
642{
643 path base = current_path(ec);
644 if (ec)
645 return path();
646 return BOOST_FILESYSTEM_VERSIONED_SYM(detail::absolute)(p, base, ec: &ec);
647}
648
649inline path absolute(path const& p, path const& base, system::error_code& ec)
650{
651 return BOOST_FILESYSTEM_VERSIONED_SYM(detail::absolute)(p, base, ec: &ec);
652}
653
654inline path canonical(path const& p, path const& base = current_path())
655{
656 return BOOST_FILESYSTEM_VERSIONED_SYM(detail::canonical)(p, base);
657}
658
659inline path canonical(path const& p, system::error_code& ec)
660{
661 path base = current_path(ec);
662 if (ec)
663 return path();
664 return BOOST_FILESYSTEM_VERSIONED_SYM(detail::canonical)(p, base, ec: &ec);
665}
666
667inline path canonical(path const& p, path const& base, system::error_code& ec)
668{
669 return BOOST_FILESYSTEM_VERSIONED_SYM(detail::canonical)(p, base, ec: &ec);
670}
671
672inline bool equivalent(path const& p1, path const& p2)
673{
674 return BOOST_FILESYSTEM_VERSIONED_SYM(detail::equivalent)(p1, p2);
675}
676
677inline bool equivalent(path const& p1, path const& p2, system::error_code& ec) noexcept
678{
679 return BOOST_FILESYSTEM_VERSIONED_SYM(detail::equivalent)(p1, p2, ec: &ec);
680}
681
682inline path weakly_canonical(path const& p, path const& base = current_path())
683{
684 return BOOST_FILESYSTEM_VERSIONED_SYM(detail::weakly_canonical)(p, base);
685}
686
687inline path weakly_canonical(path const& p, system::error_code& ec)
688{
689 path base = current_path(ec);
690 if (ec)
691 return path();
692 return BOOST_FILESYSTEM_VERSIONED_SYM(detail::weakly_canonical)(p, base, ec: &ec);
693}
694
695inline path weakly_canonical(path const& p, path const& base, system::error_code& ec)
696{
697 return BOOST_FILESYSTEM_VERSIONED_SYM(detail::weakly_canonical)(p, base, ec: &ec);
698}
699
700} // namespace BOOST_FILESYSTEM_VERSION_NAMESPACE
701
702using BOOST_FILESYSTEM_VERSION_NAMESPACE::absolute;
703using BOOST_FILESYSTEM_VERSION_NAMESPACE::canonical;
704using BOOST_FILESYSTEM_VERSION_NAMESPACE::equivalent;
705using BOOST_FILESYSTEM_VERSION_NAMESPACE::weakly_canonical;
706
707// test helper -----------------------------------------------------------------------//
708
709// Not part of the documented interface since false positives are possible;
710// there is no law that says that an OS that has large stat.st_size
711// actually supports large file sizes.
712
713namespace detail {
714
715BOOST_FILESYSTEM_DECL bool possible_large_file_size_support();
716
717} // namespace detail
718
719} // namespace filesystem
720} // namespace boost
721
722#include <boost/filesystem/detail/footer.hpp>
723
724#endif // BOOST_FILESYSTEM_OPERATIONS_HPP
725

source code of boost/libs/filesystem/include/boost/filesystem/operations.hpp