113 Commits

Author SHA1 Message Date
Jake Petroules
90953dfb68
Transparently add the \\?\ prefix to Win32 calls for extended length path handling (#1257)
On Windows, there is a built-in maximum path limitation of 260 characters under most conditions. This can be extended to 32767 characters under either of the following two conditions:

- Adding the longPathAware attribute to the executable's manifest AND enabling the LongPathsEnabled system-wide registry key or group policy.
- Ensuring fully qualified paths passed to Win32 APIs are prefixed with \?\

Unfortunately, the former is not realistic for the Swift ecosystem, since it requires developers to have awareness of this specific Windows limitation, AND set longPathAware in their apps' manifest AND expect end users of those apps to change their system configuration.

Instead, this patch transparently prefixes all eligible paths in calls to Win32 APIs with the \?\ prefix to allow them to work with paths longer than 260 characters without requiring the caller of Foundation to manually prefix the paths.

See https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation for more info.
2025-04-23 14:51:43 -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
3405691582
03fe46f43b
OpenBSD support. (#1126)
* Advise porter on where to make necessary change.

In #1075 the change was already made for BSD (thank you!); my working
edit had this guidance to ensure future porters get an error directing
them where to make a necessary change.

Otherwise, the FoundationEssentials build will fail and complain these
variables are not defined but not have guidance as to where they are
sourced from.

* OpenBSD does not support extended attributes.

* OpenBSD does not have secure_getenv.

* Remaining OpenBSD changes.

* OpenBSD also needs `pthread_mutex_t?`.

* Originally I followed Darwin's check with `d_namlen`, but this should
  work too.

* Correct statvfs type casts for OpenBSD.

On OpenBSD, fsblkcnt_t -- the type of f_blocks -- is a UInt64; therefore,
so must `blockSize` be.

Ultimately, both sides of the `totalSizeBytes` multiplication should
probably be type cast for all platforms, but that's a more significant
functional change for another time.

* Default activeProcessorCount to 1, not 0.

After a rather tedious debugging session trying to figure out why
swiftpm-bootstrap appeared to be deadlocked, this turned out to be the
culprit. Perhaps this should be #error instead, but for now, set a
sensible default.

* Use sysconf for activeProcessorCount.

This is what Dispatch does in some places for OpenBSD anyway, so do
likewise here.
2025-04-07 10:46:18 -07:00
michael-yuji
7abeeef0f2
fix a typo preventing freebsd to build (#1231) 2025-03-28 09:54:42 -07:00
Kenta Kubo
68dd6d85a6
[wasm] Make FileManager.createFile() work on WASI (#992)
* [wasm] Make `FileManager.createFile()` work on WASI

fixes swiftwasm/swift#5593

`FileManager.createFile()` currently doesn't work on WASI because it
requires `.atomic`, it requires creating a temporary file, and it isn't
suppported on WASI.

So I have fixed that by removing the `.atomic` requirement only on WASI.

* [wasm] Make `Data.WritingOptions.atomic` unavailable on WASI

`writeToFileAux`, `createTemporaryFile`, and `createProtectedTemporaryFile` also become unavailable on WASI.
2025-03-25 09:42:15 -07:00
Christopher Thielen
fa43c96f5f
Move temporarily relocated FileManager extensions back to their original locations (#1213) 2025-03-18 11:29:53 -07:00
michael-yuji
2f65d91455
Fix _copyDirectoryMetadata compilation error on FreeBSD (#1121)
* Fix FreeBSD build

* address comments
2025-03-17 14:00:41 -07:00
Johannes Weiss
601e847faa
always @preconcurrency import Glibc (#1175)
Co-authored-by: Johannes Weiss <johannes@jweiss.io>
2025-03-04 09:25:51 -08:00
Jeremy Schonfeld
c55993dd5b
Prevent crashing when calling quotactl with UIDs greater than Int32.max (#1146)
* Prevent crashing when calling quotactl with UIDs greater than Int32.max

* Add comment
2025-01-29 09:13:39 -08:00
Christopher Thielen
215f1ce06b
Publish NSString bridging (#1133)
* Publish NSString->String bridging

* Relocate Swift extensions of some Objective-C NS_TYPED_ENUM types to work around swiftlang/swift #78731
2025-01-22 13:29:41 -08:00
michael-yuji
5ed6c72484
[FreeBSD] Fix FreeBSD build/support (#1075)
* FreeBSD platform specific fixes

- Use "/usr/share/zoneinfo"
- Use platform specific types and values
- Implement extattr ops for FreeBSD
- Use copy_file_range(2) for file cloning

* Revise patch to ProcessInfo to reduce potential impact to other platforms

* Fix macOS build

* typo

* take suggested change
2025-01-10 14:20:27 -08:00
finagolfin
90700c100f
Don't try to set extended attributes on Android (#1106)
Normal users don't have permission to change these, even for their own files.
2025-01-02 10:57:08 -05:00
Yuta Saito
c0532f8c99
Follow-up fixes to make it work with wasi-libc (#1095)
* Gate `fchown` and `fchmod` calls behind `os(WASI)`

They are not available on WASI, so we gate them behind `os(WASI)`.

* Add missing constant shims for wasi-libc

* Use `futimens` instead of legacy `futimes`

wasi-libc does not provide `futimes` as it is a legacy function.
574b88da48/libc-top-half/musl/include/sys/time.h (L34)
2024-12-16 11:19:39 -08:00
Yuta Saito
0b4f13f9f4
Fix WASI build of _copyDirectoryMetadata (#1094)
Extended attributes don't exist in WASI, so we need to exclude the use
of xattr-related APIs including `flistxattr`.
2024-12-13 09:29:45 -08:00
Alex Lorenz
4339393f2c
[android] fix 32-bit build (#1086)
Regression after bb3fccfa360d00f63999ac8faf6ba37224ce5174
2024-12-12 07:34:34 -08:00
Jeremy Schonfeld
bb3fccfa36
Ensure that FileManager.copyItem cannot copy directory metadata to files (#1081)
* (135575520) Ensure that FileManager.copyItem cannot copy directory metadata to files

* Fix whitespacing

* Fix Windows test failure
2024-12-11 15:05:08 -08:00
Jeremy Schonfeld
2eeb1b0772
(140882573) Home directory for non-existent user should not fall back to /var/empty or %ALLUSERSPROFILE% (#1072) 2024-12-06 15:51:08 -08:00
Jeremy Schonfeld
86d660bfb8
(140535903) Avoid attaching removefile callbacks when delegate does not implement functions (#1070) 2024-12-04 17:24:15 -08:00
Jeremy Schonfeld
db104087d4
(140820778) Darwin SearchPaths without expanding tilde produce non-tilde-based paths (#1069) 2024-12-04 12:52:55 -08:00
Yunpeng Li
ef57eb5182
Fix 'var currentDirectoryPath: String' crash when the directory is removed (#985)
* Fix 'var currentDirectoryPath: String' crash when the directory is removed.

* Fix test failed in Windows

* Fix test failed in Windows

* Update testCurrentWorkingDirectory() for Windows

* Fix merge mistake

* Update Tests/FoundationEssentialsTests/FileManager/FileManagerTests.swift

Co-authored-by: Jeremy Schonfeld <jeremyschonfeld@gmail.com>

---------

Co-authored-by: Jeremy Schonfeld <jeremyschonfeld@gmail.com>
2024-11-20 13:09:22 -08:00
Jeremy Schonfeld
86c79a1f85
FileManager Entry Points for ObjC Directory Enumeration (#1047)
* (139469462) ObjC API for directory search paths should use Swift implementation

* Fix trash directory bug
2024-11-13 09:50:43 -08:00
Saleem Abdulrasool
a9dc42c58b
FileManager: avoid a TOCTOU issue in computing CWD (#1035)
On Windows, we could potentially return a `nil` for the current working
directory in the rare case that the current working directory was
changed during the computation:

```swift
let dwLength: DWORD = GetCurrentDirectoryW(0, nil)                                // 1
return withUnsafeTemporaryAllocation(of: WCHAR.self, capacity: Int(dwLength)) {
    if GetCurrentDirectoryW(dwLength, $0.baseAddress) == dwLength - 1 {           // 2
        return String(decodingCString: $0.baseAddress!, as: UTF16.self)
    }
    return nil                                                                    // 3
}
```

Consider the case where at step 1, we receive $n$. We then are
interrupted, the CWD changed. We then perform step 2, where we receive
$m$ (st $m != n$). We would then proceed to point 3, where we return
`nil`. Avoid this TOCTOU issue by repeating this operation to a fixed
point.

Because we are guaranteed a current directory on Windows (unless the
initial query for the buffer size fails), we will eventually succeed. In
order to avoid a DoS attack vector, limit the attempt to quiescence to a
fixed number.
2024-11-07 10:18:54 -08:00
Alex Hoppen
31fef58520
Use SHCreateDirectory to create a directory with intermediate directories on Windows (#1033) 2024-11-05 15:41:03 -08:00
Jeremy Schonfeld
c92631f340
(138602480) Fix typo in String.replacingTildeWithRealHomeDirectory (#1007) 2024-10-25 13:15:47 -07:00
Jeremy Schonfeld
71eefee7fc
Fetching user/group info causes race conditions (#994)
* Avoid racy stdlib functions for fetching user/group info

* Refactor naming

* Fix build failure
2024-10-23 09:18:15 -07:00
Yuta Saito
4784ffb3e4
[wasm] Fix CocoaError construction for unsupported copy/link operations (#944)
This is a follow-up fix for 6c0a3e8453cdde633e1c55f6112fd53a0d756979,
which re-organized CocoaError helper functions.
2024-09-26 08:23:37 +09:00
Jeremy Schonfeld
6c0a3e8453
Performance optimizations for data reading/writing (#942)
* (135743158) Performance optimizations for data reading/writing

* Fix Windows build
2024-09-24 13:16:44 -07:00
Jeremy Schonfeld
d1da0f7a1a
Fix windows creation of relative symlinks to directories (#931)
* Fix windows creation of relative symlinks to directories

* Add additional unit tests

* Fix windows test failure
2024-09-23 10:04:10 -07:00
finagolfin
53ffff0600
[Android] Enable more code and tests (#871)
* [Android] Enable more code and tests

while disabling setting extended file attributes and a test creating a hard link,
features not normally allowed on Android.

* Remove incorrect WASI check
2024-09-17 08:40:40 -07:00
Jeremy Schonfeld
da80d51fa3
(134376602) Change @_nonSendable to unavailable extensions (#892) 2024-09-03 10:46:46 -07:00
Jeremy Schonfeld
df5aa9bbf0
subpathsOfDirectory(atPath:) should throw on empty paths (#869) 2024-08-20 11:32:38 -07:00
Jeremy Schonfeld
7242610c89
FileManager.fileExists(atPath:) should follow symlinks (#859) 2024-08-16 11:19:26 -07:00
Jeremy Schonfeld
e555c629ba
Fix Windows symlink handling in FileManager APIs (#858)
* Fix Windows symlink handling in FileManager APIs

* Address feedback
2024-08-16 10:41:44 -07:00
finagolfin
c15a5e1f5c
[Android] Use the Bionic module in more places (#842)
Also, use `canImport()` wherever importing APIs, reserving `os(Android)` for
platform differences.
2024-08-15 14:34:15 -07:00
Evan Wilde
aecc1b158c
[main] Get Swift-Foundation building against MUSL for Swift Static SDK (#848)
* Get FoundationEssentials building

Adding the missing musl imports to get FoundationEssentials building for
the Swift static SDKs again.

Also providing an option to disable building the macros. The macros
aren't necessary for building the library and will not be run as part of
the static SDK. No need to bloat the SDK or build times further. For
Swift 6, the macros should be provided by the toolchain since the
toolchain and SDK are current revlocked due to swiftmodules.

* Get FoundationInternationalization building

Adding the missing Musl imports to get FoundationInternationalization
building for the static SDK.
2024-08-14 13:34:15 -07:00
Alex Lorenz
ad6ca71b4e
[android] fix the LP32 armv7/i686 android build (#846)
* [android] fix the LP32 armv7/i686 android build

* Update Sources/FoundationEssentials/Android+Extensions.swift

Co-authored-by: Jeremy Schonfeld <1004103+jmschonfeld@users.noreply.github.com>

* drop the android Lp32 specific operator &

---------

Co-authored-by: Jeremy Schonfeld <1004103+jmschonfeld@users.noreply.github.com>
2024-08-14 10:56:25 -07:00
Yuta Saito
bc47ca221d
Port directory enumeration related code to WASI (#836)
* Port directory enumeration related code to WASI

For now wasi-libc does not include fts(3) implementation, so mark
features depending on it as unsupported on WASI. Once wasi-libc includes
fts or we decide to implement and maintain our own fts-like API, we can
remove these `#if os(WASI)` guards.

wasi-libc issue tracking fts support:
https://github.com/WebAssembly/wasi-libc/issues/520

Also, wasi-libc defines some constants in a way that ClangImporter can't
understand, so we need to grab them manually through _FoundationCShims in
function call form.

* Delegate error throwing decisions to the delegate
2024-08-12 19:52:15 -07:00
Yuta Saito
41cbea8b57
[wasm] Fall back to a default chunk size when st_blksize is not available (#835)
The `st_blksize` field in `stat` struct is not provided by WASI, so we
fall back to a default chunk size 4KB, which is a common page size.
2024-08-08 16:22:40 -07:00
Jeremy Schonfeld
a57b06e39f
Remove _typeByName lookup of _FoundationNSNumberInitializer (#817) 2024-08-02 13:56:03 -07:00
Yuta Saito
c82d1673eb
Add WASI platform conditions for libc imports and word size (#776)
* Add `import WASILibc` statements to libc import chains

* Declare wasm32 arch as 32-bit environment

* Switch to _pointerBitWidth for architecture checks

This change switches the architecture checks in Data.swift to use the
_pointerBitWidth instead of the arch() checks for consistency with newer
platforms.
2024-08-02 09:55:56 -07:00
Yuta Saito
2a6afeb50a
Implement _copyRegularFile for WASI without sendfile (#787)
WASI doesn't have `sendfile`, so we need to implement the copy in user
space with `read` and `write`. It's not as efficient as `sendfile`, but
it's the best we can do.
2024-08-01 10:50:04 -07:00
Yuta Saito
e90b6c3f90
Skip sticky-bit check in isDeletableFile on WASI (#785)
WASI does not surface the sticky bit and getuid, so we cannot check
whether the file is actually deletable before attempting to delete it.
2024-08-01 09:30:17 -07:00
Yuta Saito
0b3974d351
Guard out user/group related code on WASI (#783)
* Guard out user/group related code on WASI

This change guards out the user/group related code on WASI, as WASI does
not have the concept of users or groups.

* Throw explicit unsupported error if trying to set user or group on WASI

Instead of implicitly ignoring user-given values, we should throw
exception to make it clear that those values cannot be set on WASI.
2024-08-01 09:23:13 -07:00
Yuta Saito
fab7195ea2
Guard out extended or fs attributes related code on WASI (#784)
This commit guards out the extended attributes and file system
attributes related code on WASI as WASI does not support these
features. Just return nothing or ignore the set request.
2024-08-01 09:20:05 -07:00
Jeremy Schonfeld
3f646c2a5e
Remove unneccessary _FileManagerImpl.delegate (#801) 2024-07-31 17:00:58 -07:00
Jeremy Schonfeld
b4c77a35f9
Correctly set .fileTypeSymlink on windows (#773) 2024-07-26 13:14:19 -07:00
Jeremy Schonfeld
008613336a
Enable reading attributes of directory on Windows (#770) 2024-07-25 16:11:48 -07:00
Jeremy Schonfeld
196376b5fd
Use dynamic replacement instead of _typeByName for internationalization upcalls (#756)
* Use dynamic replacement instead of _typeByName for internationalization upcalls

* Make FOUNDATION_FRAMEWORK function non-dynamic

* Fix build failures
2024-07-23 14:43:01 -07:00
Jeremy Schonfeld
27439d1ecf
(130669494) Only provide .busy file attribute when true (#759) 2024-07-23 11:37:52 -07:00
Jeremy Schonfeld
b58b05100c
Fully enable posixPermissions attribute on Windows (#733) 2024-07-15 12:25:35 -07:00