xref: /freebsd-src/contrib/llvm-project/libcxx/src/filesystem/posix_compat.h (revision fe6060f10f634930ff71b7c50291ddc610da2475)
1*fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
2*fe6060f1SDimitry Andric //
3*fe6060f1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*fe6060f1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*fe6060f1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*fe6060f1SDimitry Andric //
7*fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
8*fe6060f1SDimitry Andric 
9*fe6060f1SDimitry Andric //
10*fe6060f1SDimitry Andric // POSIX-like portability helper functions.
11*fe6060f1SDimitry Andric //
12*fe6060f1SDimitry Andric // These generally behave like the proper posix functions, with these
13*fe6060f1SDimitry Andric // exceptions:
14*fe6060f1SDimitry Andric // On Windows, they take paths in wchar_t* form, instead of char* form.
15*fe6060f1SDimitry Andric // The symlink() function is split into two frontends, symlink_file()
16*fe6060f1SDimitry Andric // and symlink_dir().
17*fe6060f1SDimitry Andric //
18*fe6060f1SDimitry Andric // These are provided within an anonymous namespace within the detail
19*fe6060f1SDimitry Andric // namespace - callers need to include this header and call them as
20*fe6060f1SDimitry Andric // detail::function(), regardless of platform.
21*fe6060f1SDimitry Andric //
22*fe6060f1SDimitry Andric 
23*fe6060f1SDimitry Andric #ifndef POSIX_COMPAT_H
24*fe6060f1SDimitry Andric #define POSIX_COMPAT_H
25*fe6060f1SDimitry Andric 
26*fe6060f1SDimitry Andric #include "filesystem"
27*fe6060f1SDimitry Andric 
28*fe6060f1SDimitry Andric #include "filesystem_common.h"
29*fe6060f1SDimitry Andric 
30*fe6060f1SDimitry Andric #if defined(_LIBCPP_WIN32API)
31*fe6060f1SDimitry Andric # define WIN32_LEAN_AND_MEAN
32*fe6060f1SDimitry Andric # define NOMINMAX
33*fe6060f1SDimitry Andric # include <windows.h>
34*fe6060f1SDimitry Andric # include <io.h>
35*fe6060f1SDimitry Andric # include <winioctl.h>
36*fe6060f1SDimitry Andric #else
37*fe6060f1SDimitry Andric # include <unistd.h>
38*fe6060f1SDimitry Andric # include <sys/stat.h>
39*fe6060f1SDimitry Andric # include <sys/statvfs.h>
40*fe6060f1SDimitry Andric #endif
41*fe6060f1SDimitry Andric #include <time.h>
42*fe6060f1SDimitry Andric 
43*fe6060f1SDimitry Andric #if defined(_LIBCPP_WIN32API)
44*fe6060f1SDimitry Andric // This struct isn't defined in the normal Windows SDK, but only in the
45*fe6060f1SDimitry Andric // Windows Driver Kit.
46*fe6060f1SDimitry Andric struct LIBCPP_REPARSE_DATA_BUFFER {
47*fe6060f1SDimitry Andric   unsigned long  ReparseTag;
48*fe6060f1SDimitry Andric   unsigned short ReparseDataLength;
49*fe6060f1SDimitry Andric   unsigned short Reserved;
50*fe6060f1SDimitry Andric   union {
51*fe6060f1SDimitry Andric     struct {
52*fe6060f1SDimitry Andric       unsigned short SubstituteNameOffset;
53*fe6060f1SDimitry Andric       unsigned short SubstituteNameLength;
54*fe6060f1SDimitry Andric       unsigned short PrintNameOffset;
55*fe6060f1SDimitry Andric       unsigned short PrintNameLength;
56*fe6060f1SDimitry Andric       unsigned long  Flags;
57*fe6060f1SDimitry Andric       wchar_t        PathBuffer[1];
58*fe6060f1SDimitry Andric     } SymbolicLinkReparseBuffer;
59*fe6060f1SDimitry Andric     struct {
60*fe6060f1SDimitry Andric       unsigned short SubstituteNameOffset;
61*fe6060f1SDimitry Andric       unsigned short SubstituteNameLength;
62*fe6060f1SDimitry Andric       unsigned short PrintNameOffset;
63*fe6060f1SDimitry Andric       unsigned short PrintNameLength;
64*fe6060f1SDimitry Andric       wchar_t        PathBuffer[1];
65*fe6060f1SDimitry Andric     } MountPointReparseBuffer;
66*fe6060f1SDimitry Andric     struct {
67*fe6060f1SDimitry Andric       unsigned char DataBuffer[1];
68*fe6060f1SDimitry Andric     } GenericReparseBuffer;
69*fe6060f1SDimitry Andric   };
70*fe6060f1SDimitry Andric };
71*fe6060f1SDimitry Andric #endif
72*fe6060f1SDimitry Andric 
73*fe6060f1SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_FILESYSTEM
74*fe6060f1SDimitry Andric 
75*fe6060f1SDimitry Andric namespace detail {
76*fe6060f1SDimitry Andric namespace {
77*fe6060f1SDimitry Andric 
78*fe6060f1SDimitry Andric #if defined(_LIBCPP_WIN32API)
79*fe6060f1SDimitry Andric 
80*fe6060f1SDimitry Andric // Various C runtime header sets provide more or less of these. As we
81*fe6060f1SDimitry Andric // provide our own implementation, undef all potential defines from the
82*fe6060f1SDimitry Andric // C runtime headers and provide a complete set of macros of our own.
83*fe6060f1SDimitry Andric 
84*fe6060f1SDimitry Andric #undef _S_IFMT
85*fe6060f1SDimitry Andric #undef _S_IFDIR
86*fe6060f1SDimitry Andric #undef _S_IFCHR
87*fe6060f1SDimitry Andric #undef _S_IFIFO
88*fe6060f1SDimitry Andric #undef _S_IFREG
89*fe6060f1SDimitry Andric #undef _S_IFBLK
90*fe6060f1SDimitry Andric #undef _S_IFLNK
91*fe6060f1SDimitry Andric #undef _S_IFSOCK
92*fe6060f1SDimitry Andric 
93*fe6060f1SDimitry Andric #define _S_IFMT   0xF000
94*fe6060f1SDimitry Andric #define _S_IFDIR  0x4000
95*fe6060f1SDimitry Andric #define _S_IFCHR  0x2000
96*fe6060f1SDimitry Andric #define _S_IFIFO  0x1000
97*fe6060f1SDimitry Andric #define _S_IFREG  0x8000
98*fe6060f1SDimitry Andric #define _S_IFBLK  0x6000
99*fe6060f1SDimitry Andric #define _S_IFLNK  0xA000
100*fe6060f1SDimitry Andric #define _S_IFSOCK 0xC000
101*fe6060f1SDimitry Andric 
102*fe6060f1SDimitry Andric #undef S_ISDIR
103*fe6060f1SDimitry Andric #undef S_ISFIFO
104*fe6060f1SDimitry Andric #undef S_ISCHR
105*fe6060f1SDimitry Andric #undef S_ISREG
106*fe6060f1SDimitry Andric #undef S_ISLNK
107*fe6060f1SDimitry Andric #undef S_ISBLK
108*fe6060f1SDimitry Andric #undef S_ISSOCK
109*fe6060f1SDimitry Andric 
110*fe6060f1SDimitry Andric #define S_ISDIR(m)      (((m) & _S_IFMT) == _S_IFDIR)
111*fe6060f1SDimitry Andric #define S_ISCHR(m)      (((m) & _S_IFMT) == _S_IFCHR)
112*fe6060f1SDimitry Andric #define S_ISFIFO(m)     (((m) & _S_IFMT) == _S_IFIFO)
113*fe6060f1SDimitry Andric #define S_ISREG(m)      (((m) & _S_IFMT) == _S_IFREG)
114*fe6060f1SDimitry Andric #define S_ISBLK(m)      (((m) & _S_IFMT) == _S_IFBLK)
115*fe6060f1SDimitry Andric #define S_ISLNK(m)      (((m) & _S_IFMT) == _S_IFLNK)
116*fe6060f1SDimitry Andric #define S_ISSOCK(m)     (((m) & _S_IFMT) == _S_IFSOCK)
117*fe6060f1SDimitry Andric 
118*fe6060f1SDimitry Andric #define O_NONBLOCK 0
119*fe6060f1SDimitry Andric 
120*fe6060f1SDimitry Andric 
121*fe6060f1SDimitry Andric // There were 369 years and 89 leap days from the Windows epoch
122*fe6060f1SDimitry Andric // (1601) to the Unix epoch (1970).
123*fe6060f1SDimitry Andric #define FILE_TIME_OFFSET_SECS (uint64_t(369 * 365 + 89) * (24 * 60 * 60))
124*fe6060f1SDimitry Andric 
125*fe6060f1SDimitry Andric TimeSpec filetime_to_timespec(LARGE_INTEGER li) {
126*fe6060f1SDimitry Andric   TimeSpec ret;
127*fe6060f1SDimitry Andric   ret.tv_sec = li.QuadPart / 10000000 - FILE_TIME_OFFSET_SECS;
128*fe6060f1SDimitry Andric   ret.tv_nsec = (li.QuadPart % 10000000) * 100;
129*fe6060f1SDimitry Andric   return ret;
130*fe6060f1SDimitry Andric }
131*fe6060f1SDimitry Andric 
132*fe6060f1SDimitry Andric TimeSpec filetime_to_timespec(FILETIME ft) {
133*fe6060f1SDimitry Andric   LARGE_INTEGER li;
134*fe6060f1SDimitry Andric   li.LowPart = ft.dwLowDateTime;
135*fe6060f1SDimitry Andric   li.HighPart = ft.dwHighDateTime;
136*fe6060f1SDimitry Andric   return filetime_to_timespec(li);
137*fe6060f1SDimitry Andric }
138*fe6060f1SDimitry Andric 
139*fe6060f1SDimitry Andric FILETIME timespec_to_filetime(TimeSpec ts) {
140*fe6060f1SDimitry Andric   LARGE_INTEGER li;
141*fe6060f1SDimitry Andric   li.QuadPart =
142*fe6060f1SDimitry Andric       ts.tv_nsec / 100 + (ts.tv_sec + FILE_TIME_OFFSET_SECS) * 10000000;
143*fe6060f1SDimitry Andric   FILETIME ft;
144*fe6060f1SDimitry Andric   ft.dwLowDateTime = li.LowPart;
145*fe6060f1SDimitry Andric   ft.dwHighDateTime = li.HighPart;
146*fe6060f1SDimitry Andric   return ft;
147*fe6060f1SDimitry Andric }
148*fe6060f1SDimitry Andric 
149*fe6060f1SDimitry Andric int set_errno(int e = GetLastError()) {
150*fe6060f1SDimitry Andric   errno = static_cast<int>(__win_err_to_errc(e));
151*fe6060f1SDimitry Andric   return -1;
152*fe6060f1SDimitry Andric }
153*fe6060f1SDimitry Andric 
154*fe6060f1SDimitry Andric class WinHandle {
155*fe6060f1SDimitry Andric public:
156*fe6060f1SDimitry Andric   WinHandle(const wchar_t *p, DWORD access, DWORD flags) {
157*fe6060f1SDimitry Andric     h = CreateFileW(
158*fe6060f1SDimitry Andric         p, access, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
159*fe6060f1SDimitry Andric         nullptr, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS | flags, nullptr);
160*fe6060f1SDimitry Andric   }
161*fe6060f1SDimitry Andric   ~WinHandle() {
162*fe6060f1SDimitry Andric     if (h != INVALID_HANDLE_VALUE)
163*fe6060f1SDimitry Andric       CloseHandle(h);
164*fe6060f1SDimitry Andric   }
165*fe6060f1SDimitry Andric   operator HANDLE() const { return h; }
166*fe6060f1SDimitry Andric   operator bool() const { return h != INVALID_HANDLE_VALUE; }
167*fe6060f1SDimitry Andric 
168*fe6060f1SDimitry Andric private:
169*fe6060f1SDimitry Andric   HANDLE h;
170*fe6060f1SDimitry Andric };
171*fe6060f1SDimitry Andric 
172*fe6060f1SDimitry Andric int stat_handle(HANDLE h, StatT *buf) {
173*fe6060f1SDimitry Andric   FILE_BASIC_INFO basic;
174*fe6060f1SDimitry Andric   if (!GetFileInformationByHandleEx(h, FileBasicInfo, &basic, sizeof(basic)))
175*fe6060f1SDimitry Andric     return set_errno();
176*fe6060f1SDimitry Andric   memset(buf, 0, sizeof(*buf));
177*fe6060f1SDimitry Andric   buf->st_mtim = filetime_to_timespec(basic.LastWriteTime);
178*fe6060f1SDimitry Andric   buf->st_atim = filetime_to_timespec(basic.LastAccessTime);
179*fe6060f1SDimitry Andric   buf->st_mode = 0555; // Read-only
180*fe6060f1SDimitry Andric   if (!(basic.FileAttributes & FILE_ATTRIBUTE_READONLY))
181*fe6060f1SDimitry Andric     buf->st_mode |= 0222; // Write
182*fe6060f1SDimitry Andric   if (basic.FileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
183*fe6060f1SDimitry Andric     buf->st_mode |= _S_IFDIR;
184*fe6060f1SDimitry Andric   } else {
185*fe6060f1SDimitry Andric     buf->st_mode |= _S_IFREG;
186*fe6060f1SDimitry Andric   }
187*fe6060f1SDimitry Andric   if (basic.FileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
188*fe6060f1SDimitry Andric     FILE_ATTRIBUTE_TAG_INFO tag;
189*fe6060f1SDimitry Andric     if (!GetFileInformationByHandleEx(h, FileAttributeTagInfo, &tag,
190*fe6060f1SDimitry Andric                                       sizeof(tag)))
191*fe6060f1SDimitry Andric       return set_errno();
192*fe6060f1SDimitry Andric     if (tag.ReparseTag == IO_REPARSE_TAG_SYMLINK)
193*fe6060f1SDimitry Andric       buf->st_mode = (buf->st_mode & ~_S_IFMT) | _S_IFLNK;
194*fe6060f1SDimitry Andric   }
195*fe6060f1SDimitry Andric   FILE_STANDARD_INFO standard;
196*fe6060f1SDimitry Andric   if (!GetFileInformationByHandleEx(h, FileStandardInfo, &standard,
197*fe6060f1SDimitry Andric                                     sizeof(standard)))
198*fe6060f1SDimitry Andric     return set_errno();
199*fe6060f1SDimitry Andric   buf->st_nlink = standard.NumberOfLinks;
200*fe6060f1SDimitry Andric   buf->st_size = standard.EndOfFile.QuadPart;
201*fe6060f1SDimitry Andric   BY_HANDLE_FILE_INFORMATION info;
202*fe6060f1SDimitry Andric   if (!GetFileInformationByHandle(h, &info))
203*fe6060f1SDimitry Andric     return set_errno();
204*fe6060f1SDimitry Andric   buf->st_dev = info.dwVolumeSerialNumber;
205*fe6060f1SDimitry Andric   memcpy(&buf->st_ino.id[0], &info.nFileIndexHigh, 4);
206*fe6060f1SDimitry Andric   memcpy(&buf->st_ino.id[4], &info.nFileIndexLow, 4);
207*fe6060f1SDimitry Andric   return 0;
208*fe6060f1SDimitry Andric }
209*fe6060f1SDimitry Andric 
210*fe6060f1SDimitry Andric int stat_file(const wchar_t *path, StatT *buf, DWORD flags) {
211*fe6060f1SDimitry Andric   WinHandle h(path, FILE_READ_ATTRIBUTES, flags);
212*fe6060f1SDimitry Andric   if (!h)
213*fe6060f1SDimitry Andric     return set_errno();
214*fe6060f1SDimitry Andric   int ret = stat_handle(h, buf);
215*fe6060f1SDimitry Andric   return ret;
216*fe6060f1SDimitry Andric }
217*fe6060f1SDimitry Andric 
218*fe6060f1SDimitry Andric int stat(const wchar_t *path, StatT *buf) { return stat_file(path, buf, 0); }
219*fe6060f1SDimitry Andric 
220*fe6060f1SDimitry Andric int lstat(const wchar_t *path, StatT *buf) {
221*fe6060f1SDimitry Andric   return stat_file(path, buf, FILE_FLAG_OPEN_REPARSE_POINT);
222*fe6060f1SDimitry Andric }
223*fe6060f1SDimitry Andric 
224*fe6060f1SDimitry Andric int fstat(int fd, StatT *buf) {
225*fe6060f1SDimitry Andric   HANDLE h = reinterpret_cast<HANDLE>(_get_osfhandle(fd));
226*fe6060f1SDimitry Andric   return stat_handle(h, buf);
227*fe6060f1SDimitry Andric }
228*fe6060f1SDimitry Andric 
229*fe6060f1SDimitry Andric int mkdir(const wchar_t *path, int permissions) {
230*fe6060f1SDimitry Andric   (void)permissions;
231*fe6060f1SDimitry Andric   return _wmkdir(path);
232*fe6060f1SDimitry Andric }
233*fe6060f1SDimitry Andric 
234*fe6060f1SDimitry Andric int symlink_file_dir(const wchar_t *oldname, const wchar_t *newname,
235*fe6060f1SDimitry Andric                      bool is_dir) {
236*fe6060f1SDimitry Andric   path dest(oldname);
237*fe6060f1SDimitry Andric   dest.make_preferred();
238*fe6060f1SDimitry Andric   oldname = dest.c_str();
239*fe6060f1SDimitry Andric   DWORD flags = is_dir ? SYMBOLIC_LINK_FLAG_DIRECTORY : 0;
240*fe6060f1SDimitry Andric   if (CreateSymbolicLinkW(newname, oldname,
241*fe6060f1SDimitry Andric                           flags | SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE))
242*fe6060f1SDimitry Andric     return 0;
243*fe6060f1SDimitry Andric   int e = GetLastError();
244*fe6060f1SDimitry Andric   if (e != ERROR_INVALID_PARAMETER)
245*fe6060f1SDimitry Andric     return set_errno(e);
246*fe6060f1SDimitry Andric   if (CreateSymbolicLinkW(newname, oldname, flags))
247*fe6060f1SDimitry Andric     return 0;
248*fe6060f1SDimitry Andric   return set_errno();
249*fe6060f1SDimitry Andric }
250*fe6060f1SDimitry Andric 
251*fe6060f1SDimitry Andric int symlink_file(const wchar_t *oldname, const wchar_t *newname) {
252*fe6060f1SDimitry Andric   return symlink_file_dir(oldname, newname, false);
253*fe6060f1SDimitry Andric }
254*fe6060f1SDimitry Andric 
255*fe6060f1SDimitry Andric int symlink_dir(const wchar_t *oldname, const wchar_t *newname) {
256*fe6060f1SDimitry Andric   return symlink_file_dir(oldname, newname, true);
257*fe6060f1SDimitry Andric }
258*fe6060f1SDimitry Andric 
259*fe6060f1SDimitry Andric int link(const wchar_t *oldname, const wchar_t *newname) {
260*fe6060f1SDimitry Andric   if (CreateHardLinkW(newname, oldname, nullptr))
261*fe6060f1SDimitry Andric     return 0;
262*fe6060f1SDimitry Andric   return set_errno();
263*fe6060f1SDimitry Andric }
264*fe6060f1SDimitry Andric 
265*fe6060f1SDimitry Andric int remove(const wchar_t *path) {
266*fe6060f1SDimitry Andric   detail::WinHandle h(path, DELETE, FILE_FLAG_OPEN_REPARSE_POINT);
267*fe6060f1SDimitry Andric   if (!h)
268*fe6060f1SDimitry Andric     return set_errno();
269*fe6060f1SDimitry Andric   FILE_DISPOSITION_INFO info;
270*fe6060f1SDimitry Andric   info.DeleteFile = TRUE;
271*fe6060f1SDimitry Andric   if (!SetFileInformationByHandle(h, FileDispositionInfo, &info, sizeof(info)))
272*fe6060f1SDimitry Andric     return set_errno();
273*fe6060f1SDimitry Andric   return 0;
274*fe6060f1SDimitry Andric }
275*fe6060f1SDimitry Andric 
276*fe6060f1SDimitry Andric int truncate_handle(HANDLE h, off_t length) {
277*fe6060f1SDimitry Andric   LARGE_INTEGER size_param;
278*fe6060f1SDimitry Andric   size_param.QuadPart = length;
279*fe6060f1SDimitry Andric   if (!SetFilePointerEx(h, size_param, 0, FILE_BEGIN))
280*fe6060f1SDimitry Andric     return set_errno();
281*fe6060f1SDimitry Andric   if (!SetEndOfFile(h))
282*fe6060f1SDimitry Andric     return set_errno();
283*fe6060f1SDimitry Andric   return 0;
284*fe6060f1SDimitry Andric }
285*fe6060f1SDimitry Andric 
286*fe6060f1SDimitry Andric int ftruncate(int fd, off_t length) {
287*fe6060f1SDimitry Andric   HANDLE h = reinterpret_cast<HANDLE>(_get_osfhandle(fd));
288*fe6060f1SDimitry Andric   return truncate_handle(h, length);
289*fe6060f1SDimitry Andric }
290*fe6060f1SDimitry Andric 
291*fe6060f1SDimitry Andric int truncate(const wchar_t *path, off_t length) {
292*fe6060f1SDimitry Andric   detail::WinHandle h(path, GENERIC_WRITE, 0);
293*fe6060f1SDimitry Andric   if (!h)
294*fe6060f1SDimitry Andric     return set_errno();
295*fe6060f1SDimitry Andric   return truncate_handle(h, length);
296*fe6060f1SDimitry Andric }
297*fe6060f1SDimitry Andric 
298*fe6060f1SDimitry Andric int rename(const wchar_t *from, const wchar_t *to) {
299*fe6060f1SDimitry Andric   if (!(MoveFileExW(from, to,
300*fe6060f1SDimitry Andric                     MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING |
301*fe6060f1SDimitry Andric                         MOVEFILE_WRITE_THROUGH)))
302*fe6060f1SDimitry Andric     return set_errno();
303*fe6060f1SDimitry Andric   return 0;
304*fe6060f1SDimitry Andric }
305*fe6060f1SDimitry Andric 
306*fe6060f1SDimitry Andric template <class... Args> int open(const wchar_t *filename, Args... args) {
307*fe6060f1SDimitry Andric   return _wopen(filename, args...);
308*fe6060f1SDimitry Andric }
309*fe6060f1SDimitry Andric int close(int fd) { return _close(fd); }
310*fe6060f1SDimitry Andric int chdir(const wchar_t *path) { return _wchdir(path); }
311*fe6060f1SDimitry Andric 
312*fe6060f1SDimitry Andric struct StatVFS {
313*fe6060f1SDimitry Andric   uint64_t f_frsize;
314*fe6060f1SDimitry Andric   uint64_t f_blocks;
315*fe6060f1SDimitry Andric   uint64_t f_bfree;
316*fe6060f1SDimitry Andric   uint64_t f_bavail;
317*fe6060f1SDimitry Andric };
318*fe6060f1SDimitry Andric 
319*fe6060f1SDimitry Andric int statvfs(const wchar_t *p, StatVFS *buf) {
320*fe6060f1SDimitry Andric   path dir = p;
321*fe6060f1SDimitry Andric   while (true) {
322*fe6060f1SDimitry Andric     error_code local_ec;
323*fe6060f1SDimitry Andric     const file_status st = status(dir, local_ec);
324*fe6060f1SDimitry Andric     if (!exists(st) || is_directory(st))
325*fe6060f1SDimitry Andric       break;
326*fe6060f1SDimitry Andric     path parent = dir.parent_path();
327*fe6060f1SDimitry Andric     if (parent == dir) {
328*fe6060f1SDimitry Andric       errno = ENOENT;
329*fe6060f1SDimitry Andric       return -1;
330*fe6060f1SDimitry Andric     }
331*fe6060f1SDimitry Andric     dir = parent;
332*fe6060f1SDimitry Andric   }
333*fe6060f1SDimitry Andric   ULARGE_INTEGER free_bytes_available_to_caller, total_number_of_bytes,
334*fe6060f1SDimitry Andric       total_number_of_free_bytes;
335*fe6060f1SDimitry Andric   if (!GetDiskFreeSpaceExW(dir.c_str(), &free_bytes_available_to_caller,
336*fe6060f1SDimitry Andric                            &total_number_of_bytes, &total_number_of_free_bytes))
337*fe6060f1SDimitry Andric     return set_errno();
338*fe6060f1SDimitry Andric   buf->f_frsize = 1;
339*fe6060f1SDimitry Andric   buf->f_blocks = total_number_of_bytes.QuadPart;
340*fe6060f1SDimitry Andric   buf->f_bfree = total_number_of_free_bytes.QuadPart;
341*fe6060f1SDimitry Andric   buf->f_bavail = free_bytes_available_to_caller.QuadPart;
342*fe6060f1SDimitry Andric   return 0;
343*fe6060f1SDimitry Andric }
344*fe6060f1SDimitry Andric 
345*fe6060f1SDimitry Andric wchar_t *getcwd(wchar_t *buff, size_t size) { return _wgetcwd(buff, size); }
346*fe6060f1SDimitry Andric 
347*fe6060f1SDimitry Andric wchar_t *realpath(const wchar_t *path, wchar_t *resolved_name) {
348*fe6060f1SDimitry Andric   // Only expected to be used with us allocating the buffer.
349*fe6060f1SDimitry Andric   _LIBCPP_ASSERT(resolved_name == nullptr,
350*fe6060f1SDimitry Andric                  "Windows realpath() assumes a null resolved_name");
351*fe6060f1SDimitry Andric 
352*fe6060f1SDimitry Andric   WinHandle h(path, FILE_READ_ATTRIBUTES, 0);
353*fe6060f1SDimitry Andric   if (!h) {
354*fe6060f1SDimitry Andric     set_errno();
355*fe6060f1SDimitry Andric     return nullptr;
356*fe6060f1SDimitry Andric   }
357*fe6060f1SDimitry Andric   size_t buff_size = MAX_PATH + 10;
358*fe6060f1SDimitry Andric   std::unique_ptr<wchar_t, decltype(&::free)> buff(
359*fe6060f1SDimitry Andric       static_cast<wchar_t *>(malloc(buff_size * sizeof(wchar_t))), &::free);
360*fe6060f1SDimitry Andric   DWORD retval = GetFinalPathNameByHandleW(
361*fe6060f1SDimitry Andric       h, buff.get(), buff_size, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
362*fe6060f1SDimitry Andric   if (retval > buff_size) {
363*fe6060f1SDimitry Andric     buff_size = retval;
364*fe6060f1SDimitry Andric     buff.reset(static_cast<wchar_t *>(malloc(buff_size * sizeof(wchar_t))));
365*fe6060f1SDimitry Andric     retval = GetFinalPathNameByHandleW(h, buff.get(), buff_size,
366*fe6060f1SDimitry Andric                                        FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
367*fe6060f1SDimitry Andric   }
368*fe6060f1SDimitry Andric   if (!retval) {
369*fe6060f1SDimitry Andric     set_errno();
370*fe6060f1SDimitry Andric     return nullptr;
371*fe6060f1SDimitry Andric   }
372*fe6060f1SDimitry Andric   wchar_t *ptr = buff.get();
373*fe6060f1SDimitry Andric   if (!wcsncmp(ptr, L"\\\\?\\", 4)) {
374*fe6060f1SDimitry Andric     if (ptr[5] == ':') { // \\?\X: -> X:
375*fe6060f1SDimitry Andric       memmove(&ptr[0], &ptr[4], (wcslen(&ptr[4]) + 1) * sizeof(wchar_t));
376*fe6060f1SDimitry Andric     } else if (!wcsncmp(&ptr[4], L"UNC\\", 4)) { // \\?\UNC\server -> \\server
377*fe6060f1SDimitry Andric       wcscpy(&ptr[0], L"\\\\");
378*fe6060f1SDimitry Andric       memmove(&ptr[2], &ptr[8], (wcslen(&ptr[8]) + 1) * sizeof(wchar_t));
379*fe6060f1SDimitry Andric     }
380*fe6060f1SDimitry Andric   }
381*fe6060f1SDimitry Andric   return buff.release();
382*fe6060f1SDimitry Andric }
383*fe6060f1SDimitry Andric 
384*fe6060f1SDimitry Andric #define AT_FDCWD -1
385*fe6060f1SDimitry Andric #define AT_SYMLINK_NOFOLLOW 1
386*fe6060f1SDimitry Andric using ModeT = int;
387*fe6060f1SDimitry Andric 
388*fe6060f1SDimitry Andric int fchmod_handle(HANDLE h, int perms) {
389*fe6060f1SDimitry Andric   FILE_BASIC_INFO basic;
390*fe6060f1SDimitry Andric   if (!GetFileInformationByHandleEx(h, FileBasicInfo, &basic, sizeof(basic)))
391*fe6060f1SDimitry Andric     return set_errno();
392*fe6060f1SDimitry Andric   DWORD orig_attributes = basic.FileAttributes;
393*fe6060f1SDimitry Andric   basic.FileAttributes &= ~FILE_ATTRIBUTE_READONLY;
394*fe6060f1SDimitry Andric   if ((perms & 0222) == 0)
395*fe6060f1SDimitry Andric     basic.FileAttributes |= FILE_ATTRIBUTE_READONLY;
396*fe6060f1SDimitry Andric   if (basic.FileAttributes != orig_attributes &&
397*fe6060f1SDimitry Andric       !SetFileInformationByHandle(h, FileBasicInfo, &basic, sizeof(basic)))
398*fe6060f1SDimitry Andric     return set_errno();
399*fe6060f1SDimitry Andric   return 0;
400*fe6060f1SDimitry Andric }
401*fe6060f1SDimitry Andric 
402*fe6060f1SDimitry Andric int fchmodat(int fd, const wchar_t *path, int perms, int flag) {
403*fe6060f1SDimitry Andric   DWORD attributes = GetFileAttributesW(path);
404*fe6060f1SDimitry Andric   if (attributes == INVALID_FILE_ATTRIBUTES)
405*fe6060f1SDimitry Andric     return set_errno();
406*fe6060f1SDimitry Andric   if (attributes & FILE_ATTRIBUTE_REPARSE_POINT &&
407*fe6060f1SDimitry Andric       !(flag & AT_SYMLINK_NOFOLLOW)) {
408*fe6060f1SDimitry Andric     // If the file is a symlink, and we are supposed to operate on the target
409*fe6060f1SDimitry Andric     // of the symlink, we need to open a handle to it, without the
410*fe6060f1SDimitry Andric     // FILE_FLAG_OPEN_REPARSE_POINT flag, to open the destination of the
411*fe6060f1SDimitry Andric     // symlink, and operate on it via the handle.
412*fe6060f1SDimitry Andric     detail::WinHandle h(path, FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES, 0);
413*fe6060f1SDimitry Andric     if (!h)
414*fe6060f1SDimitry Andric       return set_errno();
415*fe6060f1SDimitry Andric     return fchmod_handle(h, perms);
416*fe6060f1SDimitry Andric   } else {
417*fe6060f1SDimitry Andric     // For a non-symlink, or if operating on the symlink itself instead of
418*fe6060f1SDimitry Andric     // its target, we can use SetFileAttributesW, saving a few calls.
419*fe6060f1SDimitry Andric     DWORD orig_attributes = attributes;
420*fe6060f1SDimitry Andric     attributes &= ~FILE_ATTRIBUTE_READONLY;
421*fe6060f1SDimitry Andric     if ((perms & 0222) == 0)
422*fe6060f1SDimitry Andric       attributes |= FILE_ATTRIBUTE_READONLY;
423*fe6060f1SDimitry Andric     if (attributes != orig_attributes && !SetFileAttributesW(path, attributes))
424*fe6060f1SDimitry Andric       return set_errno();
425*fe6060f1SDimitry Andric   }
426*fe6060f1SDimitry Andric   return 0;
427*fe6060f1SDimitry Andric }
428*fe6060f1SDimitry Andric 
429*fe6060f1SDimitry Andric int fchmod(int fd, int perms) {
430*fe6060f1SDimitry Andric   HANDLE h = reinterpret_cast<HANDLE>(_get_osfhandle(fd));
431*fe6060f1SDimitry Andric   return fchmod_handle(h, perms);
432*fe6060f1SDimitry Andric }
433*fe6060f1SDimitry Andric 
434*fe6060f1SDimitry Andric #define MAX_SYMLINK_SIZE MAXIMUM_REPARSE_DATA_BUFFER_SIZE
435*fe6060f1SDimitry Andric using SSizeT = ::int64_t;
436*fe6060f1SDimitry Andric 
437*fe6060f1SDimitry Andric SSizeT readlink(const wchar_t *path, wchar_t *ret_buf, size_t bufsize) {
438*fe6060f1SDimitry Andric   uint8_t buf[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
439*fe6060f1SDimitry Andric   detail::WinHandle h(path, FILE_READ_ATTRIBUTES, FILE_FLAG_OPEN_REPARSE_POINT);
440*fe6060f1SDimitry Andric   if (!h)
441*fe6060f1SDimitry Andric     return set_errno();
442*fe6060f1SDimitry Andric   DWORD out;
443*fe6060f1SDimitry Andric   if (!DeviceIoControl(h, FSCTL_GET_REPARSE_POINT, nullptr, 0, buf, sizeof(buf),
444*fe6060f1SDimitry Andric                        &out, 0))
445*fe6060f1SDimitry Andric     return set_errno();
446*fe6060f1SDimitry Andric   const auto *reparse = reinterpret_cast<LIBCPP_REPARSE_DATA_BUFFER *>(buf);
447*fe6060f1SDimitry Andric   size_t path_buf_offset = offsetof(LIBCPP_REPARSE_DATA_BUFFER,
448*fe6060f1SDimitry Andric                                     SymbolicLinkReparseBuffer.PathBuffer[0]);
449*fe6060f1SDimitry Andric   if (out < path_buf_offset) {
450*fe6060f1SDimitry Andric     errno = EINVAL;
451*fe6060f1SDimitry Andric     return -1;
452*fe6060f1SDimitry Andric   }
453*fe6060f1SDimitry Andric   if (reparse->ReparseTag != IO_REPARSE_TAG_SYMLINK) {
454*fe6060f1SDimitry Andric     errno = EINVAL;
455*fe6060f1SDimitry Andric     return -1;
456*fe6060f1SDimitry Andric   }
457*fe6060f1SDimitry Andric   const auto &symlink = reparse->SymbolicLinkReparseBuffer;
458*fe6060f1SDimitry Andric   unsigned short name_offset, name_length;
459*fe6060f1SDimitry Andric   if (symlink.PrintNameLength == 0) {
460*fe6060f1SDimitry Andric     name_offset = symlink.SubstituteNameOffset;
461*fe6060f1SDimitry Andric     name_length = symlink.SubstituteNameLength;
462*fe6060f1SDimitry Andric   } else {
463*fe6060f1SDimitry Andric     name_offset = symlink.PrintNameOffset;
464*fe6060f1SDimitry Andric     name_length = symlink.PrintNameLength;
465*fe6060f1SDimitry Andric   }
466*fe6060f1SDimitry Andric   // name_offset/length are expressed in bytes, not in wchar_t
467*fe6060f1SDimitry Andric   if (path_buf_offset + name_offset + name_length > out) {
468*fe6060f1SDimitry Andric     errno = EINVAL;
469*fe6060f1SDimitry Andric     return -1;
470*fe6060f1SDimitry Andric   }
471*fe6060f1SDimitry Andric   if (name_length / sizeof(wchar_t) > bufsize) {
472*fe6060f1SDimitry Andric     errno = ENOMEM;
473*fe6060f1SDimitry Andric     return -1;
474*fe6060f1SDimitry Andric   }
475*fe6060f1SDimitry Andric   memcpy(ret_buf, &symlink.PathBuffer[name_offset / sizeof(wchar_t)],
476*fe6060f1SDimitry Andric          name_length);
477*fe6060f1SDimitry Andric   return name_length / sizeof(wchar_t);
478*fe6060f1SDimitry Andric }
479*fe6060f1SDimitry Andric 
480*fe6060f1SDimitry Andric #else
481*fe6060f1SDimitry Andric int symlink_file(const char *oldname, const char *newname) {
482*fe6060f1SDimitry Andric   return ::symlink(oldname, newname);
483*fe6060f1SDimitry Andric }
484*fe6060f1SDimitry Andric int symlink_dir(const char *oldname, const char *newname) {
485*fe6060f1SDimitry Andric   return ::symlink(oldname, newname);
486*fe6060f1SDimitry Andric }
487*fe6060f1SDimitry Andric using ::chdir;
488*fe6060f1SDimitry Andric using ::close;
489*fe6060f1SDimitry Andric using ::fchmod;
490*fe6060f1SDimitry Andric #if defined(AT_SYMLINK_NOFOLLOW) && defined(AT_FDCWD)
491*fe6060f1SDimitry Andric using ::fchmodat;
492*fe6060f1SDimitry Andric #endif
493*fe6060f1SDimitry Andric using ::fstat;
494*fe6060f1SDimitry Andric using ::ftruncate;
495*fe6060f1SDimitry Andric using ::getcwd;
496*fe6060f1SDimitry Andric using ::link;
497*fe6060f1SDimitry Andric using ::lstat;
498*fe6060f1SDimitry Andric using ::mkdir;
499*fe6060f1SDimitry Andric using ::open;
500*fe6060f1SDimitry Andric using ::readlink;
501*fe6060f1SDimitry Andric using ::realpath;
502*fe6060f1SDimitry Andric using ::remove;
503*fe6060f1SDimitry Andric using ::rename;
504*fe6060f1SDimitry Andric using ::stat;
505*fe6060f1SDimitry Andric using ::statvfs;
506*fe6060f1SDimitry Andric using ::truncate;
507*fe6060f1SDimitry Andric 
508*fe6060f1SDimitry Andric #define O_BINARY 0
509*fe6060f1SDimitry Andric 
510*fe6060f1SDimitry Andric using StatVFS = struct statvfs;
511*fe6060f1SDimitry Andric using ModeT = ::mode_t;
512*fe6060f1SDimitry Andric using SSizeT = ::ssize_t;
513*fe6060f1SDimitry Andric 
514*fe6060f1SDimitry Andric #endif
515*fe6060f1SDimitry Andric 
516*fe6060f1SDimitry Andric } // namespace
517*fe6060f1SDimitry Andric } // end namespace detail
518*fe6060f1SDimitry Andric 
519*fe6060f1SDimitry Andric _LIBCPP_END_NAMESPACE_FILESYSTEM
520*fe6060f1SDimitry Andric 
521*fe6060f1SDimitry Andric #endif // POSIX_COMPAT_H
522