-
Notifications
You must be signed in to change notification settings - Fork 1
/
io.go
75 lines (67 loc) · 1.53 KB
/
io.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package duckdbreplicator
import (
"io"
"os"
"path/filepath"
)
// copyDir copies a directory from source to destination
// It recursively copies all the contents of the source directory to the destination directory.
// Files with the same name in the destination directory will be overwritten.
func copyDir(dst, src string) error {
// Create the destination directory
err := os.MkdirAll(dst, os.ModePerm)
if err != nil {
return err
}
// Read the contents of the source directory
entries, err := os.ReadDir(src)
if err != nil {
return err
}
// Copy the contents of the source directory
for _, entry := range entries {
srcPath := filepath.Join(src, entry.Name())
dstPath := filepath.Join(dst, entry.Name())
if entry.IsDir() {
err = copyDir(dstPath, srcPath)
if err != nil {
return err
}
} else {
err = copyFile(dstPath, srcPath)
if err != nil {
return err
}
}
}
return nil
}
func copyFile(dst, src string) error {
// Open the source file
srcFile, err := os.Open(src)
if err != nil {
return err
}
defer srcFile.Close()
// Create the destination file
dstFile, err := os.Create(dst)
if err != nil {
return err
}
defer dstFile.Close()
// Copy the content from source to destination
_, err = io.Copy(dstFile, srcFile)
if err != nil {
return err
}
return nil
}
func fileSize(paths []string) int64 {
var size int64
for _, path := range paths {
if info, err := os.Stat(path); err == nil { // ignoring error since only error possible is *PathError
size += info.Size()
}
}
return size
}