Debian Codegraph @

libstdc++-v3/src/filesystem/ops-common.h

raw
1 // Filesystem operation utilities -*- C++ -*-
2
3 // Copyright (C) 2014-2025 Free Software Foundation, Inc.
4 //
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)
9 // any later version.
10
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.
15
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.
19
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/>.
24
25 #ifndef _GLIBCXX_OPS_COMMON_H
26 #define _GLIBCXX_OPS_COMMON_H 1
27
28 #include <chrono>
29 #include <bits/move.h> // std::__exchange
30
31 #ifdef _GLIBCXX_HAVE_UNISTD_H
32 # include <unistd.h>
33 # ifdef _GLIBCXX_HAVE_FCNTL_H
34 # include <fcntl.h> // AT_FDCWD, O_TRUNC etc.
35 # endif
36 # if defined(_GLIBCXX_HAVE_SYS_STAT_H) && defined(_GLIBCXX_HAVE_SYS_TYPES_H)
37 # include <sys/types.h>
38 # include <sys/stat.h> // mkdir, chmod
39 # endif
40 #endif
41 #if !_GLIBCXX_USE_UTIMENSAT && _GLIBCXX_HAVE_UTIME_H
42 # include <utime.h> // utime
43 #endif
44
45 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
46 # include <wchar.h>
47 #endif
48
49 #ifdef NEED_DO_COPY_FILE
50 # include <filesystem>
51 # include <ext/stdio_filebuf.h>
52 # ifdef _GLIBCXX_USE_COPY_FILE_RANGE
53 # include <unistd.h> // copy_file_range
54 # endif
55 # ifdef _GLIBCXX_USE_SENDFILE
56 # include <sys/sendfile.h> // sendfile
57 # include <unistd.h> // lseek
58 # endif
59 #endif
60
61 namespace std _GLIBCXX_VISIBILITY(default)
62 {
63 _GLIBCXX_BEGIN_NAMESPACE_VERSION
64
65 // Get the last OS error (for POSIX this is just errno).
66 inline error_code
67 __last_system_error() noexcept
68 {
69 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
70 // N.B. use error_code::default_error_condition() to convert to generic.
71 return {(int)::GetLastError(), std::system_category()};
72 #else
73 return {errno, std::generic_category()};
74 #endif
75 }
76
77 // Get an error code indicating unsupported functionality.
78 //
79 // This should be used when a function is unable to behave as specified
80 // due to an incomplete or partial implementation, e.g.
81 // filesystem::equivalent(a, b) if is_other(a) && is_other(b) is true.
82 //
83 // Use errc::function_not_supported for functions that are entirely
84 // unimplemented, e.g. create_symlink on Windows.
85 //
86 // Use errc::invalid_argument for requests to perform operations outside
87 // the spec, e.g. trying to copy a directory using filesystem::copy_file.
88 inline error_code
89 __unsupported() noexcept
90 {
91 #if defined __AVR__
92 // avr-libc defines ENOTSUP and EOPNOTSUPP but with nonsense values.
93 // ENOSYS is defined though, so use an error_code corresponding to that.
94 // This contradicts the comment above, but we don't have much choice.
95 return std::make_error_code(std::errc::function_not_supported);
96 #elif defined ENOTSUP
97 return std::make_error_code(std::errc::not_supported);
98 #elif defined EOPNOTSUPP
99 // This is supposed to be for socket operations
100 return std::make_error_code(std::errc::operation_not_supported);
101 #else
102 return std::make_error_code(std::errc::invalid_argument);
103 #endif
104 }
105
106 namespace filesystem
107 {
108 namespace __gnu_posix
109 {
110 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
111 // Adapt the Windows _wxxx functions to look like POSIX xxx, but for wchar_t*.
112 inline int open(const wchar_t* path, int flags)
113 { return ::_wopen(path, flags); }
114
115 inline int open(const wchar_t* path, int flags, int mode)
116 { return ::_wopen(path, flags, mode); }
117
118 inline int close(int fd)
119 { return ::_close(fd); }
120
121 using stat_type = struct ::__stat64;
122
123 inline int stat(const wchar_t* path, stat_type* buffer)
124 { return ::_wstat64(path, buffer); }
125
126 inline int lstat(const wchar_t* path, stat_type* buffer)
127 {
128 // FIXME: symlinks not currently supported
129 return stat(path, buffer);
130 }
131
132 using ::mode_t;
133
134 inline int chmod(const wchar_t* path, mode_t mode)
135 { return ::_wchmod(path, mode); }
136 #define _GLIBCXX_USE_CHMOD 1
137
138 inline int mkdir(const wchar_t* path, mode_t)
139 { return ::_wmkdir(path); }
140 #define _GLIBCXX_USE_MKDIR 1
141
142 inline wchar_t* getcwd(wchar_t* buf, size_t size)
143 { return ::_wgetcwd(buf, size > (size_t)INT_MAX ? INT_MAX : (int)size); }
144 #define _GLIBCXX_USE_GETCWD 1
145
146 inline int chdir(const wchar_t* path)
147 { return ::_wchdir(path); }
148 #define _GLIBCXX_USE_CHDIR 1
149
150 #if !_GLIBCXX_USE_UTIMENSAT && _GLIBCXX_HAVE_UTIME_H
151 using utimbuf = _utimbuf;
152
153 inline int utime(const wchar_t* path, utimbuf* times)
154 { return ::_wutime(path, times); }
155 #endif
156
157 inline int rename(const wchar_t* oldname, const wchar_t* newname)
158 {
159 if (MoveFileExW(oldname, newname,
160 MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED))
161 return 0;
162 errno = std::__last_system_error().default_error_condition().value();
163 return -1;
164 }
165
166 using off_t = _off64_t;
167 inline int truncate(const wchar_t* path, _off64_t length)
168 {
169 const int fd = ::_wopen(path, _O_BINARY|_O_RDWR);
170 if (fd == -1)
171 return fd;
172 const int ret = ::ftruncate64(fd, length);
173 int err;
174 ::_get_errno(&err);
175 ::_close(fd);
176 ::_set_errno(err);
177 return ret;
178 }
179 using char_type = wchar_t;
180 #elif defined _GLIBCXX_HAVE_UNISTD_H && ! defined __AVR__
181 using ::open;
182 using ::close;
183 # ifdef _GLIBCXX_HAVE_SYS_STAT_H
184 using stat_type = struct ::stat;
185 using ::stat;
186 # ifdef _GLIBCXX_USE_LSTAT
187 using ::lstat;
188 # else
189 inline int lstat(const char* path, stat_type* buffer)
190 { return stat(path, buffer); }
191 # endif
192 # endif
193 using ::mode_t;
194 # if _GLIBCXX_USE_CHMOD
195 using ::chmod;
196 # endif
197 # if _GLIBCXX_USE_MKDIR
198 using ::mkdir;
199 # endif
200 # if _GLIBCXX_USE_GETCWD
201 using ::getcwd;
202 # endif
203 # if _GLIBCXX_USE_CHDIR
204 using ::chdir;
205 # endif
206 # if !_GLIBCXX_USE_UTIMENSAT && _GLIBCXX_USE_UTIME
207 using ::utimbuf;
208 using ::utime;
209 # endif
210 using ::rename;
211 using ::off_t;
212 # ifdef _GLIBCXX_HAVE_TRUNCATE
213 using ::truncate;
214 # else
215 inline int truncate(const char* path, off_t length)
216 {
217 if (length == 0)
218 {
219 const int fd = ::open(path, O_WRONLY|O_TRUNC);
220 if (fd == -1)
221 return fd;
222 ::close(fd);
223 return 0;
224 }
225 errno = ENOTSUP;
226 return -1;
227 }
228 # endif
229 using char_type = char;
230 #else // ! _GLIBCXX_FILESYSTEM_IS_WINDOWS && ! _GLIBCXX_HAVE_UNISTD_H
231 inline int open(const char*, int, ...) { errno = ENOSYS; return -1; }
232 inline int close(int) { errno = ENOSYS; return -1; }
233 using mode_t = int;
234 inline int chmod(const char*, mode_t) { errno = ENOSYS; return -1; }
235 inline int mkdir(const char*, mode_t) { errno = ENOSYS; return -1; }
236 inline char* getcwd(char*, size_t) { errno = ENOSYS; return nullptr; }
237 inline int chdir(const char*) { errno = ENOSYS; return -1; }
238 inline int rename(const char*, const char*) { errno = ENOSYS; return -1; }
239 using off_t = long;
240 inline int truncate(const char*, off_t) { errno = ENOSYS; return -1; }
241 using char_type = char;
242 #endif // _GLIBCXX_FILESYSTEM_IS_WINDOWS
243 } // namespace __gnu_posix
244
245 template<typename Bitmask>
246 inline bool is_set(Bitmask obj, Bitmask bits)
247 {
248 return (obj & bits) != Bitmask::none;
249 }
250
251 inline bool
252 is_not_found_errno(int err) noexcept
253 {
254 return err == ENOENT || err == ENOTDIR;
255 }
256
257 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
258 using __gnu_posix::stat_type;
259
260 inline std::chrono::system_clock::time_point
261 file_time(const stat_type& st, std::error_code& ec) noexcept
262 {
263 using namespace std::chrono;
264 #ifdef _GLIBCXX_USE_ST_MTIM
265 time_t s = st.st_mtim.tv_sec;
266 nanoseconds ns{st.st_mtim.tv_nsec};
267 #else
268 time_t s = st.st_mtime;
269 nanoseconds ns{};
270 #endif
271
272 // FIXME
273 // There are possible timespec values which will overflow
274 // chrono::system_clock::time_point but would not overflow
275 // __file_clock::time_point, due to its different epoch.
276 //
277 // By checking for overflow of the intermediate system_clock::duration
278 // type, we report an error for values which are actually representable
279 // in the file_time_type result type.
280 //
281 // Howard Hinnant's solution for this problem is to use
282 // duration<__int128>{s} + ns, which doesn't overflow.
283 // An alternative would be to do the epoch correction on s before
284 // the addition, and then go straight to file_time_type instead of
285 // going via chrono::system_clock::time_point.
286 //
287 // (This only applies to the C++17 Filesystem library, because for the
288 // Filesystem TS we don't have a distinct __file_clock, we just use the
289 // system clock for file timestamps).
290 if (seconds{s} >= floor<seconds>(system_clock::duration::max()))
291 {
292 ec = std::make_error_code(std::errc::value_too_large); // EOVERFLOW
293 return system_clock::time_point::min();
294 }
295 ec.clear();
296 return system_clock::time_point{seconds{s} + ns};
297 }
298
299 struct copy_options_existing_file
300 {
301 bool skip, update, overwrite;
302 };
303 #endif // _GLIBCXX_HAVE_SYS_STAT_H
304
305 } // namespace filesystem
306
307 // BEGIN/END macros must be defined before including this file.
308 _GLIBCXX_BEGIN_NAMESPACE_FILESYSTEM
309
310 #ifdef _GLIBCXX_HAVE_SYS_STAT_H
311 using std::filesystem::__gnu_posix::stat_type;
312 using std::filesystem::__gnu_posix::char_type;
313
314 bool
315 do_copy_file(const char_type* from, const char_type* to,
316 std::filesystem::copy_options_existing_file options,
317 stat_type* from_st, stat_type* to_st,
318 std::error_code& ec) noexcept;
319
320 void
321 do_space(const char_type* pathname,
322 uintmax_t& capacity, uintmax_t& free, uintmax_t& available,
323 std::error_code&);
324
325
326 // Test whether two files are the same file.
327 bool
328 equiv_files(const char_type*, const stat_type&,
329 const char_type*, const stat_type&,
330 error_code&);
331
332 inline file_type
333 make_file_type(const stat_type& st) noexcept
334 {
335 #ifdef _GLIBCXX_HAVE_S_ISREG
336 if (S_ISREG(st.st_mode))
337 return file_type::regular;
338 else if (S_ISDIR(st.st_mode))
339 return file_type::directory;
340 else if (S_ISCHR(st.st_mode))
341 return file_type::character;
342 else if (S_ISBLK(st.st_mode))
343 return file_type::block;
344 else if (S_ISFIFO(st.st_mode))
345 return file_type::fifo;
346 #ifdef S_ISLNK // not present in mingw
347 else if (S_ISLNK(st.st_mode))
348 return file_type::symlink;
349 #endif
350 #ifdef S_ISSOCK // not present until POSIX:2001
351 else if (S_ISSOCK(st.st_mode))
352 return file_type::socket;
353 #endif
354 #endif
355 return file_type::unknown;
356 }
357
358 inline file_status
359 make_file_status(const stat_type& st) noexcept
360 {
361 return file_status{
362 make_file_type(st),
363 static_cast<perms>(st.st_mode) & perms::mask
364 };
365 }
366
367 inline std::filesystem::copy_options_existing_file
368 copy_file_options(copy_options opt)
369 {
370 using std::filesystem::is_set;
371 return {
372 is_set(opt, copy_options::skip_existing),
373 is_set(opt, copy_options::update_existing),
374 is_set(opt, copy_options::overwrite_existing)
375 };
376 }
377
378 #ifdef NEED_DO_COPY_FILE
379 #ifdef _GLIBCXX_USE_COPY_FILE_RANGE
380 bool
381 copy_file_copy_file_range(int fd_in, int fd_out, size_t length) noexcept
382 {
383 // a zero-length file is either empty, or not copyable by this syscall
384 // return early to avoid the syscall cost
385 if (length == 0)
386 {
387 errno = EINVAL;
388 return false;
389 }
390 size_t bytes_left = length;
391 loff_t off_in = 0, off_out = 0;
392 ssize_t bytes_copied;
393 do
394 {
395 bytes_copied = ::copy_file_range(fd_in, &off_in, fd_out, &off_out,
396 bytes_left, 0);
397 bytes_left -= bytes_copied;
398 }
399 while (bytes_left > 0 && bytes_copied > 0);
400 if (bytes_copied < 0)
401 return false;
402 return true;
403 }
404 #endif
405
406 #if defined _GLIBCXX_USE_SENDFILE && ! defined _GLIBCXX_FILESYSTEM_IS_WINDOWS
407 bool
408 copy_file_sendfile(int fd_in, int fd_out, size_t length) noexcept
409 {
410 // a zero-length file is either empty, or not copyable by this syscall
411 // return early to avoid the syscall cost
412 if (length == 0)
413 {
414 errno = EINVAL;
415 return false;
416 }
417 size_t bytes_left = length;
418 off_t offset = 0;
419 ssize_t bytes_copied;
420 do
421 {
422 bytes_copied = ::sendfile(fd_out, fd_in, &offset, bytes_left);
423 bytes_left -= bytes_copied;
424 }
425 while (bytes_left > 0 && bytes_copied > 0);
426 if (bytes_copied < 0)
427 {
428 ::lseek(fd_out, 0, SEEK_SET);
429 return false;
430 }
431 return true;
432 }
433 #endif
434
435 bool
436 do_copy_file(const char_type* from, const char_type* to,
437 std::filesystem::copy_options_existing_file options,
438 stat_type* from_st, stat_type* to_st,
439 std::error_code& ec) noexcept
440 {
441 namespace fs = std::filesystem;
442 namespace posix = fs::__gnu_posix;
443
444 stat_type st1, st2;
445 file_status t, f;
446
447 if (to_st == nullptr)
448 {
449 if (posix::stat(to, &st1))
450 {
451 const int err = errno;
452 if (!fs::is_not_found_errno(err))
453 {
454 ec.assign(err, std::generic_category());
455 return false;
456 }
457 }
458 else
459 to_st = &st1;
460 }
461 else if (to_st == from_st)
462 to_st = nullptr;
463
464 if (to_st == nullptr)
465 t = file_status{file_type::not_found};
466 else
467 t = make_file_status(*to_st);
468
469 if (from_st == nullptr)
470 {
471 if (posix::stat(from, &st2))
472 {
473 ec.assign(errno, std::generic_category());
474 return false;
475 }
476 else
477 from_st = &st2;
478 }
479 f = make_file_status(*from_st);
480 // _GLIBCXX_RESOLVE_LIB_DEFECTS
481 // 2712. copy_file() has a number of unspecified error conditions
482 if (!is_regular_file(f))
483 {
484 ec = std::make_error_code(std::errc::invalid_argument);
485 return false;
486 }
487
488 if (exists(t))
489 {
490 if (!is_regular_file(t))
491 {
492 ec = std::make_error_code(std::errc::invalid_argument);
493 return false;
494 }
495
496 if (equiv_files(from, *from_st, to, *to_st, ec))
497 {
498 ec = std::make_error_code(std::errc::file_exists);
499 return false;
500 }
501
502 if (options.skip)
503 {
504 ec.clear();
505 return false;
506 }
507 else if (options.update)
508 {
509 const auto from_mtime = fs::file_time(*from_st, ec);
510 if (ec)
511 return false;
512 if ((from_mtime <= fs::file_time(*to_st, ec)) || ec)
513 return false;
514 }
515 else if (!options.overwrite)
516 {
517 ec = std::make_error_code(std::errc::file_exists);
518 return false;
519 }
520 else if (!is_regular_file(t))
521 {
522 ec = std::make_error_code(std::errc::invalid_argument);
523 return false;
524 }
525 }
526
527 struct CloseFD {
528 ~CloseFD() { if (fd != -1) posix::close(fd); }
529 bool close() { return posix::close(std::__exchange(fd, -1)) == 0; }
530 int fd;
531 };
532
533 int common_flags = 0;
534 #ifdef O_CLOEXEC
535 common_flags |= O_CLOEXEC;
536 #endif
537 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
538 common_flags |= O_BINARY;
539 #endif
540
541 const int iflag = O_RDONLY | common_flags;
542 CloseFD in = { posix::open(from, iflag) };
543 if (in.fd == -1)
544 {
545 ec.assign(errno, std::generic_category());
546 return false;
547 }
548 int oflag = O_WRONLY | O_CREAT | common_flags;
549 if (options.overwrite || options.update)
550 oflag |= O_TRUNC;
551 else
552 oflag |= O_EXCL;
553 CloseFD out = { posix::open(to, oflag, S_IWUSR) };
554 if (out.fd == -1)
555 {
556 if (errno == EEXIST && options.skip)
557 ec.clear();
558 else
559 ec.assign(errno, std::generic_category());
560 return false;
561 }
562
563 #if defined _GLIBCXX_USE_FCHMOD && ! defined _GLIBCXX_FILESYSTEM_IS_WINDOWS
564 if (::fchmod(out.fd, from_st->st_mode))
565 #elif defined _GLIBCXX_USE_FCHMODAT && ! defined _GLIBCXX_FILESYSTEM_IS_WINDOWS
566 if (::fchmodat(AT_FDCWD, to, from_st->st_mode, 0))
567 #elif defined _GLIBCXX_USE_CHMOD
568 if (posix::chmod(to, from_st->st_mode))
569 #else
570 if (false)
571 #endif
572 {
573 ec.assign(errno, std::generic_category());
574 return false;
575 }
576
577 bool has_copied = false;
578
579 #ifdef _GLIBCXX_USE_COPY_FILE_RANGE
580 if (!has_copied)
581 has_copied = copy_file_copy_file_range(in.fd, out.fd, from_st->st_size);
582 if (!has_copied)
583 {
584 // EINVAL: src and dst are the same file (this is not cheaply
585 // detectable from userspace)
586 // EINVAL: copy_file_range is unsupported for this file type by the
587 // underlying filesystem
588 // ENOTSUP: undocumented, can arise with old kernels and NFS
589 // EOPNOTSUPP: filesystem does not implement copy_file_range
590 // ETXTBSY: src or dst is an active swapfile (nonsensical, but allowed
591 // with normal copying)
592 // EXDEV: src and dst are on different filesystems that do not support
593 // cross-fs copy_file_range
594 // ENOENT: undocumented, can arise with CIFS
595 // ENOSYS: unsupported by kernel or blocked by seccomp
596 if (errno != EINVAL && errno != ENOTSUP && errno != EOPNOTSUPP
597 && errno != ETXTBSY && errno != EXDEV && errno != ENOENT
598 && errno != ENOSYS)
599 {
600 ec.assign(errno, std::generic_category());
601 return false;
602 }
603 }
604 #endif
605
606 #if defined _GLIBCXX_USE_SENDFILE && ! defined _GLIBCXX_FILESYSTEM_IS_WINDOWS
607 if (!has_copied)
608 has_copied = copy_file_sendfile(in.fd, out.fd, from_st->st_size);
609 if (!has_copied)
610 {
611 if (errno != ENOSYS && errno != EINVAL)
612 {
613 ec.assign(errno, std::generic_category());
614 return false;
615 }
616 }
617 #endif
618
619 if (has_copied)
620 {
621 if (!out.close() || !in.close())
622 {
623 ec.assign(errno, std::generic_category());
624 return false;
625 }
626 ec.clear();
627 return true;
628 }
629
630 using std::ios;
631 __gnu_cxx::stdio_filebuf<char> sbin(in.fd, ios::in|ios::binary);
632 __gnu_cxx::stdio_filebuf<char> sbout(out.fd, ios::out|ios::binary);
633
634 if (sbin.is_open())
635 in.fd = -1;
636 if (sbout.is_open())
637 out.fd = -1;
638
639 // ostream::operator<<(streambuf*) fails if it extracts no characters,
640 // so don't try to use it for empty files. But from_st->st_size == 0 for
641 // some special files (e.g. procfs, see PR libstdc++/108178) so just try
642 // to read a character to decide whether there is anything to copy or not.
643 if (sbin.sgetc() != char_traits<char>::eof())
644 if (!(std::ostream(&sbout) << &sbin))
645 {
646 ec = std::make_error_code(std::errc::io_error);
647 return false;
648 }
649
650 if (!sbout.close() || !sbin.close())
651 {
652 ec.assign(errno, std::generic_category());
653 return false;
654 }
655 ec.clear();
656 return true;
657 }
658 #endif // NEED_DO_COPY_FILE
659
660 #ifdef NEED_DO_SPACE
661 #pragma GCC diagnostic push
662 #pragma GCC diagnostic ignored "-Wunused-parameter"
663 void
664 do_space(const char_type* pathname,
665 uintmax_t& capacity, uintmax_t& free, uintmax_t& available,
666 std::error_code& ec)
667 {
668 #ifdef _GLIBCXX_HAVE_SYS_STATVFS_H
669 struct ::statvfs f;
670 if (::statvfs(pathname, &f))
671 ec.assign(errno, std::generic_category());
672 else
673 {
674 if (f.f_frsize != (unsigned long)-1)
675 {
676 const uintmax_t fragment_size = f.f_frsize;
677 const fsblkcnt_t unknown = -1;
678 if (f.f_blocks != unknown)
679 capacity = f.f_blocks * fragment_size;
680 if (f.f_bfree != unknown)
681 free = f.f_bfree * fragment_size;
682 if (f.f_bavail != unknown)
683 available = f.f_bavail * fragment_size;
684 }
685 ec.clear();
686 }
687 #elif _GLIBCXX_FILESYSTEM_IS_WINDOWS
688 ULARGE_INTEGER bytes_avail = {}, bytes_total = {}, bytes_free = {};
689 if (GetDiskFreeSpaceExW(pathname, &bytes_avail, &bytes_total, &bytes_free))
690 {
691 if (bytes_total.QuadPart != 0)
692 capacity = bytes_total.QuadPart;
693 if (bytes_free.QuadPart != 0)
694 free = bytes_free.QuadPart;
695 if (bytes_avail.QuadPart != 0)
696 available = bytes_avail.QuadPart;
697 ec.clear();
698 }
699 else
700 ec = std::__last_system_error();
701 #else
702 ec = std::make_error_code(std::errc::function_not_supported);
703 #endif
704 }
705 #pragma GCC diagnostic pop
706 #endif // NEED_DO_SPACE
707
708 #endif // _GLIBCXX_HAVE_SYS_STAT_H
709
710 // Find OS-specific name of temporary directory from the environment,
711 // Caller must check that the path is an accessible directory.
712 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
713 inline wstring
714 get_temp_directory_from_env(error_code& ec)
715 {
716 unsigned len = 1024;
717 std::wstring buf;
718 do
719 {
720 buf.__resize_and_overwrite(len, [&len](wchar_t* p, unsigned n) {
721 len = GetTempPathW(n, p);
722 return len > n ? 0 : len;
723 });
724 }
725 while (len > buf.size());
726
727 if (len == 0)
728 ec = __last_system_error();
729 else
730 ec.clear();
731
732 return buf;
733 }
734 #else
735 inline const char*
736 get_temp_directory_from_env(error_code& ec) noexcept
737 {
738 ec.clear();
739 for (auto env : { "TMPDIR", "TMP", "TEMP", "TEMPDIR" })
740 {
741 #if _GLIBCXX_HAVE_SECURE_GETENV
742 auto tmpdir = ::secure_getenv(env);
743 #else
744 auto tmpdir = ::getenv(env);
745 #endif
746 if (tmpdir)
747 return tmpdir;
748 }
749 return "/tmp";
750 }
751 #endif
752
753 _GLIBCXX_END_NAMESPACE_FILESYSTEM
754
755 _GLIBCXX_END_NAMESPACE_VERSION
756 } // namespace std
757
758 #endif // _GLIBCXX_OPS_COMMON_H