1 // Filesystem TS operations -*- C++ -*-
3 // Copyright (C) 2014-2019 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 #include <experimental/filesystem>
35 #include <ext/stdio_filebuf.h>
39 #include <limits.h> // PATH_MAX
40 #ifdef _GLIBCXX_HAVE_FCNTL_H
41 # include <fcntl.h> // AT_FDCWD, AT_SYMLINK_NOFOLLOW
43 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
44 # include <sys/stat.h> // stat, utimensat, fchmodat
46 #ifdef _GLIBCXX_HAVE_SYS_STATVFS_H
47 # include <sys/statvfs.h> // statvfs
49 #if !_GLIBCXX_USE_UTIMENSAT && _GLIBCXX_HAVE_UTIME_H
50 # include <utime.h> // utime
52 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
56 #define _GLIBCXX_BEGIN_NAMESPACE_FILESYSTEM \
57 namespace experimental { namespace filesystem {
58 #define _GLIBCXX_END_NAMESPACE_FILESYSTEM } }
59 #include "ops-common.h"
61 namespace fs
= std::experimental::filesystem
;
62 namespace posix
= std::filesystem::__gnu_posix
;
65 fs::absolute(const path
& p
, const path
& base
)
67 const bool has_root_dir
= p
.has_root_directory();
68 const bool has_root_name
= p
.has_root_name();
70 if (has_root_dir
&& has_root_name
)
74 abs
= base
.is_absolute() ? base
: absolute(base
);
76 abs
= abs
.root_name() / p
;
77 else if (has_root_name
)
78 abs
= p
.root_name() / abs
.root_directory() / abs
.relative_path()
88 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
89 inline bool is_dot(wchar_t c
) { return c
== L
'.'; }
91 inline bool is_dot(char c
) { return c
== '.'; }
94 inline bool is_dot(const fs::path
& path
)
96 const auto& filename
= path
.native();
97 return filename
.size() == 1 && is_dot(filename
[0]);
100 inline bool is_dotdot(const fs::path
& path
)
102 const auto& filename
= path
.native();
103 return filename
.size() == 2 && is_dot(filename
[0]) && is_dot(filename
[1]);
106 struct free_as_in_malloc
108 void operator()(void* p
) const { ::free(p
); }
111 using char_ptr
= std::unique_ptr
<fs::path::value_type
[], free_as_in_malloc
>;
115 fs::canonical(const path
& p
, const path
& base
, error_code
& ec
)
117 const path pa
= absolute(p
, base
);
120 #ifdef _GLIBCXX_USE_REALPATH
121 char_ptr buf
{ nullptr };
122 # if _XOPEN_VERSION < 700
123 // Not safe to call realpath(path, NULL)
124 using char_type
= fs::path::value_type
;
125 buf
.reset( (char_type
*)::malloc(PATH_MAX
* sizeof(char_type
)) );
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 using posix::stat_type
;
246 using std::filesystem::is_not_found_errno
;
247 using std::filesystem::file_time
;
248 #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 ? posix::lstat(from
.c_str(), &from_st
)
267 : posix::stat(from
.c_str(), &from_st
))
269 ec
.assign(errno
, std::generic_category());
273 ? posix::lstat(to
.c_str(), &to_st
)
274 : posix::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
);
428 file_status st
= symlink_status(p
, ec
);
429 if (is_directory(st
))
431 else if (ec
&& !status_known(st
))
436 ec
= std::make_error_code(std::errc::not_a_directory
);
440 std::stack
<path
> missing
;
443 while (!pp
.empty() && status(pp
, ec
).type() == file_type::not_found
)
446 const auto& filename
= pp
.filename();
447 if (!is_dot(filename
) && !is_dotdot(filename
))
449 missing
.push(std::move(pp
));
450 pp
= missing
.top().parent_path();
453 pp
= pp
.parent_path();
456 if (ec
|| missing
.empty())
462 const path
& top
= missing
.top();
463 created
= create_directory(top
, ec
);
468 while (!missing
.empty());
476 create_dir(const fs::path
& p
, fs::perms perm
, std::error_code
& ec
)
478 bool created
= false;
479 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
480 posix::mode_t mode
= static_cast<std::underlying_type_t
<fs::perms
>>(perm
);
481 if (posix::mkdir(p
.c_str(), mode
))
483 const int err
= errno
;
484 if (err
!= EEXIST
|| !is_directory(p
, ec
))
485 ec
.assign(err
, std::generic_category());
493 ec
= std::make_error_code(std::errc::not_supported
);
500 fs::create_directory(const path
& p
)
503 bool result
= create_directory(p
, ec
);
505 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot create directory", p
,
511 fs::create_directory(const path
& p
, error_code
& ec
) noexcept
513 return create_dir(p
, perms::all
, ec
);
518 fs::create_directory(const path
& p
, const path
& attributes
)
521 bool result
= create_directory(p
, attributes
, ec
);
523 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot create directory", p
,
529 fs::create_directory(const path
& p
, const path
& attributes
,
530 error_code
& ec
) noexcept
532 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
534 if (posix::stat(attributes
.c_str(), &st
))
536 ec
.assign(errno
, std::generic_category());
539 return create_dir(p
, static_cast<perms
>(st
.st_mode
), ec
);
541 ec
= std::make_error_code(std::errc::not_supported
);
548 fs::create_directory_symlink(const path
& to
, const path
& new_symlink
)
551 create_directory_symlink(to
, new_symlink
, ec
);
553 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot create directory symlink",
554 to
, new_symlink
, ec
));
558 fs::create_directory_symlink(const path
& to
, const path
& new_symlink
,
559 error_code
& ec
) noexcept
561 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
562 ec
= std::make_error_code(std::errc::not_supported
);
564 create_symlink(to
, new_symlink
, ec
);
570 fs::create_hard_link(const path
& to
, const path
& new_hard_link
)
573 create_hard_link(to
, new_hard_link
, ec
);
575 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot create hard link",
576 to
, new_hard_link
, ec
));
580 fs::create_hard_link(const path
& to
, const path
& new_hard_link
,
581 error_code
& ec
) noexcept
583 #ifdef _GLIBCXX_HAVE_LINK
584 if (::link(to
.c_str(), new_hard_link
.c_str()))
585 ec
.assign(errno
, std::generic_category());
588 #elif defined _GLIBCXX_FILESYSTEM_IS_WINDOWS
589 if (CreateHardLinkW(new_hard_link
.c_str(), to
.c_str(), NULL
))
592 ec
.assign((int)GetLastError(), generic_category());
594 ec
= std::make_error_code(std::errc::not_supported
);
599 fs::create_symlink(const path
& to
, const path
& new_symlink
)
602 create_symlink(to
, new_symlink
, ec
);
604 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot create symlink",
605 to
, new_symlink
, ec
));
609 fs::create_symlink(const path
& to
, const path
& new_symlink
,
610 error_code
& ec
) noexcept
612 #ifdef _GLIBCXX_HAVE_SYMLINK
613 if (::symlink(to
.c_str(), new_symlink
.c_str()))
614 ec
.assign(errno
, std::generic_category());
618 ec
= std::make_error_code(std::errc::not_supported
);
626 path p
= current_path(ec
);
628 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot get current path", ec
));
633 fs::current_path(error_code
& ec
)
636 #ifdef _GLIBCXX_HAVE_UNISTD_H
637 #if defined __GLIBC__ || defined _GLIBCXX_FILESYSTEM_IS_WINDOWS
638 if (char_ptr cwd
= char_ptr
{posix::getcwd(nullptr, 0)})
644 ec
.assign(errno
, std::generic_category());
647 long path_max
= pathconf(".", _PC_PATH_MAX
);
651 else if (path_max
> 10240)
655 #elif defined(PATH_MAX)
656 size_t size
= PATH_MAX
;
660 for (char_ptr buf
; p
.empty(); size
*= 2)
662 using char_type
= fs::path::value_type
;
663 buf
.reset((char_type
*)malloc(size
* sizeof(char_type
)));
666 if (getcwd(buf
.get(), size
))
671 else if (errno
!= ERANGE
)
673 ec
.assign(errno
, std::generic_category());
679 ec
= std::make_error_code(std::errc::not_enough_memory
);
684 #else // _GLIBCXX_HAVE_UNISTD_H
685 ec
= std::make_error_code(std::errc::not_supported
);
691 fs::current_path(const path
& p
)
696 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot set current path", ec
));
700 fs::current_path(const path
& p
, error_code
& ec
) noexcept
702 #ifdef _GLIBCXX_HAVE_UNISTD_H
703 if (posix::chdir(p
.c_str()))
704 ec
.assign(errno
, std::generic_category());
708 ec
= std::make_error_code(std::errc::not_supported
);
713 fs::equivalent(const path
& p1
, const path
& p2
)
716 auto result
= equivalent(p1
, p2
, ec
);
718 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot check file equivalence",
724 fs::equivalent(const path
& p1
, const path
& p2
, error_code
& ec
) noexcept
726 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
730 if (posix::stat(p1
.c_str(), &st1
) == 0)
731 s1
= make_file_status(st1
);
732 else if (is_not_found_errno(errno
))
733 s1
.type(file_type::not_found
);
737 if (posix::stat(p2
.c_str(), &st2
) == 0)
738 s2
= make_file_status(st2
);
739 else if (is_not_found_errno(errno
))
740 s2
.type(file_type::not_found
);
744 if (exists(s1
) && exists(s2
))
746 if (is_other(s1
) && is_other(s2
))
748 ec
= std::make_error_code(std::errc::not_supported
);
752 if (is_other(s1
) || is_other(s2
))
754 return st1
.st_dev
== st2
.st_dev
&& st1
.st_ino
== st2
.st_ino
;
756 else if (!exists(s1
) && !exists(s2
))
757 ec
= std::make_error_code(std::errc::no_such_file_or_directory
);
759 ec
.assign(err
, std::generic_category());
764 ec
= std::make_error_code(std::errc::not_supported
);
770 fs::file_size(const path
& p
)
773 auto sz
= file_size(p
, ec
);
775 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot get file size", p
, ec
));
781 template<typename Accessor
, typename T
>
783 do_stat(const fs::path
& p
, std::error_code
& ec
, Accessor f
, T deflt
)
785 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
787 if (posix::stat(p
.c_str(), &st
))
789 ec
.assign(errno
, std::generic_category());
795 ec
= std::make_error_code(std::errc::not_supported
);
802 fs::file_size(const path
& p
, error_code
& ec
) noexcept
806 S(const stat_type
& st
) : type(make_file_type(st
)), size(st
.st_size
) { }
807 S() : type(file_type::not_found
) { }
811 auto s
= do_stat(p
, ec
, [](const auto& st
) { return S
{st
}; }, S
{});
812 if (s
.type
== file_type::regular
)
816 if (s
.type
== file_type::directory
)
817 ec
= std::make_error_code(std::errc::is_a_directory
);
819 ec
= std::make_error_code(std::errc::not_supported
);
825 fs::hard_link_count(const path
& p
)
828 auto count
= hard_link_count(p
, ec
);
830 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot get link count", p
, ec
));
835 fs::hard_link_count(const path
& p
, error_code
& ec
) noexcept
837 return do_stat(p
, ec
, std::mem_fn(&stat_type::st_nlink
),
838 static_cast<uintmax_t>(-1));
842 fs::is_empty(const path
& p
)
845 bool e
= is_empty(p
, ec
);
847 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot check if file is empty",
853 fs::is_empty(const path
& p
, error_code
& ec
) noexcept
855 auto s
= status(p
, ec
);
858 bool empty
= fs::is_directory(s
)
859 ? fs::directory_iterator(p
, ec
) == fs::directory_iterator()
860 : fs::file_size(p
, ec
) == 0;
861 return ec
? false : empty
;
865 fs::last_write_time(const path
& p
)
868 auto t
= last_write_time(p
, ec
);
870 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot get file time", p
, ec
));
875 fs::last_write_time(const path
& p
, error_code
& ec
) noexcept
877 return do_stat(p
, ec
, [&ec
](const auto& st
) { return file_time(st
, ec
); },
878 file_time_type::min());
882 fs::last_write_time(const path
& p
, file_time_type new_time
)
885 last_write_time(p
, new_time
, ec
);
887 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot set file time", p
, ec
));
891 fs::last_write_time(const path
& p
__attribute__((__unused__
)),
892 file_time_type new_time
, error_code
& ec
) noexcept
894 auto d
= new_time
.time_since_epoch();
895 auto s
= chrono::duration_cast
<chrono::seconds
>(d
);
896 #if _GLIBCXX_USE_UTIMENSAT
897 auto ns
= chrono::duration_cast
<chrono::nanoseconds
>(d
- s
);
898 if (ns
< ns
.zero()) // tv_nsec must be non-negative and less than 10e9.
901 ns
+= chrono::seconds(1);
903 struct ::timespec ts
[2];
905 ts
[0].tv_nsec
= UTIME_OMIT
;
906 ts
[1].tv_sec
= static_cast<std::time_t>(s
.count());
907 ts
[1].tv_nsec
= static_cast<long>(ns
.count());
908 if (::utimensat(AT_FDCWD
, p
.c_str(), ts
, 0))
909 ec
.assign(errno
, std::generic_category());
912 #elif _GLIBCXX_HAVE_UTIME_H
913 posix::utimbuf times
;
914 times
.modtime
= s
.count();
915 times
.actime
= do_stat(p
, ec
, [](const auto& st
) { return st
.st_atime
; },
917 if (posix::utime(p
.c_str(), ×
))
918 ec
.assign(errno
, std::generic_category());
922 ec
= std::make_error_code(std::errc::not_supported
);
927 fs::permissions(const path
& p
, perms prms
)
930 permissions(p
, prms
, ec
);
932 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot set permissions", p
, ec
));
936 fs::permissions(const path
& p
, perms prms
, error_code
& ec
) noexcept
938 const bool add
= is_set(prms
, perms::add_perms
);
939 const bool remove
= is_set(prms
, perms::remove_perms
);
940 const bool nofollow
= is_set(prms
, perms::symlink_nofollow
);
943 ec
= std::make_error_code(std::errc::invalid_argument
);
950 if (add
|| remove
|| nofollow
)
952 st
= nofollow
? symlink_status(p
, ec
) : status(p
, ec
);
955 auto curr
= st
.permissions();
963 #if _GLIBCXX_USE_FCHMODAT
964 const int flag
= (nofollow
&& is_symlink(st
)) ? AT_SYMLINK_NOFOLLOW
: 0;
965 if (::fchmodat(AT_FDCWD
, p
.c_str(), static_cast<mode_t
>(prms
), flag
))
968 if (nofollow
&& is_symlink(st
))
969 ec
= std::make_error_code(std::errc::operation_not_supported
);
970 else if (posix::chmod(p
.c_str(), static_cast<mode_t
>(prms
)))
975 ec
.assign(err
, std::generic_category());
981 fs::read_symlink(const path
& p
)
984 path tgt
= read_symlink(p
, ec
);
986 _GLIBCXX_THROW_OR_ABORT(filesystem_error("read_symlink", p
, ec
));
990 fs::path
fs::read_symlink(const path
& p
[[gnu::unused
]], error_code
& ec
)
993 #if defined(_GLIBCXX_HAVE_READLINK) && defined(_GLIBCXX_HAVE_SYS_STAT_H)
995 if (::lstat(p
.c_str(), &st
))
997 ec
.assign(errno
, std::generic_category());
1000 std::string
buf(st
.st_size
? st
.st_size
+ 1 : 128, '\0');
1003 ssize_t len
= ::readlink(p
.c_str(), buf
.data(), buf
.size());
1006 ec
.assign(errno
, std::generic_category());
1009 else if (len
== (ssize_t
)buf
.size())
1011 if (buf
.size() > 4096)
1013 ec
.assign(ENAMETOOLONG
, std::generic_category());
1016 buf
.resize(buf
.size() * 2);
1028 ec
= std::make_error_code(std::errc::not_supported
);
1035 fs::remove(const path
& p
)
1038 bool result
= fs::remove(p
, ec
);
1040 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot remove", p
, ec
));
1045 fs::remove(const path
& p
, error_code
& ec
) noexcept
1047 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1048 if (exists(symlink_status(p
, ec
)))
1050 if ((is_directory(p
, ec
) && RemoveDirectoryW(p
.c_str()))
1051 || DeleteFileW(p
.c_str()))
1057 ec
.assign((int)GetLastError(), generic_category());
1060 if (::remove(p
.c_str()) == 0)
1065 else if (errno
== ENOENT
)
1068 ec
.assign(errno
, std::generic_category());
1075 fs::remove_all(const path
& p
)
1078 const auto result
= remove_all(p
, ec
);
1080 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot remove all", p
, ec
));
1085 fs::remove_all(const path
& p
, error_code
& ec
) noexcept
1087 const auto s
= symlink_status(p
, ec
);
1088 if (!status_known(s
))
1092 if (s
.type() == file_type::not_found
)
1095 uintmax_t count
= 0;
1096 if (s
.type() == file_type::directory
)
1098 for (directory_iterator
d(p
, ec
), end
; !ec
&& d
!= end
; d
.increment(ec
))
1099 count
+= fs::remove_all(d
->path(), ec
);
1100 if (ec
.value() == ENOENT
)
1106 if (fs::remove(p
, ec
))
1108 return ec
? -1 : count
;
1112 fs::rename(const path
& from
, const path
& to
)
1115 rename(from
, to
, ec
);
1117 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot rename", from
, to
, ec
));
1121 fs::rename(const path
& from
, const path
& to
, error_code
& ec
) noexcept
1123 if (posix::rename(from
.c_str(), to
.c_str()))
1124 ec
.assign(errno
, std::generic_category());
1130 fs::resize_file(const path
& p
, uintmax_t size
)
1133 resize_file(p
, size
, ec
);
1135 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot resize file", p
, ec
));
1139 fs::resize_file(const path
& p
, uintmax_t size
, error_code
& ec
) noexcept
1141 #ifdef _GLIBCXX_HAVE_UNISTD_H
1142 if (size
> static_cast<uintmax_t>(std::numeric_limits
<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());
1149 ec
= std::make_error_code(std::errc::not_supported
);
1155 fs::space(const path
& p
)
1158 space_info s
= space(p
, ec
);
1160 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot get free space", p
, ec
));
1165 fs::space(const path
& p
, error_code
& ec
) noexcept
1168 static_cast<uintmax_t>(-1),
1169 static_cast<uintmax_t>(-1),
1170 static_cast<uintmax_t>(-1)
1172 #if _GLIBCXX_FILESYSTEM_IS_WINDOWS
1173 path dir
= absolute(p
);
1174 dir
.remove_filename();
1175 auto str
= dir
.c_str();
1177 auto str
= p
.c_str();
1179 fs::do_space(str
, info
.capacity
, info
.free
, info
.available
, ec
);
1183 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
1185 fs::status(const fs::path
& p
, error_code
& ec
) noexcept
1189 if (posix::stat(p
.c_str(), &st
))
1192 ec
.assign(err
, std::generic_category());
1193 if (is_not_found_errno(err
))
1194 status
.type(file_type::not_found
);
1196 else if (err
== EOVERFLOW
)
1197 status
.type(file_type::unknown
);
1202 status
= make_file_status(st
);
1209 fs::symlink_status(const fs::path
& p
, std::error_code
& ec
) noexcept
1213 if (posix::lstat(p
.c_str(), &st
))
1216 ec
.assign(err
, std::generic_category());
1217 if (is_not_found_errno(err
))
1218 status
.type(file_type::not_found
);
1222 status
= make_file_status(st
);
1230 fs::status(const fs::path
& p
)
1233 auto result
= status(p
, ec
);
1234 if (result
.type() == file_type::none
)
1235 _GLIBCXX_THROW_OR_ABORT(filesystem_error("status", p
, ec
));
1240 fs::symlink_status(const fs::path
& p
)
1243 auto result
= symlink_status(p
, ec
);
1244 if (result
.type() == file_type::none
)
1245 _GLIBCXX_THROW_OR_ABORT(filesystem_error("symlink_status", p
, ec
));
1250 fs::system_complete(const path
& p
)
1253 path comp
= system_complete(p
, ec
);
1255 _GLIBCXX_THROW_OR_ABORT(filesystem_error("system_complete", p
, ec
));
1260 fs::system_complete(const path
& p
, error_code
& ec
)
1262 path base
= current_path(ec
);
1263 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1264 if (p
.is_absolute() || !p
.has_root_name()
1265 || p
.root_name() == base
.root_name())
1266 return absolute(p
, base
);
1268 ec
= std::make_error_code(std::errc::not_supported
);
1273 return absolute(p
, base
);
1277 fs::path
fs::temp_directory_path()
1280 path tmp
= temp_directory_path(ec
);
1282 _GLIBCXX_THROW_OR_ABORT(filesystem_error("temp_directory_path", ec
));
1286 fs::path
fs::temp_directory_path(error_code
& ec
)
1289 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1290 unsigned len
= 1024;
1295 len
= GetTempPathW(buf
.size(), buf
.data());
1296 } while (len
> buf
.size());
1300 ec
.assign((int)GetLastError(), std::system_category());
1306 const char* tmpdir
= nullptr;
1307 const char* env
[] = { "TMPDIR", "TMP", "TEMP", "TEMPDIR", nullptr };
1308 for (auto e
= env
; tmpdir
== nullptr && *e
!= nullptr; ++e
)
1309 tmpdir
= ::getenv(*e
);
1310 p
= tmpdir
? tmpdir
: "/tmp";
1311 auto st
= status(p
, ec
);
1314 else if (!is_directory(st
))
1317 ec
= std::make_error_code(std::errc::not_a_directory
);