aboutsummaryrefslogtreecommitdiff
path: root/libcxx/src/filesystem/posix_compat.h
diff options
context:
space:
mode:
Diffstat (limited to 'libcxx/src/filesystem/posix_compat.h')
-rw-r--r--libcxx/src/filesystem/posix_compat.h231
1 files changed, 107 insertions, 124 deletions
diff --git a/libcxx/src/filesystem/posix_compat.h b/libcxx/src/filesystem/posix_compat.h
index fb213d9..ec2de49 100644
--- a/libcxx/src/filesystem/posix_compat.h
+++ b/libcxx/src/filesystem/posix_compat.h
@@ -31,17 +31,17 @@
#include "time_utils.h"
#if defined(_LIBCPP_WIN32API)
-# define WIN32_LEAN_AND_MEAN
-# define NOMINMAX
-# include <windows.h>
-# include <io.h>
-# include <winioctl.h>
+# define WIN32_LEAN_AND_MEAN
+# define NOMINMAX
+# include <io.h>
+# include <windows.h>
+# include <winioctl.h>
#else
-# include <fcntl.h>
-# include <unistd.h>
-# include <sys/stat.h>
-# include <sys/statvfs.h>
-# include <sys/time.h>
+# include <fcntl.h>
+# include <sys/stat.h>
+# include <sys/statvfs.h>
+# include <sys/time.h>
+# include <unistd.h>
#endif
#include <stdlib.h>
#include <time.h>
@@ -50,7 +50,7 @@
// This struct isn't defined in the normal Windows SDK, but only in the
// Windows Driver Kit.
struct LIBCPP_REPARSE_DATA_BUFFER {
- unsigned long ReparseTag;
+ unsigned long ReparseTag;
unsigned short ReparseDataLength;
unsigned short Reserved;
union {
@@ -59,15 +59,15 @@ struct LIBCPP_REPARSE_DATA_BUFFER {
unsigned short SubstituteNameLength;
unsigned short PrintNameOffset;
unsigned short PrintNameLength;
- unsigned long Flags;
- wchar_t PathBuffer[1];
+ unsigned long Flags;
+ wchar_t PathBuffer[1];
} SymbolicLinkReparseBuffer;
struct {
unsigned short SubstituteNameOffset;
unsigned short SubstituteNameLength;
unsigned short PrintNameOffset;
unsigned short PrintNameLength;
- wchar_t PathBuffer[1];
+ wchar_t PathBuffer[1];
} MountPointReparseBuffer;
struct {
unsigned char DataBuffer[1];
@@ -86,41 +86,41 @@ namespace detail {
// provide our own implementation, undef all potential defines from the
// C runtime headers and provide a complete set of macros of our own.
-#undef _S_IFMT
-#undef _S_IFDIR
-#undef _S_IFCHR
-#undef _S_IFIFO
-#undef _S_IFREG
-#undef _S_IFBLK
-#undef _S_IFLNK
-#undef _S_IFSOCK
-
-#define _S_IFMT 0xF000
-#define _S_IFDIR 0x4000
-#define _S_IFCHR 0x2000
-#define _S_IFIFO 0x1000
-#define _S_IFREG 0x8000
-#define _S_IFBLK 0x6000
-#define _S_IFLNK 0xA000
-#define _S_IFSOCK 0xC000
-
-#undef S_ISDIR
-#undef S_ISFIFO
-#undef S_ISCHR
-#undef S_ISREG
-#undef S_ISLNK
-#undef S_ISBLK
-#undef S_ISSOCK
-
-#define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
-#define S_ISCHR(m) (((m) & _S_IFMT) == _S_IFCHR)
-#define S_ISFIFO(m) (((m) & _S_IFMT) == _S_IFIFO)
-#define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
-#define S_ISBLK(m) (((m) & _S_IFMT) == _S_IFBLK)
-#define S_ISLNK(m) (((m) & _S_IFMT) == _S_IFLNK)
-#define S_ISSOCK(m) (((m) & _S_IFMT) == _S_IFSOCK)
-
-#define O_NONBLOCK 0
+# undef _S_IFMT
+# undef _S_IFDIR
+# undef _S_IFCHR
+# undef _S_IFIFO
+# undef _S_IFREG
+# undef _S_IFBLK
+# undef _S_IFLNK
+# undef _S_IFSOCK
+
+# define _S_IFMT 0xF000
+# define _S_IFDIR 0x4000
+# define _S_IFCHR 0x2000
+# define _S_IFIFO 0x1000
+# define _S_IFREG 0x8000
+# define _S_IFBLK 0x6000
+# define _S_IFLNK 0xA000
+# define _S_IFSOCK 0xC000
+
+# undef S_ISDIR
+# undef S_ISFIFO
+# undef S_ISCHR
+# undef S_ISREG
+# undef S_ISLNK
+# undef S_ISBLK
+# undef S_ISSOCK
+
+# define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
+# define S_ISCHR(m) (((m) & _S_IFMT) == _S_IFCHR)
+# define S_ISFIFO(m) (((m) & _S_IFMT) == _S_IFIFO)
+# define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
+# define S_ISBLK(m) (((m) & _S_IFMT) == _S_IFBLK)
+# define S_ISLNK(m) (((m) & _S_IFMT) == _S_IFLNK)
+# define S_ISSOCK(m) (((m) & _S_IFMT) == _S_IFSOCK)
+
+# define O_NONBLOCK 0
inline int set_errno(int e = GetLastError()) {
errno = static_cast<int>(__win_err_to_errc(e));
@@ -129,10 +129,15 @@ inline int set_errno(int e = GetLastError()) {
class WinHandle {
public:
- WinHandle(const wchar_t *p, DWORD access, DWORD flags) {
+ WinHandle(const wchar_t* p, DWORD access, DWORD flags) {
h = CreateFileW(
- p, access, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
- nullptr, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS | flags, nullptr);
+ p,
+ access,
+ FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
+ nullptr,
+ OPEN_EXISTING,
+ FILE_FLAG_BACKUP_SEMANTICS | flags,
+ nullptr);
}
~WinHandle() {
if (h != INVALID_HANDLE_VALUE)
@@ -145,7 +150,7 @@ private:
HANDLE h;
};
-inline int stat_handle(HANDLE h, StatT *buf) {
+inline int stat_handle(HANDLE h, StatT* buf) {
FILE_BASIC_INFO basic;
if (!GetFileInformationByHandleEx(h, FileBasicInfo, &basic, sizeof(basic)))
return set_errno();
@@ -162,18 +167,16 @@ inline int stat_handle(HANDLE h, StatT *buf) {
}
if (basic.FileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
FILE_ATTRIBUTE_TAG_INFO tag;
- if (!GetFileInformationByHandleEx(h, FileAttributeTagInfo, &tag,
- sizeof(tag)))
+ if (!GetFileInformationByHandleEx(h, FileAttributeTagInfo, &tag, sizeof(tag)))
return set_errno();
if (tag.ReparseTag == IO_REPARSE_TAG_SYMLINK)
buf->st_mode = (buf->st_mode & ~_S_IFMT) | _S_IFLNK;
}
FILE_STANDARD_INFO standard;
- if (!GetFileInformationByHandleEx(h, FileStandardInfo, &standard,
- sizeof(standard)))
+ if (!GetFileInformationByHandleEx(h, FileStandardInfo, &standard, sizeof(standard)))
return set_errno();
buf->st_nlink = standard.NumberOfLinks;
- buf->st_size = standard.EndOfFile.QuadPart;
+ buf->st_size = standard.EndOfFile.QuadPart;
BY_HANDLE_FILE_INFORMATION info;
if (!GetFileInformationByHandle(h, &info))
return set_errno();
@@ -183,7 +186,7 @@ inline int stat_handle(HANDLE h, StatT *buf) {
return 0;
}
-inline int stat_file(const wchar_t *path, StatT *buf, DWORD flags) {
+inline int stat_file(const wchar_t* path, StatT* buf, DWORD flags) {
WinHandle h(path, FILE_READ_ATTRIBUTES, flags);
if (!h)
return set_errno();
@@ -191,32 +194,28 @@ inline int stat_file(const wchar_t *path, StatT *buf, DWORD flags) {
return ret;
}
-inline int stat(const wchar_t *path, StatT *buf) { return stat_file(path, buf, 0); }
+inline int stat(const wchar_t* path, StatT* buf) { return stat_file(path, buf, 0); }
-inline int lstat(const wchar_t *path, StatT *buf) {
- return stat_file(path, buf, FILE_FLAG_OPEN_REPARSE_POINT);
-}
+inline int lstat(const wchar_t* path, StatT* buf) { return stat_file(path, buf, FILE_FLAG_OPEN_REPARSE_POINT); }
-inline int fstat(int fd, StatT *buf) {
+inline int fstat(int fd, StatT* buf) {
HANDLE h = reinterpret_cast<HANDLE>(_get_osfhandle(fd));
return stat_handle(h, buf);
}
-inline int mkdir(const wchar_t *path, int permissions) {
+inline int mkdir(const wchar_t* path, int permissions) {
(void)permissions;
if (!CreateDirectoryW(path, nullptr))
return set_errno();
return 0;
}
-inline int symlink_file_dir(const wchar_t *oldname, const wchar_t *newname,
- bool is_dir) {
+inline int symlink_file_dir(const wchar_t* oldname, const wchar_t* newname, bool is_dir) {
path dest(oldname);
dest.make_preferred();
- oldname = dest.c_str();
+ oldname = dest.c_str();
DWORD flags = is_dir ? SYMBOLIC_LINK_FLAG_DIRECTORY : 0;
- if (CreateSymbolicLinkW(newname, oldname,
- flags | SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE))
+ if (CreateSymbolicLinkW(newname, oldname, flags | SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE))
return 0;
int e = GetLastError();
if (e != ERROR_INVALID_PARAMETER)
@@ -226,21 +225,21 @@ inline int symlink_file_dir(const wchar_t *oldname, const wchar_t *newname,
return set_errno();
}
-inline int symlink_file(const wchar_t *oldname, const wchar_t *newname) {
+inline int symlink_file(const wchar_t* oldname, const wchar_t* newname) {
return symlink_file_dir(oldname, newname, false);
}
-inline int symlink_dir(const wchar_t *oldname, const wchar_t *newname) {
+inline int symlink_dir(const wchar_t* oldname, const wchar_t* newname) {
return symlink_file_dir(oldname, newname, true);
}
-inline int link(const wchar_t *oldname, const wchar_t *newname) {
+inline int link(const wchar_t* oldname, const wchar_t* newname) {
if (CreateHardLinkW(newname, oldname, nullptr))
return 0;
return set_errno();
}
-inline int remove(const wchar_t *path) {
+inline int remove(const wchar_t* path) {
detail::WinHandle h(path, DELETE, FILE_FLAG_OPEN_REPARSE_POINT);
if (!h)
return set_errno();
@@ -266,17 +265,15 @@ inline int ftruncate(int fd, off_t length) {
return truncate_handle(h, length);
}
-inline int truncate(const wchar_t *path, off_t length) {
+inline int truncate(const wchar_t* path, off_t length) {
detail::WinHandle h(path, GENERIC_WRITE, 0);
if (!h)
return set_errno();
return truncate_handle(h, length);
}
-inline int rename(const wchar_t *from, const wchar_t *to) {
- if (!(MoveFileExW(from, to,
- MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING |
- MOVEFILE_WRITE_THROUGH)))
+inline int rename(const wchar_t* from, const wchar_t* to) {
+ if (!(MoveFileExW(from, to, MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH)))
return set_errno();
return 0;
}
@@ -294,7 +291,7 @@ struct StatVFS {
uint64_t f_bavail;
};
-inline int statvfs(const wchar_t *p, StatVFS *buf) {
+inline int statvfs(const wchar_t* p, StatVFS* buf) {
path dir = p;
while (true) {
error_code local_ec;
@@ -308,14 +305,13 @@ inline int statvfs(const wchar_t *p, StatVFS *buf) {
}
dir = parent;
}
- ULARGE_INTEGER free_bytes_available_to_caller, total_number_of_bytes,
- total_number_of_free_bytes;
- if (!GetDiskFreeSpaceExW(dir.c_str(), &free_bytes_available_to_caller,
- &total_number_of_bytes, &total_number_of_free_bytes))
+ ULARGE_INTEGER free_bytes_available_to_caller, total_number_of_bytes, total_number_of_free_bytes;
+ if (!GetDiskFreeSpaceExW(
+ dir.c_str(), &free_bytes_available_to_caller, &total_number_of_bytes, &total_number_of_free_bytes))
return set_errno();
buf->f_frsize = 1;
buf->f_blocks = total_number_of_bytes.QuadPart;
- buf->f_bfree = total_number_of_free_bytes.QuadPart;
+ buf->f_bfree = total_number_of_free_bytes.QuadPart;
buf->f_bavail = free_bytes_available_to_caller.QuadPart;
return 0;
}
@@ -340,10 +336,9 @@ inline wchar_t* getcwd([[maybe_unused]] wchar_t* in_buf, [[maybe_unused]] size_t
return buff.release();
}
-inline wchar_t *realpath(const wchar_t *path, [[maybe_unused]] wchar_t *resolved_name) {
+inline wchar_t* realpath(const wchar_t* path, [[maybe_unused]] wchar_t* resolved_name) {
// Only expected to be used with us allocating the buffer.
- _LIBCPP_ASSERT_UNCATEGORIZED(resolved_name == nullptr,
- "Windows realpath() assumes a null resolved_name");
+ _LIBCPP_ASSERT_UNCATEGORIZED(resolved_name == nullptr, "Windows realpath() assumes a null resolved_name");
WinHandle h(path, FILE_READ_ATTRIBUTES, 0);
if (!h) {
@@ -351,21 +346,18 @@ inline wchar_t *realpath(const wchar_t *path, [[maybe_unused]] wchar_t *resolved
return nullptr;
}
size_t buff_size = MAX_PATH + 10;
- std::unique_ptr<wchar_t, decltype(&::free)> buff(
- static_cast<wchar_t *>(malloc(buff_size * sizeof(wchar_t))), &::free);
- DWORD retval = GetFinalPathNameByHandleW(
- h, buff.get(), buff_size, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
+ std::unique_ptr<wchar_t, decltype(&::free)> buff(static_cast<wchar_t*>(malloc(buff_size * sizeof(wchar_t))), &::free);
+ DWORD retval = GetFinalPathNameByHandleW(h, buff.get(), buff_size, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
if (retval > buff_size) {
buff_size = retval;
- buff.reset(static_cast<wchar_t *>(malloc(buff_size * sizeof(wchar_t))));
- retval = GetFinalPathNameByHandleW(h, buff.get(), buff_size,
- FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
+ buff.reset(static_cast<wchar_t*>(malloc(buff_size * sizeof(wchar_t))));
+ retval = GetFinalPathNameByHandleW(h, buff.get(), buff_size, FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
}
if (!retval) {
set_errno();
return nullptr;
}
- wchar_t *ptr = buff.get();
+ wchar_t* ptr = buff.get();
if (!wcsncmp(ptr, L"\\\\?\\", 4)) {
if (ptr[5] == ':') { // \\?\X: -> X:
memmove(&ptr[0], &ptr[4], (wcslen(&ptr[4]) + 1) * sizeof(wchar_t));
@@ -377,8 +369,8 @@ inline wchar_t *realpath(const wchar_t *path, [[maybe_unused]] wchar_t *resolved
return buff.release();
}
-#define AT_FDCWD -1
-#define AT_SYMLINK_NOFOLLOW 1
+# define AT_FDCWD -1
+# define AT_SYMLINK_NOFOLLOW 1
using ModeT = int;
inline int fchmod_handle(HANDLE h, int perms) {
@@ -389,18 +381,16 @@ inline int fchmod_handle(HANDLE h, int perms) {
basic.FileAttributes &= ~FILE_ATTRIBUTE_READONLY;
if ((perms & 0222) == 0)
basic.FileAttributes |= FILE_ATTRIBUTE_READONLY;
- if (basic.FileAttributes != orig_attributes &&
- !SetFileInformationByHandle(h, FileBasicInfo, &basic, sizeof(basic)))
+ if (basic.FileAttributes != orig_attributes && !SetFileInformationByHandle(h, FileBasicInfo, &basic, sizeof(basic)))
return set_errno();
return 0;
}
-inline int fchmodat(int /*fd*/, const wchar_t *path, int perms, int flag) {
+inline int fchmodat(int /*fd*/, const wchar_t* path, int perms, int flag) {
DWORD attributes = GetFileAttributesW(path);
if (attributes == INVALID_FILE_ATTRIBUTES)
return set_errno();
- if (attributes & FILE_ATTRIBUTE_REPARSE_POINT &&
- !(flag & AT_SYMLINK_NOFOLLOW)) {
+ if (attributes & FILE_ATTRIBUTE_REPARSE_POINT && !(flag & AT_SYMLINK_NOFOLLOW)) {
// If the file is a symlink, and we are supposed to operate on the target
// of the symlink, we need to open a handle to it, without the
// FILE_FLAG_OPEN_REPARSE_POINT flag, to open the destination of the
@@ -427,21 +417,19 @@ inline int fchmod(int fd, int perms) {
return fchmod_handle(h, perms);
}
-#define MAX_SYMLINK_SIZE MAXIMUM_REPARSE_DATA_BUFFER_SIZE
+# define MAX_SYMLINK_SIZE MAXIMUM_REPARSE_DATA_BUFFER_SIZE
using SSizeT = ::int64_t;
-inline SSizeT readlink(const wchar_t *path, wchar_t *ret_buf, size_t bufsize) {
+inline SSizeT readlink(const wchar_t* path, wchar_t* ret_buf, size_t bufsize) {
uint8_t buf[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
detail::WinHandle h(path, FILE_READ_ATTRIBUTES, FILE_FLAG_OPEN_REPARSE_POINT);
if (!h)
return set_errno();
DWORD out;
- if (!DeviceIoControl(h, FSCTL_GET_REPARSE_POINT, nullptr, 0, buf, sizeof(buf),
- &out, 0))
+ if (!DeviceIoControl(h, FSCTL_GET_REPARSE_POINT, nullptr, 0, buf, sizeof(buf), &out, 0))
return set_errno();
- const auto *reparse = reinterpret_cast<LIBCPP_REPARSE_DATA_BUFFER *>(buf);
- size_t path_buf_offset = offsetof(LIBCPP_REPARSE_DATA_BUFFER,
- SymbolicLinkReparseBuffer.PathBuffer[0]);
+ const auto* reparse = reinterpret_cast<LIBCPP_REPARSE_DATA_BUFFER*>(buf);
+ size_t path_buf_offset = offsetof(LIBCPP_REPARSE_DATA_BUFFER, SymbolicLinkReparseBuffer.PathBuffer[0]);
if (out < path_buf_offset) {
errno = EINVAL;
return -1;
@@ -450,7 +438,7 @@ inline SSizeT readlink(const wchar_t *path, wchar_t *ret_buf, size_t bufsize) {
errno = EINVAL;
return -1;
}
- const auto &symlink = reparse->SymbolicLinkReparseBuffer;
+ const auto& symlink = reparse->SymbolicLinkReparseBuffer;
unsigned short name_offset, name_length;
if (symlink.PrintNameLength == 0) {
name_offset = symlink.SubstituteNameOffset;
@@ -468,23 +456,18 @@ inline SSizeT readlink(const wchar_t *path, wchar_t *ret_buf, size_t bufsize) {
errno = ENOMEM;
return -1;
}
- memcpy(ret_buf, &symlink.PathBuffer[name_offset / sizeof(wchar_t)],
- name_length);
+ memcpy(ret_buf, &symlink.PathBuffer[name_offset / sizeof(wchar_t)], name_length);
return name_length / sizeof(wchar_t);
}
#else
-inline int symlink_file(const char *oldname, const char *newname) {
- return ::symlink(oldname, newname);
-}
-inline int symlink_dir(const char *oldname, const char *newname) {
- return ::symlink(oldname, newname);
-}
+inline int symlink_file(const char* oldname, const char* newname) { return ::symlink(oldname, newname); }
+inline int symlink_dir(const char* oldname, const char* newname) { return ::symlink(oldname, newname); }
using ::chdir;
using ::fchmod;
-#if defined(AT_SYMLINK_NOFOLLOW) && defined(AT_FDCWD)
+# if defined(AT_SYMLINK_NOFOLLOW) && defined(AT_FDCWD)
using ::fchmodat;
-#endif
+# endif
using ::fstat;
using ::ftruncate;
using ::getcwd;
@@ -499,11 +482,11 @@ using ::stat;
using ::statvfs;
using ::truncate;
-#define O_BINARY 0
+# define O_BINARY 0
using StatVFS = struct statvfs;
-using ModeT = ::mode_t;
-using SSizeT = ::ssize_t;
+using ModeT = ::mode_t;
+using SSizeT = ::ssize_t;
#endif