-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopydir.go
93 lines (79 loc) · 2.02 KB
/
copydir.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"fmt"
"io"
"os"
"path/filepath"
"time"
)
func TmpCopyDir(src string) (string, error) {
// Create copy of the directory in the OS temporary directory with the current timestamp
timenow := time.Now().UTC().Format(time.RFC3339)
dirName := filepath.Base(src)
tmpDirPath := fmt.Sprintf("%s/%s-%s",os.TempDir(), dirName, timenow)
if err := copyDir(src, tmpDirPath); err != nil {
return "", err
}
return tmpDirPath, nil
}
func copyDir(src string, dest string) error {
// Get properties of source dir
srcInfo, err := os.Stat(src)
if err != nil {
return err
}
// Create the destination directory
err = os.MkdirAll(dest, srcInfo.Mode())
if err != nil {
return err
}
// Get the list of files in the source directory
entries, err := os.ReadDir(src)
if err != nil {
return err
}
// Iterate over each file or directory
for _, entry := range entries {
srcPath := filepath.Join(src, entry.Name())
destPath := filepath.Join(dest, entry.Name())
if entry.IsDir() {
// Recursively copy sub-directories
err = copyDir(srcPath, destPath)
if err != nil {
return err
}
} else {
// Copy files
err = copyFile(srcPath, destPath)
if err != nil {
return err
}
}
}
return nil
}
func copyFile(src, dest string) error {
// Open the source file
srcFile, err := os.Open(src)
if err != nil {
return err
}
defer srcFile.Close()
// Create the destination file
destFile, err := os.Create(dest)
if err != nil {
return err
}
defer destFile.Close()
// Copy the file contents from src to dest
_, err = io.Copy(destFile, srcFile)
if err != nil {
return err
}
// Copy the file mode (permissions)
srcInfo, err := os.Stat(src)
if err != nil {
return err
}
return os.Chmod(dest, srcInfo.Mode())
}