swiftlang / swift-corelibs-foundation

The Foundation Project, providing core utilities, internationalization, and OS independence
swift.org
Apache License 2.0
5.27k stars 1.13k forks source link

[SR-13307] Default `mode` to `open(2)` is not same between Foundation and corelibs Foundation #3247

Open niw opened 4 years ago

niw commented 4 years ago
Previous ID SR-13307
Radar None
Original Reporter @niw
Type Bug
Environment macOS 10.15.6 Ubuntu 18.04 (Official swift:latest Docker image)
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | Foundation | |Labels | Bug | |Assignee | None | |Priority | Medium | md5: 915184f699d2e0392da90f9a7be55035

Issue Description:

For example, with following code, users expected to create a new file (if it doesn’t exist) with permission that respects current umask.

import Foundation
try! Data().write(to: URL(fileURLWithPath: "file"))

On macOS, In case umask is 022, it creates file with permission 0644 on macOS.
in case umask is 0, it creates file with permission 0666.
Therefore, default mode to open(2) seems having 0666.

However, on Linux, since corelibs Foundation is using default mode 0600 to open(2) (called via _CFOpenFileWithMode()), in case umask is 022, it creates file with permission 0600 instead of 0644.
https://github.com/apple/swift-corelibs-foundation/blame/master/Sources/Foundation/NSData.swift#L438

This behavior's inconsistency surprising Foundation library users.

niw commented 4 years ago

Probably this is a bug introduced by
https://github.com/apple/swift-corelibs-foundation/pull/1876
The original code is using 0666 for mode to open(2) but it changed to 0600.
cc: @spevans

niw commented 4 years ago

The patch is available at https://github.com/apple/swift-corelibs-foundation/pull/2851