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

plugins/bundle: use unique temporary files #4786

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 7 additions & 8 deletions plugins/bundle/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -653,10 +653,9 @@ func (p *Plugin) configDelta(newConfig *Config) (map[string]*Source, map[string]
func (p *Plugin) saveBundleToDisk(name string, raw io.Reader) error {

bundleDir := filepath.Join(p.bundlePersistPath, name)
tmpFile := filepath.Join(bundleDir, ".bundle.tar.gz.tmp")
bundleFile := filepath.Join(bundleDir, "bundle.tar.gz")

saveErr := saveCurrentBundleToDisk(bundleDir, ".bundle.tar.gz.tmp", raw)
tmpFile, saveErr := saveCurrentBundleToDisk(bundleDir, raw)
if saveErr != nil {
p.log(name).Error("Failed to save new bundle to disk: %v", saveErr)

Expand All @@ -674,26 +673,26 @@ func (p *Plugin) saveBundleToDisk(name string, raw io.Reader) error {
return os.Rename(tmpFile, bundleFile)
}

func saveCurrentBundleToDisk(path, filename string, raw io.Reader) error {
func saveCurrentBundleToDisk(path string, raw io.Reader) (string, error) {
if _, err := os.Stat(path); os.IsNotExist(err) {
err = os.MkdirAll(path, os.ModePerm)
if err != nil {
return err
return "", err
}
}

if raw == nil {
return fmt.Errorf("no raw bundle bytes to persist to disk")
return "", fmt.Errorf("no raw bundle bytes to persist to disk")
}

dest, err := os.OpenFile(filepath.Join(path, filename), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
dest, err := os.CreateTemp(path, ".bundle.tar.gz.*.tmp")
Copy link
Contributor

Choose a reason for hiding this comment

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

f, err := OpenFile(name, O_RDWR|O_CREATE|O_EXCL, 0600)

this is what's called under the hood, so it's changed a bit -- but I think that's OK.

if err != nil {
return err
return "", err
}
defer dest.Close()

_, err = io.Copy(dest, raw)
return err
return dest.Name(), err
}

func loadBundleFromDisk(path, name string, src *Source) (*bundle.Bundle, error) {
Expand Down
6 changes: 3 additions & 3 deletions plugins/bundle/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2530,16 +2530,16 @@ func TestSaveCurrentBundleToDisk(t *testing.T) {

defer os.RemoveAll(srcDir)

err = saveCurrentBundleToDisk(srcDir, "bundle.tar.gz", getTestRawBundle(t))
bundlePath, err := saveCurrentBundleToDisk(srcDir, getTestRawBundle(t))
if err != nil {
t.Fatalf("unexpected error %v", err)
}

if _, err := os.Stat(filepath.Join(srcDir, "bundle.tar.gz")); err != nil {
if _, err := os.Stat(bundlePath); err != nil {
t.Fatalf("unexpected error %v", err)
}

err = saveCurrentBundleToDisk(srcDir, "bundle.tar.gz", nil)
_, err = saveCurrentBundleToDisk(srcDir, nil)
if err == nil {
t.Fatal("expected error but got nil")
}
Expand Down