2018-12-08 19:29:30 +01:00
|
|
|
package goutil
|
|
|
|
|
|
|
|
/*
|
|
|
|
Copied from https://github.com/otiai10/copy and slightly altered.
|
|
|
|
|
|
|
|
The MIT License (MIT)
|
|
|
|
|
2019-02-20 00:19:41 +01:00
|
|
|
Copyright (c) 2019 Alexander Weinhold
|
2018-12-08 19:29:30 +01:00
|
|
|
Copyright (c) 2018 otiai10
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2019-02-20 00:27:51 +01:00
|
|
|
"time"
|
2018-12-08 19:29:30 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Copy copies src to dest, doesn't matter if src is a directory or a file
|
|
|
|
func Copy(src, dest string) error {
|
2019-07-27 22:24:44 +02:00
|
|
|
return run(src, dest, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func CopyDirsLinkFiles(src, dest string) error {
|
|
|
|
return run(src, dest, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func run(src, dest string, linkFiles bool) error {
|
2018-12-08 19:29:30 +01:00
|
|
|
info, err := os.Lstat(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-07-27 22:24:44 +02:00
|
|
|
return copy(src, dest, info, linkFiles)
|
2018-12-08 19:29:30 +01:00
|
|
|
}
|
|
|
|
|
2019-07-27 22:24:44 +02:00
|
|
|
func copy(src, dest string, info os.FileInfo, linkFiles bool) error {
|
2018-12-08 19:29:30 +01:00
|
|
|
if info.Mode()&os.ModeSymlink != 0 {
|
|
|
|
return linkcopy(src, dest, info)
|
|
|
|
}
|
|
|
|
if info.IsDir() {
|
2019-07-27 22:24:44 +02:00
|
|
|
return dircopy(src, dest, info, linkFiles)
|
|
|
|
}
|
|
|
|
if !linkFiles {
|
|
|
|
return filecopy(src, dest, info)
|
|
|
|
} else {
|
|
|
|
return os.Link(src, dest)
|
2018-12-08 19:29:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func filecopy(src, dest string, info os.FileInfo) error {
|
|
|
|
if err := os.MkdirAll(filepath.Dir(dest), os.ModePerm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f, err := os.Create(dest)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
if err = os.Chmod(f.Name(), info.Mode()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s, err := os.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer s.Close()
|
|
|
|
_, err = io.Copy(f, s)
|
2019-02-20 00:27:51 +01:00
|
|
|
if err = os.Chtimes(f.Name(), time.Now().Local(), info.ModTime()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-12-08 19:29:30 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-07-27 22:24:44 +02:00
|
|
|
func dircopy(srcdir, destdir string, info os.FileInfo, linkFiles bool) error {
|
2018-12-08 19:29:30 +01:00
|
|
|
if err := os.MkdirAll(destdir, info.Mode()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
contents, err := ioutil.ReadDir(srcdir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, content := range contents {
|
|
|
|
cs, cd := filepath.Join(srcdir, content.Name()), filepath.Join(destdir, content.Name())
|
2019-07-27 22:24:44 +02:00
|
|
|
if err := copy(cs, cd, content, linkFiles); err != nil {
|
2018-12-08 19:29:30 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func linkcopy(src, dest string, info os.FileInfo) error {
|
|
|
|
src, err := os.Readlink(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return os.Symlink(src, dest)
|
|
|
|
}
|