offer hardlinking of files instead of full copy

This commit is contained in:
gutmet 2019-07-27 22:24:44 +02:00
parent e251b8616e
commit 1a369915ff

24
copy.go
View File

@ -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)
}
return filecopy(src, dest, info)
}
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
}
}