Skip to content

Create a file with permissions that respects umask #2858

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 10, 2020

Conversation

niw
Copy link
Contributor

@niw niw commented Aug 25, 2020

Problem

Data.write(to:) is a only method in the Foundation that can create a
regular file.
However, it ignores umask and always set 0600 permission unlike
macOS Foundation, which respects process umask.

Solution

  1. With .atomic write option

    It uses mkstemp(3) in _NSCreateTemporaryFile, which is always
    creating a file with 0600 permission, if the system follows
    the latest POSIX specification or the permission is undefined.

    On macOS Foundation, therefore _NSCreateTemporaryFile uses
    mktemp(3) and open(2) instead to respect umask.

  2. Without .atomic write option

    It uses 0o600 even if it uses open(2) that respects umask.
    Simply gives 0o666 instead.

This is a bug caused by previous commit in
#1876.

Swift JIRA is https://bugs.swift.org/browse/SR-13307.

NOTE: This patch addressed the issue on #2854, which reverts
original pull request #2851.

@spevans
Copy link
Contributor

spevans commented Aug 25, 2020

@swift-ci test

@millenomi
Copy link
Contributor

It looks like this was just heavy traffic triggering the type evaluation timeout.

@millenomi
Copy link
Contributor

@swift-ci please test

@niw
Copy link
Contributor Author

niw commented Aug 26, 2020

/home/buildnode/jenkins/workspace/swift-corelibs-foundation-PR-Linux/swift-corelibs-foundation/Sources/Foundation/NSData.swift:464:30: error: the compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions
16:20:59             let createMode = Int(S_IREAD | S_IWRITE | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
16:20:59                              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Hmm, intersting...

@niw
Copy link
Contributor Author

niw commented Aug 26, 2020

Oh, no, S_IREAD | S_IWRITE shouldn't be here. My bad.
Also now I understand that each flag are probably literal on Linux and that's why it takes long time to type checking.
Addressed these issues.

@niw niw force-pushed the fix_nsdata_ignores_umask branch from d01bd2f to 8fa17ed Compare August 26, 2020 02:55
@millenomi
Copy link
Contributor

@swift-ci please test

@niw
Copy link
Contributor Author

niw commented Aug 28, 2020

/home/buildnode/jenkins/workspace/swift-corelibs-foundation-PR-Linux@2/swift-corelibs-foundation/Sources/Foundation/NSData.swift:468:34: error: ambiguous use of 'S_IRUSR'
11:31:40             let createMode = Int(S_IRUSR) | Int(S_IWUSR) | Int(S_IRGRP) | Int(S_IWGRP) | Int(S_IROTH) | Int(S_IWOTH)
11:31:40                                  ^

Ugh, multi-platform change isn't easy really.

niw added 3 commits August 28, 2020 02:02
**Problem**

`Data.write(to:)` is a only method in the Foundation that can create a
regular file.
However, it ignores `umask` and always set 0600 permission unlike
macOS Foundation, which respects process `umask`.

**Solution**

1. With `.atomic` write option

    It uses `mkstemp(3)` in `_NSCreateTemporaryFile`, which is always
    creating a file with 0600 permission, if the system follows
    the latest POSIX specification or the permission is undefined.

    On macOS Foundation, therefore `_NSCreateTemporaryFile` uses
    `mktemp(3)` and `open(2)` instead to respect `umask`.

2. Without `.atomic` write option

    It uses `0o600` even if it uses `open(2)` that respects `umask`.
    Simply gives `0o666` instead.

This is a bug caused by previous commit in
swiftlang#1876.

Swift JIRA is https://bugs.swift.org/browse/SR-13307.
@niw niw force-pushed the fix_nsdata_ignores_umask branch from 9058f72 to 2c8ff0a Compare August 28, 2020 09:03
@spevans
Copy link
Contributor

spevans commented Aug 29, 2020

@swift-ci please test

1 similar comment
@spevans
Copy link
Contributor

spevans commented Aug 29, 2020

@swift-ci please test

@niw
Copy link
Contributor Author

niw commented Aug 29, 2020

05:02:09 Test Suite 'JobExecutorTests' started at 2020-08-29 12:02:09.126
05:02:09 Test Case 'JobExecutorTests.testStubProcessProtocol' started at 2020-08-29 12:02:09.126
05:02:09 Exited with signal code 4
03:59:39 ******************** TEST 'Swift-Unit :: ClangImporter/./SwiftClangImporterTests/ClangImporterTest.missingSubmodule' FAILED ********************
03:59:39 Note: Google Test filter = ClangImporterTest.missingSubmodule
03:59:39 [==========] Running 1 test from 1 test case.
03:59:39 [----------] Global test environment set-up.
03:59:39 [----------] 1 test from ClangImporterTest
03:59:39 [ RUN      ] ClangImporterTest.missingSubmodule
03:59:39 /Users/buildnode/jenkins/workspace/swift-corelibs-foundation-PR-macOS/branch-master/swift/unittests/ClangImporter/ClangImporterTests.cpp:194: Failure
03:59:39 Value of: context->getModule({ Located<Identifier>{ CLib2, {} } }) == nullptr
03:59:39   Actual: false
03:59:39 Expected: true
03:59:39 [  FAILED  ] ClangImporterTest.missingSubmodule (29 ms)
03:59:39 [----------] 1 test from ClangImporterTest (29 ms total)
03:59:39 
03:59:39 [----------] Global test environment tear-down
03:59:39 [==========] 1 test from 1 test case ran. (29 ms total)
03:59:39 [  PASSED  ] 0 tests.
03:59:39 [  FAILED  ] 1 test, listed below:
03:59:39 [  FAILED  ] ClangImporterTest.missingSubmodule
03:59:39 
03:59:39  1 FAILED TEST

Hmm... both CI failed on different position and... seems unrelated to the patch itself...? 🤔

@spevans
Copy link
Contributor

spevans commented Aug 30, 2020

@swift-ci please test

@millenomi millenomi merged commit abb1349 into swiftlang:master Sep 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants