100 lines
2.6 KiB
Go
100 lines
2.6 KiB
Go
|
package goutil
|
||
|
|
||
|
/*
|
||
|
Copied from https://github.com/otiai10/copy and slightly altered.
|
||
|
|
||
|
The MIT License (MIT)
|
||
|
|
||
|
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"
|
||
|
)
|
||
|
|
||
|
// Copy copies src to dest, doesn't matter if src is a directory or a file
|
||
|
func Copy(src, dest string) error {
|
||
|
info, err := os.Lstat(src)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
return copy(src, dest, info)
|
||
|
}
|
||
|
|
||
|
func copy(src, dest string, info os.FileInfo) error {
|
||
|
if info.Mode()&os.ModeSymlink != 0 {
|
||
|
return linkcopy(src, dest, info)
|
||
|
}
|
||
|
if info.IsDir() {
|
||
|
return dircopy(src, dest, info)
|
||
|
}
|
||
|
return filecopy(src, dest, info)
|
||
|
}
|
||
|
|
||
|
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)
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
func dircopy(srcdir, destdir string, info os.FileInfo) error {
|
||
|
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())
|
||
|
if err := copy(cs, cd, content); err != nil {
|
||
|
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)
|
||
|
}
|