1 // Filesystem operations -*- C++ -*-
3 // Copyright (C) 2014-2016 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 #ifndef _GLIBCXX_USE_CXX11_ABI
26 # define _GLIBCXX_USE_CXX11_ABI 1
29 #include <experimental/filesystem>
35 #include <limits.h> // PATH_MAX
36 #ifdef _GLIBCXX_HAVE_UNISTD_H
38 # if defined(_GLIBCXX_HAVE_SYS_STAT_H) && defined(_GLIBCXX_HAVE_SYS_TYPES_H)
39 # include <sys/types.h>
40 # include <sys/stat.h>
43 #ifdef _GLIBCXX_HAVE_FCNTL_H
46 #ifdef _GLIBCXX_HAVE_SYS_STATVFS_H
47 # include <sys/statvfs.h>
49 #ifdef _GLIBCXX_USE_SENDFILE
50 # include <sys/sendfile.h>
52 # include <ext/stdio_filebuf.h>
55 #if _GLIBCXX_HAVE_UTIME_H
59 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
61 # define utime _wutime
63 # define chmod _wchmod
66 namespace fs
= std::experimental::filesystem
;
69 fs::absolute(const path
& p
, const path
& base
)
71 const bool has_root_dir
= p
.has_root_directory();
72 const bool has_root_name
= p
.has_root_name();
74 if (has_root_dir
&& has_root_name
)
78 abs
= base
.is_absolute() ? base
: absolute(base
);
80 abs
= abs
.root_name() / p
;
81 else if (has_root_name
)
82 abs
= p
.root_name() / abs
.root_directory() / abs
.relative_path()
92 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
93 inline bool is_dot(wchar_t c
) { return c
== L
'.'; }
95 inline bool is_dot(char c
) { return c
== '.'; }
98 inline bool is_dot(const fs::path
& path
)
100 const auto& filename
= path
.native();
101 return filename
.size() == 1 && is_dot(filename
[0]);
104 inline bool is_dotdot(const fs::path
& path
)
106 const auto& filename
= path
.native();
107 return filename
.size() == 2 && is_dot(filename
[0]) && is_dot(filename
[1]);
110 struct free_as_in_malloc
112 void operator()(void* p
) const { ::free(p
); }
115 using char_ptr
= std::unique_ptr
<char[], free_as_in_malloc
>;
119 fs::canonical(const path
& p
, const path
& base
, error_code
& ec
)
121 const path pa
= absolute(p
, base
);
124 #ifdef _GLIBCXX_USE_REALPATH
125 char_ptr buf
{ nullptr };
126 # if _XOPEN_VERSION < 700
127 // Not safe to call realpath(path, NULL)
128 buf
.reset( (char*)::malloc(PATH_MAX
) );
130 if (char* rp
= ::realpath(pa
.c_str(), buf
.get()))
138 if (errno
!= ENAMETOOLONG
)
140 ec
.assign(errno
, std::generic_category());
147 // else: we know there are (currently) no unresolvable symlink loops
149 result
= pa
.root_path();
152 for (auto& f
: pa
.relative_path())
155 int max_allowed_symlinks
= 40;
157 while (!cmpts
.empty() && !ec
)
159 path f
= std::move(cmpts
.front());
164 if (!is_directory(result
, ec
) && !ec
)
165 ec
.assign(ENOTDIR
, std::generic_category());
167 else if (is_dotdot(f
))
169 auto parent
= result
.parent_path();
171 result
= pa
.root_path();
179 if (is_symlink(result
, ec
))
181 path link
= read_symlink(result
, ec
);
184 if (--max_allowed_symlinks
== 0)
185 ec
.assign(ELOOP
, std::generic_category());
188 if (link
.is_absolute())
190 result
= link
.root_path();
191 link
= link
.relative_path();
194 result
.remove_filename();
196 cmpts
.insert(cmpts
.begin(), link
.begin(), link
.end());
203 if (ec
|| !exists(result
, ec
))
210 fs::canonical(const path
& p
, error_code
& ec
)
212 path cur
= current_path(ec
);
215 return canonical(p
, cur
, ec
);
219 fs::canonical(const path
& p
, const path
& base
)
222 path can
= canonical(p
, base
, ec
);
224 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot canonicalize", p
, ec
));
229 fs::copy(const path
& from
, const path
& to
, copy_options options
)
232 copy(from
, to
, options
, ec
);
234 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot copy", from
, to
, ec
));
239 template<typename Bitmask
>
240 inline bool is_set(Bitmask obj
, Bitmask bits
)
242 return (obj
& bits
) != Bitmask::none
;
246 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
249 typedef struct ::stat stat_type
;
252 make_file_type(const stat_type
& st
)
255 #ifdef _GLIBCXX_HAVE_S_ISREG
256 if (S_ISREG(st
.st_mode
))
257 return file_type::regular
;
258 else if (S_ISDIR(st
.st_mode
))
259 return file_type::directory
;
260 else if (S_ISCHR(st
.st_mode
))
261 return file_type::character
;
262 else if (S_ISBLK(st
.st_mode
))
263 return file_type::block
;
264 else if (S_ISFIFO(st
.st_mode
))
265 return file_type::fifo
;
266 else if (S_ISLNK(st
.st_mode
))
267 return file_type::symlink
;
268 else if (S_ISSOCK(st
.st_mode
))
269 return file_type::socket
;
271 return file_type::unknown
;
275 inline fs::file_status
276 make_file_status(const stat_type
& st
)
278 return fs::file_status
{
280 static_cast<fs::perms
>(st
.st_mode
) & fs::perms::mask
285 is_not_found_errno(int err
)
287 return err
== ENOENT
|| err
== ENOTDIR
;
290 inline fs::file_time_type
291 file_time(const stat_type
& st
)
293 using namespace std::chrono
;
294 return fs::file_time_type
{
295 #ifdef _GLIBCXX_USE_ST_MTIM
296 seconds
{st
.st_mtim
.tv_sec
} + nanoseconds
{st
.st_mtim
.tv_nsec
}
304 do_copy_file(const fs::path
& from
, const fs::path
& to
,
305 fs::copy_options option
,
306 stat_type
* from_st
, stat_type
* to_st
,
307 std::error_code
& ec
) noexcept
310 fs::file_status t
, f
;
312 if (to_st
== nullptr)
314 if (::stat(to
.c_str(), &st1
))
317 if (!is_not_found_errno(err
))
319 ec
.assign(err
, std::generic_category());
326 else if (to_st
== from_st
)
329 if (to_st
== nullptr)
330 t
= fs::file_status
{fs::file_type::not_found
};
332 t
= make_file_status(*to_st
);
334 if (from_st
== nullptr)
336 if (::stat(from
.c_str(), &st2
))
338 ec
.assign(errno
, std::generic_category());
344 f
= make_file_status(*from_st
);
346 using opts
= fs::copy_options
;
350 if (!is_other(t
) && !is_other(f
)
351 && to_st
->st_dev
== from_st
->st_dev
352 && to_st
->st_ino
== from_st
->st_ino
)
354 ec
= std::make_error_code(std::errc::file_exists
);
358 if (is_set(option
, opts::skip_existing
))
363 else if (is_set(option
, opts::update_existing
))
365 if (file_time(*from_st
) <= file_time(*to_st
))
371 else if (!is_set(option
, opts::overwrite_existing
))
373 ec
= std::make_error_code(std::errc::file_exists
);
379 ~CloseFD() { if (fd
!= -1) ::close(fd
); }
383 CloseFD in
= { ::open(from
.c_str(), O_RDONLY
) };
386 ec
.assign(errno
, std::generic_category());
389 int oflag
= O_WRONLY
|O_CREAT
;
390 if (is_set(option
, opts::overwrite_existing
|opts::update_existing
))
394 CloseFD out
= { ::open(to
.c_str(), oflag
, S_IWUSR
) };
397 if (errno
== EEXIST
&& is_set(option
, opts::skip_existing
))
400 ec
.assign(errno
, std::generic_category());
404 #ifdef _GLIBCXX_USE_SENDFILE
405 auto n
= ::sendfile(out
.fd
, in
.fd
, nullptr, from_st
->st_size
);
406 if (n
!= from_st
->st_size
)
408 ec
.assign(errno
, std::generic_category());
412 __gnu_cxx::stdio_filebuf
<char> sbin(in
.fd
, std::ios::in
);
413 __gnu_cxx::stdio_filebuf
<char> sbout(out
.fd
, std::ios::out
);
414 if ( !(std::ostream(&sbout
) << &sbin
) )
416 ec
= std::make_error_code(std::errc::io_error
);
421 #ifdef _GLIBCXX_USE_FCHMOD
422 if (::fchmod(out
.fd
, from_st
->st_mode
))
423 #elif _GLIBCXX_USE_FCHMODAT
424 if (::fchmodat(AT_FDCWD
, to
.c_str(), from_st
->st_mode
, 0))
426 if (::chmod(to
.c_str(), from_st
->st_mode
))
429 ec
.assign(errno
, std::generic_category());
439 fs::copy(const path
& from
, const path
& to
, copy_options options
,
440 error_code
& ec
) noexcept
442 bool skip_symlinks
= is_set(options
, copy_options::skip_symlinks
);
443 bool create_symlinks
= is_set(options
, copy_options::create_symlinks
);
444 bool use_lstat
= create_symlinks
|| skip_symlinks
;
447 stat_type from_st
, to_st
;
449 ? ::lstat(from
.c_str(), &from_st
)
450 : ::stat(from
.c_str(), &from_st
))
452 ec
.assign(errno
, std::generic_category());
456 ? ::lstat(to
.c_str(), &to_st
)
457 : ::stat(to
.c_str(), &to_st
))
459 if (!is_not_found_errno(errno
))
461 ec
.assign(errno
, std::generic_category());
464 t
= file_status
{file_type::not_found
};
467 t
= make_file_status(to_st
);
468 f
= make_file_status(from_st
);
470 if (exists(t
) && !is_other(t
) && !is_other(f
)
471 && to_st
.st_dev
== from_st
.st_dev
&& to_st
.st_ino
== from_st
.st_ino
)
473 ec
= std::make_error_code(std::errc::file_exists
);
476 if (is_other(f
) || is_other(t
))
478 ec
= std::make_error_code(std::errc::not_supported
);
481 if (is_directory(f
) && is_regular_file(t
))
483 ec
= std::make_error_code(std::errc::is_a_directory
);
491 else if (!exists(t
) && is_set(options
, copy_options::copy_symlinks
))
492 copy_symlink(from
, to
, ec
);
494 // Not clear what should be done here.
495 // "Otherwise report an error as specified in Error reporting (7)."
496 ec
= std::make_error_code(std::errc::invalid_argument
);
498 else if (is_regular_file(f
))
500 if (is_set(options
, copy_options::directories_only
))
502 else if (create_symlinks
)
503 create_symlink(from
, to
, ec
);
504 else if (is_set(options
, copy_options::create_hard_links
))
505 create_hard_link(from
, to
, ec
);
506 else if (is_directory(t
))
507 do_copy_file(from
, to
/ from
.filename(), options
, &from_st
, 0, ec
);
510 auto ptr
= exists(t
) ? &to_st
: &from_st
;
511 do_copy_file(from
, to
, options
, &from_st
, ptr
, ec
);
514 else if (is_directory(f
) && (is_set(options
, copy_options::recursive
)
515 || options
== copy_options::none
))
518 if (!create_directory(to
, from
, ec
))
520 // set an unused bit in options to disable further recursion
521 if (!is_set(options
, copy_options::recursive
))
522 options
|= static_cast<copy_options
>(4096);
523 for (const directory_entry
& x
: directory_iterator(from
))
524 copy(x
.path(), to
/x
.path().filename(), options
, ec
);
526 // "Otherwise no effects." (should ec.clear() be called?)
530 fs::copy_file(const path
& from
, const path
& to
, copy_options option
)
533 bool result
= copy_file(from
, to
, option
, ec
);
535 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot copy file", from
, to
,
541 fs::copy_file(const path
& from
, const path
& to
, copy_options option
,
542 error_code
& ec
) noexcept
544 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
545 return do_copy_file(from
, to
, option
, nullptr, nullptr, ec
);
547 ec
= std::make_error_code(std::errc::not_supported
);
554 fs::copy_symlink(const path
& existing_symlink
, const path
& new_symlink
)
557 copy_symlink(existing_symlink
, new_symlink
, ec
);
559 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot copy symlink",
560 existing_symlink
, new_symlink
, ec
));
564 fs::copy_symlink(const path
& existing_symlink
, const path
& new_symlink
,
565 error_code
& ec
) noexcept
567 auto p
= read_symlink(existing_symlink
, ec
);
570 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
573 create_directory_symlink(p
, new_symlink
, ec
);
577 create_symlink(p
, new_symlink
, ec
);
582 fs::create_directories(const path
& p
)
585 bool result
= create_directories(p
, ec
);
587 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot create directories", p
,
593 fs::create_directories(const path
& p
, error_code
& ec
) noexcept
597 ec
= std::make_error_code(errc::invalid_argument
);
600 std::stack
<path
> missing
;
603 while (!pp
.empty() && status(pp
, ec
).type() == file_type::not_found
)
606 const auto& filename
= pp
.filename();
607 if (!is_dot(filename
) && !is_dotdot(filename
))
609 pp
.remove_filename();
612 if (ec
|| missing
.empty())
617 const path
& top
= missing
.top();
618 create_directory(top
, ec
);
619 if (ec
&& is_directory(top
))
623 while (!missing
.empty() && !ec
);
625 return missing
.empty();
631 create_dir(const fs::path
& p
, fs::perms perm
, std::error_code
& ec
)
633 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
634 ::mode_t mode
= static_cast<std::underlying_type_t
<fs::perms
>>(perm
);
635 if (::mkdir(p
.c_str(), mode
))
637 ec
.assign(errno
, std::generic_category());
646 ec
= std::make_error_code(std::errc::not_supported
);
653 fs::create_directory(const path
& p
)
656 bool result
= create_directory(p
, ec
);
658 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot create directory", p
,
664 fs::create_directory(const path
& p
, error_code
& ec
) noexcept
666 return create_dir(p
, perms::all
, ec
);
671 fs::create_directory(const path
& p
, const path
& attributes
)
674 bool result
= create_directory(p
, attributes
, ec
);
676 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot create directory", p
,
682 fs::create_directory(const path
& p
, const path
& attributes
,
683 error_code
& ec
) noexcept
685 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
687 if (::stat(attributes
.c_str(), &st
))
689 ec
.assign(errno
, std::generic_category());
692 return create_dir(p
, static_cast<perms
>(st
.st_mode
), ec
);
694 ec
= std::make_error_code(std::errc::not_supported
);
701 fs::create_directory_symlink(const path
& to
, const path
& new_symlink
)
704 create_directory_symlink(to
, new_symlink
, ec
);
706 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot create directory symlink",
707 to
, new_symlink
, ec
));
711 fs::create_directory_symlink(const path
& to
, const path
& new_symlink
,
712 error_code
& ec
) noexcept
714 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
715 ec
= std::make_error_code(std::errc::not_supported
);
717 create_symlink(to
, new_symlink
, ec
);
723 fs::create_hard_link(const path
& to
, const path
& new_hard_link
)
726 create_hard_link(to
, new_hard_link
, ec
);
728 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot create hard link",
729 to
, new_hard_link
, ec
));
733 fs::create_hard_link(const path
& to
, const path
& new_hard_link
,
734 error_code
& ec
) noexcept
736 #ifdef _GLIBCXX_HAVE_UNISTD_H
737 if (::link(to
.c_str(), new_hard_link
.c_str()))
738 ec
.assign(errno
, std::generic_category());
742 ec
= std::make_error_code(std::errc::not_supported
);
747 fs::create_symlink(const path
& to
, const path
& new_symlink
)
750 create_symlink(to
, new_symlink
, ec
);
752 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot create symlink",
753 to
, new_symlink
, ec
));
757 fs::create_symlink(const path
& to
, const path
& new_symlink
,
758 error_code
& ec
) noexcept
760 #ifdef _GLIBCXX_HAVE_UNISTD_H
761 if (::symlink(to
.c_str(), new_symlink
.c_str()))
762 ec
.assign(errno
, std::generic_category());
766 ec
= std::make_error_code(std::errc::not_supported
);
775 path p
= current_path(ec
);
777 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot get current path", ec
));
782 fs::current_path(error_code
& ec
)
785 #ifdef _GLIBCXX_HAVE_UNISTD_H
787 if (char_ptr cwd
= char_ptr
{::getcwd(nullptr, 0)})
793 ec
.assign(errno
, std::generic_category());
795 long path_max
= pathconf(".", _PC_PATH_MAX
);
799 else if (path_max
> 10240)
803 for (char_ptr buf
; p
.empty(); size
*= 2)
805 buf
.reset((char*)malloc(size
));
808 if (getcwd(buf
.get(), size
))
813 else if (errno
!= ERANGE
)
815 ec
.assign(errno
, std::generic_category());
821 ec
= std::make_error_code(std::errc::not_enough_memory
);
826 #else // _GLIBCXX_HAVE_UNISTD_H
827 ec
= std::make_error_code(std::errc::not_supported
);
833 fs::current_path(const path
& p
)
838 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot set current path", ec
));
842 fs::current_path(const path
& p
, error_code
& ec
) noexcept
844 #ifdef _GLIBCXX_HAVE_UNISTD_H
845 if (::chdir(p
.c_str()))
846 ec
.assign(errno
, std::generic_category());
850 ec
= std::make_error_code(std::errc::not_supported
);
855 fs::equivalent(const path
& p1
, const path
& p2
)
858 auto result
= equivalent(p1
, p2
, ec
);
860 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot check file equivalence",
866 fs::equivalent(const path
& p1
, const path
& p2
, error_code
& ec
) noexcept
868 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
870 if (::stat(p1
.c_str(), &st1
) == 0 && ::stat(p2
.c_str(), &st2
) == 0)
872 file_status s1
= make_file_status(st1
);
873 file_status s2
= make_file_status(st2
);
874 if (is_other(s1
) && is_other(s2
))
876 ec
= std::make_error_code(std::errc::not_supported
);
880 return st1
.st_dev
== st2
.st_dev
&& st1
.st_ino
== st2
.st_ino
;
882 else if (is_not_found_errno(errno
))
884 ec
= std::make_error_code(std::errc::no_such_file_or_directory
);
887 ec
.assign(errno
, std::generic_category());
889 ec
= std::make_error_code(std::errc::not_supported
);
895 fs::file_size(const path
& p
)
898 auto sz
= file_size(p
, ec
);
900 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot get file size", p
, ec
));
906 template<typename Accessor
, typename T
>
908 do_stat(const fs::path
& p
, std::error_code
& ec
, Accessor f
, T deflt
)
910 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
912 if (::stat(p
.c_str(), &st
))
914 ec
.assign(errno
, std::generic_category());
920 ec
= std::make_error_code(std::errc::not_supported
);
927 fs::file_size(const path
& p
, error_code
& ec
) noexcept
931 S(const stat_type
& st
) : type(make_file_type(st
)), size(st
.st_size
) { }
932 S() : type(file_type::not_found
) { }
936 auto s
= do_stat(p
, ec
, [](const auto& st
) { return S
{st
}; }, S
{});
937 if (s
.type
== file_type::regular
)
941 if (s
.type
== file_type::directory
)
942 ec
= std::make_error_code(std::errc::is_a_directory
);
944 ec
= std::make_error_code(std::errc::not_supported
);
950 fs::hard_link_count(const path
& p
)
953 auto count
= hard_link_count(p
, ec
);
955 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot get link count", p
, ec
));
960 fs::hard_link_count(const path
& p
, error_code
& ec
) noexcept
962 return do_stat(p
, ec
, std::mem_fn(&stat::st_nlink
),
963 static_cast<uintmax_t>(-1));
967 fs::is_empty(const path
& p
)
969 return fs::is_directory(status(p
))
970 ? fs::directory_iterator(p
) == fs::directory_iterator()
971 : fs::file_size(p
) == 0;
975 fs::is_empty(const path
& p
, error_code
& ec
) noexcept
977 auto s
= status(p
, ec
);
980 return fs::is_directory(s
)
981 ? fs::directory_iterator(p
, ec
) == fs::directory_iterator()
982 : fs::file_size(p
, ec
) == 0;
986 fs::last_write_time(const path
& p
)
989 auto t
= last_write_time(p
, ec
);
991 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot get file time", p
, ec
));
996 fs::last_write_time(const path
& p
, error_code
& ec
) noexcept
998 return do_stat(p
, ec
, [](const auto& st
) { return file_time(st
); },
999 file_time_type::min());
1003 fs::last_write_time(const path
& p
, file_time_type new_time
)
1006 last_write_time(p
, new_time
, ec
);
1008 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot set file time", p
, ec
));
1012 fs::last_write_time(const path
& p
__attribute__((__unused__
)),
1013 file_time_type new_time
, error_code
& ec
) noexcept
1015 auto d
= new_time
.time_since_epoch();
1016 auto s
= chrono::duration_cast
<chrono::seconds
>(d
);
1017 #if _GLIBCXX_USE_UTIMENSAT
1018 auto ns
= chrono::duration_cast
<chrono::nanoseconds
>(d
- s
);
1019 struct ::timespec ts
[2];
1021 ts
[0].tv_nsec
= UTIME_OMIT
;
1022 ts
[1].tv_sec
= static_cast<std::time_t>(s
.count());
1023 ts
[1].tv_nsec
= static_cast<long>(ns
.count());
1024 if (::utimensat(AT_FDCWD
, p
.c_str(), ts
, 0))
1025 ec
.assign(errno
, std::generic_category());
1028 #elif _GLIBCXX_HAVE_UTIME_H
1030 times
.modtime
= s
.count();
1031 times
.actime
= do_stat(p
, ec
, [](const auto& st
) { return st
.st_atime
; },
1033 if (::utime(p
.c_str(), ×
))
1034 ec
.assign(errno
, std::generic_category());
1038 ec
= std::make_error_code(std::errc::not_supported
);
1043 fs::permissions(const path
& p
, perms prms
)
1046 permissions(p
, prms
, ec
);
1048 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot set permissions", p
, ec
));
1051 void fs::permissions(const path
& p
, perms prms
, error_code
& ec
) noexcept
1053 #if _GLIBCXX_USE_FCHMODAT
1054 if (::fchmodat(AT_FDCWD
, p
.c_str(), static_cast<mode_t
>(prms
), 0))
1056 if (::chmod(p
.c_str(), static_cast<mode_t
>(prms
)))
1058 ec
.assign(errno
, std::generic_category());
1064 fs::read_symlink(const path
& p
)
1067 path tgt
= read_symlink(p
, ec
);
1069 _GLIBCXX_THROW_OR_ABORT(filesystem_error("read_symlink", p
, ec
));
1073 fs::path
fs::read_symlink(const path
& p
, error_code
& ec
)
1075 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
1077 if (::lstat(p
.c_str(), &st
))
1079 ec
.assign(errno
, std::generic_category());
1082 std::string
buf(st
.st_size
, '\0');
1083 ssize_t len
= ::readlink(p
.c_str(), &buf
.front(), buf
.size());
1086 ec
.assign(errno
, std::generic_category());
1089 return path
{buf
.data(), buf
.data()+len
};
1091 ec
= std::make_error_code(std::errc::not_supported
);
1098 fs::remove(const path
& p
)
1101 bool result
= fs::remove(p
, ec
);
1103 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot remove", p
, ec
));
1108 fs::remove(const path
& p
, error_code
& ec
) noexcept
1110 if (exists(symlink_status(p
, ec
)))
1112 if (::remove(p
.c_str()) == 0)
1118 ec
.assign(errno
, std::generic_category());
1125 fs::remove_all(const path
& p
)
1128 bool result
= remove_all(p
, ec
);
1130 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot remove all", p
, ec
));
1135 fs::remove_all(const path
& p
, error_code
& ec
) noexcept
1137 auto fs
= symlink_status(p
, ec
);
1138 uintmax_t count
= 0;
1139 if (ec
.value() == 0 && fs
.type() == file_type::directory
)
1140 for (directory_iterator
d(p
, ec
), end
; ec
.value() == 0 && d
!= end
; ++d
)
1141 count
+= fs::remove(d
->path(), ec
);
1144 return fs::remove(p
, ec
) ? ++count
: -1; // fs:remove() calls ec.clear()
1148 fs::rename(const path
& from
, const path
& to
)
1151 rename(from
, to
, ec
);
1153 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot rename", from
, to
, ec
));
1157 fs::rename(const path
& from
, const path
& to
, error_code
& ec
) noexcept
1159 if (::rename(from
.c_str(), to
.c_str()))
1160 ec
.assign(errno
, std::generic_category());
1166 fs::resize_file(const path
& p
, uintmax_t size
)
1169 resize_file(p
, size
, ec
);
1171 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot resize file", p
, ec
));
1175 fs::resize_file(const path
& p
, uintmax_t size
, error_code
& ec
) noexcept
1177 #ifdef _GLIBCXX_HAVE_UNISTD_H
1178 if (size
> static_cast<uintmax_t>(std::numeric_limits
<off_t
>::max()))
1179 ec
.assign(EINVAL
, std::generic_category());
1180 else if (::truncate(p
.c_str(), size
))
1181 ec
.assign(errno
, std::generic_category());
1185 ec
= std::make_error_code(std::errc::not_supported
);
1191 fs::space(const path
& p
)
1194 space_info s
= space(p
, ec
);
1196 _GLIBCXX_THROW_OR_ABORT(filesystem_error("cannot get free space", p
, ec
));
1201 fs::space(const path
& p
, error_code
& ec
) noexcept
1204 static_cast<uintmax_t>(-1),
1205 static_cast<uintmax_t>(-1),
1206 static_cast<uintmax_t>(-1)
1208 #ifdef _GLIBCXX_HAVE_SYS_STATVFS_H
1210 if (::statvfs(p
.c_str(), &f
))
1211 ec
.assign(errno
, std::generic_category());
1215 f
.f_blocks
* f
.f_frsize
,
1216 f
.f_bfree
* f
.f_frsize
,
1217 f
.f_bavail
* f
.f_frsize
1222 ec
= std::make_error_code(std::errc::not_supported
);
1227 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
1229 fs::status(const fs::path
& p
, std::error_code
& ec
) noexcept
1233 if (::stat(p
.c_str(), &st
))
1236 ec
.assign(err
, std::generic_category());
1237 if (is_not_found_errno(err
))
1238 status
.type(file_type::not_found
);
1242 status
= make_file_status(st
);
1249 fs::symlink_status(const fs::path
& p
, std::error_code
& ec
) noexcept
1253 if (::lstat(p
.c_str(), &st
))
1256 ec
.assign(err
, std::generic_category());
1257 if (is_not_found_errno(err
))
1258 status
.type(file_type::not_found
);
1262 status
= make_file_status(st
);
1270 fs::status(const fs::path
& p
)
1273 auto result
= status(p
, ec
);
1274 if (result
.type() == file_type::none
)
1275 _GLIBCXX_THROW_OR_ABORT(filesystem_error("status", p
, ec
));
1280 fs::symlink_status(const fs::path
& p
)
1283 auto result
= symlink_status(p
, ec
);
1284 if (result
.type() == file_type::none
)
1285 _GLIBCXX_THROW_OR_ABORT(filesystem_error("symlink_status", p
, ec
));
1290 fs::system_complete(const path
& p
)
1293 path comp
= system_complete(p
, ec
);
1295 _GLIBCXX_THROW_OR_ABORT(filesystem_error("system_complete", p
, ec
));
1300 fs::system_complete(const path
& p
, error_code
& ec
)
1302 path base
= current_path(ec
);
1303 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1304 if (p
.is_absolute() || !p
.has_root_name()
1305 || p
.root_name() == base
.root_name())
1306 return absolute(p
, base
);
1308 ec
= std::make_error_code(std::errc::not_supported
);
1313 return absolute(p
, base
);
1317 fs::path
fs::temp_directory_path()
1320 path tmp
= temp_directory_path(ec
);
1322 _GLIBCXX_THROW_OR_ABORT(filesystem_error("temp_directory_path", ec
));
1326 fs::path
fs::temp_directory_path(error_code
& ec
)
1328 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
1329 ec
= std::make_error_code(std::errc::not_supported
);
1332 const char* tmpdir
= nullptr;
1333 const char* env
[] = { "TMPDIR", "TMP", "TEMP", "TEMPDIR", nullptr };
1334 for (auto e
= env
; tmpdir
== nullptr && *e
!= nullptr; ++e
)
1335 tmpdir
= ::getenv(*e
);
1336 path p
= tmpdir
? tmpdir
: "/tmp";
1337 if (exists(p
) && is_directory(p
))
1342 ec
= std::make_error_code(std::errc::not_a_directory
);