diff --git a/copy.go b/copy.go index 2e6bf3d..5281503 100644 --- a/copy.go +++ b/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) } - 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 } }