1 // Filesystem operations -*- C++ -*-
3 // Copyright (C) 2014-2016 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
29 #include <experimental/filesystem>
35 #include <limits.h> // PATH_MAX
36 #ifdef _GLIBCXX_HAVE_UNISTD_H
38 # if defined(_GLIBCXX_HAVE_SYS_STAT_H) && defined(_GLIBCXX_HAVE_SYS_TYPES_H)
39 # include <sys/types.h>
40 # include <sys/stat.h>
43 #ifdef _GLIBCXX_HAVE_FCNTL_H
46 #ifdef _GLIBCXX_HAVE_SYS_STATVFS_H
47 # include <sys/statvfs.h>
49 #ifdef _GLIBCXX_USE_SENDFILE
50 # include <sys/sendfile.h>
52 # include <ext/stdio_filebuf.h>
55 #if _GLIBCXX_HAVE_UTIME_H
59 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
61 # define utime _wutime
63 # define chmod _wchmod
66 namespace fs
= std::experimental::filesystem
;
69 fs::absolute(const path
& p
, const path
& base
)
71 const bool has_root_dir
= p
.has_root_directory();
72 const bool has_root_name
= p
.has_root_name();
74 if (has_root_dir
&& has_root_name
)
78 abs
= base
.is_absolute() ? base
: absolute(base
);
80 abs
= abs
.root_name() / p
;
81 else if (has_root_name
)
82 abs
= p
.root_name() / abs
.root_directory() / abs
.relative_path()
92 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
93 inline bool is_dot(wchar_t c
) { return c
== L
'.'; }
95 inline bool is_dot(char c
) { return c
== '.'; }
98 inline bool is_dot(const fs::path
& path
)
100 const auto& filename
= path
.native();
101 return filename
.size() == 1 && is_dot(filename
[0]);
104 inline bool is_dotdot(const fs::path
& path
)
106 const auto& filename
= path
.native();
107 return filename
.size() == 2 && is_dot(filename
[0]) && is_dot(filename
[1]);
110 struct free_as_in_malloc
112 void operator()(void* p
) const { ::free(p
); }
115 using char_ptr
= std::unique_ptr
<char[], free_as_in_malloc
>;
119 fs::canonical(const path
& p
, const path
& base
, error_code
& ec
)
121 const path pa
= absolute(p
, base
);
124 #ifdef _GLIBCXX_USE_REALPATH
125 char_ptr buf
{ nullptr };
126 # if _XOPEN_VERSION < 700
127 // Not safe to call realpath(path, NULL)
128 buf
.reset( (char*)::malloc(PATH_MAX
) );
130 if (char* rp
= ::realpath(pa
.c_str(), buf
.get()))
138 if (errno
!= ENAMETOOLONG
)
140 ec
.assign(errno
, std::generic_category());
147 // else: we know there are (currently) no unresolvable symlink loops
149 result
= pa
.root_path();
152 for (auto& f
: pa
.relative_path())
155 int max_allowed_symlinks
= 40;
157 while (!cmpts
.empty() && !ec
)
159 path f
= std::move(cmpts
.front());
164 if (!is_directory(result
, ec
) && !ec
)
165 ec
.assign(ENOTDIR
, std::generic_category());
167 else if (is_dotdot(f
))
169 auto parent
= result
.parent_path();
171 result
= pa
.root_path();
179 if (is_symlink(result
, ec
))
181 path link
= read_symlink(result
, ec
);
184 if (--max_allowed_symlinks
== 0)
185 ec
.assign(ELOOP
, std::generic_category());
188 if (link
.is_absolute())
190 result
= link
.root_path();
191 link
= link
.relative_path();
194 result
.remove_filename();
196 cmpts
.insert(cmpts
.begin(), link
.begin(), link
.end());
203 if (ec
|| !exists(result
, ec
))
210 fs::canonical(const path
& p
, error_code
& ec
)
212 path cur
= current_path(ec
);
215 return canonical(p
, cur
, ec
);
219 fs::canonical(const path
& p
, const path
& base
)
222 path can
= canonical(p
, base
, ec
);
224 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot canonicalize", p
, ec
));
229 fs::copy(const path
& from
, const path
& to
, copy_options options
)
232 copy(from
, to
, options
, ec
);
234 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot copy", from
, to
, ec
));
239 template<typename Bitmask
>
240 inline bool is_set(Bitmask obj
, Bitmask bits
)
242 return (obj
& bits
) != Bitmask::none
;
246 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
249 typedef struct ::stat stat_type
;
252 make_file_type(const stat_type
& st
) noexcept
255 #ifdef _GLIBCXX_HAVE_S_ISREG
256 if (S_ISREG(st
.st_mode
))
257 return file_type::regular
;
258 else if (S_ISDIR(st
.st_mode
))
259 return file_type::directory
;
260 else if (S_ISCHR(st
.st_mode
))
261 return file_type::character
;
262 else if (S_ISBLK(st
.st_mode
))
263 return file_type::block
;
264 else if (S_ISFIFO(st
.st_mode
))
265 return file_type::fifo
;
266 else if (S_ISLNK(st
.st_mode
))
267 return file_type::symlink
;
268 else if (S_ISSOCK(st
.st_mode
))
269 return file_type::socket
;
271 return file_type::unknown
;
275 inline fs::file_status
276 make_file_status(const stat_type
& st
) noexcept
278 return fs::file_status
{
280 static_cast<fs::perms
>(st
.st_mode
) & fs::perms::mask
285 is_not_found_errno(int err
) noexcept
287 return err
== ENOENT
|| err
== ENOTDIR
;
290 inline fs::file_time_type
291 file_time(const stat_type
& st
) noexcept
293 using namespace std::chrono
;
294 return fs::file_time_type
{
295 #ifdef _GLIBCXX_USE_ST_MTIM
296 seconds
{st
.st_mtim
.tv_sec
} + nanoseconds
{st
.st_mtim
.tv_nsec
}
303 // Returns true if the file descriptor was successfully closed,
304 // otherwise returns false and the reason will be in errno.
315 do_copy_file(const fs::path
& from
, const fs::path
& to
,
316 fs::copy_options option
,
317 stat_type
* from_st
, stat_type
* to_st
,
318 std::error_code
& ec
) noexcept
321 fs::file_status t
, f
;
323 if (to_st
== nullptr)
325 if (::stat(to
.c_str(), &st1
))
328 if (!is_not_found_errno(err
))
330 ec
.assign(err
, std::generic_category());
337 else if (to_st
== from_st
)
340 if (to_st
== nullptr)
341 t
= fs::file_status
{fs::file_type::not_found
};
343 t
= make_file_status(*to_st
);
345 if (from_st
== nullptr)
347 if (::stat(from
.c_str(), &st2
))
349 ec
.assign(errno
, std::generic_category());
355 f
= make_file_status(*from_st
);
357 using opts
= fs::copy_options
;
361 if (!is_other(t
) && !is_other(f
)
362 && to_st
->st_dev
== from_st
->st_dev
363 && to_st
->st_ino
== from_st
->st_ino
)
365 ec
= std::make_error_code(std::errc::file_exists
);
369 if (is_set(option
, opts::skip_existing
))
374 else if (is_set(option
, opts::update_existing
))
376 if (file_time(*from_st
) <= file_time(*to_st
))
382 else if (!is_set(option
, opts::overwrite_existing
))
384 ec
= std::make_error_code(std::errc::file_exists
);
390 ~CloseFD() { if (fd
!= -1) close_fd(fd
); }
391 bool close() { return close_fd(std::exchange(fd
, -1)); }
395 CloseFD in
= { ::open(from
.c_str(), O_RDONLY
) };
398 ec
.assign(errno
, std::generic_category());
401 int oflag
= O_WRONLY
|O_CREAT
;
402 if (is_set(option
, opts::overwrite_existing
|opts::update_existing
))
406 CloseFD out
= { ::open(to
.c_str(), oflag
, S_IWUSR
) };
409 if (errno
== EEXIST
&& is_set(option
, opts::skip_existing
))
412 ec
.assign(errno
, std::generic_category());
416 #ifdef _GLIBCXX_USE_FCHMOD
417 if (::fchmod(out
.fd
, from_st
->st_mode
))
418 #elif _GLIBCXX_USE_FCHMODAT
419 if (::fchmodat(AT_FDCWD
, to
.c_str(), from_st
->st_mode
, 0))
421 if (::chmod(to
.c_str(), from_st
->st_mode
))
424 ec
.assign(errno
, std::generic_category());
428 #ifdef _GLIBCXX_USE_SENDFILE
429 const auto n
= ::sendfile(out
.fd
, in
.fd
, nullptr, from_st
->st_size
);
430 if (n
!= from_st
->st_size
)
432 ec
.assign(errno
, std::generic_category());
435 if (!out
.close() || !in
.close())
437 ec
.assign(errno
, std::generic_category());
441 __gnu_cxx::stdio_filebuf
<char> sbin(in
.fd
, std::ios::in
);
442 __gnu_cxx::stdio_filebuf
<char> sbout(out
.fd
, std::ios::out
);
447 if (from_st
->st_size
&& !(std::ostream(&sbout
) << &sbin
))
449 ec
= std::make_error_code(std::errc::io_error
);
452 if (sbout
.close() || sbin
.close())
454 ec
.assign(errno
, std::generic_category());
466 fs::copy(const path
& from
, const path
& to
, copy_options options
,
467 error_code
& ec
) noexcept
469 const bool skip_symlinks
= is_set(options
, copy_options::skip_symlinks
);
470 const bool create_symlinks
= is_set(options
, copy_options::create_symlinks
);
471 const bool copy_symlinks
= is_set(options
, copy_options::copy_symlinks
);
472 const bool use_lstat
= create_symlinks
|| skip_symlinks
;
475 stat_type from_st
, to_st
;
476 // N4099 doesn't check copy_symlinks here, but I think that's a defect.
477 if (use_lstat
|| copy_symlinks
478 ? ::lstat(from
.c_str(), &from_st
)
479 : ::stat(from
.c_str(), &from_st
))
481 ec
.assign(errno
, std::generic_category());
485 ? ::lstat(to
.c_str(), &to_st
)
486 : ::stat(to
.c_str(), &to_st
))
488 if (!is_not_found_errno(errno
))
490 ec
.assign(errno
, std::generic_category());
493 t
= file_status
{file_type::not_found
};
496 t
= make_file_status(to_st
);
497 f
= make_file_status(from_st
);
499 if (exists(t
) && !is_other(t
) && !is_other(f
)
500 && to_st
.st_dev
== from_st
.st_dev
&& to_st
.st_ino
== from_st
.st_ino
)
502 ec
= std::make_error_code(std::errc::file_exists
);
505 if (is_other(f
) || is_other(t
))
507 ec
= std::make_error_code(std::errc::not_supported
);
510 if (is_directory(f
) && is_regular_file(t
))
512 ec
= std::make_error_code(std::errc::is_a_directory
);
520 else if (!exists(t
) && copy_symlinks
)
521 copy_symlink(from
, to
, ec
);
523 // Not clear what should be done here.
524 // "Otherwise report an error as specified in Error reporting (7)."
525 ec
= std::make_error_code(std::errc::invalid_argument
);
527 else if (is_regular_file(f
))
529 if (is_set(options
, copy_options::directories_only
))
531 else if (create_symlinks
)
532 create_symlink(from
, to
, ec
);
533 else if (is_set(options
, copy_options::create_hard_links
))
534 create_hard_link(from
, to
, ec
);
535 else if (is_directory(t
))
536 do_copy_file(from
, to
/ from
.filename(), options
, &from_st
, 0, ec
);
539 auto ptr
= exists(t
) ? &to_st
: &from_st
;
540 do_copy_file(from
, to
, options
, &from_st
, ptr
, ec
);
543 else if (is_directory(f
) && (is_set(options
, copy_options::recursive
)
544 || options
== copy_options::none
))
547 if (!create_directory(to
, from
, ec
))
549 // set an unused bit in options to disable further recursion
550 if (!is_set(options
, copy_options::recursive
))
551 options
|= static_cast<copy_options
>(4096);
552 for (const directory_entry
& x
: directory_iterator(from
))
553 copy(x
.path(), to
/x
.path().filename(), options
, ec
);
555 // "Otherwise no effects." (should ec.clear() be called?)
559 fs::copy_file(const path
& from
, const path
& to
, copy_options option
)
562 bool result
= copy_file(from
, to
, option
, ec
);
564 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot copy file", from
, to
,
570 fs::copy_file(const path
& from
, const path
& to
, copy_options option
,
571 error_code
& ec
) noexcept
573 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
574 return do_copy_file(from
, to
, option
, nullptr, nullptr, ec
);
576 ec
= std::make_error_code(std::errc::not_supported
);
583 fs::copy_symlink(const path
& existing_symlink
, const path
& new_symlink
)
586 copy_symlink(existing_symlink
, new_symlink
, ec
);
588 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot copy symlink",
589 existing_symlink
, new_symlink
, ec
));
593 fs::copy_symlink(const path
& existing_symlink
, const path
& new_symlink
,
594 error_code
& ec
) noexcept
596 auto p
= read_symlink(existing_symlink
, ec
);
599 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
602 create_directory_symlink(p
, new_symlink
, ec
);
606 create_symlink(p
, new_symlink
, ec
);
611 fs::create_directories(const path
& p
)
614 bool result
= create_directories(p
, ec
);
616 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot create directories", p
,
622 fs::create_directories(const path
& p
, error_code
& ec
) noexcept
626 ec
= std::make_error_code(errc::invalid_argument
);
629 std::stack
<path
> missing
;
632 while (!pp
.empty() && status(pp
, ec
).type() == file_type::not_found
)
635 const auto& filename
= pp
.filename();
636 if (!is_dot(filename
) && !is_dotdot(filename
))
638 pp
.remove_filename();
641 if (ec
|| missing
.empty())
646 const path
& top
= missing
.top();
647 create_directory(top
, ec
);
648 if (ec
&& is_directory(top
))
652 while (!missing
.empty() && !ec
);
654 return missing
.empty();
660 create_dir(const fs::path
& p
, fs::perms perm
, std::error_code
& ec
)
662 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
663 ::mode_t mode
= static_cast<std::underlying_type_t
<fs::perms
>>(perm
);
664 if (::mkdir(p
.c_str(), mode
))
666 ec
.assign(errno
, std::generic_category());
675 ec
= std::make_error_code(std::errc::not_supported
);
682 fs::create_directory(const path
& p
)
685 bool result
= create_directory(p
, ec
);
687 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot create directory", p
,
693 fs::create_directory(const path
& p
, error_code
& ec
) noexcept
695 return create_dir(p
, perms::all
, ec
);
700 fs::create_directory(const path
& p
, const path
& attributes
)
703 bool result
= create_directory(p
, attributes
, ec
);
705 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot create directory", p
,
711 fs::create_directory(const path
& p
, const path
& attributes
,
712 error_code
& ec
) noexcept
714 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
716 if (::stat(attributes
.c_str(), &st
))
718 ec
.assign(errno
, std::generic_category());
721 return create_dir(p
, static_cast<perms
>(st
.st_mode
), ec
);
723 ec
= std::make_error_code(std::errc::not_supported
);
730 fs::create_directory_symlink(const path
& to
, const path
& new_symlink
)
733 create_directory_symlink(to
, new_symlink
, ec
);
735 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot create directory symlink",
736 to
, new_symlink
, ec
));
740 fs::create_directory_symlink(const path
& to
, const path
& new_symlink
,
741 error_code
& ec
) noexcept
743 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
744 ec
= std::make_error_code(std::errc::not_supported
);
746 create_symlink(to
, new_symlink
, ec
);
752 fs::create_hard_link(const path
& to
, const path
& new_hard_link
)
755 create_hard_link(to
, new_hard_link
, ec
);
757 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot create hard link",
758 to
, new_hard_link
, ec
));
762 fs::create_hard_link(const path
& to
, const path
& new_hard_link
,
763 error_code
& ec
) noexcept
765 #ifdef _GLIBCXX_HAVE_UNISTD_H
766 if (::link(to
.c_str(), new_hard_link
.c_str()))
767 ec
.assign(errno
, std::generic_category());
771 ec
= std::make_error_code(std::errc::not_supported
);
776 fs::create_symlink(const path
& to
, const path
& new_symlink
)
779 create_symlink(to
, new_symlink
, ec
);
781 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot create symlink",
782 to
, new_symlink
, ec
));
786 fs::create_symlink(const path
& to
, const path
& new_symlink
,
787 error_code
& ec
) noexcept
789 #ifdef _GLIBCXX_HAVE_UNISTD_H
790 if (::symlink(to
.c_str(), new_symlink
.c_str()))
791 ec
.assign(errno
, std::generic_category());
795 ec
= std::make_error_code(std::errc::not_supported
);
804 path p
= current_path(ec
);
806 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot get current path", ec
));
811 fs::current_path(error_code
& ec
)
814 #ifdef _GLIBCXX_HAVE_UNISTD_H
816 if (char_ptr cwd
= char_ptr
{::getcwd(nullptr, 0)})
822 ec
.assign(errno
, std::generic_category());
824 long path_max
= pathconf(".", _PC_PATH_MAX
);
828 else if (path_max
> 10240)
832 for (char_ptr buf
; p
.empty(); size
*= 2)
834 buf
.reset((char*)malloc(size
));
837 if (getcwd(buf
.get(), size
))
842 else if (errno
!= ERANGE
)
844 ec
.assign(errno
, std::generic_category());
850 ec
= std::make_error_code(std::errc::not_enough_memory
);
855 #else // _GLIBCXX_HAVE_UNISTD_H
856 ec
= std::make_error_code(std::errc::not_supported
);
862 fs::current_path(const path
& p
)
867 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot set current path", ec
));
871 fs::current_path(const path
& p
, error_code
& ec
) noexcept
873 #ifdef _GLIBCXX_HAVE_UNISTD_H
874 if (::chdir(p
.c_str()))
875 ec
.assign(errno
, std::generic_category());
879 ec
= std::make_error_code(std::errc::not_supported
);
884 fs::equivalent(const path
& p1
, const path
& p2
)
887 auto result
= equivalent(p1
, p2
, ec
);
889 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot check file equivalence",
895 fs::equivalent(const path
& p1
, const path
& p2
, error_code
& ec
) noexcept
897 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
899 if (::stat(p1
.c_str(), &st1
) == 0 && ::stat(p2
.c_str(), &st2
) == 0)
901 file_status s1
= make_file_status(st1
);
902 file_status s2
= make_file_status(st2
);
903 if (is_other(s1
) && is_other(s2
))
905 ec
= std::make_error_code(std::errc::not_supported
);
909 return st1
.st_dev
== st2
.st_dev
&& st1
.st_ino
== st2
.st_ino
;
911 else if (is_not_found_errno(errno
))
913 ec
= std::make_error_code(std::errc::no_such_file_or_directory
);
916 ec
.assign(errno
, std::generic_category());
918 ec
= std::make_error_code(std::errc::not_supported
);
924 fs::file_size(const path
& p
)
927 auto sz
= file_size(p
, ec
);
929 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot get file size", p
, ec
));
935 template<typename Accessor
, typename T
>
937 do_stat(const fs::path
& p
, std::error_code
& ec
, Accessor f
, T deflt
)
939 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
941 if (::stat(p
.c_str(), &st
))
943 ec
.assign(errno
, std::generic_category());
949 ec
= std::make_error_code(std::errc::not_supported
);
956 fs::file_size(const path
& p
, error_code
& ec
) noexcept
960 S(const stat_type
& st
) : type(make_file_type(st
)), size(st
.st_size
) { }
961 S() : type(file_type::not_found
) { }
965 auto s
= do_stat(p
, ec
, [](const auto& st
) { return S
{st
}; }, S
{});
966 if (s
.type
== file_type::regular
)
970 if (s
.type
== file_type::directory
)
971 ec
= std::make_error_code(std::errc::is_a_directory
);
973 ec
= std::make_error_code(std::errc::not_supported
);
979 fs::hard_link_count(const path
& p
)
982 auto count
= hard_link_count(p
, ec
);
984 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot get link count", p
, ec
));
989 fs::hard_link_count(const path
& p
, error_code
& ec
) noexcept
991 return do_stat(p
, ec
, std::mem_fn(&stat::st_nlink
),
992 static_cast<uintmax_t>(-1));
996 fs::is_empty(const path
& p
)
998 return fs::is_directory(status(p
))
999 ? fs::directory_iterator(p
) == fs::directory_iterator()
1000 : fs::file_size(p
) == 0;
1004 fs::is_empty(const path
& p
, error_code
& ec
) noexcept
1006 auto s
= status(p
, ec
);
1009 return fs::is_directory(s
)
1010 ? fs::directory_iterator(p
, ec
) == fs::directory_iterator()
1011 : fs::file_size(p
, ec
) == 0;
1015 fs::last_write_time(const path
& p
)
1018 auto t
= last_write_time(p
, ec
);
1020 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot get file time", p
, ec
));
1025 fs::last_write_time(const path
& p
, error_code
& ec
) noexcept
1027 return do_stat(p
, ec
, [](const auto& st
) { return file_time(st
); },
1028 file_time_type::min());
1032 fs::last_write_time(const path
& p
, file_time_type new_time
)
1035 last_write_time(p
, new_time
, ec
);
1037 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot set file time", p
, ec
));
1041 fs::last_write_time(const path
& p
__attribute__((__unused__
)),
1042 file_time_type new_time
, error_code
& ec
) noexcept
1044 auto d
= new_time
.time_since_epoch();
1045 auto s
= chrono::duration_cast
<chrono::seconds
>(d
);
1046 #if _GLIBCXX_USE_UTIMENSAT
1047 auto ns
= chrono::duration_cast
<chrono::nanoseconds
>(d
- s
);
1048 struct ::timespec ts
[2];
1050 ts
[0].tv_nsec
= UTIME_OMIT
;
1051 ts
[1].tv_sec
= static_cast<std::time_t>(s
.count());
1052 ts
[1].tv_nsec
= static_cast<long>(ns
.count());
1053 if (::utimensat(AT_FDCWD
, p
.c_str(), ts
, 0))
1054 ec
.assign(errno
, std::generic_category());
1057 #elif _GLIBCXX_HAVE_UTIME_H
1059 times
.modtime
= s
.count();
1060 times
.actime
= do_stat(p
, ec
, [](const auto& st
) { return st
.st_atime
; },
1062 if (::utime(p
.c_str(), ×
))
1063 ec
.assign(errno
, std::generic_category());
1067 ec
= std::make_error_code(std::errc::not_supported
);
1072 fs::permissions(const path
& p
, perms prms
)
1075 permissions(p
, prms
, ec
);
1077 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot set permissions", p
, ec
));
1080 void fs::permissions(const path
& p
, perms prms
, error_code
& ec
) noexcept
1082 #if _GLIBCXX_USE_FCHMODAT
1083 if (::fchmodat(AT_FDCWD
, p
.c_str(), static_cast<mode_t
>(prms
), 0))
1085 if (::chmod(p
.c_str(), static_cast<mode_t
>(prms
)))
1087 ec
.assign(errno
, std::generic_category());
1093 fs::read_symlink(const path
& p
)
1096 path tgt
= read_symlink(p
, ec
);
1098 _GLIBCXX_THROW_OR_ABORT(filesystem_error("read_symlink", p
, ec
));
1102 fs::path
fs::read_symlink(const path
& p
, error_code
& ec
)
1104 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
1106 if (::lstat(p
.c_str(), &st
))
1108 ec
.assign(errno
, std::generic_category());
1111 std::string
buf(st
.st_size
, '\0');
1112 ssize_t len
= ::readlink(p
.c_str(), &buf
.front(), buf
.size());
1115 ec
.assign(errno
, std::generic_category());
1118 return path
{buf
.data(), buf
.data()+len
};
1120 ec
= std::make_error_code(std::errc::not_supported
);
1127 fs::remove(const path
& p
)
1130 bool result
= fs::remove(p
, ec
);
1132 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot remove", p
, ec
));
1137 fs::remove(const path
& p
, error_code
& ec
) noexcept
1139 if (exists(symlink_status(p
, ec
)))
1141 if (::remove(p
.c_str()) == 0)
1147 ec
.assign(errno
, std::generic_category());
1154 fs::remove_all(const path
& p
)
1157 bool result
= remove_all(p
, ec
);
1159 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot remove all", p
, ec
));
1164 fs::remove_all(const path
& p
, error_code
& ec
) noexcept
1166 auto fs
= symlink_status(p
, ec
);
1167 uintmax_t count
= 0;
1168 if (ec
.value() == 0 && fs
.type() == file_type::directory
)
1169 for (directory_iterator
d(p
, ec
), end
; ec
.value() == 0 && d
!= end
; ++d
)
1170 count
+= fs::remove(d
->path(), ec
);
1173 return fs::remove(p
, ec
) ? ++count
: -1; // fs:remove() calls ec.clear()
1177 fs::rename(const path
& from
, const path
& to
)
1180 rename(from
, to
, ec
);
1182 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot rename", from
, to
, ec
));
1186 fs::rename(const path
& from
, const path
& to
, error_code
& ec
) noexcept
1188 if (::rename(from
.c_str(), to
.c_str()))
1189 ec
.assign(errno
, std::generic_category());
1195 fs::resize_file(const path
& p
, uintmax_t size
)
1198 resize_file(p
, size
, ec
);
1200 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot resize file", p
, ec
));
1204 fs::resize_file(const path
& p
, uintmax_t size
, error_code
& ec
) noexcept
1206 #ifdef _GLIBCXX_HAVE_UNISTD_H
1207 if (size
> static_cast<uintmax_t>(std::numeric_limits
<off_t
>::max()))
1208 ec
.assign(EINVAL
, std::generic_category());
1209 else if (::truncate(p
.c_str(), size
))
1210 ec
.assign(errno
, std::generic_category());
1214 ec
= std::make_error_code(std::errc::not_supported
);
1220 fs::space(const path
& p
)
1223 space_info s
= space(p
, ec
);
1225 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot get free space", p
, ec
));
1230 fs::space(const path
& p
, error_code
& ec
) noexcept
1233 static_cast<uintmax_t>(-1),
1234 static_cast<uintmax_t>(-1),
1235 static_cast<uintmax_t>(-1)
1237 #ifdef _GLIBCXX_HAVE_SYS_STATVFS_H
1239 if (::statvfs(p
.c_str(), &f
))
1240 ec
.assign(errno
, std::generic_category());
1244 f
.f_blocks
* f
.f_frsize
,
1245 f
.f_bfree
* f
.f_frsize
,
1246 f
.f_bavail
* f
.f_frsize
1251 ec
= std::make_error_code(std::errc::not_supported
);
1256 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
1258 fs::status(const fs::path
& p
, std::error_code
& ec
) noexcept
1262 if (::stat(p
.c_str(), &st
))
1265 ec
.assign(err
, std::generic_category());
1266 if (is_not_found_errno(err
))
1267 status
.type(file_type::not_found
);
1271 status
= make_file_status(st
);
1278 fs::symlink_status(const fs::path
& p
, std::error_code
& ec
) noexcept
1282 if (::lstat(p
.c_str(), &st
))
1285 ec
.assign(err
, std::generic_category());
1286 if (is_not_found_errno(err
))
1287 status
.type(file_type::not_found
);
1291 status
= make_file_status(st
);
1299 fs::status(const fs::path
& p
)
1302 auto result
= status(p
, ec
);
1303 if (result
.type() == file_type::none
)
1304 _GLIBCXX_THROW_OR_ABORT(filesystem_error("status", p
, ec
));
1309 fs::symlink_status(const fs::path
& p
)
1312 auto result
= symlink_status(p
, ec
);
1313 if (result
.type() == file_type::none
)
1314 _GLIBCXX_THROW_OR_ABORT(filesystem_error("symlink_status", p
, ec
));
1319 fs::system_complete(const path
& p
)
1322 path comp
= system_complete(p
, ec
);
1324 _GLIBCXX_THROW_OR_ABORT(filesystem_error("system_complete", p
, ec
));
1329 fs::system_complete(const path
& p
, error_code
& ec
)
1331 path base
= current_path(ec
);
1332 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1333 if (p
.is_absolute() || !p
.has_root_name()
1334 || p
.root_name() == base
.root_name())
1335 return absolute(p
, base
);
1337 ec
= std::make_error_code(std::errc::not_supported
);
1342 return absolute(p
, base
);
1346 fs::path
fs::temp_directory_path()
1349 path tmp
= temp_directory_path(ec
);
1351 _GLIBCXX_THROW_OR_ABORT(filesystem_error("temp_directory_path", ec
));
1355 fs::path
fs::temp_directory_path(error_code
& ec
)
1357 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1358 ec
= std::make_error_code(std::errc::not_supported
);
1361 const char* tmpdir
= nullptr;
1362 const char* env
[] = { "TMPDIR", "TMP", "TEMP", "TEMPDIR", nullptr };
1363 for (auto e
= env
; tmpdir
== nullptr && *e
!= nullptr; ++e
)
1364 tmpdir
= ::getenv(*e
);
1365 path p
= tmpdir
? tmpdir
: "/tmp";
1366 if (exists(p
) && is_directory(p
))
1371 ec
= std::make_error_code(std::errc::not_a_directory
);