2016-03-20 23:01:11 +01:00
|
|
|
// +build !windows
|
|
|
|
|
|
|
|
package getter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (g *FileGetter) Get(dst string, u *url.URL) error {
|
2016-08-25 01:48:59 +02:00
|
|
|
path := u.Path
|
|
|
|
if u.RawPath != "" {
|
|
|
|
path = u.RawPath
|
|
|
|
}
|
|
|
|
|
2016-03-20 23:01:11 +01:00
|
|
|
// The source path must exist and be a directory to be usable.
|
2016-08-25 01:48:59 +02:00
|
|
|
if fi, err := os.Stat(path); err != nil {
|
2016-03-20 23:01:11 +01:00
|
|
|
return fmt.Errorf("source path error: %s", err)
|
|
|
|
} else if !fi.IsDir() {
|
|
|
|
return fmt.Errorf("source path must be a directory")
|
|
|
|
}
|
|
|
|
|
|
|
|
fi, err := os.Lstat(dst)
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the destination already exists, it must be a symlink
|
|
|
|
if err == nil {
|
|
|
|
mode := fi.Mode()
|
|
|
|
if mode&os.ModeSymlink == 0 {
|
|
|
|
return fmt.Errorf("destination exists and is not a symlink")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the destination
|
|
|
|
if err := os.Remove(dst); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create all the parent directories
|
|
|
|
if err := os.MkdirAll(filepath.Dir(dst), 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-08-25 01:48:59 +02:00
|
|
|
return os.Symlink(path, dst)
|
2016-03-20 23:01:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *FileGetter) GetFile(dst string, u *url.URL) error {
|
2019-02-21 09:52:47 +01:00
|
|
|
ctx := g.Context()
|
2016-08-25 01:48:59 +02:00
|
|
|
path := u.Path
|
|
|
|
if u.RawPath != "" {
|
|
|
|
path = u.RawPath
|
|
|
|
}
|
|
|
|
|
2017-01-26 21:33:22 +01:00
|
|
|
// The source path must exist and be a file to be usable.
|
2016-08-25 01:48:59 +02:00
|
|
|
if fi, err := os.Stat(path); err != nil {
|
2016-03-20 23:01:11 +01:00
|
|
|
return fmt.Errorf("source path error: %s", err)
|
|
|
|
} else if fi.IsDir() {
|
|
|
|
return fmt.Errorf("source path must be a file")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := os.Lstat(dst)
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the destination already exists, it must be a symlink
|
|
|
|
if err == nil {
|
|
|
|
// Remove the destination
|
|
|
|
if err := os.Remove(dst); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create all the parent directories
|
|
|
|
if err := os.MkdirAll(filepath.Dir(dst), 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we're not copying, just symlink and we're done
|
|
|
|
if !g.Copy {
|
2016-08-25 01:48:59 +02:00
|
|
|
return os.Symlink(path, dst)
|
2016-03-20 23:01:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Copy
|
2016-08-25 01:48:59 +02:00
|
|
|
srcF, err := os.Open(path)
|
2016-03-20 23:01:11 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer srcF.Close()
|
|
|
|
|
|
|
|
dstF, err := os.Create(dst)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer dstF.Close()
|
|
|
|
|
2019-02-21 09:52:47 +01:00
|
|
|
_, err = Copy(ctx, dstF, srcF)
|
2016-03-20 23:01:11 +01:00
|
|
|
return err
|
|
|
|
}
|