mirror of
https://github.com/anchore/grype
synced 2024-11-10 06:34:13 +00:00
0781fb028b
The io/ioutil package has been deprecated as of Go 1.16, see https://golang.org/doc/go1.16#ioutil. This commit replaces the existing io/ioutil functions with their new definitions in io and os packages. Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
68 lines
1.3 KiB
Go
68 lines
1.3 KiB
Go
package file
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path"
|
|
|
|
"github.com/spf13/afero"
|
|
)
|
|
|
|
func CopyDir(fs afero.Fs, src string, dst string) error {
|
|
var err error
|
|
var fds []os.DirEntry
|
|
var srcinfo os.FileInfo
|
|
|
|
if srcinfo, err = fs.Stat(src); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err = fs.MkdirAll(dst, srcinfo.Mode()); err != nil {
|
|
return err
|
|
}
|
|
|
|
if fds, err = os.ReadDir(src); err != nil {
|
|
return err
|
|
}
|
|
for _, fd := range fds {
|
|
srcPath := path.Join(src, fd.Name())
|
|
dstPath := path.Join(dst, fd.Name())
|
|
|
|
if fd.IsDir() {
|
|
if err = CopyDir(fs, srcPath, dstPath); err != nil {
|
|
return fmt.Errorf("could not copy dir (%s -> %s): %w", srcPath, dstPath, err)
|
|
}
|
|
} else {
|
|
if err = CopyFile(fs, srcPath, dstPath); err != nil {
|
|
return fmt.Errorf("could not copy file (%s -> %s): %w", srcPath, dstPath, err)
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func CopyFile(fs afero.Fs, src, dst string) error {
|
|
var err error
|
|
var srcFd afero.File
|
|
var dstFd afero.File
|
|
var srcinfo os.FileInfo
|
|
|
|
if srcFd, err = fs.Open(src); err != nil {
|
|
return err
|
|
}
|
|
defer srcFd.Close()
|
|
|
|
if dstFd, err = fs.Create(dst); err != nil {
|
|
return err
|
|
}
|
|
defer dstFd.Close()
|
|
|
|
if _, err = io.Copy(dstFd, srcFd); err != nil {
|
|
return err
|
|
}
|
|
if srcinfo, err = fs.Stat(src); err != nil {
|
|
return err
|
|
}
|
|
return fs.Chmod(dst, srcinfo.Mode())
|
|
}
|