2017-01-31 23:17:33 +00:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CopyDirectory copies the directory from src to dst.
|
|
|
|
// Most efficient copy of files is attempted.
|
|
|
|
func CopyDirectory(dst, src string) error {
|
|
|
|
inodes := map[uint64]string{}
|
|
|
|
return copyDirectory(dst, src, inodes)
|
|
|
|
}
|
|
|
|
|
|
|
|
func copyDirectory(dst, src string, inodes map[uint64]string) error {
|
|
|
|
stat, err := os.Stat(src)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to stat %s", src)
|
|
|
|
}
|
|
|
|
if !stat.IsDir() {
|
|
|
|
return errors.Errorf("source is not directory")
|
|
|
|
}
|
|
|
|
|
2017-02-01 22:23:56 +00:00
|
|
|
if st, err := os.Stat(dst); err != nil {
|
2017-01-31 23:17:33 +00:00
|
|
|
if err := os.Mkdir(dst, stat.Mode()); err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to mkdir %s", dst)
|
|
|
|
}
|
2017-02-01 22:23:56 +00:00
|
|
|
} else if !st.IsDir() {
|
|
|
|
return errors.Errorf("cannot copy to non-directory: %s", dst)
|
2017-01-31 23:17:33 +00:00
|
|
|
} else {
|
|
|
|
if err := os.Chmod(dst, stat.Mode()); err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to chmod on %s", dst)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fis, err := ioutil.ReadDir(src)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to read %s", src)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := copyFileInfo(stat, dst); err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to copy file info for %s", dst)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, fi := range fis {
|
|
|
|
source := filepath.Join(src, fi.Name())
|
|
|
|
target := filepath.Join(dst, fi.Name())
|
2017-02-01 22:23:56 +00:00
|
|
|
|
|
|
|
switch {
|
|
|
|
case fi.IsDir():
|
2017-01-31 23:17:33 +00:00
|
|
|
if err := copyDirectory(target, source, inodes); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
continue
|
2017-02-01 22:23:56 +00:00
|
|
|
case (fi.Mode() & os.ModeType) == 0:
|
2017-01-31 23:17:33 +00:00
|
|
|
link, err := GetLinkSource(target, fi, inodes)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to get hardlink")
|
|
|
|
}
|
|
|
|
if link != "" {
|
|
|
|
if err := os.Link(link, target); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to create hard link")
|
|
|
|
}
|
|
|
|
} else if err := copyFile(source, target); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to copy files")
|
|
|
|
}
|
2017-02-01 22:23:56 +00:00
|
|
|
case (fi.Mode() & os.ModeSymlink) == os.ModeSymlink:
|
2017-01-31 23:17:33 +00:00
|
|
|
link, err := os.Readlink(source)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to read link: %s", source)
|
|
|
|
}
|
|
|
|
if err := os.Symlink(link, target); err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to create symlink: %s", target)
|
|
|
|
}
|
2017-02-01 22:23:56 +00:00
|
|
|
case (fi.Mode() & os.ModeDevice) == os.ModeDevice:
|
|
|
|
if err := copyDevice(target, fi); err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to create device")
|
|
|
|
}
|
|
|
|
default:
|
2017-01-31 23:17:33 +00:00
|
|
|
// TODO: Support pipes and sockets
|
|
|
|
return errors.Wrapf(err, "unsupported mode %s", fi.Mode())
|
|
|
|
}
|
|
|
|
if err := copyFileInfo(fi, target); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to copy file info")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := copyXAttrs(target, source); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to copy xattrs")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func copyFile(source, target string) error {
|
|
|
|
src, err := os.Open(source)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to open source %s", err)
|
|
|
|
}
|
|
|
|
defer src.Close()
|
|
|
|
tgt, err := os.Create(target)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to open target %s", err)
|
|
|
|
}
|
|
|
|
defer tgt.Close()
|
|
|
|
|
|
|
|
return copyFileContent(tgt, src)
|
|
|
|
}
|