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>
33 #include <ext/stdio_filebuf.h>
37 #include <limits.h> // PATH_MAX
38 #ifdef _GLIBCXX_HAVE_UNISTD_H
40 # if defined(_GLIBCXX_HAVE_SYS_STAT_H) && defined(_GLIBCXX_HAVE_SYS_TYPES_H)
41 # include <sys/types.h>
42 # include <sys/stat.h>
45 #ifdef _GLIBCXX_HAVE_FCNTL_H
48 #ifdef _GLIBCXX_HAVE_SYS_STATVFS_H
49 # include <sys/statvfs.h>
51 #ifdef _GLIBCXX_USE_SENDFILE
52 # include <sys/sendfile.h>
54 #if _GLIBCXX_HAVE_UTIME_H
58 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
60 # define utime _wutime
62 # define chmod _wchmod
65 namespace fs
= std::experimental::filesystem
;
68 fs::absolute(const path
& p
, const path
& base
)
70 const bool has_root_dir
= p
.has_root_directory();
71 const bool has_root_name
= p
.has_root_name();
73 if (has_root_dir
&& has_root_name
)
77 abs
= base
.is_absolute() ? base
: absolute(base
);
79 abs
= abs
.root_name() / p
;
80 else if (has_root_name
)
81 abs
= p
.root_name() / abs
.root_directory() / abs
.relative_path()
91 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
92 inline bool is_dot(wchar_t c
) { return c
== L
'.'; }
94 inline bool is_dot(char c
) { return c
== '.'; }
97 inline bool is_dot(const fs::path
& path
)
99 const auto& filename
= path
.native();
100 return filename
.size() == 1 && is_dot(filename
[0]);
103 inline bool is_dotdot(const fs::path
& path
)
105 const auto& filename
= path
.native();
106 return filename
.size() == 2 && is_dot(filename
[0]) && is_dot(filename
[1]);
109 struct free_as_in_malloc
111 void operator()(void* p
) const { ::free(p
); }
114 using char_ptr
= std::unique_ptr
<char[], free_as_in_malloc
>;
118 fs::canonical(const path
& p
, const path
& base
, error_code
& ec
)
120 const path pa
= absolute(p
, base
);
123 #ifdef _GLIBCXX_USE_REALPATH
124 char_ptr buf
{ nullptr };
125 # if _XOPEN_VERSION < 700
126 // Not safe to call realpath(path, NULL)
127 buf
.reset( (char*)::malloc(PATH_MAX
) );
129 if (char* rp
= ::realpath(pa
.c_str(), buf
.get()))
137 if (errno
!= ENAMETOOLONG
)
139 ec
.assign(errno
, std::generic_category());
146 // else: we know there are (currently) no unresolvable symlink loops
148 result
= pa
.root_path();
151 for (auto& f
: pa
.relative_path())
154 int max_allowed_symlinks
= 40;
156 while (!cmpts
.empty() && !ec
)
158 path f
= std::move(cmpts
.front());
163 if (!is_directory(result
, ec
) && !ec
)
164 ec
.assign(ENOTDIR
, std::generic_category());
166 else if (is_dotdot(f
))
168 auto parent
= result
.parent_path();
170 result
= pa
.root_path();
178 if (is_symlink(result
, ec
))
180 path link
= read_symlink(result
, ec
);
183 if (--max_allowed_symlinks
== 0)
184 ec
.assign(ELOOP
, std::generic_category());
187 if (link
.is_absolute())
189 result
= link
.root_path();
190 link
= link
.relative_path();
193 result
.remove_filename();
195 cmpts
.insert(cmpts
.begin(), link
.begin(), link
.end());
202 if (ec
|| !exists(result
, ec
))
209 fs::canonical(const path
& p
, error_code
& ec
)
211 path cur
= current_path(ec
);
214 return canonical(p
, cur
, ec
);
218 fs::canonical(const path
& p
, const path
& base
)
221 path can
= canonical(p
, base
, ec
);
223 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot canonicalize", p
, base
,
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
, std::error_code
& ec
) noexcept
293 using namespace std::chrono
;
294 #ifdef _GLIBCXX_USE_ST_MTIM
295 time_t s
= st
.st_mtim
.tv_sec
;
296 nanoseconds ns
{st
.st_mtim
.tv_nsec
};
298 time_t s
= st
.st_mtime
;
302 if (s
>= (nanoseconds::max().count() / 1e9
))
304 ec
= std::make_error_code(std::errc::value_too_large
); // EOVERFLOW
305 return fs::file_time_type::min();
308 return fs::file_time_type
{seconds
{s
} + ns
};
312 do_copy_file(const fs::path
& from
, const fs::path
& to
,
313 fs::copy_options option
,
314 stat_type
* from_st
, stat_type
* to_st
,
315 std::error_code
& ec
) noexcept
318 fs::file_status t
, f
;
320 if (to_st
== nullptr)
322 if (::stat(to
.c_str(), &st1
))
325 if (!is_not_found_errno(err
))
327 ec
.assign(err
, std::generic_category());
334 else if (to_st
== from_st
)
337 if (to_st
== nullptr)
338 t
= fs::file_status
{fs::file_type::not_found
};
340 t
= make_file_status(*to_st
);
342 if (from_st
== nullptr)
344 if (::stat(from
.c_str(), &st2
))
346 ec
.assign(errno
, std::generic_category());
352 f
= make_file_status(*from_st
);
353 // _GLIBCXX_RESOLVE_LIB_DEFECTS
354 // 2712. copy_file() has a number of unspecified error conditions
355 if (!is_regular_file(f
))
357 ec
= std::make_error_code(std::errc::not_supported
);
361 using opts
= fs::copy_options
;
365 if (!is_regular_file(t
))
367 ec
= std::make_error_code(std::errc::not_supported
);
371 if (to_st
->st_dev
== from_st
->st_dev
372 && to_st
->st_ino
== from_st
->st_ino
)
374 ec
= std::make_error_code(std::errc::file_exists
);
378 if (is_set(option
, opts::skip_existing
))
383 else if (is_set(option
, opts::update_existing
))
385 const auto from_mtime
= file_time(*from_st
, ec
);
388 if ((from_mtime
<= file_time(*to_st
, ec
)) || ec
)
391 else if (!is_set(option
, opts::overwrite_existing
))
393 ec
= std::make_error_code(std::errc::file_exists
);
396 else if (!is_regular_file(t
))
398 ec
= std::make_error_code(std::errc::not_supported
);
404 ~CloseFD() { if (fd
!= -1) ::close(fd
); }
405 bool close() { return ::close(std::exchange(fd
, -1)) == 0; }
409 CloseFD in
= { ::open(from
.c_str(), O_RDONLY
) };
412 ec
.assign(errno
, std::generic_category());
415 int oflag
= O_WRONLY
|O_CREAT
;
416 if (is_set(option
, opts::overwrite_existing
|opts::update_existing
))
420 CloseFD out
= { ::open(to
.c_str(), oflag
, S_IWUSR
) };
423 if (errno
== EEXIST
&& is_set(option
, opts::skip_existing
))
426 ec
.assign(errno
, std::generic_category());
430 #ifdef _GLIBCXX_USE_FCHMOD
431 if (::fchmod(out
.fd
, from_st
->st_mode
))
432 #elif defined _GLIBCXX_USE_FCHMODAT
433 if (::fchmodat(AT_FDCWD
, to
.c_str(), from_st
->st_mode
, 0))
435 if (::chmod(to
.c_str(), from_st
->st_mode
))
438 ec
.assign(errno
, std::generic_category());
442 #ifdef _GLIBCXX_USE_SENDFILE
443 const auto n
= ::sendfile(out
.fd
, in
.fd
, nullptr, from_st
->st_size
);
444 if (n
< 0 && (errno
== ENOSYS
|| errno
== EINVAL
))
447 __gnu_cxx::stdio_filebuf
<char> sbin(in
.fd
, std::ios::in
);
448 __gnu_cxx::stdio_filebuf
<char> sbout(out
.fd
, std::ios::out
);
453 if (from_st
->st_size
&& !(std::ostream(&sbout
) << &sbin
))
455 ec
= std::make_error_code(std::errc::io_error
);
458 if (!sbout
.close() || !sbin
.close())
460 ec
.assign(errno
, std::generic_category());
467 #ifdef _GLIBCXX_USE_SENDFILE
469 if (n
!= from_st
->st_size
)
471 ec
.assign(errno
, std::generic_category());
474 if (!out
.close() || !in
.close())
476 ec
.assign(errno
, std::generic_category());
488 fs::copy(const path
& from
, const path
& to
, copy_options options
,
489 error_code
& ec
) noexcept
491 const bool skip_symlinks
= is_set(options
, copy_options::skip_symlinks
);
492 const bool create_symlinks
= is_set(options
, copy_options::create_symlinks
);
493 const bool copy_symlinks
= is_set(options
, copy_options::copy_symlinks
);
494 const bool use_lstat
= create_symlinks
|| skip_symlinks
;
497 stat_type from_st
, to_st
;
498 // _GLIBCXX_RESOLVE_LIB_DEFECTS
499 // 2681. filesystem::copy() cannot copy symlinks
500 if (use_lstat
|| copy_symlinks
501 ? ::lstat(from
.c_str(), &from_st
)
502 : ::stat(from
.c_str(), &from_st
))
504 ec
.assign(errno
, std::generic_category());
508 ? ::lstat(to
.c_str(), &to_st
)
509 : ::stat(to
.c_str(), &to_st
))
511 if (!is_not_found_errno(errno
))
513 ec
.assign(errno
, std::generic_category());
516 t
= file_status
{file_type::not_found
};
519 t
= make_file_status(to_st
);
520 f
= make_file_status(from_st
);
522 if (exists(t
) && !is_other(t
) && !is_other(f
)
523 && to_st
.st_dev
== from_st
.st_dev
&& to_st
.st_ino
== from_st
.st_ino
)
525 ec
= std::make_error_code(std::errc::file_exists
);
528 if (is_other(f
) || is_other(t
))
530 ec
= std::make_error_code(std::errc::not_supported
);
533 if (is_directory(f
) && is_regular_file(t
))
535 ec
= std::make_error_code(std::errc::is_a_directory
);
543 else if (!exists(t
) && copy_symlinks
)
544 copy_symlink(from
, to
, ec
);
546 // Not clear what should be done here.
547 // "Otherwise report an error as specified in Error reporting (7)."
548 ec
= std::make_error_code(std::errc::invalid_argument
);
550 else if (is_regular_file(f
))
552 if (is_set(options
, copy_options::directories_only
))
554 else if (create_symlinks
)
555 create_symlink(from
, to
, ec
);
556 else if (is_set(options
, copy_options::create_hard_links
))
557 create_hard_link(from
, to
, ec
);
558 else if (is_directory(t
))
559 do_copy_file(from
, to
/ from
.filename(), options
, &from_st
, 0, ec
);
562 auto ptr
= exists(t
) ? &to_st
: &from_st
;
563 do_copy_file(from
, to
, options
, &from_st
, ptr
, ec
);
566 // _GLIBCXX_RESOLVE_LIB_DEFECTS
567 // 2682. filesystem::copy() won't create a symlink to a directory
568 else if (is_directory(f
) && create_symlinks
)
569 ec
= std::make_error_code(errc::is_a_directory
);
570 else if (is_directory(f
) && (is_set(options
, copy_options::recursive
)
571 || options
== copy_options::none
))
574 if (!create_directory(to
, from
, ec
))
576 // set an unused bit in options to disable further recursion
577 if (!is_set(options
, copy_options::recursive
))
578 options
|= static_cast<copy_options
>(4096);
579 for (const directory_entry
& x
: directory_iterator(from
))
580 copy(x
.path(), to
/x
.path().filename(), options
, ec
);
582 // _GLIBCXX_RESOLVE_LIB_DEFECTS
583 // 2683. filesystem::copy() says "no effects"
589 fs::copy_file(const path
& from
, const path
& to
, copy_options option
)
592 bool result
= copy_file(from
, to
, option
, ec
);
594 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot copy file", from
, to
,
600 fs::copy_file(const path
& from
, const path
& to
, copy_options option
,
601 error_code
& ec
) noexcept
603 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
604 return do_copy_file(from
, to
, option
, nullptr, nullptr, ec
);
606 ec
= std::make_error_code(std::errc::not_supported
);
613 fs::copy_symlink(const path
& existing_symlink
, const path
& new_symlink
)
616 copy_symlink(existing_symlink
, new_symlink
, ec
);
618 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot copy symlink",
619 existing_symlink
, new_symlink
, ec
));
623 fs::copy_symlink(const path
& existing_symlink
, const path
& new_symlink
,
624 error_code
& ec
) noexcept
626 auto p
= read_symlink(existing_symlink
, ec
);
629 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
632 create_directory_symlink(p
, new_symlink
, ec
);
636 create_symlink(p
, new_symlink
, ec
);
641 fs::create_directories(const path
& p
)
644 bool result
= create_directories(p
, ec
);
646 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot create directories", p
,
652 fs::create_directories(const path
& p
, error_code
& ec
) noexcept
656 ec
= std::make_error_code(errc::invalid_argument
);
659 std::stack
<path
> missing
;
662 while (!pp
.empty() && status(pp
, ec
).type() == file_type::not_found
)
665 const auto& filename
= pp
.filename();
666 if (!is_dot(filename
) && !is_dotdot(filename
))
668 pp
.remove_filename();
671 if (ec
|| missing
.empty())
676 const path
& top
= missing
.top();
677 create_directory(top
, ec
);
678 if (ec
&& is_directory(top
))
682 while (!missing
.empty() && !ec
);
684 return missing
.empty();
690 create_dir(const fs::path
& p
, fs::perms perm
, std::error_code
& ec
)
692 bool created
= false;
693 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
694 ::mode_t mode
= static_cast<std::underlying_type_t
<fs::perms
>>(perm
);
695 if (::mkdir(p
.c_str(), mode
))
697 const int err
= errno
;
698 if (err
!= EEXIST
|| !is_directory(p
))
699 ec
.assign(err
, std::generic_category());
709 ec
= std::make_error_code(std::errc::not_supported
);
716 fs::create_directory(const path
& p
)
719 bool result
= create_directory(p
, ec
);
721 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot create directory", p
,
727 fs::create_directory(const path
& p
, error_code
& ec
) noexcept
729 return create_dir(p
, perms::all
, ec
);
734 fs::create_directory(const path
& p
, const path
& attributes
)
737 bool result
= create_directory(p
, attributes
, ec
);
739 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot create directory", p
,
745 fs::create_directory(const path
& p
, const path
& attributes
,
746 error_code
& ec
) noexcept
748 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
750 if (::stat(attributes
.c_str(), &st
))
752 ec
.assign(errno
, std::generic_category());
755 return create_dir(p
, static_cast<perms
>(st
.st_mode
), ec
);
757 ec
= std::make_error_code(std::errc::not_supported
);
764 fs::create_directory_symlink(const path
& to
, const path
& new_symlink
)
767 create_directory_symlink(to
, new_symlink
, ec
);
769 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot create directory symlink",
770 to
, new_symlink
, ec
));
774 fs::create_directory_symlink(const path
& to
, const path
& new_symlink
,
775 error_code
& ec
) noexcept
777 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
778 ec
= std::make_error_code(std::errc::not_supported
);
780 create_symlink(to
, new_symlink
, ec
);
786 fs::create_hard_link(const path
& to
, const path
& new_hard_link
)
789 create_hard_link(to
, new_hard_link
, ec
);
791 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot create hard link",
792 to
, new_hard_link
, ec
));
796 fs::create_hard_link(const path
& to
, const path
& new_hard_link
,
797 error_code
& ec
) noexcept
799 #ifdef _GLIBCXX_HAVE_UNISTD_H
800 if (::link(to
.c_str(), new_hard_link
.c_str()))
801 ec
.assign(errno
, std::generic_category());
805 ec
= std::make_error_code(std::errc::not_supported
);
810 fs::create_symlink(const path
& to
, const path
& new_symlink
)
813 create_symlink(to
, new_symlink
, ec
);
815 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot create symlink",
816 to
, new_symlink
, ec
));
820 fs::create_symlink(const path
& to
, const path
& new_symlink
,
821 error_code
& ec
) noexcept
823 #ifdef _GLIBCXX_HAVE_UNISTD_H
824 if (::symlink(to
.c_str(), new_symlink
.c_str()))
825 ec
.assign(errno
, std::generic_category());
829 ec
= std::make_error_code(std::errc::not_supported
);
838 path p
= current_path(ec
);
840 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot get current path", ec
));
845 fs::current_path(error_code
& ec
)
848 #ifdef _GLIBCXX_HAVE_UNISTD_H
850 if (char_ptr cwd
= char_ptr
{::getcwd(nullptr, 0)})
856 ec
.assign(errno
, std::generic_category());
858 long path_max
= pathconf(".", _PC_PATH_MAX
);
862 else if (path_max
> 10240)
866 for (char_ptr buf
; p
.empty(); size
*= 2)
868 buf
.reset((char*)malloc(size
));
871 if (getcwd(buf
.get(), size
))
876 else if (errno
!= ERANGE
)
878 ec
.assign(errno
, std::generic_category());
884 ec
= std::make_error_code(std::errc::not_enough_memory
);
889 #else // _GLIBCXX_HAVE_UNISTD_H
890 ec
= std::make_error_code(std::errc::not_supported
);
896 fs::current_path(const path
& p
)
901 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot set current path", ec
));
905 fs::current_path(const path
& p
, error_code
& ec
) noexcept
907 #ifdef _GLIBCXX_HAVE_UNISTD_H
908 if (::chdir(p
.c_str()))
909 ec
.assign(errno
, std::generic_category());
913 ec
= std::make_error_code(std::errc::not_supported
);
918 fs::equivalent(const path
& p1
, const path
& p2
)
921 auto result
= equivalent(p1
, p2
, ec
);
923 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot check file equivalence",
929 fs::equivalent(const path
& p1
, const path
& p2
, error_code
& ec
) noexcept
931 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
935 if (::stat(p1
.c_str(), &st1
) == 0)
936 s1
= make_file_status(st1
);
937 else if (is_not_found_errno(errno
))
938 s1
.type(file_type::not_found
);
942 if (::stat(p2
.c_str(), &st2
) == 0)
943 s2
= make_file_status(st2
);
944 else if (is_not_found_errno(errno
))
945 s2
.type(file_type::not_found
);
949 if (exists(s1
) && exists(s2
))
951 if (is_other(s1
) && is_other(s2
))
953 ec
= std::make_error_code(std::errc::not_supported
);
957 if (is_other(s1
) || is_other(s2
))
959 return st1
.st_dev
== st2
.st_dev
&& st1
.st_ino
== st2
.st_ino
;
961 else if (!exists(s1
) && !exists(s2
))
962 ec
= std::make_error_code(std::errc::no_such_file_or_directory
);
964 ec
.assign(err
, std::generic_category());
969 ec
= std::make_error_code(std::errc::not_supported
);
975 fs::file_size(const path
& p
)
978 auto sz
= file_size(p
, ec
);
980 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot get file size", p
, ec
));
986 template<typename Accessor
, typename T
>
988 do_stat(const fs::path
& p
, std::error_code
& ec
, Accessor f
, T deflt
)
990 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
992 if (::stat(p
.c_str(), &st
))
994 ec
.assign(errno
, std::generic_category());
1000 ec
= std::make_error_code(std::errc::not_supported
);
1007 fs::file_size(const path
& p
, error_code
& ec
) noexcept
1011 S(const stat_type
& st
) : type(make_file_type(st
)), size(st
.st_size
) { }
1012 S() : type(file_type::not_found
) { }
1016 auto s
= do_stat(p
, ec
, [](const auto& st
) { return S
{st
}; }, S
{});
1017 if (s
.type
== file_type::regular
)
1021 if (s
.type
== file_type::directory
)
1022 ec
= std::make_error_code(std::errc::is_a_directory
);
1024 ec
= std::make_error_code(std::errc::not_supported
);
1030 fs::hard_link_count(const path
& p
)
1033 auto count
= hard_link_count(p
, ec
);
1035 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot get link count", p
, ec
));
1040 fs::hard_link_count(const path
& p
, error_code
& ec
) noexcept
1042 return do_stat(p
, ec
, std::mem_fn(&stat::st_nlink
),
1043 static_cast<uintmax_t>(-1));
1047 fs::is_empty(const path
& p
)
1050 bool e
= is_empty(p
, ec
);
1052 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot check is file is empty",
1058 fs::is_empty(const path
& p
, error_code
& ec
) noexcept
1060 auto s
= status(p
, ec
);
1063 bool empty
= fs::is_directory(s
)
1064 ? fs::directory_iterator(p
, ec
) == fs::directory_iterator()
1065 : fs::file_size(p
, ec
) == 0;
1066 return ec
? false : empty
;
1070 fs::last_write_time(const path
& p
)
1073 auto t
= last_write_time(p
, ec
);
1075 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot get file time", p
, ec
));
1080 fs::last_write_time(const path
& p
, error_code
& ec
) noexcept
1082 return do_stat(p
, ec
, [&ec
](const auto& st
) { return file_time(st
, ec
); },
1083 file_time_type::min());
1087 fs::last_write_time(const path
& p
, file_time_type new_time
)
1090 last_write_time(p
, new_time
, ec
);
1092 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot set file time", p
, ec
));
1096 fs::last_write_time(const path
& p
__attribute__((__unused__
)),
1097 file_time_type new_time
, error_code
& ec
) noexcept
1099 auto d
= new_time
.time_since_epoch();
1100 auto s
= chrono::duration_cast
<chrono::seconds
>(d
);
1101 #if _GLIBCXX_USE_UTIMENSAT
1102 auto ns
= chrono::duration_cast
<chrono::nanoseconds
>(d
- s
);
1103 if (ns
< ns
.zero()) // tv_nsec must be non-negative and less than 10e9.
1106 ns
+= chrono::seconds(1);
1108 struct ::timespec ts
[2];
1110 ts
[0].tv_nsec
= UTIME_OMIT
;
1111 ts
[1].tv_sec
= static_cast<std::time_t>(s
.count());
1112 ts
[1].tv_nsec
= static_cast<long>(ns
.count());
1113 if (::utimensat(AT_FDCWD
, p
.c_str(), ts
, 0))
1114 ec
.assign(errno
, std::generic_category());
1117 #elif _GLIBCXX_HAVE_UTIME_H
1119 times
.modtime
= s
.count();
1120 times
.actime
= do_stat(p
, ec
, [](const auto& st
) { return st
.st_atime
; },
1122 if (::utime(p
.c_str(), ×
))
1123 ec
.assign(errno
, std::generic_category());
1127 ec
= std::make_error_code(std::errc::not_supported
);
1132 fs::permissions(const path
& p
, perms prms
)
1135 permissions(p
, prms
, ec
);
1137 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot set permissions", p
, ec
));
1141 fs::permissions(const path
& p
, perms prms
, error_code
& ec
) noexcept
1143 const bool add
= is_set(prms
, perms::add_perms
);
1144 const bool remove
= is_set(prms
, perms::remove_perms
);
1145 const bool nofollow
= is_set(prms
, perms::symlink_nofollow
);
1148 ec
= std::make_error_code(std::errc::invalid_argument
);
1152 prms
&= perms::mask
;
1155 if (add
|| remove
|| nofollow
)
1157 st
= nofollow
? symlink_status(p
, ec
) : status(p
, ec
);
1160 auto curr
= st
.permissions();
1164 prms
= curr
& ~prms
;
1168 #if _GLIBCXX_USE_FCHMODAT
1169 const int flag
= (nofollow
&& is_symlink(st
)) ? AT_SYMLINK_NOFOLLOW
: 0;
1170 if (::fchmodat(AT_FDCWD
, p
.c_str(), static_cast<mode_t
>(prms
), flag
))
1173 if (nofollow
&& is_symlink(st
))
1174 ec
= std::make_error_code(std::errc::operation_not_supported
);
1175 else if (::chmod(p
.c_str(), static_cast<mode_t
>(prms
)))
1180 ec
.assign(err
, std::generic_category());
1186 fs::read_symlink(const path
& p
)
1189 path tgt
= read_symlink(p
, ec
);
1191 _GLIBCXX_THROW_OR_ABORT(filesystem_error("read_symlink", p
, ec
));
1195 fs::path
fs::read_symlink(const path
& p
, error_code
& ec
)
1197 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
1199 if (::lstat(p
.c_str(), &st
))
1201 ec
.assign(errno
, std::generic_category());
1204 std::string
buf(st
.st_size
, '\0');
1205 ssize_t len
= ::readlink(p
.c_str(), &buf
.front(), buf
.size());
1208 ec
.assign(errno
, std::generic_category());
1212 return path
{buf
.data(), buf
.data()+len
};
1214 ec
= std::make_error_code(std::errc::not_supported
);
1221 fs::remove(const path
& p
)
1224 bool result
= fs::remove(p
, ec
);
1226 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot remove", p
, ec
));
1231 fs::remove(const path
& p
, error_code
& ec
) noexcept
1233 if (exists(symlink_status(p
, ec
)))
1235 if (::remove(p
.c_str()) == 0)
1241 ec
.assign(errno
, std::generic_category());
1248 fs::remove_all(const path
& p
)
1251 bool result
= remove_all(p
, ec
);
1253 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot remove all", p
, ec
));
1258 fs::remove_all(const path
& p
, error_code
& ec
) noexcept
1260 auto fs
= symlink_status(p
, ec
);
1261 uintmax_t count
= 0;
1262 if (ec
.value() == 0 && fs
.type() == file_type::directory
)
1263 for (directory_iterator
d(p
, ec
), end
; ec
.value() == 0 && d
!= end
; ++d
)
1264 count
+= fs::remove_all(d
->path(), ec
);
1267 return fs::remove(p
, ec
) ? ++count
: -1; // fs:remove() calls ec.clear()
1271 fs::rename(const path
& from
, const path
& to
)
1274 rename(from
, to
, ec
);
1276 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot rename", from
, to
, ec
));
1280 fs::rename(const path
& from
, const path
& to
, error_code
& ec
) noexcept
1282 if (::rename(from
.c_str(), to
.c_str()))
1283 ec
.assign(errno
, std::generic_category());
1289 fs::resize_file(const path
& p
, uintmax_t size
)
1292 resize_file(p
, size
, ec
);
1294 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot resize file", p
, ec
));
1298 fs::resize_file(const path
& p
, uintmax_t size
, error_code
& ec
) noexcept
1300 #ifdef _GLIBCXX_HAVE_UNISTD_H
1301 if (size
> static_cast<uintmax_t>(std::numeric_limits
<off_t
>::max()))
1302 ec
.assign(EINVAL
, std::generic_category());
1303 else if (::truncate(p
.c_str(), size
))
1304 ec
.assign(errno
, std::generic_category());
1308 ec
= std::make_error_code(std::errc::not_supported
);
1314 fs::space(const path
& p
)
1317 space_info s
= space(p
, ec
);
1319 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot get free space", p
, ec
));
1324 fs::space(const path
& p
, error_code
& ec
) noexcept
1327 static_cast<uintmax_t>(-1),
1328 static_cast<uintmax_t>(-1),
1329 static_cast<uintmax_t>(-1)
1331 #ifdef _GLIBCXX_HAVE_SYS_STATVFS_H
1333 if (::statvfs(p
.c_str(), &f
))
1334 ec
.assign(errno
, std::generic_category());
1338 f
.f_blocks
* f
.f_frsize
,
1339 f
.f_bfree
* f
.f_frsize
,
1340 f
.f_bavail
* f
.f_frsize
1345 ec
= std::make_error_code(std::errc::not_supported
);
1350 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
1352 fs::status(const fs::path
& p
, error_code
& ec
) noexcept
1356 if (::stat(p
.c_str(), &st
))
1359 ec
.assign(err
, std::generic_category());
1360 if (is_not_found_errno(err
))
1361 status
.type(file_type::not_found
);
1363 else if (err
== EOVERFLOW
)
1364 status
.type(file_type::unknown
);
1369 status
= make_file_status(st
);
1376 fs::symlink_status(const fs::path
& p
, std::error_code
& ec
) noexcept
1380 if (::lstat(p
.c_str(), &st
))
1383 ec
.assign(err
, std::generic_category());
1384 if (is_not_found_errno(err
))
1385 status
.type(file_type::not_found
);
1389 status
= make_file_status(st
);
1397 fs::status(const fs::path
& p
)
1400 auto result
= status(p
, ec
);
1401 if (result
.type() == file_type::none
)
1402 _GLIBCXX_THROW_OR_ABORT(filesystem_error("status", p
, ec
));
1407 fs::symlink_status(const fs::path
& p
)
1410 auto result
= symlink_status(p
, ec
);
1411 if (result
.type() == file_type::none
)
1412 _GLIBCXX_THROW_OR_ABORT(filesystem_error("symlink_status", p
, ec
));
1417 fs::system_complete(const path
& p
)
1420 path comp
= system_complete(p
, ec
);
1422 _GLIBCXX_THROW_OR_ABORT(filesystem_error("system_complete", p
, ec
));
1427 fs::system_complete(const path
& p
, error_code
& ec
)
1429 path base
= current_path(ec
);
1430 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1431 if (p
.is_absolute() || !p
.has_root_name()
1432 || p
.root_name() == base
.root_name())
1433 return absolute(p
, base
);
1435 ec
= std::make_error_code(std::errc::not_supported
);
1440 return absolute(p
, base
);
1444 fs::path
fs::temp_directory_path()
1447 path tmp
= temp_directory_path(ec
);
1449 _GLIBCXX_THROW_OR_ABORT(filesystem_error("temp_directory_path", ec
));
1453 fs::path
fs::temp_directory_path(error_code
& ec
)
1455 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1456 ec
= std::make_error_code(std::errc::not_supported
);
1459 const char* tmpdir
= nullptr;
1460 const char* env
[] = { "TMPDIR", "TMP", "TEMP", "TEMPDIR", nullptr };
1461 for (auto e
= env
; tmpdir
== nullptr && *e
!= nullptr; ++e
)
1462 tmpdir
= ::getenv(*e
);
1463 path p
= tmpdir
? tmpdir
: "/tmp";
1464 auto st
= status(p
, ec
);
1467 if (is_directory(st
))
1473 ec
= std::make_error_code(std::errc::not_a_directory
);