Merge pull request #3318 from hashicorp/issue_3207

Clean up temporary files on write errors, and ignore any temporary se…
pull/3321/head
preetapan 7 years ago committed by GitHub
commit 56d651f991

@ -5,6 +5,7 @@ FEATURES:
IMPROVEMENTS: IMPROVEMENTS:
BUG FIXES: BUG FIXES:
* agent: Clean up temporary files during disk write errors when persisting services and checks. [GH-3207]
## 0.9.0 (July 20, 2017) ## 0.9.0 (July 20, 2017)

@ -1485,15 +1485,25 @@ func writeFileAtomic(path string, contents []byte) error {
return err return err
} }
if _, err := fh.Write(contents); err != nil { if _, err := fh.Write(contents); err != nil {
fh.Close()
os.Remove(tempPath)
return err return err
} }
if err := fh.Sync(); err != nil { if err := fh.Sync(); err != nil {
fh.Close()
os.Remove(tempPath)
return err return err
} }
if err := fh.Close(); err != nil { if err := fh.Close(); err != nil {
fh.Close()
os.Remove(tempPath)
return err
}
if err := os.Rename(tempPath, path); err != nil {
os.Remove(tempPath)
return err return err
} }
return os.Rename(tempPath, path) return nil
} }
// AddService is used to add a service entry. // AddService is used to add a service entry.
@ -2072,6 +2082,12 @@ func (a *Agent) loadServices(conf *Config) error {
continue continue
} }
// Skip all partially written temporary files
if strings.HasSuffix(fi.Name(), "tmp") {
a.logger.Printf("[WARN] Ignoring temporary service file %v", fi.Name())
continue
}
// Open the file for reading // Open the file for reading
file := filepath.Join(svcDir, fi.Name()) file := filepath.Join(svcDir, fi.Name())
fh, err := os.Open(file) fh, err := os.Open(file)

Loading…
Cancel
Save