Index: libcxx/src/filesystem/operations.cpp =================================================================== --- libcxx/src/filesystem/operations.cpp +++ libcxx/src/filesystem/operations.cpp @@ -455,9 +455,15 @@ return static_cast(st.st_mode) & perms::mask; } +#if !defined(_LIBCPP_WIN32API) ::mode_t posix_convert_perms(perms prms) { return static_cast< ::mode_t>(prms & perms::mask); } +#else +int posix_convert_perms(perms prms) { + return static_cast(prms & perms::mask); +} +#endif file_status create_file_status(error_code& m_ec, path const& p, const StatT& path_stat, error_code* ec) { @@ -530,7 +536,7 @@ } bool posix_fchmod(const FileDescriptor& fd, const StatT& st, error_code& ec) { - if (::fchmod(fd.fd, st.st_mode) == -1) { + if (detail::fchmod(fd.fd, st.st_mode) == -1) { ec = capture_errno(); return true; } @@ -1199,7 +1205,7 @@ #if defined(AT_SYMLINK_NOFOLLOW) && defined(AT_FDCWD) const int flags = set_sym_perms ? AT_SYMLINK_NOFOLLOW : 0; - if (::fchmodat(AT_FDCWD, p.c_str(), real_perms, flags) == -1) { + if (detail::fchmodat(AT_FDCWD, p.c_str(), real_perms, flags) == -1) { return err.report(capture_errno()); } #else Index: libcxx/src/filesystem/posix_compat.h =================================================================== --- libcxx/src/filesystem/posix_compat.h +++ libcxx/src/filesystem/posix_compat.h @@ -316,6 +316,56 @@ } return buff.release(); } + +#define AT_FDCWD -1 +#define AT_SYMLINK_NOFOLLOW 1 + +int fchmod_handle(HANDLE h, int perms) { + FILE_BASIC_INFO basic; + if (!GetFileInformationByHandleEx(h, FileBasicInfo, &basic, sizeof(basic))) + return set_errno(); + DWORD orig_attributes = basic.FileAttributes; + 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))) + return set_errno(); + return 0; +} + +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 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 + // symlink, and operate on it via the handle. + detail::WinHandle h(path, FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES, 0); + if (!h) + return set_errno(); + return fchmod_handle(h, perms); + } else { + // For a non-symlink, or if operating on the symlink itself instead of + // its target, we can use SetFileAttributesW, saving a few calls. + DWORD orig_attributes = attributes; + attributes &= ~FILE_ATTRIBUTE_READONLY; + if ((perms & 0222) == 0) + attributes |= FILE_ATTRIBUTE_READONLY; + if (attributes != orig_attributes && !SetFileAttributesW(path, attributes)) + return set_errno(); + } + return 0; +} + +int fchmod(int fd, int perms) { + HANDLE h = reinterpret_cast(_get_osfhandle(fd)); + return fchmod_handle(h, perms); +} + #else int symlink_file(const char *oldname, const char *newname) { return ::symlink(oldname, newname); @@ -325,6 +375,8 @@ } using ::chdir; using ::close; +using ::fchmod; +using ::fchmodat; using ::fstat; using ::ftruncate; using ::getcwd;