Codebase list golang-github-rakyll-statik / 68f5d96
remove the temporary source file if we copied byte by byte. Burcu Dogan 9 years ago
1 changed file(s) with 8 addition(s) and 7 deletion(s). Raw diff Collapse all Expand all
5959 }
6060
6161 func copy(src, dest string) error {
62 // Try to rename generated source ...
62 // Try to rename generated source.
6363 if err := os.Rename(src, dest); err == nil {
6464 return nil
6565 }
66
67 // ... if the rename failed (might do so due to temporary file residing on a
68 // different device) try to copy byte by byte.
69
66 // If the rename failed (might do so due to temporary file residing on a
67 // different device), try to copy byte by byte.
7068 rc, err := os.Open(src)
7169 if err != nil {
7270 return err
7371 }
74 defer rc.Close()
72 defer func() {
73 rc.Close()
74 os.Remove(src) // ignore the error, source is in tmp.
75 }()
7576
7677 if _, err = os.Stat(dest); !os.IsNotExist(err) {
7778 return fmt.Errorf("file %q already exists", dest)
8586
8687 if _, err = io.Copy(wc, rc); err != nil {
8788 // Delete remains of failed copy attempt.
88 _ = os.Remove(dest)
89 os.Remove(dest)
8990 }
9091 return err
9192 }