MCPcopy Create free account
hub / github.com/gokcehan/lf / copyAll

Function copyAll

copy.go:101–187  ·  view source on GitHub ↗
(srcs []string, dstDir string, preserve []string)

Source from the content-addressed store, hash-verified

99}
100
101func copyAll(srcs []string, dstDir string, preserve []string) (nums chan int64, errs chan error) {
102 nums = make(chan int64, 1024)
103 errs = make(chan error, 1024)
104
105 go func() {
106 dirInfos := make(map[string]os.FileInfo)
107
108 for _, src := range srcs {
109 file := filepath.Base(src)
110 dst := filepath.Join(dstDir, file)
111
112 if lstat, err := os.Lstat(dst); err == nil {
113 ext := getFileExtension(lstat)
114 basename := file[:len(file)-len(ext)]
115 var newPath string
116 for i := 1; !os.IsNotExist(err); i++ {
117 file = strings.ReplaceAll(gOpts.dupfilefmt, "%f", basename+ext)
118 file = strings.ReplaceAll(file, "%b", basename)
119 file = strings.ReplaceAll(file, "%e", ext)
120 file = strings.ReplaceAll(file, "%n", strconv.Itoa(i))
121 newPath = filepath.Join(dstDir, file)
122 _, err = os.Lstat(newPath)
123 }
124 dst = newPath
125 }
126
127 if rel, err := filepath.Rel(src, dst); err == nil && rel != "." && filepath.IsLocal(rel) {
128 errs <- fmt.Errorf("cannot copy %s into a subdirectory of itself", src)
129 continue
130 }
131
132 err := filepath.Walk(src, func(path string, info os.FileInfo, err error) error {
133 if err != nil {
134 errs <- fmt.Errorf("walk: %w", err)
135 return nil
136 }
137 rel, err := filepath.Rel(src, path)
138 if err != nil {
139 errs <- fmt.Errorf("relative: %w", err)
140 return nil
141 }
142 newPath := filepath.Join(dst, rel)
143 switch {
144 case info.IsDir():
145 dstMode := os.ModePerm
146 if slices.Contains(preserve, "mode") {
147 dstMode = info.Mode()
148 }
149 if err := os.MkdirAll(newPath, dstMode); err != nil {
150 errs <- fmt.Errorf("mkdir: %w", err)
151 }
152 if slices.Contains(preserve, "timestamps") {
153 dirInfos[newPath] = info
154 }
155 nums <- info.Size()
156 case info.Mode()&os.ModeSymlink != 0:
157 if rlink, err := os.Readlink(path); err != nil {
158 errs <- fmt.Errorf("symlink: %w", err)

Callers 2

copyAsyncMethod · 0.85
moveAsyncMethod · 0.85

Calls 6

getFileExtensionFunction · 0.85
copyFileFunction · 0.85
IsDirMethod · 0.45
ModeMethod · 0.45
SizeMethod · 0.45
ModTimeMethod · 0.45

Tested by

no test coverage detected