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
))
467 ec
.assign(err
, std::generic_category());
477 ec
= std::make_error_code(std::errc::not_supported
);
484 fs::create_directory(const path
& p
)
487 bool result
= create_directory(p
, ec
);
489 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot create directory", p
,
495 fs::create_directory(const path
& p
, error_code
& ec
) noexcept
497 return create_dir(p
, perms::all
, ec
);
502 fs::create_directory(const path
& p
, const path
& attributes
)
505 bool result
= create_directory(p
, attributes
, ec
);
507 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot create directory", p
,
513 fs::create_directory(const path
& p
, const path
& attributes
,
514 error_code
& ec
) noexcept
516 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
518 if (::stat(attributes
.c_str(), &st
))
520 ec
.assign(errno
, std::generic_category());
523 return create_dir(p
, static_cast<perms
>(st
.st_mode
), ec
);
525 ec
= std::make_error_code(std::errc::not_supported
);
532 fs::create_directory_symlink(const path
& to
, const path
& new_symlink
)
535 create_directory_symlink(to
, new_symlink
, ec
);
537 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot create directory symlink",
538 to
, new_symlink
, ec
));
542 fs::create_directory_symlink(const path
& to
, const path
& new_symlink
,
543 error_code
& ec
) noexcept
545 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
546 ec
= std::make_error_code(std::errc::not_supported
);
548 create_symlink(to
, new_symlink
, ec
);
554 fs::create_hard_link(const path
& to
, const path
& new_hard_link
)
557 create_hard_link(to
, new_hard_link
, ec
);
559 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot create hard link",
560 to
, new_hard_link
, ec
));
564 fs::create_hard_link(const path
& to
, const path
& new_hard_link
,
565 error_code
& ec
) noexcept
567 #ifdef _GLIBCXX_HAVE_UNISTD_H
568 if (::link(to
.c_str(), new_hard_link
.c_str()))
569 ec
.assign(errno
, std::generic_category());
573 ec
= std::make_error_code(std::errc::not_supported
);
578 fs::create_symlink(const path
& to
, const path
& new_symlink
)
581 create_symlink(to
, new_symlink
, ec
);
583 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot create symlink",
584 to
, new_symlink
, ec
));
588 fs::create_symlink(const path
& to
, const path
& new_symlink
,
589 error_code
& ec
) noexcept
591 #ifdef _GLIBCXX_HAVE_UNISTD_H
592 if (::symlink(to
.c_str(), new_symlink
.c_str()))
593 ec
.assign(errno
, std::generic_category());
597 ec
= std::make_error_code(std::errc::not_supported
);
606 path p
= current_path(ec
);
608 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot get current path", ec
));
613 fs::current_path(error_code
& ec
)
616 #ifdef _GLIBCXX_HAVE_UNISTD_H
618 if (char_ptr cwd
= char_ptr
{::getcwd(nullptr, 0)})
624 ec
.assign(errno
, std::generic_category());
626 long path_max
= pathconf(".", _PC_PATH_MAX
);
630 else if (path_max
> 10240)
634 for (char_ptr buf
; p
.empty(); size
*= 2)
636 buf
.reset((char*)malloc(size
));
639 if (getcwd(buf
.get(), size
))
644 else if (errno
!= ERANGE
)
646 ec
.assign(errno
, std::generic_category());
652 ec
= std::make_error_code(std::errc::not_enough_memory
);
657 #else // _GLIBCXX_HAVE_UNISTD_H
658 ec
= std::make_error_code(std::errc::not_supported
);
664 fs::current_path(const path
& p
)
669 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot set current path", ec
));
673 fs::current_path(const path
& p
, error_code
& ec
) noexcept
675 #ifdef _GLIBCXX_HAVE_UNISTD_H
676 if (::chdir(p
.c_str()))
677 ec
.assign(errno
, std::generic_category());
681 ec
= std::make_error_code(std::errc::not_supported
);
686 fs::equivalent(const path
& p1
, const path
& p2
)
689 auto result
= equivalent(p1
, p2
, ec
);
691 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot check file equivalence",
697 fs::equivalent(const path
& p1
, const path
& p2
, error_code
& ec
) noexcept
699 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
703 if (::stat(p1
.c_str(), &st1
) == 0)
704 s1
= make_file_status(st1
);
705 else if (is_not_found_errno(errno
))
706 s1
.type(file_type::not_found
);
710 if (::stat(p2
.c_str(), &st2
) == 0)
711 s2
= make_file_status(st2
);
712 else if (is_not_found_errno(errno
))
713 s2
.type(file_type::not_found
);
717 if (exists(s1
) && exists(s2
))
719 if (is_other(s1
) && is_other(s2
))
721 ec
= std::make_error_code(std::errc::not_supported
);
725 if (is_other(s1
) || is_other(s2
))
727 return st1
.st_dev
== st2
.st_dev
&& st1
.st_ino
== st2
.st_ino
;
729 else if (!exists(s1
) && !exists(s2
))
730 ec
= std::make_error_code(std::errc::no_such_file_or_directory
);
732 ec
.assign(err
, std::generic_category());
737 ec
= std::make_error_code(std::errc::not_supported
);
743 fs::file_size(const path
& p
)
746 auto sz
= file_size(p
, ec
);
748 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot get file size", p
, ec
));
754 template<typename Accessor
, typename T
>
756 do_stat(const fs::path
& p
, std::error_code
& ec
, Accessor f
, T deflt
)
758 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
760 if (::stat(p
.c_str(), &st
))
762 ec
.assign(errno
, std::generic_category());
768 ec
= std::make_error_code(std::errc::not_supported
);
775 fs::file_size(const path
& p
, error_code
& ec
) noexcept
779 S(const stat_type
& st
) : type(make_file_type(st
)), size(st
.st_size
) { }
780 S() : type(file_type::not_found
) { }
784 auto s
= do_stat(p
, ec
, [](const auto& st
) { return S
{st
}; }, S
{});
785 if (s
.type
== file_type::regular
)
789 if (s
.type
== file_type::directory
)
790 ec
= std::make_error_code(std::errc::is_a_directory
);
792 ec
= std::make_error_code(std::errc::not_supported
);
798 fs::hard_link_count(const path
& p
)
801 auto count
= hard_link_count(p
, ec
);
803 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot get link count", p
, ec
));
808 fs::hard_link_count(const path
& p
, error_code
& ec
) noexcept
810 return do_stat(p
, ec
, std::mem_fn(&stat::st_nlink
),
811 static_cast<uintmax_t>(-1));
815 fs::is_empty(const path
& p
)
818 bool e
= is_empty(p
, ec
);
820 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot check if file is empty",
826 fs::is_empty(const path
& p
, error_code
& ec
) noexcept
828 auto s
= status(p
, ec
);
831 bool empty
= fs::is_directory(s
)
832 ? fs::directory_iterator(p
, ec
) == fs::directory_iterator()
833 : fs::file_size(p
, ec
) == 0;
834 return ec
? false : empty
;
838 fs::last_write_time(const path
& p
)
841 auto t
= last_write_time(p
, ec
);
843 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot get file time", p
, ec
));
848 fs::last_write_time(const path
& p
, error_code
& ec
) noexcept
850 return do_stat(p
, ec
, [&ec
](const auto& st
) { return file_time(st
, ec
); },
851 file_time_type::min());
855 fs::last_write_time(const path
& p
, file_time_type new_time
)
858 last_write_time(p
, new_time
, ec
);
860 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot set file time", p
, ec
));
864 fs::last_write_time(const path
& p
__attribute__((__unused__
)),
865 file_time_type new_time
, error_code
& ec
) noexcept
867 auto d
= new_time
.time_since_epoch();
868 auto s
= chrono::duration_cast
<chrono::seconds
>(d
);
869 #if _GLIBCXX_USE_UTIMENSAT
870 auto ns
= chrono::duration_cast
<chrono::nanoseconds
>(d
- s
);
871 if (ns
< ns
.zero()) // tv_nsec must be non-negative and less than 10e9.
874 ns
+= chrono::seconds(1);
876 struct ::timespec ts
[2];
878 ts
[0].tv_nsec
= UTIME_OMIT
;
879 ts
[1].tv_sec
= static_cast<std::time_t>(s
.count());
880 ts
[1].tv_nsec
= static_cast<long>(ns
.count());
881 if (::utimensat(AT_FDCWD
, p
.c_str(), ts
, 0))
882 ec
.assign(errno
, std::generic_category());
885 #elif _GLIBCXX_HAVE_UTIME_H
887 times
.modtime
= s
.count();
888 times
.actime
= do_stat(p
, ec
, [](const auto& st
) { return st
.st_atime
; },
890 if (::utime(p
.c_str(), ×
))
891 ec
.assign(errno
, std::generic_category());
895 ec
= std::make_error_code(std::errc::not_supported
);
900 fs::permissions(const path
& p
, perms prms
)
903 permissions(p
, prms
, ec
);
905 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot set permissions", p
, ec
));
909 fs::permissions(const path
& p
, perms prms
, error_code
& ec
) noexcept
911 const bool add
= is_set(prms
, perms::add_perms
);
912 const bool remove
= is_set(prms
, perms::remove_perms
);
913 const bool nofollow
= is_set(prms
, perms::symlink_nofollow
);
916 ec
= std::make_error_code(std::errc::invalid_argument
);
923 if (add
|| remove
|| nofollow
)
925 st
= nofollow
? symlink_status(p
, ec
) : status(p
, ec
);
928 auto curr
= st
.permissions();
936 #if _GLIBCXX_USE_FCHMODAT
937 const int flag
= (nofollow
&& is_symlink(st
)) ? AT_SYMLINK_NOFOLLOW
: 0;
938 if (::fchmodat(AT_FDCWD
, p
.c_str(), static_cast<mode_t
>(prms
), flag
))
941 if (nofollow
&& is_symlink(st
))
942 ec
= std::make_error_code(std::errc::operation_not_supported
);
943 else if (::chmod(p
.c_str(), static_cast<mode_t
>(prms
)))
948 ec
.assign(err
, std::generic_category());
954 fs::read_symlink(const path
& p
)
957 path tgt
= read_symlink(p
, ec
);
959 _GLIBCXX_THROW_OR_ABORT(filesystem_error("read_symlink", p
, ec
));
963 fs::path
fs::read_symlink(const path
& p
, error_code
& ec
)
966 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
968 if (::lstat(p
.c_str(), &st
))
970 ec
.assign(errno
, std::generic_category());
973 std::string
buf(st
.st_size
? st
.st_size
+ 1 : 128, '\0');
976 ssize_t len
= ::readlink(p
.c_str(), buf
.data(), buf
.size());
979 ec
.assign(errno
, std::generic_category());
982 else if (len
== (ssize_t
)buf
.size())
984 if (buf
.size() > 4096)
986 ec
.assign(ENAMETOOLONG
, std::generic_category());
989 buf
.resize(buf
.size() * 2);
1001 ec
= std::make_error_code(std::errc::not_supported
);
1008 fs::remove(const path
& p
)
1011 bool result
= fs::remove(p
, ec
);
1013 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot remove", p
, ec
));
1018 fs::remove(const path
& p
, error_code
& ec
) noexcept
1020 if (::remove(p
.c_str()) == 0)
1025 else if (errno
== ENOENT
)
1028 ec
.assign(errno
, std::generic_category());
1034 fs::remove_all(const path
& p
)
1037 const auto result
= remove_all(p
, ec
);
1039 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot remove all", p
, ec
));
1044 fs::remove_all(const path
& p
, error_code
& ec
) noexcept
1046 const auto s
= symlink_status(p
, ec
);
1047 if (!status_known(s
))
1051 if (s
.type() == file_type::not_found
)
1054 uintmax_t count
= 0;
1055 if (s
.type() == file_type::directory
)
1057 for (directory_iterator
d(p
, ec
), end
; !ec
&& d
!= end
; d
.increment(ec
))
1058 count
+= fs::remove_all(d
->path(), ec
);
1059 if (ec
.value() == ENOENT
)
1065 if (fs::remove(p
, ec
))
1067 return ec
? -1 : count
;
1071 fs::rename(const path
& from
, const path
& to
)
1074 rename(from
, to
, ec
);
1076 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot rename", from
, to
, ec
));
1080 fs::rename(const path
& from
, const path
& to
, error_code
& ec
) noexcept
1082 if (::rename(from
.c_str(), to
.c_str()))
1083 ec
.assign(errno
, std::generic_category());
1089 fs::resize_file(const path
& p
, uintmax_t size
)
1092 resize_file(p
, size
, ec
);
1094 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot resize file", p
, ec
));
1098 fs::resize_file(const path
& p
, uintmax_t size
, error_code
& ec
) noexcept
1100 #ifdef _GLIBCXX_HAVE_UNISTD_H
1101 if (size
> static_cast<uintmax_t>(std::numeric_limits
<off_t
>::max()))
1102 ec
.assign(EINVAL
, std::generic_category());
1103 else if (::truncate(p
.c_str(), size
))
1104 ec
.assign(errno
, std::generic_category());
1108 ec
= std::make_error_code(std::errc::not_supported
);
1114 fs::space(const path
& p
)
1117 space_info s
= space(p
, ec
);
1119 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot get free space", p
, ec
));
1124 fs::space(const path
& p
, error_code
& ec
) noexcept
1127 static_cast<uintmax_t>(-1),
1128 static_cast<uintmax_t>(-1),
1129 static_cast<uintmax_t>(-1)
1131 #ifdef _GLIBCXX_HAVE_SYS_STATVFS_H
1133 if (::statvfs(p
.c_str(), &f
))
1134 ec
.assign(errno
, std::generic_category());
1138 f
.f_blocks
* f
.f_frsize
,
1139 f
.f_bfree
* f
.f_frsize
,
1140 f
.f_bavail
* f
.f_frsize
1145 ec
= std::make_error_code(std::errc::not_supported
);
1150 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
1152 fs::status(const fs::path
& p
, error_code
& ec
) noexcept
1156 if (::stat(p
.c_str(), &st
))
1159 ec
.assign(err
, std::generic_category());
1160 if (is_not_found_errno(err
))
1161 status
.type(file_type::not_found
);
1163 else if (err
== EOVERFLOW
)
1164 status
.type(file_type::unknown
);
1169 status
= make_file_status(st
);
1176 fs::symlink_status(const fs::path
& p
, std::error_code
& ec
) noexcept
1180 if (::lstat(p
.c_str(), &st
))
1183 ec
.assign(err
, std::generic_category());
1184 if (is_not_found_errno(err
))
1185 status
.type(file_type::not_found
);
1189 status
= make_file_status(st
);
1197 fs::status(const fs::path
& p
)
1200 auto result
= status(p
, ec
);
1201 if (result
.type() == file_type::none
)
1202 _GLIBCXX_THROW_OR_ABORT(filesystem_error("status", p
, ec
));
1207 fs::symlink_status(const fs::path
& p
)
1210 auto result
= symlink_status(p
, ec
);
1211 if (result
.type() == file_type::none
)
1212 _GLIBCXX_THROW_OR_ABORT(filesystem_error("symlink_status", p
, ec
));
1217 fs::system_complete(const path
& p
)
1220 path comp
= system_complete(p
, ec
);
1222 _GLIBCXX_THROW_OR_ABORT(filesystem_error("system_complete", p
, ec
));
1227 fs::system_complete(const path
& p
, error_code
& ec
)
1229 path base
= current_path(ec
);
1230 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1231 if (p
.is_absolute() || !p
.has_root_name()
1232 || p
.root_name() == base
.root_name())
1233 return absolute(p
, base
);
1235 ec
= std::make_error_code(std::errc::not_supported
);
1240 return absolute(p
, base
);
1244 fs::path
fs::temp_directory_path()
1247 path tmp
= temp_directory_path(ec
);
1249 _GLIBCXX_THROW_OR_ABORT(filesystem_error("temp_directory_path", ec
));
1253 fs::path
fs::temp_directory_path(error_code
& ec
)
1255 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1256 ec
= std::make_error_code(std::errc::not_supported
);
1259 const char* tmpdir
= nullptr;
1260 const char* env
[] = { "TMPDIR", "TMP", "TEMP", "TEMPDIR", nullptr };
1261 for (auto e
= env
; tmpdir
== nullptr && *e
!= nullptr; ++e
)
1262 tmpdir
= ::getenv(*e
);
1263 path p
= tmpdir
? tmpdir
: "/tmp";
1264 auto st
= status(p
, ec
);
1267 if (is_directory(st
))
1273 ec
= std::make_error_code(std::errc::not_a_directory
);