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

change backup to use os.Link #1265

Merged
merged 1 commit into from
Aug 30, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 3 additions & 35 deletions renderer/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,10 @@ func AtomicWrite(path string, createDestDirs bool, contents []byte, perms os.Fil
}

// If we got this far, it means we are about to save the file. Copy the
// current contents of the file onto disk (if it exists) so we have a backup.
// current file so we have a backup. Note that os.Link preserves the Mode.
if backup {
if _, err := os.Stat(path); !os.IsNotExist(err) {
if err := copyFile(path, path+".bak"); err != nil {
return err
}
if err := os.Link(path, path+".bak"); err != nil {
log.Printf("[WARN] (runner) could not backup %q: %v", path, err)
}
}

Expand All @@ -178,33 +176,3 @@ func AtomicWrite(path string, createDestDirs bool, contents []byte, perms os.Fil

return nil
}

// copyFile copies the file at src to the path at dst. Any errors that occur
// are returned.
func copyFile(src, dst string) error {
s, err := os.Open(src)
if err != nil {
return err
}
defer s.Close()

stat, err := s.Stat()
if err != nil {
return err
}

d, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, stat.Mode())
if err != nil {
return err
}
if _, err := io.Copy(d, s); err != nil {
d.Close()
return err
}
if err := d.Close(); err != nil {
return err
}

// io.Copy can restrict file permissions based on umask.
return os.Chmod(dst, stat.Mode())
}