-
Notifications
You must be signed in to change notification settings - Fork 464
Create temp file in target directory to prevent cross-device link error #1972
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
base: master
Are you sure you want to change the base?
Changes from all commits
428b4f9
e7efa02
f2ec003
bbf200b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -399,7 +399,10 @@ def _open(self): | |
| self.f = compress(self.f, mode=self.mode) | ||
| else: | ||
| # TODO: check if path is writable? | ||
| i, name = tempfile.mkstemp() | ||
| i, name = tempfile.mkstemp( | ||
| dir=os.path.dirname(self.path) or None, | ||
| prefix=os.path.basename(self.path) + "-", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. side effect of moving the temp next to the target: it is now visible to listings while the transaction is open, so ls/glob("*") on that directory returns things like "file.txt-8xk2p9" until commit, which they never did before. prefixing with a dot instead ("."+basename+"-") would at least keep it out of the common glob patterns. |
||
| ) | ||
| os.close(i) # we want normal open and normal buffered file | ||
| self.temp = name | ||
| self.f = open(name, mode=self.mode) | ||
|
|
@@ -438,22 +441,12 @@ def __getstate__(self): | |
| def commit(self): | ||
| if self.autocommit: | ||
| raise RuntimeError("Can only commit if not already set to autocommit") | ||
| shutil.move(self.temp, self.path) | ||
| try: | ||
| shutil.move(self.temp, self.path) | ||
| except PermissionError as e: | ||
| # shutil.move raises PermissionError if os.rename | ||
| # and the default copy2 fallback with shutil.copystats fail. | ||
| # The file should be there nonetheless, but without copied permissions. | ||
| # If it doesn't exist, there was no permission to create the file. | ||
| if not os.path.exists(self.path): | ||
| raise e | ||
| else: | ||
| # If PermissionError is not raised, permissions can be set. | ||
| try: | ||
| mask = 0o666 | ||
| os.chmod(self.path, mask & ~get_umask(mask)) | ||
| except RuntimeError: | ||
| pass | ||
| mask = 0o666 | ||
| os.chmod(self.path, mask & ~get_umask(mask)) | ||
| except (RuntimeError, PermissionError): | ||
| pass | ||
|
|
||
| def discard(self): | ||
| if self.autocommit: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we are not using a system temporary location, we could just use normal open() and avoid the chmod call further down. The only thing mkstemp is doing for us here is generating the filename with some uuid.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
keeping mkstemp is fine since it still works and avoids breaking umask cache tests
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry, which exactly?