1 // Filesystem TS operations -*- C++ -*-
3 // Copyright (C) 2014-2018 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_FCNTL_H
39 # include <fcntl.h> // AT_FDCWD, AT_SYMLINK_NOFOLLOW
41 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
42 # include <sys/stat.h> // stat, utimensat, fchmodat
44 #ifdef _GLIBCXX_HAVE_SYS_STATVFS_H
45 # include <sys/statvfs.h> // statvfs
47 #if !_GLIBCXX_USE_UTIMENSAT && _GLIBCXX_HAVE_UTIME_H
48 # include <utime.h> // utime
51 #define _GLIBCXX_BEGIN_NAMESPACE_FILESYSTEM \
52 namespace experimental { namespace filesystem {
53 #define _GLIBCXX_END_NAMESPACE_FILESYSTEM } }
54 #include "ops-common.h"
56 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
58 # define utime _wutime
60 # define chmod _wchmod
63 namespace fs
= std::experimental::filesystem
;
66 fs::absolute(const path
& p
, const path
& base
)
68 const bool has_root_dir
= p
.has_root_directory();
69 const bool has_root_name
= p
.has_root_name();
71 if (has_root_dir
&& has_root_name
)
75 abs
= base
.is_absolute() ? base
: absolute(base
);
77 abs
= abs
.root_name() / p
;
78 else if (has_root_name
)
79 abs
= p
.root_name() / abs
.root_directory() / abs
.relative_path()
89 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
90 inline bool is_dot(wchar_t c
) { return c
== L
'.'; }
92 inline bool is_dot(char c
) { return c
== '.'; }
95 inline bool is_dot(const fs::path
& path
)
97 const auto& filename
= path
.native();
98 return filename
.size() == 1 && is_dot(filename
[0]);
101 inline bool is_dotdot(const fs::path
& path
)
103 const auto& filename
= path
.native();
104 return filename
.size() == 2 && is_dot(filename
[0]) && is_dot(filename
[1]);
107 struct free_as_in_malloc
109 void operator()(void* p
) const { ::free(p
); }
112 using char_ptr
= std::unique_ptr
<char[], free_as_in_malloc
>;
116 fs::canonical(const path
& p
, const path
& base
, error_code
& ec
)
118 const path pa
= absolute(p
, base
);
121 #ifdef _GLIBCXX_USE_REALPATH
122 char_ptr buf
{ nullptr };
123 # if _XOPEN_VERSION < 700
124 // Not safe to call realpath(path, NULL)
125 buf
.reset( (char*)::malloc(PATH_MAX
) );
127 if (char* rp
= ::realpath(pa
.c_str(), buf
.get()))
135 if (errno
!= ENAMETOOLONG
)
137 ec
.assign(errno
, std::generic_category());
145 ec
= make_error_code(std::errc::no_such_file_or_directory
);
148 // else: we know there are (currently) no unresolvable symlink loops
150 result
= pa
.root_path();
153 for (auto& f
: pa
.relative_path())
156 int max_allowed_symlinks
= 40;
158 while (!cmpts
.empty() && !ec
)
160 path f
= std::move(cmpts
.front());
165 if (!is_directory(result
, ec
) && !ec
)
166 ec
.assign(ENOTDIR
, std::generic_category());
168 else if (is_dotdot(f
))
170 auto parent
= result
.parent_path();
172 result
= pa
.root_path();
180 if (is_symlink(result
, ec
))
182 path link
= read_symlink(result
, ec
);
185 if (--max_allowed_symlinks
== 0)
186 ec
.assign(ELOOP
, std::generic_category());
189 if (link
.is_absolute())
191 result
= link
.root_path();
192 link
= link
.relative_path();
195 result
.remove_filename();
197 cmpts
.insert(cmpts
.begin(), link
.begin(), link
.end());
204 if (ec
|| !exists(result
, ec
))
211 fs::canonical(const path
& p
, error_code
& ec
)
213 path cur
= current_path(ec
);
216 return canonical(p
, cur
, ec
);
220 fs::canonical(const path
& p
, const path
& base
)
223 path can
= canonical(p
, base
, ec
);
225 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot canonicalize", p
, base
,
231 fs::copy(const path
& from
, const path
& to
, copy_options options
)
234 copy(from
, to
, options
, ec
);
236 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot copy", from
, to
, ec
));
241 using std::filesystem::is_set
;
243 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
244 typedef struct ::stat stat_type
;
246 using std::filesystem::is_not_found_errno
;
247 using std::filesystem::file_time
;
248 using std::filesystem::do_copy_file
;
249 #endif // _GLIBCXX_HAVE_SYS_STAT_H
253 fs::copy(const path
& from
, const path
& to
, copy_options options
,
254 error_code
& ec
) noexcept
256 const bool skip_symlinks
= is_set(options
, copy_options::skip_symlinks
);
257 const bool create_symlinks
= is_set(options
, copy_options::create_symlinks
);
258 const bool copy_symlinks
= is_set(options
, copy_options::copy_symlinks
);
259 const bool use_lstat
= create_symlinks
|| skip_symlinks
;
262 stat_type from_st
, to_st
;
263 // _GLIBCXX_RESOLVE_LIB_DEFECTS
264 // 2681. filesystem::copy() cannot copy symlinks
265 if (use_lstat
|| copy_symlinks
266 ? ::lstat(from
.c_str(), &from_st
)
267 : ::stat(from
.c_str(), &from_st
))
269 ec
.assign(errno
, std::generic_category());
273 ? ::lstat(to
.c_str(), &to_st
)
274 : ::stat(to
.c_str(), &to_st
))
276 if (!is_not_found_errno(errno
))
278 ec
.assign(errno
, std::generic_category());
281 t
= file_status
{file_type::not_found
};
284 t
= make_file_status(to_st
);
285 f
= make_file_status(from_st
);
287 if (exists(t
) && !is_other(t
) && !is_other(f
)
288 && to_st
.st_dev
== from_st
.st_dev
&& to_st
.st_ino
== from_st
.st_ino
)
290 ec
= std::make_error_code(std::errc::file_exists
);
293 if (is_other(f
) || is_other(t
))
295 ec
= std::make_error_code(std::errc::not_supported
);
298 if (is_directory(f
) && is_regular_file(t
))
300 ec
= std::make_error_code(std::errc::is_a_directory
);
308 else if (!exists(t
) && copy_symlinks
)
309 copy_symlink(from
, to
, ec
);
311 // Not clear what should be done here.
312 // "Otherwise report an error as specified in Error reporting (7)."
313 ec
= std::make_error_code(std::errc::invalid_argument
);
315 else if (is_regular_file(f
))
317 if (is_set(options
, copy_options::directories_only
))
319 else if (create_symlinks
)
320 create_symlink(from
, to
, ec
);
321 else if (is_set(options
, copy_options::create_hard_links
))
322 create_hard_link(from
, to
, ec
);
323 else if (is_directory(t
))
324 do_copy_file(from
.c_str(), (to
/ from
.filename()).c_str(),
325 copy_file_options(options
), &from_st
, nullptr, ec
);
328 auto ptr
= exists(t
) ? &to_st
: &from_st
;
329 do_copy_file(from
.c_str(), to
.c_str(), copy_file_options(options
),
333 // _GLIBCXX_RESOLVE_LIB_DEFECTS
334 // 2682. filesystem::copy() won't create a symlink to a directory
335 else if (is_directory(f
) && create_symlinks
)
336 ec
= std::make_error_code(errc::is_a_directory
);
337 else if (is_directory(f
) && (is_set(options
, copy_options::recursive
)
338 || options
== copy_options::none
))
341 if (!create_directory(to
, from
, ec
))
343 // set an unused bit in options to disable further recursion
344 if (!is_set(options
, copy_options::recursive
))
345 options
|= static_cast<copy_options
>(4096);
346 for (const directory_entry
& x
: directory_iterator(from
))
347 copy(x
.path(), to
/x
.path().filename(), options
, ec
);
349 // _GLIBCXX_RESOLVE_LIB_DEFECTS
350 // 2683. filesystem::copy() says "no effects"
356 fs::copy_file(const path
& from
, const path
& to
, copy_options option
)
359 bool result
= copy_file(from
, to
, option
, ec
);
361 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot copy file", from
, to
,
367 fs::copy_file(const path
& from
, const path
& to
, copy_options options
,
368 error_code
& ec
) noexcept
370 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
371 return do_copy_file(from
.c_str(), to
.c_str(), copy_file_options(options
),
372 nullptr, nullptr, ec
);
374 ec
= std::make_error_code(std::errc::not_supported
);
381 fs::copy_symlink(const path
& existing_symlink
, const path
& new_symlink
)
384 copy_symlink(existing_symlink
, new_symlink
, ec
);
386 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot copy symlink",
387 existing_symlink
, new_symlink
, ec
));
391 fs::copy_symlink(const path
& existing_symlink
, const path
& new_symlink
,
392 error_code
& ec
) noexcept
394 auto p
= read_symlink(existing_symlink
, ec
);
397 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
400 create_directory_symlink(p
, new_symlink
, ec
);
404 create_symlink(p
, new_symlink
, ec
);
409 fs::create_directories(const path
& p
)
412 bool result
= create_directories(p
, ec
);
414 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot create directories", p
,
420 fs::create_directories(const path
& p
, error_code
& ec
) noexcept
424 ec
= std::make_error_code(errc::invalid_argument
);
427 std::stack
<path
> missing
;
430 while (!pp
.empty() && status(pp
, ec
).type() == file_type::not_found
)
433 const auto& filename
= pp
.filename();
434 if (!is_dot(filename
) && !is_dotdot(filename
))
436 pp
.remove_filename();
439 if (ec
|| missing
.empty())
444 const path
& top
= missing
.top();
445 create_directory(top
, ec
);
446 if (ec
&& is_directory(top
))
450 while (!missing
.empty() && !ec
);
452 return missing
.empty();
458 create_dir(const fs::path
& p
, fs::perms perm
, std::error_code
& ec
)
460 bool created
= false;
461 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
462 ::mode_t mode
= static_cast<std::underlying_type_t
<fs::perms
>>(perm
);
463 if (::mkdir(p
.c_str(), mode
))
465 const int err
= errno
;
466 if (err
!= EEXIST
|| !is_directory(p
, ec
))
467 ec
.assign(err
, std::generic_category());
475 ec
= std::make_error_code(std::errc::not_supported
);
482 fs::create_directory(const path
& p
)
485 bool result
= create_directory(p
, ec
);
487 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot create directory", p
,
493 fs::create_directory(const path
& p
, error_code
& ec
) noexcept
495 return create_dir(p
, perms::all
, ec
);
500 fs::create_directory(const path
& p
, const path
& attributes
)
503 bool result
= create_directory(p
, attributes
, ec
);
505 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot create directory", p
,
511 fs::create_directory(const path
& p
, const path
& attributes
,
512 error_code
& ec
) noexcept
514 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
516 if (::stat(attributes
.c_str(), &st
))
518 ec
.assign(errno
, std::generic_category());
521 return create_dir(p
, static_cast<perms
>(st
.st_mode
), ec
);
523 ec
= std::make_error_code(std::errc::not_supported
);
530 fs::create_directory_symlink(const path
& to
, const path
& new_symlink
)
533 create_directory_symlink(to
, new_symlink
, ec
);
535 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot create directory symlink",
536 to
, new_symlink
, ec
));
540 fs::create_directory_symlink(const path
& to
, const path
& new_symlink
,
541 error_code
& ec
) noexcept
543 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
544 ec
= std::make_error_code(std::errc::not_supported
);
546 create_symlink(to
, new_symlink
, ec
);
552 fs::create_hard_link(const path
& to
, const path
& new_hard_link
)
555 create_hard_link(to
, new_hard_link
, ec
);
557 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot create hard link",
558 to
, new_hard_link
, ec
));
562 fs::create_hard_link(const path
& to
, const path
& new_hard_link
,
563 error_code
& ec
) noexcept
565 #ifdef _GLIBCXX_HAVE_UNISTD_H
566 if (::link(to
.c_str(), new_hard_link
.c_str()))
567 ec
.assign(errno
, std::generic_category());
571 ec
= std::make_error_code(std::errc::not_supported
);
576 fs::create_symlink(const path
& to
, const path
& new_symlink
)
579 create_symlink(to
, new_symlink
, ec
);
581 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot create symlink",
582 to
, new_symlink
, ec
));
586 fs::create_symlink(const path
& to
, const path
& new_symlink
,
587 error_code
& ec
) noexcept
589 #ifdef _GLIBCXX_HAVE_UNISTD_H
590 if (::symlink(to
.c_str(), new_symlink
.c_str()))
591 ec
.assign(errno
, std::generic_category());
595 ec
= std::make_error_code(std::errc::not_supported
);
604 path p
= current_path(ec
);
606 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot get current path", ec
));
611 fs::current_path(error_code
& ec
)
614 #ifdef _GLIBCXX_HAVE_UNISTD_H
616 if (char_ptr cwd
= char_ptr
{::getcwd(nullptr, 0)})
622 ec
.assign(errno
, std::generic_category());
624 long path_max
= pathconf(".", _PC_PATH_MAX
);
628 else if (path_max
> 10240)
632 for (char_ptr buf
; p
.empty(); size
*= 2)
634 buf
.reset((char*)malloc(size
));
637 if (getcwd(buf
.get(), size
))
642 else if (errno
!= ERANGE
)
644 ec
.assign(errno
, std::generic_category());
650 ec
= std::make_error_code(std::errc::not_enough_memory
);
655 #else // _GLIBCXX_HAVE_UNISTD_H
656 ec
= std::make_error_code(std::errc::not_supported
);
662 fs::current_path(const path
& p
)
667 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot set current path", ec
));
671 fs::current_path(const path
& p
, error_code
& ec
) noexcept
673 #ifdef _GLIBCXX_HAVE_UNISTD_H
674 if (::chdir(p
.c_str()))
675 ec
.assign(errno
, std::generic_category());
679 ec
= std::make_error_code(std::errc::not_supported
);
684 fs::equivalent(const path
& p1
, const path
& p2
)
687 auto result
= equivalent(p1
, p2
, ec
);
689 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot check file equivalence",
695 fs::equivalent(const path
& p1
, const path
& p2
, error_code
& ec
) noexcept
697 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
701 if (::stat(p1
.c_str(), &st1
) == 0)
702 s1
= make_file_status(st1
);
703 else if (is_not_found_errno(errno
))
704 s1
.type(file_type::not_found
);
708 if (::stat(p2
.c_str(), &st2
) == 0)
709 s2
= make_file_status(st2
);
710 else if (is_not_found_errno(errno
))
711 s2
.type(file_type::not_found
);
715 if (exists(s1
) && exists(s2
))
717 if (is_other(s1
) && is_other(s2
))
719 ec
= std::make_error_code(std::errc::not_supported
);
723 if (is_other(s1
) || is_other(s2
))
725 return st1
.st_dev
== st2
.st_dev
&& st1
.st_ino
== st2
.st_ino
;
727 else if (!exists(s1
) && !exists(s2
))
728 ec
= std::make_error_code(std::errc::no_such_file_or_directory
);
730 ec
.assign(err
, std::generic_category());
735 ec
= std::make_error_code(std::errc::not_supported
);
741 fs::file_size(const path
& p
)
744 auto sz
= file_size(p
, ec
);
746 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot get file size", p
, ec
));
752 template<typename Accessor
, typename T
>
754 do_stat(const fs::path
& p
, std::error_code
& ec
, Accessor f
, T deflt
)
756 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
758 if (::stat(p
.c_str(), &st
))
760 ec
.assign(errno
, std::generic_category());
766 ec
= std::make_error_code(std::errc::not_supported
);
773 fs::file_size(const path
& p
, error_code
& ec
) noexcept
777 S(const stat_type
& st
) : type(make_file_type(st
)), size(st
.st_size
) { }
778 S() : type(file_type::not_found
) { }
782 auto s
= do_stat(p
, ec
, [](const auto& st
) { return S
{st
}; }, S
{});
783 if (s
.type
== file_type::regular
)
787 if (s
.type
== file_type::directory
)
788 ec
= std::make_error_code(std::errc::is_a_directory
);
790 ec
= std::make_error_code(std::errc::not_supported
);
796 fs::hard_link_count(const path
& p
)
799 auto count
= hard_link_count(p
, ec
);
801 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot get link count", p
, ec
));
806 fs::hard_link_count(const path
& p
, error_code
& ec
) noexcept
808 return do_stat(p
, ec
, std::mem_fn(&stat::st_nlink
),
809 static_cast<uintmax_t>(-1));
813 fs::is_empty(const path
& p
)
816 bool e
= is_empty(p
, ec
);
818 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot check if file is empty",
824 fs::is_empty(const path
& p
, error_code
& ec
) noexcept
826 auto s
= status(p
, ec
);
829 bool empty
= fs::is_directory(s
)
830 ? fs::directory_iterator(p
, ec
) == fs::directory_iterator()
831 : fs::file_size(p
, ec
) == 0;
832 return ec
? false : empty
;
836 fs::last_write_time(const path
& p
)
839 auto t
= last_write_time(p
, ec
);
841 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot get file time", p
, ec
));
846 fs::last_write_time(const path
& p
, error_code
& ec
) noexcept
848 return do_stat(p
, ec
, [&ec
](const auto& st
) { return file_time(st
, ec
); },
849 file_time_type::min());
853 fs::last_write_time(const path
& p
, file_time_type new_time
)
856 last_write_time(p
, new_time
, ec
);
858 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot set file time", p
, ec
));
862 fs::last_write_time(const path
& p
__attribute__((__unused__
)),
863 file_time_type new_time
, error_code
& ec
) noexcept
865 auto d
= new_time
.time_since_epoch();
866 auto s
= chrono::duration_cast
<chrono::seconds
>(d
);
867 #if _GLIBCXX_USE_UTIMENSAT
868 auto ns
= chrono::duration_cast
<chrono::nanoseconds
>(d
- s
);
869 if (ns
< ns
.zero()) // tv_nsec must be non-negative and less than 10e9.
872 ns
+= chrono::seconds(1);
874 struct ::timespec ts
[2];
876 ts
[0].tv_nsec
= UTIME_OMIT
;
877 ts
[1].tv_sec
= static_cast<std::time_t>(s
.count());
878 ts
[1].tv_nsec
= static_cast<long>(ns
.count());
879 if (::utimensat(AT_FDCWD
, p
.c_str(), ts
, 0))
880 ec
.assign(errno
, std::generic_category());
883 #elif _GLIBCXX_HAVE_UTIME_H
885 times
.modtime
= s
.count();
886 times
.actime
= do_stat(p
, ec
, [](const auto& st
) { return st
.st_atime
; },
888 if (::utime(p
.c_str(), ×
))
889 ec
.assign(errno
, std::generic_category());
893 ec
= std::make_error_code(std::errc::not_supported
);
898 fs::permissions(const path
& p
, perms prms
)
901 permissions(p
, prms
, ec
);
903 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot set permissions", p
, ec
));
907 fs::permissions(const path
& p
, perms prms
, error_code
& ec
) noexcept
909 const bool add
= is_set(prms
, perms::add_perms
);
910 const bool remove
= is_set(prms
, perms::remove_perms
);
911 const bool nofollow
= is_set(prms
, perms::symlink_nofollow
);
914 ec
= std::make_error_code(std::errc::invalid_argument
);
921 if (add
|| remove
|| nofollow
)
923 st
= nofollow
? symlink_status(p
, ec
) : status(p
, ec
);
926 auto curr
= st
.permissions();
934 #if _GLIBCXX_USE_FCHMODAT
935 const int flag
= (nofollow
&& is_symlink(st
)) ? AT_SYMLINK_NOFOLLOW
: 0;
936 if (::fchmodat(AT_FDCWD
, p
.c_str(), static_cast<mode_t
>(prms
), flag
))
939 if (nofollow
&& is_symlink(st
))
940 ec
= std::make_error_code(std::errc::operation_not_supported
);
941 else if (::chmod(p
.c_str(), static_cast<mode_t
>(prms
)))
946 ec
.assign(err
, std::generic_category());
952 fs::read_symlink(const path
& p
)
955 path tgt
= read_symlink(p
, ec
);
957 _GLIBCXX_THROW_OR_ABORT(filesystem_error("read_symlink", p
, ec
));
961 fs::path
fs::read_symlink(const path
& p
, error_code
& ec
)
964 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
966 if (::lstat(p
.c_str(), &st
))
968 ec
.assign(errno
, std::generic_category());
971 std::string
buf(st
.st_size
? st
.st_size
+ 1 : 128, '\0');
974 ssize_t len
= ::readlink(p
.c_str(), buf
.data(), buf
.size());
977 ec
.assign(errno
, std::generic_category());
980 else if (len
== (ssize_t
)buf
.size())
982 if (buf
.size() > 4096)
984 ec
.assign(ENAMETOOLONG
, std::generic_category());
987 buf
.resize(buf
.size() * 2);
999 ec
= std::make_error_code(std::errc::not_supported
);
1006 fs::remove(const path
& p
)
1009 bool result
= fs::remove(p
, ec
);
1011 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot remove", p
, ec
));
1016 fs::remove(const path
& p
, error_code
& ec
) noexcept
1018 if (::remove(p
.c_str()) == 0)
1023 else if (errno
== ENOENT
)
1026 ec
.assign(errno
, std::generic_category());
1032 fs::remove_all(const path
& p
)
1035 const auto result
= remove_all(p
, ec
);
1037 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot remove all", p
, ec
));
1042 fs::remove_all(const path
& p
, error_code
& ec
) noexcept
1044 const auto s
= symlink_status(p
, ec
);
1045 if (!status_known(s
))
1049 if (s
.type() == file_type::not_found
)
1052 uintmax_t count
= 0;
1053 if (s
.type() == file_type::directory
)
1055 for (directory_iterator
d(p
, ec
), end
; !ec
&& d
!= end
; d
.increment(ec
))
1056 count
+= fs::remove_all(d
->path(), ec
);
1057 if (ec
.value() == ENOENT
)
1063 if (fs::remove(p
, ec
))
1065 return ec
? -1 : count
;
1069 fs::rename(const path
& from
, const path
& to
)
1072 rename(from
, to
, ec
);
1074 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot rename", from
, to
, ec
));
1078 fs::rename(const path
& from
, const path
& to
, error_code
& ec
) noexcept
1080 if (::rename(from
.c_str(), to
.c_str()))
1081 ec
.assign(errno
, std::generic_category());
1087 fs::resize_file(const path
& p
, uintmax_t size
)
1090 resize_file(p
, size
, ec
);
1092 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot resize file", p
, ec
));
1096 fs::resize_file(const path
& p
, uintmax_t size
, error_code
& ec
) noexcept
1098 #ifdef _GLIBCXX_HAVE_UNISTD_H
1099 if (size
> static_cast<uintmax_t>(std::numeric_limits
<off_t
>::max()))
1100 ec
.assign(EINVAL
, std::generic_category());
1101 else if (::truncate(p
.c_str(), size
))
1102 ec
.assign(errno
, std::generic_category());
1106 ec
= std::make_error_code(std::errc::not_supported
);
1112 fs::space(const path
& p
)
1115 space_info s
= space(p
, ec
);
1117 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot get free space", p
, ec
));
1122 fs::space(const path
& p
, error_code
& ec
) noexcept
1125 static_cast<uintmax_t>(-1),
1126 static_cast<uintmax_t>(-1),
1127 static_cast<uintmax_t>(-1)
1129 #ifdef _GLIBCXX_HAVE_SYS_STATVFS_H
1131 if (::statvfs(p
.c_str(), &f
))
1132 ec
.assign(errno
, std::generic_category());
1136 f
.f_blocks
* f
.f_frsize
,
1137 f
.f_bfree
* f
.f_frsize
,
1138 f
.f_bavail
* f
.f_frsize
1143 ec
= std::make_error_code(std::errc::not_supported
);
1148 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
1150 fs::status(const fs::path
& p
, error_code
& ec
) noexcept
1154 if (::stat(p
.c_str(), &st
))
1157 ec
.assign(err
, std::generic_category());
1158 if (is_not_found_errno(err
))
1159 status
.type(file_type::not_found
);
1161 else if (err
== EOVERFLOW
)
1162 status
.type(file_type::unknown
);
1167 status
= make_file_status(st
);
1174 fs::symlink_status(const fs::path
& p
, std::error_code
& ec
) noexcept
1178 if (::lstat(p
.c_str(), &st
))
1181 ec
.assign(err
, std::generic_category());
1182 if (is_not_found_errno(err
))
1183 status
.type(file_type::not_found
);
1187 status
= make_file_status(st
);
1195 fs::status(const fs::path
& p
)
1198 auto result
= status(p
, ec
);
1199 if (result
.type() == file_type::none
)
1200 _GLIBCXX_THROW_OR_ABORT(filesystem_error("status", p
, ec
));
1205 fs::symlink_status(const fs::path
& p
)
1208 auto result
= symlink_status(p
, ec
);
1209 if (result
.type() == file_type::none
)
1210 _GLIBCXX_THROW_OR_ABORT(filesystem_error("symlink_status", p
, ec
));
1215 fs::system_complete(const path
& p
)
1218 path comp
= system_complete(p
, ec
);
1220 _GLIBCXX_THROW_OR_ABORT(filesystem_error("system_complete", p
, ec
));
1225 fs::system_complete(const path
& p
, error_code
& ec
)
1227 path base
= current_path(ec
);
1228 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1229 if (p
.is_absolute() || !p
.has_root_name()
1230 || p
.root_name() == base
.root_name())
1231 return absolute(p
, base
);
1233 ec
= std::make_error_code(std::errc::not_supported
);
1238 return absolute(p
, base
);
1242 fs::path
fs::temp_directory_path()
1245 path tmp
= temp_directory_path(ec
);
1247 _GLIBCXX_THROW_OR_ABORT(filesystem_error("temp_directory_path", ec
));
1251 fs::path
fs::temp_directory_path(error_code
& ec
)
1253 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1254 ec
= std::make_error_code(std::errc::not_supported
);
1257 const char* tmpdir
= nullptr;
1258 const char* env
[] = { "TMPDIR", "TMP", "TEMP", "TEMPDIR", nullptr };
1259 for (auto e
= env
; tmpdir
== nullptr && *e
!= nullptr; ++e
)
1260 tmpdir
= ::getenv(*e
);
1261 path p
= tmpdir
? tmpdir
: "/tmp";
1262 auto st
= status(p
, ec
);
1265 if (is_directory(st
))
1271 ec
= std::make_error_code(std::errc::not_a_directory
);