14 Commits

Author SHA1 Message Date
Jake Petroules
f77a636a01 Switch to a safer technique for obtaining the working directory on Windows
Instead of looping 8 times to work around the TOCTOU issue with sizing the current directory buffer, instead keep doubling the buffer up until the 32767 character limit until the result fits. This ensures we always get a working directory if GetWorkingDirectoryW didn't return some other error, rather than returning nil in the case of a race condition.
2025-05-02 11:29:49 -07:00
Jake Petroules
c16e0d9a48
Fix FileManager.isExecutableFile for emulated x86 processes on ARM systems (#1246)
GetBinaryType will return ERROR_BAD_EXE_FORMAT when querying an arm64 executable from an x86 process running on an ARM system. This change switches the implementation to use SHGetFileInfoW, which isn't subject to this quirk.

This also makes isExecutableFile behave more similarly to other platforms -- e.g. isExecutableFile already returns true for any file with the execute bit, even for an arm64 executable on an x86_64 macOS system (which it can't actually run). The spirit of the API is that the file is of an executable type, not necessarily that the running system is capable of executing it.

The practical consequence of fixing this bug is that queries like:

```swift
FileManager.default.isExecutableFile(atPath: "C:\\Windows\\system32\\cmd.exe")
```

will now correctly return true regardless of what architecture the binary is compiled for or what type of system it's running on.

Closes #860
2025-04-11 09:12:53 -07:00
Saleem Abdulrasool
e473ab75be
FoundationEssentials: fix up linkOrCopyFile on Windows (#675)
Adjust the Windows path for copying (or linking) files. This adds the
missing recursion in the copy (the documentation for `CopyFile2` was not
clear that the recursive copy is not performed).
2024-06-12 09:26:03 -07:00
Saleem Abdulrasool
c43fb94fda
FoundationEssentials: correct file overwriting behaviour (#648)
We would previously always overwrite the file which would not match the
expectation. Correct the flags to allow us to properly create or
overwrite the file.
2024-06-03 14:04:37 -07:00
Saleem Abdulrasool
98c8b2983c
FoundationEssentials: add some type overloads for WinSDK (#640)
clang imported types in WinSDK do not sometimes match the expected
types. Add overloads to shadow the types and match expectations. This
repairs the build after #634.
2024-05-29 09:24:54 -07:00
Saleem Abdulrasool
d91433d415
(128068690) FoundationEssentials: repair the Windows build (#608)
Repair the Windows build after recent additions for FileManager.
2024-05-14 11:37:08 -07:00
Jeremy Schonfeld
cae149702b
Add non-darwin search paths (#598)
* Add non-darwin search paths

* Address review feedback

* Resolve build failures

* Resolve more build failures

* Fix test failures
2024-05-10 15:17:19 -07:00
Saleem Abdulrasool
1092331e7d
(127606484) FoundationEssentials: support data writing on Windows (#581)
This implements the IO aspects of data writing for Windows to ensure
that we can support the full API surface.
2024-05-06 11:54:27 -07:00
Saleem Abdulrasool
aabac27b21
FoundationEssentials: implement file operations (#566)
This adds an initial implementation for file operations on Windows. With
this, `FileManager` should at least build on Windows, which unblocks
future work as well as brings us closer to enabling
`FoundationEssentials` on Windows.
2024-04-29 15:34:46 -07:00
Saleem Abdulrasool
c5e734c89a
FoundationEssentials: enable unprivilleged symlinks if possible (#556)
If developer mode is enabled or the user is assigned the
SeCreateSymbolicLinkPrivilege privilege, permit the unelevated symbolic
link creation.
2024-04-26 10:11:51 -07:00
Saleem Abdulrasool
d80478289c
(127077121) FoundationEssentials: support directory enumeration operations on Windows (#554)
Implement the directory enumeration operations in FileManager on
Windows. The implementation uses the Win32 API surface to ensure that we
can use the full features of the platform.
2024-04-25 16:11:30 -07:00
Saleem Abdulrasool
32b136e9b2
FoundationEssentials: improve Windows coverage for Platform.swift (#546)
Adjust the codepaths to improve Windows support in the file. This is
simply blindly trying to match semantics to reduce the errors reported
during building `FoundationEssentials`.
2024-04-15 11:42:46 -07:00
Saleem Abdulrasool
d7bad7ce73
FoundationEssentials: support symbolic link operations on Windows
This adjusts the symbolic link paths to be supported on Windows. The
symbolic link destination computation is particularly gnarly as it
requires re-defining the device driver interface in a shim header to
access that and then performing aliasing contortions to gain access to
the trailing VLA to get the value.
2024-04-12 11:40:18 -07:00
Saleem Abdulrasool
b80fb683b6
FoundationEssentials: provide typed overloads for constants from WinSDK
This allows for us to use the constants without explicitly casting the
type to `DWORD` on each site of use. By providing the shadowing
overload we can simply use the constants without impacting the
readability of the surrounding code.
2024-04-11 15:53:31 -07:00