2019-05-15 18:05:51 +02:00
|
|
|
package shutil
|
|
|
|
|
|
|
|
import (
|
2020-11-13 18:42:10 +01:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2019-05-15 18:05:51 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type SameFileError struct {
|
2020-11-13 18:42:10 +01:00
|
|
|
Src string
|
|
|
|
Dst string
|
2019-05-15 18:05:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e SameFileError) Error() string {
|
2020-11-13 18:42:10 +01:00
|
|
|
return fmt.Sprintf("%s and %s are the same file", e.Src, e.Dst)
|
2019-05-15 18:05:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type SpecialFileError struct {
|
2020-11-13 18:42:10 +01:00
|
|
|
File string
|
|
|
|
FileInfo os.FileInfo
|
2019-05-15 18:05:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e SpecialFileError) Error() string {
|
2020-11-13 18:42:10 +01:00
|
|
|
return fmt.Sprintf("`%s` is a named pipe", e.File)
|
2019-05-15 18:05:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type NotADirectoryError struct {
|
2020-11-13 18:42:10 +01:00
|
|
|
Src string
|
2019-05-15 18:05:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e NotADirectoryError) Error() string {
|
2020-11-13 18:42:10 +01:00
|
|
|
return fmt.Sprintf("`%s` is not a directory", e.Src)
|
2019-05-15 18:05:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type AlreadyExistsError struct {
|
2020-11-13 18:42:10 +01:00
|
|
|
Dst string
|
2019-05-15 18:05:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e AlreadyExistsError) Error() string {
|
2020-11-13 18:42:10 +01:00
|
|
|
return fmt.Sprintf("`%s` already exists", e.Dst)
|
2019-05-15 18:05:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func samefile(src string, dst string) bool {
|
2020-11-13 18:42:10 +01:00
|
|
|
srcInfo, _ := os.Stat(src)
|
|
|
|
dstInfo, _ := os.Stat(dst)
|
|
|
|
return os.SameFile(srcInfo, dstInfo)
|
2019-05-15 18:05:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func specialfile(fi os.FileInfo) bool {
|
2020-11-13 18:42:10 +01:00
|
|
|
return (fi.Mode() & os.ModeNamedPipe) == os.ModeNamedPipe
|
2019-05-15 18:05:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func stringInSlice(a string, list []string) bool {
|
2020-11-13 18:42:10 +01:00
|
|
|
for _, b := range list {
|
|
|
|
if b == a {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
2019-05-15 18:05:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func IsSymlink(fi os.FileInfo) bool {
|
2020-11-13 18:42:10 +01:00
|
|
|
return (fi.Mode() & os.ModeSymlink) == os.ModeSymlink
|
2019-05-15 18:05:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Copy data from src to dst
|
|
|
|
//
|
|
|
|
// If followSymlinks is not set and src is a symbolic link, a
|
|
|
|
// new symlink will be created instead of copying the file it points
|
|
|
|
// to.
|
2020-11-13 18:42:10 +01:00
|
|
|
func CopyFile(src, dst string, followSymlinks bool) error {
|
|
|
|
if samefile(src, dst) {
|
|
|
|
return &SameFileError{src, dst}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure src exists and neither are special files
|
|
|
|
srcStat, err := os.Lstat(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if specialfile(srcStat) {
|
|
|
|
return &SpecialFileError{src, srcStat}
|
|
|
|
}
|
|
|
|
|
|
|
|
dstStat, err := os.Stat(dst)
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
} else if err == nil {
|
|
|
|
if specialfile(dstStat) {
|
|
|
|
return &SpecialFileError{dst, dstStat}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we don't follow symlinks and it's a symlink, just link it and be done
|
|
|
|
if !followSymlinks && IsSymlink(srcStat) {
|
|
|
|
return os.Symlink(src, dst)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we are a symlink, follow it
|
|
|
|
if IsSymlink(srcStat) {
|
|
|
|
src, err = os.Readlink(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
srcStat, err = os.Stat(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do the actual copy
|
|
|
|
fsrc, err := os.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer fsrc.Close()
|
|
|
|
|
|
|
|
fdst, err := os.Create(dst)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer fdst.Close()
|
|
|
|
|
|
|
|
size, err := io.Copy(fdst, fsrc)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if size != srcStat.Size() {
|
|
|
|
return fmt.Errorf("%s: %d/%d copied", src, size, srcStat.Size())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2019-05-15 18:05:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Copy mode bits from src to dst.
|
|
|
|
//
|
|
|
|
// If followSymlinks is false, symlinks aren't followed if and only
|
|
|
|
// if both `src` and `dst` are symlinks. If `lchmod` isn't available
|
|
|
|
// and both are symlinks this does nothing. (I don't think lchmod is
|
|
|
|
// available in Go)
|
|
|
|
func CopyMode(src, dst string, followSymlinks bool) error {
|
2020-11-13 18:42:10 +01:00
|
|
|
srcStat, err := os.Lstat(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
dstStat, err := os.Lstat(dst)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// They are both symlinks and we can't change mode on symlinks.
|
|
|
|
if !followSymlinks && IsSymlink(srcStat) && IsSymlink(dstStat) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Atleast one is not a symlink, get the actual file stats
|
|
|
|
srcStat, _ = os.Stat(src)
|
|
|
|
err = os.Chmod(dst, srcStat.Mode())
|
|
|
|
return err
|
2019-05-15 18:05:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Copy data and mode bits ("cp src dst"). Return the file's destination.
|
|
|
|
//
|
|
|
|
// The destination may be a directory.
|
|
|
|
//
|
|
|
|
// If followSymlinks is false, symlinks won't be followed. This
|
|
|
|
// resembles GNU's "cp -P src dst".
|
|
|
|
//
|
|
|
|
// If source and destination are the same file, a SameFileError will be
|
|
|
|
// rased.
|
2020-11-13 18:42:10 +01:00
|
|
|
func Copy(src, dst string, followSymlinks bool) (string, error) {
|
|
|
|
dstInfo, err := os.Stat(dst)
|
2019-05-15 18:05:51 +02:00
|
|
|
|
2020-11-13 18:42:10 +01:00
|
|
|
if err == nil && dstInfo.Mode().IsDir() {
|
|
|
|
dst = filepath.Join(dst, filepath.Base(src))
|
|
|
|
}
|
2019-05-15 18:05:51 +02:00
|
|
|
|
2020-11-13 18:42:10 +01:00
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
return dst, err
|
|
|
|
}
|
2019-05-15 18:05:51 +02:00
|
|
|
|
2020-11-13 18:42:10 +01:00
|
|
|
err = CopyFile(src, dst, followSymlinks)
|
|
|
|
if err != nil {
|
|
|
|
return dst, err
|
|
|
|
}
|
2019-05-15 18:05:51 +02:00
|
|
|
|
2020-11-13 18:42:10 +01:00
|
|
|
err = CopyMode(src, dst, followSymlinks)
|
|
|
|
if err != nil {
|
|
|
|
return dst, err
|
|
|
|
}
|
2019-05-15 18:05:51 +02:00
|
|
|
|
2020-11-13 18:42:10 +01:00
|
|
|
return dst, nil
|
2019-05-15 18:05:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type CopyTreeOptions struct {
|
2020-11-13 18:42:10 +01:00
|
|
|
Symlinks bool
|
|
|
|
IgnoreDanglingSymlinks bool
|
|
|
|
CopyFunction func(string, string, bool) (string, error)
|
|
|
|
Ignore func(string, []os.FileInfo) []string
|
2019-05-15 18:05:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Recursively copy a directory tree.
|
|
|
|
//
|
|
|
|
// The destination directory must not already exist.
|
|
|
|
//
|
|
|
|
// If the optional Symlinks flag is true, symbolic links in the
|
|
|
|
// source tree result in symbolic links in the destination tree; if
|
|
|
|
// it is false, the contents of the files pointed to by symbolic
|
|
|
|
// links are copied. If the file pointed by the symlink doesn't
|
|
|
|
// exist, an error will be returned.
|
|
|
|
//
|
|
|
|
// You can set the optional IgnoreDanglingSymlinks flag to true if you
|
|
|
|
// want to silence this error. Notice that this has no effect on
|
|
|
|
// platforms that don't support os.Symlink.
|
|
|
|
//
|
|
|
|
// The optional ignore argument is a callable. If given, it
|
|
|
|
// is called with the `src` parameter, which is the directory
|
|
|
|
// being visited by CopyTree(), and `names` which is the list of
|
|
|
|
// `src` contents, as returned by ioutil.ReadDir():
|
|
|
|
//
|
|
|
|
// callable(src, entries) -> ignoredNames
|
|
|
|
//
|
|
|
|
// Since CopyTree() is called recursively, the callable will be
|
|
|
|
// called once for each directory that is copied. It returns a
|
|
|
|
// list of names relative to the `src` directory that should
|
|
|
|
// not be copied.
|
|
|
|
//
|
|
|
|
// The optional copyFunction argument is a callable that will be used
|
|
|
|
// to copy each file. It will be called with the source path and the
|
|
|
|
// destination path as arguments. By default, Copy() is used, but any
|
|
|
|
// function that supports the same signature (like Copy2() when it
|
|
|
|
// exists) can be used.
|
|
|
|
func CopyTree(src, dst string, options *CopyTreeOptions) error {
|
2020-11-13 18:42:10 +01:00
|
|
|
if options == nil {
|
|
|
|
options = &CopyTreeOptions{Symlinks: false,
|
|
|
|
Ignore: nil,
|
|
|
|
CopyFunction: Copy,
|
|
|
|
IgnoreDanglingSymlinks: false}
|
|
|
|
}
|
|
|
|
|
|
|
|
srcFileInfo, err := os.Stat(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !srcFileInfo.IsDir() {
|
|
|
|
return &NotADirectoryError{src}
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = os.Open(dst)
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return &AlreadyExistsError{dst}
|
|
|
|
}
|
|
|
|
|
|
|
|
entries, err := ioutil.ReadDir(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.MkdirAll(dst, srcFileInfo.Mode())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ignoredNames := []string{}
|
|
|
|
if options.Ignore != nil {
|
|
|
|
ignoredNames = options.Ignore(src, entries)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, entry := range entries {
|
|
|
|
if stringInSlice(entry.Name(), ignoredNames) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
srcPath := filepath.Join(src, entry.Name())
|
|
|
|
dstPath := filepath.Join(dst, entry.Name())
|
|
|
|
|
|
|
|
entryFileInfo, err := os.Lstat(srcPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deal with symlinks
|
|
|
|
if IsSymlink(entryFileInfo) {
|
|
|
|
linkTo, err := os.Readlink(srcPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if options.Symlinks {
|
|
|
|
os.Symlink(linkTo, dstPath)
|
|
|
|
//CopyStat(srcPath, dstPath, false)
|
|
|
|
} else {
|
|
|
|
// ignore dangling symlink if flag is on
|
|
|
|
_, err = os.Stat(linkTo)
|
|
|
|
if os.IsNotExist(err) && options.IgnoreDanglingSymlinks {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
_, err = options.CopyFunction(srcPath, dstPath, false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if entryFileInfo.IsDir() {
|
|
|
|
err = CopyTree(srcPath, dstPath, options)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_, err = options.CopyFunction(srcPath, dstPath, false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2019-05-15 18:05:51 +02:00
|
|
|
}
|