2021-01-09 13:50:14 +01:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Copies a file to a destination.
|
|
|
|
func Copy(src, dest string) (err error) {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
err = fmt.Errorf("copy: %s (panic)", r)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-10-31 15:01:48 +01:00
|
|
|
if err := os.MkdirAll(filepath.Dir(dest), ModeDir); err != nil {
|
2021-01-09 13:50:14 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
thisFile, err := os.Open(src)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer thisFile.Close()
|
|
|
|
|
2022-10-31 15:01:48 +01:00
|
|
|
destFile, err := os.OpenFile(dest, os.O_RDWR|os.O_CREATE, ModeFile)
|
2021-01-09 13:50:14 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer destFile.Close()
|
|
|
|
|
|
|
|
_, err = io.Copy(destFile, thisFile)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|