FileManager posixPermissions effective mode across Apple SDK and Foundation on Linux
0 reputation · 20 Apr 2023, 00:14 UTC
0 reputation · 20 Apr 2023, 00:14 UTC
Goal: create directories and files with restrictive POSIX permissions using Foundation FileManager so other local users cannot read them by default.
The effective mode depends on both FileAttributeKey.posixPermissions and the process umask at creation time. FileProtectionKey adds runtime data protection on Apple platforms, but availability differs across macOS, iOS, iPadOS, tvOS, watchOS, and Foundation on Linux. It is unclear whether attributes apply before the item becomes visible to other processes or sync extensions, and whether callers must capture and restore umask around creation calls without affecting other threads.
Assumption: the code targets multiple deployment targets, including an Apple SDK release and a Linux Foundation port, and cannot assume identical default modes.
35275 reputation · 20 Apr 2023, 02:46 UTC
FileManager applies posixPermissions as a mask against the process umask at creation time; the effective mode is never more permissive than requested. For directories, createDirectory applies the attribute atomically to the final path, but intermediate directories receive default permissions. For files, createFile is not guaranteed to apply permissions atomically—on some platforms and Foundation versions the file may be created with a default mode and then adjusted afterward, creating a brief window where other users could read it. FileProtectionKey is orthogonal: it controls encrypted-at-rest behavior on iOS, iPadOS, tvOS, and watchOS, but is unavailable on macOS and Linux, and it does not replace POSIX permissions.
Confirmed: the effective mode equals requested permissions masked by umask. createDirectory with .posixPermissions sets the final directory's mode atomically. FileProtectionKey availability is platform-specific and excludes macOS and Linux.
Likely / verify locally: createFile may not apply permissions atomically. swift-corelibs-foundation on Linux follows POSIX semantics, but the exact timing of the permission change relative to file creation should be verified against the specific Foundation version.
0o700). This eliminates the cross-process visibility problem without touching umask..posixPermissions to createDirectory; the final directory is protected atomically.createFile returns, read back attributesOfItem(atPath:) and compare with stat to confirm the effective mode. If the parent directory was not already restricted, assume a race window exists..protectionKey only if you need locked-device encryption; select the case available on your minimum deployment target. On macOS and Linux, rely solely on POSIX modes.Is the parent directory already restricted to the current user? If yes, the createFile race window is contained and no further action is needed. If no, and the parent is world-readable, create the file in a temporary restricted subdirectory and then move it into place, or verify the specific Foundation version's createFile implementation.
Use comments to ask for clarification. Post a solution as an answer.
35,275 reputation · 20 Apr 2023, 03:16 UTC