Skip to content
This repository has been archived by the owner on Jul 16, 2022. It is now read-only.

Add atomic_folder #35

Closed
sotte opened this issue Aug 7, 2018 · 7 comments
Closed

Add atomic_folder #35

sotte opened this issue Aug 7, 2018 · 7 comments

Comments

@sotte
Copy link

sotte commented Aug 7, 2018

Sometimes I have a bunch of operations which create many files in a folder. The folder should only be there in the end of the operation. Something like this:

with atomic_folder("foo") as folder:
    unzip(something, folder)
    # "foo/" doesn't exist yet.
# now it does
@untitaker
Copy link
Owner

mkdir is already atomic though.

@sotte
Copy link
Author

sotte commented Aug 7, 2018

Yes, but I want the folder "foo" only to exists if the operation (in the example above unzip) succeeds. I don't want the result of a partially unpacked zip file.

@untitaker
Copy link
Owner

I guess that makes sense! I am open to PRs

@sotte
Copy link
Author

sotte commented Aug 8, 2018

I actually have a (buggy) version of the atomic_folder that I want to retire in favor of a maintained package. I'll check if I can recycle and contribute this.

@untitaker
Copy link
Owner

Sounds fine to me. I guess it's in the spirit of this package to provide a less buggy one.

@sotte sotte mentioned this issue Aug 12, 2018
6 tasks
@asottile
Copy link

afaik there isn't a way to make folder rename atomic

$ mv --no-target-directory dir1 dir2
mv: cannot move 'dir1' to 'dir2': Directory not empty

(you would then need to delete / rename which is always going to race with another process)

The usual trick is to use symlinks:

mkdir -p _targets/dir1 _targets/dir2
touch _targets/dir1/1 _targets/dir2/2
ln -s "$PWD/_targets/dir1" dir
ln -s "$PWD/_targets/dir2" tmpdir
mv tmpdir dir

but symlinks require platform support and sometimes the contents of the directories can be sensitive to their actual location vs the symlinked location

@untitaker
Copy link
Owner

@asottile I think the assumption was to force a value of overwrite=False for folders but even then I don't see how this will work out. I'll just close this.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants