Skip to content

filesystem.py: improve write_tmp_and_move and use throughout - #52653

Merged
alalazo merged 1 commit into
developfrom
hs/fix/mkstemp
Jul 6, 2026
Merged

alalazo merged 1 commit into
developfrom
hs/fix/mkstemp

Conversation

@haampie

@haampie haampie commented Jul 3, 2026 •

Copy link
Copy Markdown
Member

Closes #52651

The mkstemp + rename pattern is used in various places in Spack, which is
a reasonable way to get a new file in a given directory to atomically
replace a target file. The downside is that it doesn't respect umask, cause
it's supposed to be "safe" too (mode 0600).

That was realized in #52369 but not in #52041.

Instead, improve write_tmp_and_move to create a randomized temporary
file name opened with "x", which both keeps the atomicity guarantees and
respects umask. Permissions of an existing destination file are preserved, and
the temporary file is removed on failure.

Call sites can still do an fchmod on the yielded file.

(Not part of spack.package fortunately)

@haampie haampie added the v1.2.1 PRs to backport for v1.2.1 label Jul 3, 2026
The `mkstemp` + `rename` pattern is used in various in Spack, which is a
reasonable way to get a new file in a given directory to atomically
replace a target file.

The downside is that it doesn't respect umask, cause it's supposed to
"safe" with mode 0600.

Instead, improve `write_tmp_and_move` to create a randomized temporary
file name opened with "x" (O_CREAT | O_EXCL), which both keeps the
atomicity guarantees and creates new files with 0666 & ~umask like a
plain open(..., "w"). Permissions of an existing destination file are
preserved, and the temporary file is removed on failure.

Users can still do an `fchmod` with the yielded file.

Signed-off-by: Harmen Stoppels <[email protected]>
@haampie haampie changed the title Use write_tmp_and_move with O_EXCL throughout filesystem.py: improve write_tmp_and_move and use throughout Jul 3, 2026
@haampie haampie mentioned this pull request Jul 6, 2026
17 tasks done
@alalazo alalazo self-assigned this Jul 6, 2026
@alalazo
alalazo merged commit a31c502 into develop Jul 6, 2026
33 of 34 checks passed
@alalazo
alalazo deleted the hs/fix/mkstemp branch July 6, 2026 12:03
haampie added a commit that referenced this pull request Jul 6, 2026
The `mkstemp` + `rename` pattern is used in various places in Spack,
which is a reasonable way to get a new file in a given directory to
atomically replace a target file.

The downside is that it doesn't respect umask, cause it's supposed
to be "safe" too (mode 0600).

That was realized in #52369 but not in #52041.

Instead, improve `write_tmp_and_move` to create a randomized temporary
file name opened with "x", which both keeps the atomicity guarantees and
respects umask. Permissions of an existing destination file are preserved, and
the temporary file is removed on failure.

Call sites can still do an `fchmod` on the yielded file.

(Not part of `spack.package` fortunately)

Signed-off-by: Harmen Stoppels <[email protected]>
haampie added a commit that referenced this pull request Jul 6, 2026
The `mkstemp` + `rename` pattern is used in various places in Spack,
which is a reasonable way to get a new file in a given directory to
atomically replace a target file.

The downside is that it doesn't respect umask, cause it's supposed
to be "safe" too (mode 0600).

That was realized in #52369 but not in #52041.

Instead, improve `write_tmp_and_move` to create a randomized temporary
file name opened with "x", which both keeps the atomicity guarantees and
respects umask. Permissions of an existing destination file are preserved, and
the temporary file is removed on failure.

Call sites can still do an `fchmod` on the yielded file.

(Not part of `spack.package` fortunately)

Signed-off-by: Harmen Stoppels <[email protected]>
becker33 pushed a commit that referenced this pull request Jul 6, 2026
The `mkstemp` + `rename` pattern is used in various places in Spack,
which is a reasonable way to get a new file in a given directory to
atomically replace a target file.

The downside is that it doesn't respect umask, cause it's supposed
to be "safe" too (mode 0600).

That was realized in #52369 but not in #52041.

Instead, improve `write_tmp_and_move` to create a randomized temporary
file name opened with "x", which both keeps the atomicity guarantees and
respects umask. Permissions of an existing destination file are preserved, and
the temporary file is removed on failure.

Call sites can still do an `fchmod` on the yielded file.

(Not part of `spack.package` fortunately)

Signed-off-by: Harmen Stoppels <[email protected]>
Aiden2244 pushed a commit to Aiden2244/spack that referenced this pull request Jul 20, 2026
…#52653)

The `mkstemp` + `rename` pattern is used in various places in Spack,
which is a reasonable way to get a new file in a given directory to 
atomically replace a target file. 

The downside is that it doesn't respect umask, cause it's supposed 
to be "safe" too (mode 0600).

That was realized in spack#52369 but not in spack#52041.

Instead, improve `write_tmp_and_move` to create a randomized temporary
file name opened with "x", which both keeps the atomicity guarantees and
respects umask. Permissions of an existing destination file are preserved, and
the temporary file is removed on failure.

Call sites can still do an `fchmod` on the yielded file.

(Not part of `spack.package` fortunately)

Signed-off-by: Harmen Stoppels <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

config unit-tests v1.2.1 PRs to backport for v1.2.1

Projects

None yet

Development

Successfully merging this pull request may close these issues.

spack config always imposes 0600 permissions (v1.2 regression)

2 participants