Skip to content
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

proposal: os: extend Root with Rename #73041

Closed
adam-azarchs opened this issue Mar 25, 2025 · 1 comment
Closed

proposal: os: extend Root with Rename #73041

adam-azarchs opened this issue Mar 25, 2025 · 1 comment
Labels
Milestone

Comments

@adam-azarchs
Copy link
Contributor

Proposal Details

While there are certainly other use cases, my primary motivation here is the fairly common pattern of

  1. Create a temporary file
  2. Write content to the temporary file
  3. Rename the temporary file to to final expected location.

This is done for ensuring that the file at the final destination location is never an incompletely written file, particularly in cases where the destination is being replaced1. Aside from all of the path traversal issues which originally motivated os.Root, using it for this solves an additional problem, at least in cases like linux where renameat can be used, where this procedure can misbehave if the directory gets renamed between the first and last step.

I will open a separate issue for the CreateTemp part of this, since they're fairly separate features and might have differing implementation difficulties.

See google/renameio#44 for additional discussion.

Footnotes

  1. renaming the temp file over the existing file is usually atomic in Unix filesystems; Windows doesn't allow renaming one file over another but even so this technique at least drastically shortens the window for potential corruption.

@neild
Copy link
Contributor

neild commented Apr 1, 2025

Rename is actually part of the accepted proposal in #67002: #67002 (comment)

It was implemented in https://go.dev/cl/659416 and will be in Go 1.25.

@neild neild closed this as completed Apr 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants