Skip to content

Conversation

@eseliger
Copy link
Member

@eseliger eseliger commented Aug 15, 2022

When the download of a zip was aborted, it could've left behind a corrupted archive zip. This fixes it by first storing it to a temporary location and then creating the actual file atomically by doing os.Rename.

Closes https://github.com/sourcegraph/sourcegraph/issues/39743

Test plan

Verified things still work as expected.

When the download of a zip was aborted, it could've left behind a corrupted archive zip. This fixes it by first storing it to a temporary location and then creating the actual file atomically by doing os.Rename.
@eseliger eseliger marked this pull request as ready for review August 15, 2022 21:15
@eseliger eseliger requested a review from a team August 15, 2022 21:15
Comment on lines 340 to 343
tmpFileDir := fmt.Sprintf("%s.%d.tmp", dest, rand.Int())
// Ensure the file doesn't exist.
_ = os.Remove(tmpFileDir)
f, err := os.Create(tmpFileDir)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should use os.CreateTemp here — it does essentially the same thing, but in a slightly safer way. (We also need to ensure it creates the file in the same directory that it will end up in, as I think you already know, since rename operations usually can't cross filesystem boundaries.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about 8c04c68

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we'll need a defer func(path string) { _ = os.Remove(path) }(f.Name()) after the CreateTemp call, since there's a possible return path before the Rename, but otherwise LGTM.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. I also made sure we close the file before we rename it, although on macOS at least that doesn't seem to make a difference.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think that would only matter on Windows, and I'm not even sure there. No harm in being safe, though!

@eseliger eseliger merged commit d0a443b into main Aug 15, 2022
@eseliger eseliger deleted the es/no-corrupted-repos branch August 15, 2022 21:52
scjohns pushed a commit that referenced this pull request Apr 24, 2023
When the download of a zip was aborted, it could've left behind a corrupted archive zip. This fixes it by first storing it to a temporary location and then creating the actual file atomically by doing os.Rename.
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.

src: Investigate repo archive corruption

4 participants