Skip to content

Commit

Permalink
Only delete files older than 5min
Browse files Browse the repository at this point in the history
  • Loading branch information
f-blass committed May 21, 2024
1 parent 5deff18 commit afa9d17
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 14 deletions.
11 changes: 10 additions & 1 deletion util/configv3/load_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"math"
"os"
"path/filepath"
"time"

"code.cloudfoundry.org/cli/command/translatableerror"
"golang.org/x/crypto/ssh/terminal"
Expand Down Expand Up @@ -177,8 +178,16 @@ func removeOldTempConfigFiles() error {
}

for _, oldTempFileName := range oldTempFileNames {
fi, err := os.Lstat(oldTempFileName)
// ignore if file doesn't exist anymore due to race conditions if multiple cli commands are running in parallel
if err != nil && !errors.Is(err, os.ErrNotExist) {
return err
}
// only delete old orphans which are not caught by the signal handler in WriteConfig
if fi.ModTime().After(time.Now().Add(-5 * time.Minute)) {
continue
}
err = os.Remove(oldTempFileName)
// ignore if file doesn't exist anymore due to race conditions if multiple cli commands are running
if err != nil && !errors.Is(err, os.ErrNotExist) {
return err
}
Expand Down
53 changes: 40 additions & 13 deletions util/configv3/load_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"time"

"code.cloudfoundry.org/cli/command/translatableerror"
"code.cloudfoundry.org/cli/integration/helpers"
Expand Down Expand Up @@ -60,22 +61,48 @@ var _ = Describe("Config", func() {
})

When("there are old temp-config* files lingering from previous failed attempts to write the config", func() {
BeforeEach(func() {
configDir := filepath.Join(homeDir, ".cf")
Expect(os.MkdirAll(configDir, 0777)).To(Succeed())
for i := 0; i < 3; i++ {
tmpFile, fileErr := ioutil.TempFile(configDir, "temp-config")
Expect(fileErr).ToNot(HaveOccurred())
tmpFile.Close()
}
Context("and the files are younger than 5 minutes", func() {
BeforeEach(func() {
configDir := filepath.Join(homeDir, ".cf")
Expect(os.MkdirAll(configDir, 0777)).To(Succeed())
for i := 0; i < 3; i++ {
configDir := filepath.Join(homeDir, ".cf")
tmpFile, fileErr := ioutil.TempFile(configDir, "temp-config")
Expect(fileErr).ToNot(HaveOccurred())
tmpFile.Close()
}
})

It("keeps the files", func() {
Expect(loadErr).ToNot(HaveOccurred())

oldTempFileNames, configErr := filepath.Glob(filepath.Join(homeDir, ".cf", "temp-config?*"))
Expect(configErr).ToNot(HaveOccurred())
Expect(oldTempFileNames).To(HaveLen(3))
})
})

It("removes the lingering temp-config* files", func() {
Expect(loadErr).ToNot(HaveOccurred())
Context("and the files are older than 5 minutes", func() {
BeforeEach(func() {
configDir := filepath.Join(homeDir, ".cf")
Expect(os.MkdirAll(configDir, 0777)).To(Succeed())
for i := 0; i < 3; i++ {
tmpFile, fileErr := ioutil.TempFile(configDir, "temp-config")
Expect(fileErr).ToNot(HaveOccurred())
tmpFile.Close()
oldTime := time.Now().Add(-time.Minute * 10)
err := os.Chtimes(tmpFile.Name(), oldTime, oldTime)
Expect(err).ToNot(HaveOccurred())
}
})

oldTempFileNames, configErr := filepath.Glob(filepath.Join(homeDir, ".cf", "temp-config?*"))
Expect(configErr).ToNot(HaveOccurred())
Expect(oldTempFileNames).To(BeEmpty())
It("removes the lingering temp-config* files", func() {
Expect(loadErr).ToNot(HaveOccurred())

oldTempFileNames, configErr := filepath.Glob(filepath.Join(homeDir, ".cf", "temp-config?*"))
Expect(configErr).ToNot(HaveOccurred())
Expect(oldTempFileNames).To(BeEmpty())
})
})
})

Expand Down

0 comments on commit afa9d17

Please sign in to comment.