-
Notifications
You must be signed in to change notification settings - Fork 1
/
copyfile.go
35 lines (29 loc) · 927 Bytes
/
copyfile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package filework
import (
"io"
)
// CopyFile copies data from source to dest.
func CopyFile(sourceFS FileSystem, sourcePath string, source File, destFS FileSystemWriter, destPath string) Result {
action := Action{SourcePath: sourcePath, DestPath: destPath, Op: CopyOperation}
diff, err := CompareFileContent(source, destFS, destPath)
if err != nil {
return Result{Action: action, Outcome: Unchanged, Err: err}
}
if diff == Same {
return Result{Action: action, Outcome: Unchanged}
}
dest, err := destFS.Create(destPath)
if err != nil {
return Result{Action: action, Outcome: Unchanged, Err: err}
}
if closer, ok := dest.(io.Closer); ok {
defer closer.Close()
}
if _, err := io.Copy(dest, source); err != nil {
return Result{Action: action, Outcome: Modified, Err: err}
}
if diff == Missing {
return Result{Action: action, Outcome: Created}
}
return Result{Action: action, Outcome: Modified}
}