offer hardlinking of files instead of full copy
This commit is contained in:
parent
e251b8616e
commit
1a369915ff
22
copy.go
22
copy.go
|
@ -37,21 +37,33 @@ import (
|
|||
|
||||
// Copy copies src to dest, doesn't matter if src is a directory or a file
|
||||
func Copy(src, dest string) error {
|
||||
return run(src, dest, false)
|
||||
}
|
||||
|
||||
func CopyDirsLinkFiles(src, dest string) error {
|
||||
return run(src, dest, true)
|
||||
}
|
||||
|
||||
func run(src, dest string, linkFiles bool) error {
|
||||
info, err := os.Lstat(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return copy(src, dest, info)
|
||||
return copy(src, dest, info, linkFiles)
|
||||
}
|
||||
|
||||
func copy(src, dest string, info os.FileInfo) error {
|
||||
func copy(src, dest string, info os.FileInfo, linkFiles bool) error {
|
||||
if info.Mode()&os.ModeSymlink != 0 {
|
||||
return linkcopy(src, dest, info)
|
||||
}
|
||||
if info.IsDir() {
|
||||
return dircopy(src, dest, info)
|
||||
return dircopy(src, dest, info, linkFiles)
|
||||
}
|
||||
if !linkFiles {
|
||||
return filecopy(src, dest, info)
|
||||
} else {
|
||||
return os.Link(src, dest)
|
||||
}
|
||||
}
|
||||
|
||||
func filecopy(src, dest string, info os.FileInfo) error {
|
||||
|
@ -78,7 +90,7 @@ func filecopy(src, dest string, info os.FileInfo) error {
|
|||
return err
|
||||
}
|
||||
|
||||
func dircopy(srcdir, destdir string, info os.FileInfo) error {
|
||||
func dircopy(srcdir, destdir string, info os.FileInfo, linkFiles bool) error {
|
||||
if err := os.MkdirAll(destdir, info.Mode()); err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -88,7 +100,7 @@ func dircopy(srcdir, destdir string, info os.FileInfo) error {
|
|||
}
|
||||
for _, content := range contents {
|
||||
cs, cd := filepath.Join(srcdir, content.Name()), filepath.Join(destdir, content.Name())
|
||||
if err := copy(cs, cd, content); err != nil {
|
||||
if err := copy(cs, cd, content, linkFiles); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user