| 139 | } |
| 140 | |
| 141 | func copyFileContents(in io.Reader, out syncWriteCloser, dst string) (err error) { |
| 142 | removePartial := false |
| 143 | |
| 144 | defer func() { |
| 145 | if closeErr := out.Close(); closeErr != nil && err == nil { |
| 146 | err = closeErr |
| 147 | } |
| 148 | if removePartial { |
| 149 | if removeErr := os.Remove(dst); removeErr != nil { |
| 150 | fileutilLog.Printf("Failed to remove partial destination file during cleanup: %s", removeErr) |
| 151 | } |
| 152 | } |
| 153 | }() |
| 154 | |
| 155 | if _, err = io.Copy(out, in); err != nil { |
| 156 | removePartial = true |
| 157 | return err |
| 158 | } |
| 159 | |
| 160 | return out.Sync() |
| 161 | } |
| 162 | |
| 163 | // CopyFile copies a file from src to dst using buffered IO. |
| 164 | func CopyFile(src, dst string) error { |