1 // Filesystem operations -*- C++ -*-
3 // Copyright (C) 2014-2017 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library 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 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 #ifndef _GLIBCXX_USE_CXX11_ABI
26 # define _GLIBCXX_USE_CXX11_ABI 1
27 # define NEED_DO_COPY_FILE
31 #include <experimental/filesystem>
35 #include <ext/stdio_filebuf.h>
39 #include <limits.h> // PATH_MAX
40 #ifdef _GLIBCXX_HAVE_FCNTL_H
41 # include <fcntl.h> // AT_FDCWD, AT_SYMLINK_NOFOLLOW
43 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
44 # include <sys/stat.h> // stat, utimensat, fchmodat
46 #ifdef _GLIBCXX_HAVE_SYS_STATVFS_H
47 # include <sys/statvfs.h> // statvfs
49 #ifdef _GLIBCXX_USE_SENDFILE
50 # include <sys/sendfile.h> // sendfile
52 #if !_GLIBCXX_USE_UTIMENSAT && _GLIBCXX_HAVE_UTIME_H
53 # include <utime.h> // utime
56 #define _GLIBCXX_BEGIN_NAMESPACE_FILESYSTEM namespace filesystem {
57 #define _GLIBCXX_END_NAMESPACE_FILESYSTEM }
58 #include "ops-common.h"
60 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
62 # define utime _wutime
64 # define chmod _wchmod
67 namespace fs
= std::filesystem
;
70 fs::absolute(const path
& p
)
72 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
74 path ret
= absolute(p
, ec
);
76 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot make absolute path", p
,
77 std::make_error_code(errc::not_supported
)));
80 return current_path() / p
;
85 fs::absolute(const path
& p
, error_code
& ec
)
87 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
88 ec
= std::make_error_code(errc::not_supported
);
92 return current_path() / p
;
98 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
99 inline bool is_dot(wchar_t c
) { return c
== L
'.'; }
101 inline bool is_dot(char c
) { return c
== '.'; }
104 inline bool is_dot(const fs::path
& path
)
106 const auto& filename
= path
.native();
107 return filename
.size() == 1 && is_dot(filename
[0]);
110 inline bool is_dotdot(const fs::path
& path
)
112 const auto& filename
= path
.native();
113 return filename
.size() == 2 && is_dot(filename
[0]) && is_dot(filename
[1]);
116 struct free_as_in_malloc
118 void operator()(void* p
) const { ::free(p
); }
121 using char_ptr
= std::unique_ptr
<char[], free_as_in_malloc
>;
125 fs::canonical(const path
& p
, error_code
& ec
)
128 const path pa
= absolute(p
, ec
);
132 #ifdef _GLIBCXX_USE_REALPATH
133 char_ptr buf
{ nullptr };
134 # if _XOPEN_VERSION < 700
135 // Not safe to call realpath(path, NULL)
136 buf
.reset( (char*)::malloc(PATH_MAX
) );
138 if (char* rp
= ::realpath(pa
.c_str(), buf
.get()))
146 if (errno
!= ENAMETOOLONG
)
148 ec
.assign(errno
, std::generic_category());
156 ec
= make_error_code(std::errc::no_such_file_or_directory
);
159 // else: we know there are (currently) no unresolvable symlink loops
161 result
= pa
.root_path();
164 for (auto& f
: pa
.relative_path())
167 int max_allowed_symlinks
= 40;
169 while (!cmpts
.empty() && !ec
)
171 path f
= std::move(cmpts
.front());
176 // ignore empty element
180 if (!is_directory(result
, ec
) && !ec
)
181 ec
.assign(ENOTDIR
, std::generic_category());
183 else if (is_dotdot(f
))
185 auto parent
= result
.parent_path();
187 result
= pa
.root_path();
195 if (is_symlink(result
, ec
))
197 path link
= read_symlink(result
, ec
);
200 if (--max_allowed_symlinks
== 0)
201 ec
.assign(ELOOP
, std::generic_category());
204 if (link
.is_absolute())
206 result
= link
.root_path();
207 link
= link
.relative_path();
210 result
= result
.parent_path();
212 cmpts
.insert(cmpts
.begin(), link
.begin(), link
.end());
219 if (ec
|| !exists(result
, ec
))
226 fs::canonical(const path
& p
)
229 path res
= canonical(p
, ec
);
231 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot make canonical path",
237 fs::copy(const path
& from
, const path
& to
, copy_options options
)
240 copy(from
, to
, options
, ec
);
242 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot copy", from
, to
, ec
));
245 namespace std::filesystem
247 // Need this as there's no 'perm_options::none' enumerator.
248 inline bool is_set(fs::perm_options obj
, fs::perm_options bits
)
250 return (obj
& bits
) != fs::perm_options
{};
254 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
255 #ifdef NEED_DO_COPY_FILE
257 fs::do_copy_file(const char* from
, const char* to
,
258 copy_options_existing_file options
,
259 stat_type
* from_st
, stat_type
* to_st
,
260 std::error_code
& ec
) noexcept
263 fs::file_status t
, f
;
265 if (to_st
== nullptr)
267 if (::stat(to
, &st1
))
269 const int err
= errno
;
270 if (!is_not_found_errno(err
))
272 ec
.assign(err
, std::generic_category());
279 else if (to_st
== from_st
)
282 if (to_st
== nullptr)
283 t
= fs::file_status
{fs::file_type::not_found
};
285 t
= make_file_status(*to_st
);
287 if (from_st
== nullptr)
289 if (::stat(from
, &st2
))
291 ec
.assign(errno
, std::generic_category());
297 f
= make_file_status(*from_st
);
298 // _GLIBCXX_RESOLVE_LIB_DEFECTS
299 // 2712. copy_file() has a number of unspecified error conditions
300 if (!is_regular_file(f
))
302 ec
= std::make_error_code(std::errc::not_supported
);
308 if (!is_regular_file(t
))
310 ec
= std::make_error_code(std::errc::not_supported
);
314 if (to_st
->st_dev
== from_st
->st_dev
315 && to_st
->st_ino
== from_st
->st_ino
)
317 ec
= std::make_error_code(std::errc::file_exists
);
326 else if (options
.update
)
328 const auto from_mtime
= file_time(*from_st
, ec
);
331 if ((from_mtime
<= file_time(*to_st
, ec
)) || ec
)
334 else if (!options
.overwrite
)
336 ec
= std::make_error_code(std::errc::file_exists
);
339 else if (!is_regular_file(t
))
341 ec
= std::make_error_code(std::errc::not_supported
);
347 ~CloseFD() { if (fd
!= -1) ::close(fd
); }
348 bool close() { return ::close(std::exchange(fd
, -1)) == 0; }
352 CloseFD in
= { ::open(from
, O_RDONLY
) };
355 ec
.assign(errno
, std::generic_category());
358 int oflag
= O_WRONLY
|O_CREAT
;
359 if (options
.overwrite
|| options
.update
)
363 CloseFD out
= { ::open(to
, oflag
, S_IWUSR
) };
366 if (errno
== EEXIST
&& options
.skip
)
369 ec
.assign(errno
, std::generic_category());
373 #ifdef _GLIBCXX_USE_FCHMOD
374 if (::fchmod(out
.fd
, from_st
->st_mode
))
375 #elif defined _GLIBCXX_USE_FCHMODAT
376 if (::fchmodat(AT_FDCWD
, to
, from_st
->st_mode
, 0))
378 if (::chmod(to
, from_st
->st_mode
))
381 ec
.assign(errno
, std::generic_category());
385 #ifdef _GLIBCXX_USE_SENDFILE
387 const auto n
= ::sendfile(out
.fd
, in
.fd
, &offset
, from_st
->st_size
);
388 if (n
< 0 && (errno
== ENOSYS
|| errno
== EINVAL
))
390 #endif // _GLIBCXX_USE_SENDFILE
391 __gnu_cxx::stdio_filebuf
<char> sbin(in
.fd
, std::ios::in
);
392 __gnu_cxx::stdio_filebuf
<char> sbout(out
.fd
, std::ios::out
);
397 if (from_st
->st_size
&& !(std::ostream(&sbout
) << &sbin
))
399 ec
= std::make_error_code(std::errc::io_error
);
402 if (!sbout
.close() || !sbin
.close())
404 ec
.assign(errno
, std::generic_category());
411 #ifdef _GLIBCXX_USE_SENDFILE
413 if (n
!= from_st
->st_size
)
415 ec
.assign(errno
, std::generic_category());
418 if (!out
.close() || !in
.close())
420 ec
.assign(errno
, std::generic_category());
426 #endif // _GLIBCXX_USE_SENDFILE
428 #endif // NEED_DO_COPY_FILE
429 #endif // _GLIBCXX_HAVE_SYS_STAT_H
432 fs::copy(const path
& from
, const path
& to
, copy_options options
,
435 const bool skip_symlinks
= is_set(options
, copy_options::skip_symlinks
);
436 const bool create_symlinks
= is_set(options
, copy_options::create_symlinks
);
437 const bool copy_symlinks
= is_set(options
, copy_options::copy_symlinks
);
438 const bool use_lstat
= create_symlinks
|| skip_symlinks
;
441 stat_type from_st
, to_st
;
442 // _GLIBCXX_RESOLVE_LIB_DEFECTS
443 // 2681. filesystem::copy() cannot copy symlinks
444 if (use_lstat
|| copy_symlinks
445 ? ::lstat(from
.c_str(), &from_st
)
446 : ::stat(from
.c_str(), &from_st
))
448 ec
.assign(errno
, std::generic_category());
452 ? ::lstat(to
.c_str(), &to_st
)
453 : ::stat(to
.c_str(), &to_st
))
455 if (!is_not_found_errno(errno
))
457 ec
.assign(errno
, std::generic_category());
460 t
= file_status
{file_type::not_found
};
463 t
= make_file_status(to_st
);
464 f
= make_file_status(from_st
);
466 if (exists(t
) && !is_other(t
) && !is_other(f
)
467 && to_st
.st_dev
== from_st
.st_dev
&& to_st
.st_ino
== from_st
.st_ino
)
469 ec
= std::make_error_code(std::errc::file_exists
);
472 if (is_other(f
) || is_other(t
))
474 ec
= std::make_error_code(std::errc::not_supported
);
477 if (is_directory(f
) && is_regular_file(t
))
479 ec
= std::make_error_code(std::errc::is_a_directory
);
487 else if (!exists(t
) && copy_symlinks
)
488 copy_symlink(from
, to
, ec
);
490 // Not clear what should be done here.
491 // "Otherwise report an error as specified in Error reporting (7)."
492 ec
= std::make_error_code(std::errc::invalid_argument
);
494 else if (is_regular_file(f
))
496 if (is_set(options
, copy_options::directories_only
))
498 else if (create_symlinks
)
499 create_symlink(from
, to
, ec
);
500 else if (is_set(options
, copy_options::create_hard_links
))
501 create_hard_link(from
, to
, ec
);
502 else if (is_directory(t
))
503 do_copy_file(from
.c_str(), (to
/ from
.filename()).c_str(),
504 copy_file_options(options
), &from_st
, nullptr, ec
);
507 auto ptr
= exists(t
) ? &to_st
: &from_st
;
508 do_copy_file(from
.c_str(), to
.c_str(), copy_file_options(options
),
512 // _GLIBCXX_RESOLVE_LIB_DEFECTS
513 // 2682. filesystem::copy() won't create a symlink to a directory
514 else if (is_directory(f
) && create_symlinks
)
515 ec
= std::make_error_code(errc::is_a_directory
);
516 else if (is_directory(f
) && (is_set(options
, copy_options::recursive
)
517 || options
== copy_options::none
))
520 if (!create_directory(to
, from
, ec
))
522 // set an unused bit in options to disable further recursion
523 if (!is_set(options
, copy_options::recursive
))
524 options
|= static_cast<copy_options
>(4096);
525 for (const directory_entry
& x
: directory_iterator(from
))
526 copy(x
.path(), to
/x
.path().filename(), options
, ec
);
528 // _GLIBCXX_RESOLVE_LIB_DEFECTS
529 // 2683. filesystem::copy() says "no effects"
535 fs::copy_file(const path
& from
, const path
& to
, copy_options option
)
538 bool result
= copy_file(from
, to
, option
, ec
);
540 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot copy file", from
, to
,
546 fs::copy_file(const path
& from
, const path
& to
, copy_options options
,
549 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
550 return do_copy_file(from
.c_str(), to
.c_str(), copy_file_options(options
),
551 nullptr, nullptr, ec
);
553 ec
= std::make_error_code(std::errc::not_supported
);
560 fs::copy_symlink(const path
& existing_symlink
, const path
& new_symlink
)
563 copy_symlink(existing_symlink
, new_symlink
, ec
);
565 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot copy symlink",
566 existing_symlink
, new_symlink
, ec
));
570 fs::copy_symlink(const path
& existing_symlink
, const path
& new_symlink
,
571 error_code
& ec
) noexcept
573 auto p
= read_symlink(existing_symlink
, ec
);
576 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
579 create_directory_symlink(p
, new_symlink
, ec
);
583 create_symlink(p
, new_symlink
, ec
);
588 fs::create_directories(const path
& p
)
591 bool result
= create_directories(p
, ec
);
593 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot create directories", p
,
599 fs::create_directories(const path
& p
, error_code
& ec
)
603 ec
= std::make_error_code(errc::invalid_argument
);
606 std::stack
<path
> missing
;
609 while (pp
.has_filename() && status(pp
, ec
).type() == file_type::not_found
)
612 const auto& filename
= pp
.filename();
613 if (!is_dot(filename
) && !is_dotdot(filename
))
615 pp
= pp
.parent_path();
617 if (missing
.size() > 1000) // sanity check
619 ec
= std::make_error_code(std::errc::filename_too_long
);
624 if (ec
|| missing
.empty())
629 const path
& top
= missing
.top();
630 create_directory(top
, ec
);
631 if (ec
&& is_directory(top
))
635 while (!missing
.empty() && !ec
);
637 return missing
.empty();
643 create_dir(const fs::path
& p
, fs::perms perm
, std::error_code
& ec
)
645 bool created
= false;
646 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
647 ::mode_t mode
= static_cast<std::underlying_type_t
<fs::perms
>>(perm
);
648 if (::mkdir(p
.c_str(), mode
))
650 const int err
= errno
;
651 if (err
!= EEXIST
|| !is_directory(p
))
652 ec
.assign(err
, std::generic_category());
662 ec
= std::make_error_code(std::errc::not_supported
);
669 fs::create_directory(const path
& p
)
672 bool result
= create_directory(p
, ec
);
674 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot create directory", p
,
680 fs::create_directory(const path
& p
, error_code
& ec
) noexcept
682 return create_dir(p
, perms::all
, ec
);
687 fs::create_directory(const path
& p
, const path
& attributes
)
690 bool result
= create_directory(p
, attributes
, ec
);
692 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot create directory", p
,
698 fs::create_directory(const path
& p
, const path
& attributes
,
699 error_code
& ec
) noexcept
701 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
703 if (::stat(attributes
.c_str(), &st
))
705 ec
.assign(errno
, std::generic_category());
708 return create_dir(p
, static_cast<perms
>(st
.st_mode
), ec
);
710 ec
= std::make_error_code(std::errc::not_supported
);
717 fs::create_directory_symlink(const path
& to
, const path
& new_symlink
)
720 create_directory_symlink(to
, new_symlink
, ec
);
722 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot create directory symlink",
723 to
, new_symlink
, ec
));
727 fs::create_directory_symlink(const path
& to
, const path
& new_symlink
,
728 error_code
& ec
) noexcept
730 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
731 ec
= std::make_error_code(std::errc::not_supported
);
733 create_symlink(to
, new_symlink
, ec
);
739 fs::create_hard_link(const path
& to
, const path
& new_hard_link
)
742 create_hard_link(to
, new_hard_link
, ec
);
744 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot create hard link",
745 to
, new_hard_link
, ec
));
749 fs::create_hard_link(const path
& to
, const path
& new_hard_link
,
750 error_code
& ec
) noexcept
752 #ifdef _GLIBCXX_HAVE_UNISTD_H
753 if (::link(to
.c_str(), new_hard_link
.c_str()))
754 ec
.assign(errno
, std::generic_category());
758 ec
= std::make_error_code(std::errc::not_supported
);
763 fs::create_symlink(const path
& to
, const path
& new_symlink
)
766 create_symlink(to
, new_symlink
, ec
);
768 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot create symlink",
769 to
, new_symlink
, ec
));
773 fs::create_symlink(const path
& to
, const path
& new_symlink
,
774 error_code
& ec
) noexcept
776 #ifdef _GLIBCXX_HAVE_UNISTD_H
777 if (::symlink(to
.c_str(), new_symlink
.c_str()))
778 ec
.assign(errno
, std::generic_category());
782 ec
= std::make_error_code(std::errc::not_supported
);
791 path p
= current_path(ec
);
793 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot get current path", ec
));
798 fs::current_path(error_code
& ec
)
801 #ifdef _GLIBCXX_HAVE_UNISTD_H
803 if (char_ptr cwd
= char_ptr
{::getcwd(nullptr, 0)})
809 ec
.assign(errno
, std::generic_category());
811 long path_max
= pathconf(".", _PC_PATH_MAX
);
815 else if (path_max
> 10240)
819 for (char_ptr buf
; p
.empty(); size
*= 2)
821 buf
.reset((char*)malloc(size
));
824 if (getcwd(buf
.get(), size
))
829 else if (errno
!= ERANGE
)
831 ec
.assign(errno
, std::generic_category());
837 ec
= std::make_error_code(std::errc::not_enough_memory
);
842 #else // _GLIBCXX_HAVE_UNISTD_H
843 ec
= std::make_error_code(std::errc::not_supported
);
849 fs::current_path(const path
& p
)
854 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot set current path", ec
));
858 fs::current_path(const path
& p
, error_code
& ec
) noexcept
860 #ifdef _GLIBCXX_HAVE_UNISTD_H
861 if (::chdir(p
.c_str()))
862 ec
.assign(errno
, std::generic_category());
866 ec
= std::make_error_code(std::errc::not_supported
);
871 fs::equivalent(const path
& p1
, const path
& p2
)
874 auto result
= equivalent(p1
, p2
, ec
);
876 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot check file equivalence",
882 fs::equivalent(const path
& p1
, const path
& p2
, error_code
& ec
) noexcept
884 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
888 if (::stat(p1
.c_str(), &st1
) == 0)
889 s1
= make_file_status(st1
);
890 else if (is_not_found_errno(errno
))
891 s1
.type(file_type::not_found
);
895 if (::stat(p2
.c_str(), &st2
) == 0)
896 s2
= make_file_status(st2
);
897 else if (is_not_found_errno(errno
))
898 s2
.type(file_type::not_found
);
902 if (exists(s1
) && exists(s2
))
904 if (is_other(s1
) && is_other(s2
))
906 ec
= std::make_error_code(std::errc::not_supported
);
910 if (is_other(s1
) || is_other(s2
))
912 return st1
.st_dev
== st2
.st_dev
&& st1
.st_ino
== st2
.st_ino
;
914 else if (!exists(s1
) && !exists(s2
))
915 ec
= std::make_error_code(std::errc::no_such_file_or_directory
);
917 ec
.assign(err
, std::generic_category());
922 ec
= std::make_error_code(std::errc::not_supported
);
928 fs::file_size(const path
& p
)
931 auto sz
= file_size(p
, ec
);
933 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot get file size", p
, ec
));
939 template<typename Accessor
, typename T
>
941 do_stat(const fs::path
& p
, std::error_code
& ec
, Accessor f
, T deflt
)
943 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
945 if (::stat(p
.c_str(), &st
))
947 ec
.assign(errno
, std::generic_category());
953 ec
= std::make_error_code(std::errc::not_supported
);
960 fs::file_size(const path
& p
, error_code
& ec
) noexcept
964 S(const stat_type
& st
) : type(make_file_type(st
)), size(st
.st_size
) { }
965 S() : type(file_type::not_found
) { }
969 auto s
= do_stat(p
, ec
, [](const auto& st
) { return S
{st
}; }, S
{});
970 if (s
.type
== file_type::regular
)
974 if (s
.type
== file_type::directory
)
975 ec
= std::make_error_code(std::errc::is_a_directory
);
977 ec
= std::make_error_code(std::errc::not_supported
);
983 fs::hard_link_count(const path
& p
)
986 auto count
= hard_link_count(p
, ec
);
988 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot get link count", p
, ec
));
993 fs::hard_link_count(const path
& p
, error_code
& ec
) noexcept
995 return do_stat(p
, ec
, std::mem_fn(&stat::st_nlink
),
996 static_cast<uintmax_t>(-1));
1000 fs::is_empty(const path
& p
)
1003 bool e
= is_empty(p
, ec
);
1005 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot check if file is empty",
1011 fs::is_empty(const path
& p
, error_code
& ec
)
1013 auto s
= status(p
, ec
);
1016 bool empty
= fs::is_directory(s
)
1017 ? fs::directory_iterator(p
, ec
) == fs::directory_iterator()
1018 : fs::file_size(p
, ec
) == 0;
1019 return ec
? false : empty
;
1023 fs::last_write_time(const path
& p
)
1026 auto t
= last_write_time(p
, ec
);
1028 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot get file time", p
, ec
));
1033 fs::last_write_time(const path
& p
, error_code
& ec
) noexcept
1035 return do_stat(p
, ec
, [&ec
](const auto& st
) { return file_time(st
, ec
); },
1036 file_time_type::min());
1040 fs::last_write_time(const path
& p
, file_time_type new_time
)
1043 last_write_time(p
, new_time
, ec
);
1045 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot set file time", p
, ec
));
1049 fs::last_write_time(const path
& p
__attribute__((__unused__
)),
1050 file_time_type new_time
, error_code
& ec
) noexcept
1052 auto d
= new_time
.time_since_epoch();
1053 auto s
= chrono::duration_cast
<chrono::seconds
>(d
);
1054 #if _GLIBCXX_USE_UTIMENSAT
1055 auto ns
= chrono::duration_cast
<chrono::nanoseconds
>(d
- s
);
1056 if (ns
< ns
.zero()) // tv_nsec must be non-negative and less than 10e9.
1059 ns
+= chrono::seconds(1);
1061 struct ::timespec ts
[2];
1063 ts
[0].tv_nsec
= UTIME_OMIT
;
1064 ts
[1].tv_sec
= static_cast<std::time_t>(s
.count());
1065 ts
[1].tv_nsec
= static_cast<long>(ns
.count());
1066 if (::utimensat(AT_FDCWD
, p
.c_str(), ts
, 0))
1067 ec
.assign(errno
, std::generic_category());
1070 #elif _GLIBCXX_HAVE_UTIME_H
1072 times
.modtime
= s
.count();
1073 times
.actime
= do_stat(p
, ec
, [](const auto& st
) { return st
.st_atime
; },
1075 if (::utime(p
.c_str(), ×
))
1076 ec
.assign(errno
, std::generic_category());
1080 ec
= std::make_error_code(std::errc::not_supported
);
1085 fs::permissions(const path
& p
, perms prms
, perm_options opts
)
1088 permissions(p
, prms
, opts
, ec
);
1090 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot set permissions", p
, ec
));
1094 fs::permissions(const path
& p
, perms prms
, perm_options opts
,
1095 error_code
& ec
) noexcept
1097 const bool replace
= is_set(opts
, perm_options::replace
);
1098 const bool add
= is_set(opts
, perm_options::add
);
1099 const bool remove
= is_set(opts
, perm_options::remove
);
1100 const bool nofollow
= is_set(opts
, perm_options::nofollow
);
1101 if (((int)replace
+ (int)add
+ (int)remove
) != 1)
1103 ec
= std::make_error_code(std::errc::invalid_argument
);
1107 prms
&= perms::mask
;
1110 if (add
|| remove
|| nofollow
)
1112 st
= nofollow
? symlink_status(p
, ec
) : status(p
, ec
);
1115 auto curr
= st
.permissions();
1119 prms
= curr
& ~prms
;
1123 #if _GLIBCXX_USE_FCHMODAT
1124 const int flag
= (nofollow
&& is_symlink(st
)) ? AT_SYMLINK_NOFOLLOW
: 0;
1125 if (::fchmodat(AT_FDCWD
, p
.c_str(), static_cast<mode_t
>(prms
), flag
))
1128 if (nofollow
&& is_symlink(st
))
1129 ec
= std::make_error_code(std::errc::operation_not_supported
);
1130 else if (::chmod(p
.c_str(), static_cast<mode_t
>(prms
)))
1135 ec
.assign(err
, std::generic_category());
1141 fs::proximate(const path
& p
, const path
& base
)
1143 return weakly_canonical(p
).lexically_proximate(weakly_canonical(base
));
1147 fs::proximate(const path
& p
, const path
& base
, error_code
& ec
)
1150 const auto p2
= weakly_canonical(p
, ec
);
1153 const auto base2
= weakly_canonical(base
, ec
);
1155 result
= p2
.lexically_proximate(base2
);
1161 fs::read_symlink(const path
& p
)
1164 path tgt
= read_symlink(p
, ec
);
1166 _GLIBCXX_THROW_OR_ABORT(filesystem_error("read_symlink", p
, ec
));
1170 fs::path
fs::read_symlink(const path
& p
, error_code
& ec
)
1173 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
1175 if (::lstat(p
.c_str(), &st
))
1177 ec
.assign(errno
, std::generic_category());
1180 std::string
buf(st
.st_size
? st
.st_size
+ 1 : 128, '\0');
1183 ssize_t len
= ::readlink(p
.c_str(), buf
.data(), buf
.size());
1186 ec
.assign(errno
, std::generic_category());
1189 else if (len
== (ssize_t
)buf
.size())
1191 if (buf
.size() > 4096)
1193 ec
.assign(ENAMETOOLONG
, std::generic_category());
1196 buf
.resize(buf
.size() * 2);
1208 ec
= std::make_error_code(std::errc::not_supported
);
1214 fs::relative(const path
& p
, const path
& base
)
1216 return weakly_canonical(p
).lexically_relative(weakly_canonical(base
));
1220 fs::relative(const path
& p
, const path
& base
, error_code
& ec
)
1222 auto result
= weakly_canonical(p
, ec
);
1225 cbase
= weakly_canonical(base
, ec
);
1227 result
= result
.lexically_relative(cbase
);
1234 fs::remove(const path
& p
)
1237 bool result
= fs::remove(p
, ec
);
1239 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot remove", p
, ec
));
1244 fs::remove(const path
& p
, error_code
& ec
) noexcept
1246 if (exists(symlink_status(p
, ec
)))
1248 if (::remove(p
.c_str()) == 0)
1254 ec
.assign(errno
, std::generic_category());
1261 fs::remove_all(const path
& p
)
1264 bool result
= remove_all(p
, ec
);
1266 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot remove all", p
, ec
));
1271 fs::remove_all(const path
& p
, error_code
& ec
)
1273 auto fs
= symlink_status(p
, ec
);
1274 uintmax_t count
= 0;
1275 if (!ec
&& fs
.type() == file_type::directory
)
1276 for (directory_iterator
d(p
, ec
), end
; !ec
&& d
!= end
; ++d
)
1277 count
+= fs::remove_all(d
->path(), ec
);
1280 return fs::remove(p
, ec
) ? ++count
: -1; // fs:remove() calls ec.clear()
1284 fs::rename(const path
& from
, const path
& to
)
1287 rename(from
, to
, ec
);
1289 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot rename", from
, to
, ec
));
1293 fs::rename(const path
& from
, const path
& to
, error_code
& ec
) noexcept
1295 if (::rename(from
.c_str(), to
.c_str()))
1296 ec
.assign(errno
, std::generic_category());
1302 fs::resize_file(const path
& p
, uintmax_t size
)
1305 resize_file(p
, size
, ec
);
1307 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot resize file", p
, ec
));
1311 fs::resize_file(const path
& p
, uintmax_t size
, error_code
& ec
) noexcept
1313 #ifdef _GLIBCXX_HAVE_UNISTD_H
1314 if (size
> static_cast<uintmax_t>(std::numeric_limits
<off_t
>::max()))
1315 ec
.assign(EINVAL
, std::generic_category());
1316 else if (::truncate(p
.c_str(), size
))
1317 ec
.assign(errno
, std::generic_category());
1321 ec
= std::make_error_code(std::errc::not_supported
);
1327 fs::space(const path
& p
)
1330 space_info s
= space(p
, ec
);
1332 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot get free space", p
, ec
));
1337 fs::space(const path
& p
, error_code
& ec
) noexcept
1340 static_cast<uintmax_t>(-1),
1341 static_cast<uintmax_t>(-1),
1342 static_cast<uintmax_t>(-1)
1344 #ifdef _GLIBCXX_HAVE_SYS_STATVFS_H
1346 if (::statvfs(p
.c_str(), &f
))
1347 ec
.assign(errno
, std::generic_category());
1351 f
.f_blocks
* f
.f_frsize
,
1352 f
.f_bfree
* f
.f_frsize
,
1353 f
.f_bavail
* f
.f_frsize
1358 ec
= std::make_error_code(std::errc::not_supported
);
1363 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
1365 fs::status(const fs::path
& p
, error_code
& ec
) noexcept
1369 if (::stat(p
.c_str(), &st
))
1372 ec
.assign(err
, std::generic_category());
1373 if (is_not_found_errno(err
))
1374 status
.type(file_type::not_found
);
1376 else if (err
== EOVERFLOW
)
1377 status
.type(file_type::unknown
);
1382 status
= make_file_status(st
);
1389 fs::symlink_status(const fs::path
& p
, std::error_code
& ec
) noexcept
1393 if (::lstat(p
.c_str(), &st
))
1396 ec
.assign(err
, std::generic_category());
1397 if (is_not_found_errno(err
))
1398 status
.type(file_type::not_found
);
1402 status
= make_file_status(st
);
1410 fs::status(const fs::path
& p
)
1413 auto result
= status(p
, ec
);
1414 if (result
.type() == file_type::none
)
1415 _GLIBCXX_THROW_OR_ABORT(filesystem_error("status", p
, ec
));
1420 fs::symlink_status(const fs::path
& p
)
1423 auto result
= symlink_status(p
, ec
);
1424 if (result
.type() == file_type::none
)
1425 _GLIBCXX_THROW_OR_ABORT(filesystem_error("symlink_status", p
, ec
));
1429 fs::path
fs::temp_directory_path()
1432 path tmp
= temp_directory_path(ec
);
1434 _GLIBCXX_THROW_OR_ABORT(filesystem_error("temp_directory_path", ec
));
1438 fs::path
fs::temp_directory_path(error_code
& ec
)
1440 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1441 ec
= std::make_error_code(std::errc::not_supported
);
1444 const char* tmpdir
= nullptr;
1445 const char* env
[] = { "TMPDIR", "TMP", "TEMP", "TEMPDIR", nullptr };
1446 for (auto e
= env
; tmpdir
== nullptr && *e
!= nullptr; ++e
)
1447 tmpdir
= ::getenv(*e
);
1448 path p
= tmpdir
? tmpdir
: "/tmp";
1449 auto st
= status(p
, ec
);
1452 if (is_directory(st
))
1458 ec
= std::make_error_code(std::errc::not_a_directory
);
1465 fs::weakly_canonical(const path
& p
)
1468 if (exists(status(p
)))
1469 return canonical(p
);
1472 auto iter
= p
.begin(), end
= p
.end();
1473 // find leading elements of p that exist:
1476 tmp
= result
/ *iter
;
1477 if (exists(status(tmp
)))
1484 result
= canonical(result
);
1485 // append the non-existing elements:
1489 return result
.lexically_normal();
1493 fs::weakly_canonical(const path
& p
, error_code
& ec
)
1496 file_status st
= status(p
, ec
);
1498 return canonical(p
, ec
);
1499 else if (status_known(st
))
1505 auto iter
= p
.begin(), end
= p
.end();
1506 // find leading elements of p that exist:
1509 tmp
= result
/ *iter
;
1510 st
= status(tmp
, ec
);
1515 if (status_known(st
))
1523 result
= canonical(result
, ec
);
1528 // append the non-existing elements:
1532 result
= result
.lexically_normal();