1 // Filesystem TS operations -*- C++ -*-
3 // Copyright (C) 2014-2022 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 #ifndef _GLIBCXX_USE_CXX11_ABI
26 # define _GLIBCXX_USE_CXX11_ABI 1
27 # define NEED_DO_COPY_FILE
28 # define NEED_DO_SPACE
31 // Cygwin needs this for secure_getenv
32 # define _GNU_SOURCE 1
35 #include <bits/largefile-config.h>
36 #include <experimental/filesystem>
40 #include <ext/stdio_filebuf.h>
44 #include <limits.h> // PATH_MAX
45 #ifdef _GLIBCXX_HAVE_FCNTL_H
46 # include <fcntl.h> // AT_FDCWD, AT_SYMLINK_NOFOLLOW
48 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
49 # include <sys/stat.h> // stat, utimensat, fchmodat
51 #ifdef _GLIBCXX_HAVE_SYS_STATVFS_H
52 # include <sys/statvfs.h> // statvfs
54 #if !_GLIBCXX_USE_UTIMENSAT && _GLIBCXX_HAVE_UTIME_H
55 # include <utime.h> // utime
57 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
58 # define WIN32_LEAN_AND_MEAN
62 #define _GLIBCXX_BEGIN_NAMESPACE_FILESYSTEM \
63 namespace experimental { namespace filesystem {
64 #define _GLIBCXX_END_NAMESPACE_FILESYSTEM } }
65 #include "ops-common.h"
67 #include <filesystem> // std::filesystem::remove_all
69 namespace fs
= std::experimental::filesystem
;
70 namespace posix
= std::filesystem::__gnu_posix
;
73 fs::absolute(const path
& p
, const path
& base
)
75 const bool has_root_dir
= p
.has_root_directory();
76 const bool has_root_name
= p
.has_root_name();
78 if (has_root_dir
&& has_root_name
)
82 abs
= base
.is_absolute() ? base
: absolute(base
);
84 abs
= abs
.root_name() / p
;
85 else if (has_root_name
)
86 abs
= p
.root_name() / abs
.root_directory() / abs
.relative_path()
96 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
97 inline bool is_dot(wchar_t c
) { return c
== L
'.'; }
99 inline bool is_dot(char c
) { return c
== '.'; }
102 inline bool is_dot(const fs::path
& path
)
104 const auto& filename
= path
.native();
105 return filename
.size() == 1 && is_dot(filename
[0]);
108 inline bool is_dotdot(const fs::path
& path
)
110 const auto& filename
= path
.native();
111 return filename
.size() == 2 && is_dot(filename
[0]) && is_dot(filename
[1]);
114 struct free_as_in_malloc
116 void operator()(void* p
) const { ::free(p
); }
119 using char_ptr
= std::unique_ptr
<fs::path::value_type
[], free_as_in_malloc
>;
123 fs::canonical(const path
& p
, const path
& base
, error_code
& ec
)
125 const path pa
= absolute(p
, base
);
128 #ifdef _GLIBCXX_USE_REALPATH
129 char_ptr buf
{ nullptr };
130 # if _XOPEN_VERSION < 700
131 // Not safe to call realpath(path, NULL)
132 using char_type
= fs::path::value_type
;
133 buf
.reset( (char_type
*)::malloc(PATH_MAX
* sizeof(char_type
)) );
135 if (char* rp
= ::realpath(pa
.c_str(), buf
.get()))
143 if (errno
!= ENAMETOOLONG
)
145 ec
.assign(errno
, std::generic_category());
153 ec
= make_error_code(std::errc::no_such_file_or_directory
);
156 // else: we know there are (currently) no unresolvable symlink loops
158 result
= pa
.root_path();
161 for (auto& f
: pa
.relative_path())
164 int max_allowed_symlinks
= 40;
166 while (!cmpts
.empty() && !ec
)
168 path f
= std::move(cmpts
.front());
173 if (!is_directory(result
, ec
) && !ec
)
174 ec
.assign(ENOTDIR
, std::generic_category());
176 else if (is_dotdot(f
))
178 auto parent
= result
.parent_path();
180 result
= pa
.root_path();
188 if (is_symlink(result
, ec
))
190 path link
= read_symlink(result
, ec
);
193 if (--max_allowed_symlinks
== 0)
194 ec
.assign(ELOOP
, std::generic_category());
197 if (link
.is_absolute())
199 result
= link
.root_path();
200 link
= link
.relative_path();
203 result
.remove_filename();
205 cmpts
.insert(cmpts
.begin(), link
.begin(), link
.end());
212 if (ec
|| !exists(result
, ec
))
219 fs::canonical(const path
& p
, error_code
& ec
)
221 path cur
= current_path(ec
);
224 return canonical(p
, cur
, ec
);
228 fs::canonical(const path
& p
, const path
& base
)
231 path can
= canonical(p
, base
, ec
);
233 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot canonicalize", p
, base
,
239 fs::copy(const path
& from
, const path
& to
, copy_options options
)
242 copy(from
, to
, options
, ec
);
244 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot copy", from
, to
, ec
));
249 using std::filesystem::is_set
;
251 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
252 using posix::stat_type
;
254 using std::filesystem::is_not_found_errno
;
255 using std::filesystem::file_time
;
256 #endif // _GLIBCXX_HAVE_SYS_STAT_H
261 fs::copy(const path
& from
, const path
& to
, copy_options options
,
262 error_code
& ec
) noexcept
264 const bool skip_symlinks
= is_set(options
, copy_options::skip_symlinks
);
265 const bool create_symlinks
= is_set(options
, copy_options::create_symlinks
);
266 const bool copy_symlinks
= is_set(options
, copy_options::copy_symlinks
);
267 const bool use_lstat
= create_symlinks
|| skip_symlinks
;
270 stat_type from_st
, to_st
;
271 // _GLIBCXX_RESOLVE_LIB_DEFECTS
272 // 2681. filesystem::copy() cannot copy symlinks
273 if (use_lstat
|| copy_symlinks
274 ? posix::lstat(from
.c_str(), &from_st
)
275 : posix::stat(from
.c_str(), &from_st
))
277 ec
.assign(errno
, std::generic_category());
281 ? posix::lstat(to
.c_str(), &to_st
)
282 : posix::stat(to
.c_str(), &to_st
))
284 if (!is_not_found_errno(errno
))
286 ec
.assign(errno
, std::generic_category());
289 t
= file_status
{file_type::not_found
};
292 t
= make_file_status(to_st
);
293 f
= make_file_status(from_st
);
295 if (exists(t
) && !is_other(t
) && !is_other(f
)
296 && to_st
.st_dev
== from_st
.st_dev
&& to_st
.st_ino
== from_st
.st_ino
)
298 ec
= std::make_error_code(std::errc::file_exists
);
301 if (is_other(f
) || is_other(t
))
303 ec
= std::make_error_code(std::errc::invalid_argument
);
306 if (is_directory(f
) && is_regular_file(t
))
308 ec
= std::make_error_code(std::errc::is_a_directory
);
316 else if (!exists(t
) && copy_symlinks
)
317 copy_symlink(from
, to
, ec
);
319 // Not clear what should be done here.
320 // "Otherwise report an error as specified in Error reporting (7)."
321 ec
= std::make_error_code(std::errc::invalid_argument
);
323 else if (is_regular_file(f
))
325 if (is_set(options
, copy_options::directories_only
))
327 else if (create_symlinks
)
328 create_symlink(from
, to
, ec
);
329 else if (is_set(options
, copy_options::create_hard_links
))
330 create_hard_link(from
, to
, ec
);
331 else if (is_directory(t
))
332 do_copy_file(from
.c_str(), (to
/ from
.filename()).c_str(),
333 copy_file_options(options
), &from_st
, nullptr, ec
);
336 auto ptr
= exists(t
) ? &to_st
: &from_st
;
337 do_copy_file(from
.c_str(), to
.c_str(), copy_file_options(options
),
341 // _GLIBCXX_RESOLVE_LIB_DEFECTS
342 // 2682. filesystem::copy() won't create a symlink to a directory
343 else if (is_directory(f
) && create_symlinks
)
344 ec
= std::make_error_code(errc::is_a_directory
);
345 else if (is_directory(f
) && (is_set(options
, copy_options::recursive
)
346 || options
== copy_options::none
))
349 if (!create_directory(to
, from
, ec
))
351 // set an unused bit in options to disable further recursion
352 if (!is_set(options
, copy_options::recursive
))
353 options
|= static_cast<copy_options
>(4096);
354 for (const directory_entry
& x
: directory_iterator(from
, ec
))
356 copy(x
.path(), to
/x
.path().filename(), options
, ec
);
361 // _GLIBCXX_RESOLVE_LIB_DEFECTS
362 // 2683. filesystem::copy() says "no effects"
368 fs::copy_file(const path
& from
, const path
& to
, copy_options option
)
371 bool result
= copy_file(from
, to
, option
, ec
);
373 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot copy file", from
, to
,
379 fs::copy_file(const path
& from
, const path
& to
, copy_options options
,
382 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
383 return do_copy_file(from
.c_str(), to
.c_str(), copy_file_options(options
),
384 nullptr, nullptr, ec
);
386 ec
= std::make_error_code(std::errc::function_not_supported
);
393 fs::copy_symlink(const path
& existing_symlink
, const path
& new_symlink
)
396 copy_symlink(existing_symlink
, new_symlink
, ec
);
398 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot copy symlink",
399 existing_symlink
, new_symlink
, ec
));
403 fs::copy_symlink(const path
& existing_symlink
, const path
& new_symlink
,
404 error_code
& ec
) noexcept
406 auto p
= read_symlink(existing_symlink
, ec
);
409 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
412 create_directory_symlink(p
, new_symlink
, ec
);
416 create_symlink(p
, new_symlink
, ec
);
421 fs::create_directories(const path
& p
)
424 bool result
= create_directories(p
, ec
);
426 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot create directories", p
,
432 fs::create_directories(const path
& p
, error_code
& ec
)
436 ec
= std::make_error_code(errc::invalid_argument
);
440 file_status st
= status(p
, ec
);
441 if (is_directory(st
))
443 else if (ec
&& !status_known(st
))
448 ec
= std::make_error_code(std::errc::not_a_directory
);
452 std::stack
<path
> missing
;
455 while (!pp
.empty() && status(pp
, ec
).type() == file_type::not_found
)
458 const auto& filename
= pp
.filename();
459 if (!is_dot(filename
) && !is_dotdot(filename
))
461 missing
.push(std::move(pp
));
462 pp
= missing
.top().parent_path();
465 pp
= pp
.parent_path();
468 if (ec
|| missing
.empty())
474 const path
& top
= missing
.top();
475 created
= create_directory(top
, ec
);
480 while (!missing
.empty());
488 create_dir(const fs::path
& p
, fs::perms perm
, std::error_code
& ec
)
490 bool created
= false;
491 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
492 posix::mode_t mode
= static_cast<std::underlying_type_t
<fs::perms
>>(perm
);
493 if (posix::mkdir(p
.c_str(), mode
))
495 const int err
= errno
;
496 if (err
!= EEXIST
|| !is_directory(p
, ec
))
497 ec
.assign(err
, std::generic_category());
505 ec
= std::make_error_code(std::errc::function_not_supported
);
512 fs::create_directory(const path
& p
)
515 bool result
= create_directory(p
, ec
);
517 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot create directory", p
,
523 fs::create_directory(const path
& p
, error_code
& ec
) noexcept
525 return create_dir(p
, perms::all
, ec
);
530 fs::create_directory(const path
& p
, const path
& attributes
)
533 bool result
= create_directory(p
, attributes
, ec
);
535 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot create directory", p
,
541 fs::create_directory(const path
& p
, const path
& attributes
,
542 error_code
& ec
) noexcept
544 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
546 if (posix::stat(attributes
.c_str(), &st
))
548 ec
.assign(errno
, std::generic_category());
551 return create_dir(p
, static_cast<perms
>(st
.st_mode
), ec
);
553 ec
= std::make_error_code(std::errc::function_not_supported
);
560 fs::create_directory_symlink(const path
& to
, const path
& new_symlink
)
563 create_directory_symlink(to
, new_symlink
, ec
);
565 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot create directory symlink",
566 to
, new_symlink
, ec
));
570 fs::create_directory_symlink(const path
& to
, const path
& new_symlink
,
571 error_code
& ec
) noexcept
573 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
574 ec
= std::make_error_code(std::errc::function_not_supported
);
576 create_symlink(to
, new_symlink
, ec
);
582 fs::create_hard_link(const path
& to
, const path
& new_hard_link
)
585 create_hard_link(to
, new_hard_link
, ec
);
587 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot create hard link",
588 to
, new_hard_link
, ec
));
592 fs::create_hard_link(const path
& to
, const path
& new_hard_link
,
593 error_code
& ec
) noexcept
595 #ifdef _GLIBCXX_HAVE_LINK
596 if (::link(to
.c_str(), new_hard_link
.c_str()))
597 ec
.assign(errno
, std::generic_category());
600 #elif defined _GLIBCXX_FILESYSTEM_IS_WINDOWS
601 if (CreateHardLinkW(new_hard_link
.c_str(), to
.c_str(), NULL
))
604 ec
= __last_system_error();
606 ec
= std::make_error_code(std::errc::function_not_supported
);
611 fs::create_symlink(const path
& to
, const path
& new_symlink
)
614 create_symlink(to
, new_symlink
, ec
);
616 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot create symlink",
617 to
, new_symlink
, ec
));
621 fs::create_symlink(const path
& to
, const path
& new_symlink
,
622 error_code
& ec
) noexcept
624 #ifdef _GLIBCXX_HAVE_SYMLINK
625 if (::symlink(to
.c_str(), new_symlink
.c_str()))
626 ec
.assign(errno
, std::generic_category());
630 ec
= std::make_error_code(std::errc::function_not_supported
);
638 path p
= current_path(ec
);
640 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot get current path", ec
));
645 fs::current_path(error_code
& ec
)
648 #ifdef _GLIBCXX_HAVE_UNISTD_H
649 #if defined __GLIBC__ || defined _GLIBCXX_FILESYSTEM_IS_WINDOWS
650 if (char_ptr cwd
= char_ptr
{posix::getcwd(nullptr, 0)})
656 ec
.assign(errno
, std::generic_category());
659 long path_max
= pathconf(".", _PC_PATH_MAX
);
663 else if (path_max
> 10240)
667 #elif defined(PATH_MAX)
668 size_t size
= PATH_MAX
;
672 for (char_ptr buf
; p
.empty(); size
*= 2)
674 using char_type
= fs::path::value_type
;
675 buf
.reset((char_type
*)malloc(size
* sizeof(char_type
)));
678 if (getcwd(buf
.get(), size
))
683 else if (errno
!= ERANGE
)
685 ec
.assign(errno
, std::generic_category());
691 ec
= std::make_error_code(std::errc::not_enough_memory
);
696 #else // _GLIBCXX_HAVE_UNISTD_H
697 ec
= std::make_error_code(std::errc::function_not_supported
);
703 fs::current_path(const path
& p
)
708 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot set current path", ec
));
712 fs::current_path(const path
& p
, error_code
& ec
) noexcept
714 #ifdef _GLIBCXX_HAVE_UNISTD_H
715 if (posix::chdir(p
.c_str()))
716 ec
.assign(errno
, std::generic_category());
720 ec
= std::make_error_code(std::errc::function_not_supported
);
725 fs::equivalent(const path
& p1
, const path
& p2
)
728 auto result
= equivalent(p1
, p2
, ec
);
730 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot check file equivalence",
736 fs::equivalent(const path
& p1
, const path
& p2
, error_code
& ec
) noexcept
738 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
742 if (posix::stat(p1
.c_str(), &st1
) == 0)
743 s1
= make_file_status(st1
);
744 else if (is_not_found_errno(errno
))
745 s1
.type(file_type::not_found
);
749 if (posix::stat(p2
.c_str(), &st2
) == 0)
750 s2
= make_file_status(st2
);
751 else if (is_not_found_errno(errno
))
752 s2
.type(file_type::not_found
);
756 if (exists(s1
) && exists(s2
))
758 if (is_other(s1
) && is_other(s2
))
760 ec
= std::__unsupported();
764 if (is_other(s1
) || is_other(s2
))
766 return st1
.st_dev
== st2
.st_dev
&& st1
.st_ino
== st2
.st_ino
;
768 else if (!exists(s1
) && !exists(s2
))
769 ec
= std::make_error_code(std::errc::no_such_file_or_directory
);
771 ec
.assign(err
, std::generic_category());
776 ec
= std::make_error_code(std::errc::function_not_supported
);
782 fs::file_size(const path
& p
)
785 auto sz
= file_size(p
, ec
);
787 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot get file size", p
, ec
));
793 template<typename Accessor
, typename T
>
795 do_stat(const fs::path
& p
, std::error_code
& ec
, Accessor f
, T deflt
)
797 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
799 if (posix::stat(p
.c_str(), &st
))
801 ec
.assign(errno
, std::generic_category());
807 ec
= std::make_error_code(std::errc::function_not_supported
);
814 fs::file_size(const path
& p
, error_code
& ec
) noexcept
818 S(const stat_type
& st
) : type(make_file_type(st
)), size(st
.st_size
) { }
819 S() : type(file_type::not_found
) { }
823 auto s
= do_stat(p
, ec
, [](const auto& st
) { return S
{st
}; }, S
{});
824 if (s
.type
== file_type::regular
)
828 if (s
.type
== file_type::directory
)
829 ec
= std::make_error_code(std::errc::is_a_directory
);
831 ec
= std::__unsupported();
837 fs::hard_link_count(const path
& p
)
840 auto count
= hard_link_count(p
, ec
);
842 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot get link count", p
, ec
));
847 fs::hard_link_count(const path
& p
, error_code
& ec
) noexcept
849 return do_stat(p
, ec
, std::mem_fn(&stat_type::st_nlink
),
850 static_cast<uintmax_t>(-1));
854 fs::is_empty(const path
& p
)
857 bool e
= is_empty(p
, ec
);
859 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot check if file is empty",
865 fs::is_empty(const path
& p
, error_code
& ec
) noexcept
867 auto s
= status(p
, ec
);
870 bool empty
= fs::is_directory(s
)
871 ? fs::directory_iterator(p
, ec
) == fs::directory_iterator()
872 : fs::file_size(p
, ec
) == 0;
873 return ec
? false : empty
;
877 fs::last_write_time(const path
& p
)
880 auto t
= last_write_time(p
, ec
);
882 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot get file time", p
, ec
));
887 fs::last_write_time(const path
& p
, error_code
& ec
) noexcept
889 return do_stat(p
, ec
, [&ec
](const auto& st
) { return file_time(st
, ec
); },
890 file_time_type::min());
894 fs::last_write_time(const path
& p
, file_time_type new_time
)
897 last_write_time(p
, new_time
, ec
);
899 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot set file time", p
, ec
));
903 fs::last_write_time(const path
& p
__attribute__((__unused__
)),
904 file_time_type new_time
, error_code
& ec
) noexcept
906 auto d
= new_time
.time_since_epoch();
907 auto s
= chrono::duration_cast
<chrono::seconds
>(d
);
908 #if _GLIBCXX_USE_UTIMENSAT
909 auto ns
= chrono::duration_cast
<chrono::nanoseconds
>(d
- s
);
910 if (ns
< ns
.zero()) // tv_nsec must be non-negative and less than 10e9.
913 ns
+= chrono::seconds(1);
915 struct ::timespec ts
[2];
917 ts
[0].tv_nsec
= UTIME_OMIT
;
918 ts
[1].tv_sec
= static_cast<std::time_t>(s
.count());
919 ts
[1].tv_nsec
= static_cast<long>(ns
.count());
920 if (::utimensat(AT_FDCWD
, p
.c_str(), ts
, 0))
921 ec
.assign(errno
, std::generic_category());
924 #elif _GLIBCXX_USE_UTIME && _GLIBCXX_HAVE_SYS_STAT_H
925 posix::utimbuf times
;
926 times
.modtime
= s
.count();
927 times
.actime
= do_stat(p
, ec
, [](const auto& st
) { return st
.st_atime
; },
929 if (posix::utime(p
.c_str(), ×
))
930 ec
.assign(errno
, std::generic_category());
934 ec
= std::make_error_code(std::errc::function_not_supported
);
939 fs::permissions(const path
& p
, perms prms
)
942 permissions(p
, prms
, ec
);
944 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot set permissions", p
, ec
));
948 fs::permissions(const path
& p
, perms prms
, error_code
& ec
) noexcept
950 const bool add
= is_set(prms
, perms::add_perms
);
951 const bool remove
= is_set(prms
, perms::remove_perms
);
952 const bool nofollow
= is_set(prms
, perms::symlink_nofollow
);
955 ec
= std::make_error_code(std::errc::invalid_argument
);
962 if (add
|| remove
|| nofollow
)
964 st
= nofollow
? symlink_status(p
, ec
) : status(p
, ec
);
967 auto curr
= st
.permissions();
975 #if _GLIBCXX_USE_FCHMODAT
976 const int flag
= (nofollow
&& is_symlink(st
)) ? AT_SYMLINK_NOFOLLOW
: 0;
977 if (::fchmodat(AT_FDCWD
, p
.c_str(), static_cast<mode_t
>(prms
), flag
))
980 if (nofollow
&& is_symlink(st
))
981 ec
= std::__unsupported();
982 else if (posix::chmod(p
.c_str(), static_cast<mode_t
>(prms
)))
987 ec
.assign(err
, std::generic_category());
993 fs::read_symlink(const path
& p
)
996 path tgt
= read_symlink(p
, ec
);
998 _GLIBCXX_THROW_OR_ABORT(filesystem_error("read_symlink", p
, ec
));
1002 fs::path
fs::read_symlink(const path
& p
[[gnu::unused
]], error_code
& ec
)
1005 #if defined(_GLIBCXX_HAVE_READLINK) && defined(_GLIBCXX_HAVE_SYS_STAT_H)
1007 if (posix::lstat(p
.c_str(), &st
))
1009 ec
.assign(errno
, std::generic_category());
1012 else if (!fs::is_symlink(make_file_status(st
)))
1014 ec
.assign(EINVAL
, std::generic_category());
1018 std::string
buf(st
.st_size
? st
.st_size
+ 1 : 128, '\0');
1021 ssize_t len
= ::readlink(p
.c_str(), buf
.data(), buf
.size());
1024 ec
.assign(errno
, std::generic_category());
1027 else if (len
== (ssize_t
)buf
.size())
1029 if (buf
.size() > 4096)
1031 ec
.assign(ENAMETOOLONG
, std::generic_category());
1034 buf
.resize(buf
.size() * 2);
1046 ec
= std::make_error_code(std::errc::function_not_supported
);
1053 fs::remove(const path
& p
)
1056 bool result
= fs::remove(p
, ec
);
1058 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot remove", p
, ec
));
1063 fs::remove(const path
& p
, error_code
& ec
) noexcept
1065 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1066 auto st
= symlink_status(p
, ec
);
1069 if ((is_directory(p
, ec
) && RemoveDirectoryW(p
.c_str()))
1070 || DeleteFileW(p
.c_str()))
1076 ec
= __last_system_error();
1078 else if (status_known(st
))
1081 if (::remove(p
.c_str()) == 0)
1086 else if (errno
== ENOENT
)
1089 ec
.assign(errno
, std::generic_category());
1096 fs::remove_all(const path
& p
)
1099 const auto result
= remove_all(p
, ec
);
1101 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot remove all", p
, ec
));
1106 fs::remove_all(const path
& p
, error_code
& ec
)
1108 // Use the C++17 implementation.
1109 return std::filesystem::remove_all(p
.native(), ec
);
1113 fs::rename(const path
& from
, const path
& to
)
1116 rename(from
, to
, ec
);
1118 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot rename", from
, to
, ec
));
1122 fs::rename(const path
& from
, const path
& to
, error_code
& ec
) noexcept
1124 if (posix::rename(from
.c_str(), to
.c_str()))
1125 ec
.assign(errno
, std::generic_category());
1131 fs::resize_file(const path
& p
, uintmax_t size
)
1134 resize_file(p
, size
, ec
);
1136 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot resize file", p
, ec
));
1140 fs::resize_file(const path
& p
, uintmax_t size
, error_code
& ec
) noexcept
1142 if (size
> static_cast<uintmax_t>(std::numeric_limits
<posix::off_t
>::max()))
1143 ec
.assign(EINVAL
, std::generic_category());
1144 else if (posix::truncate(p
.c_str(), size
))
1145 ec
.assign(errno
, std::generic_category());
1152 fs::space(const path
& p
)
1155 space_info s
= space(p
, ec
);
1157 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot get free space", p
, ec
));
1162 fs::space(const path
& p
, error_code
& ec
) noexcept
1165 static_cast<uintmax_t>(-1),
1166 static_cast<uintmax_t>(-1),
1167 static_cast<uintmax_t>(-1)
1169 #if _GLIBCXX_FILESYSTEM_IS_WINDOWS
1170 path dir
= absolute(p
);
1171 dir
.remove_filename();
1172 auto str
= dir
.c_str();
1174 auto str
= p
.c_str();
1176 fs::do_space(str
, info
.capacity
, info
.free
, info
.available
, ec
);
1180 #if _GLIBCXX_FILESYSTEM_IS_WINDOWS
1181 static bool has_trailing_slash(const fs::path
& p
)
1183 wchar_t c
= p
.native().back();
1184 return c
== '/' || c
== L
'\\';
1188 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
1190 fs::status(const fs::path
& p
, error_code
& ec
) noexcept
1194 auto str
= p
.c_str();
1196 #if _GLIBCXX_FILESYSTEM_IS_WINDOWS
1197 // stat() fails if there's a trailing slash (PR 88881)
1199 if (p
.has_relative_path() && has_trailing_slash(p
))
1203 p2
= p
.parent_path();
1206 __catch(const bad_alloc
&)
1208 ec
= std::make_error_code(std::errc::not_enough_memory
);
1216 if (posix::stat(str
, &st
))
1219 ec
.assign(err
, std::generic_category());
1220 if (is_not_found_errno(err
))
1221 status
.type(file_type::not_found
);
1223 else if (err
== EOVERFLOW
)
1224 status
.type(file_type::unknown
);
1229 status
= make_file_status(st
);
1236 fs::symlink_status(const fs::path
& p
, std::error_code
& ec
) noexcept
1240 auto str
= p
.c_str();
1242 #if _GLIBCXX_FILESYSTEM_IS_WINDOWS
1243 // stat() fails if there's a trailing slash (PR 88881)
1245 if (p
.has_relative_path() && has_trailing_slash(p
))
1249 p2
= p
.parent_path();
1252 __catch(const bad_alloc
&)
1254 ec
= std::make_error_code(std::errc::not_enough_memory
);
1262 if (posix::lstat(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
);
1279 fs::status(const fs::path
& p
)
1282 auto result
= status(p
, ec
);
1283 if (result
.type() == file_type::none
)
1284 _GLIBCXX_THROW_OR_ABORT(filesystem_error("status", p
, ec
));
1289 fs::symlink_status(const fs::path
& p
)
1292 auto result
= symlink_status(p
, ec
);
1293 if (result
.type() == file_type::none
)
1294 _GLIBCXX_THROW_OR_ABORT(filesystem_error("symlink_status", p
, ec
));
1299 fs::system_complete(const path
& p
)
1302 path comp
= system_complete(p
, ec
);
1304 _GLIBCXX_THROW_OR_ABORT(filesystem_error("system_complete", p
, ec
));
1309 fs::system_complete(const path
& p
, error_code
& ec
)
1311 path base
= current_path(ec
);
1312 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1313 if (p
.is_absolute() || !p
.has_root_name()
1314 || p
.root_name() == base
.root_name())
1315 return absolute(p
, base
);
1317 ec
= std::__unsupported();
1322 return absolute(p
, base
);
1327 fs::temp_directory_path()
1330 path p
= fs::get_temp_directory_from_env(ec
);
1333 auto st
= status(p
, ec
);
1334 if (!ec
&& !is_directory(st
))
1335 ec
= std::make_error_code(std::errc::not_a_directory
);
1338 _GLIBCXX_THROW_OR_ABORT(filesystem_error("temp_directory_path", p
, ec
));
1343 fs::temp_directory_path(error_code
& ec
)
1345 path p
= fs::get_temp_directory_from_env(ec
);
1348 auto st
= status(p
, ec
);
1351 else if (!is_directory(st
))
1354 ec
= std::make_error_code(std::errc::not_a_directory
);