MCPcopy Create free account
hub / github.com/celer-pkg/celer / CopyFile

Function CopyFile

pkgs/fileio/file.go:186–240  ·  view source on GitHub ↗

CopyFile copy file from src to dest.

(src, dest string)

Source from the content-addressed store, hash-verified

184
185// CopyFile copy file from src to dest.
186func CopyFile(src, dest string) error {
187 // Read file info.
188 info, err := os.Lstat(src)
189 if err != nil {
190 return err
191 }
192
193 // Create symlink if it's a symlink.
194 if info.Mode()&os.ModeSymlink != 0 {
195 target, err := os.Readlink(src)
196 if err != nil {
197 return err
198 }
199
200 // Remove dest if it exists before creating symlink.
201 if _, err := os.Lstat(dest); err == nil {
202 if err := os.Remove(dest); err != nil {
203 return err
204 }
205 }
206
207 return os.Symlink(target, dest)
208 }
209
210 // Copy normal file.
211 srcFile, err := os.Open(src)
212 if err != nil {
213 return err
214 }
215 defer srcFile.Close()
216
217 // Remove dest if it exists to avoid "permission denied" for read-only files.
218 if _, err := os.Lstat(dest); err == nil {
219 if err := os.Remove(dest); err != nil {
220 return err
221 }
222 }
223
224 destFile, err := os.Create(dest)
225 if err != nil {
226 return err
227 }
228 defer destFile.Close()
229
230 if _, err := io.Copy(destFile, srcFile); err != nil {
231 return err
232 }
233
234 // Set the same permissions as the source file.
235 if err := os.Chmod(dest, info.Mode()); err != nil {
236 return err
237 }
238
239 return nil
240}
241
242func RenameFile(src, dst string) error {
243 info, err := os.Lstat(src)

Callers 15

InstallFromPackageMethod · 0.92
doInstallFromPackageMethod · 0.92
installBinaryMethod · 0.92
installBinaryMethod · 0.92
installBinaryMethod · 0.92
CloneMethod · 0.92
ApplyPatchesMethod · 0.92
InstallMethod · 0.92
StoreMethod · 0.92
exportPortsMethod · 0.92

Calls 1

RemoveMethod · 0.45