Skip to content

Commit

Permalink
Gracefully handle files/symlinks/dirs deleted while copy
Browse files Browse the repository at this point in the history
Ignore not-exist errors while doing the actualy copy because
files/symlinks/dirs may be deleted while reading the source directory.

fixes #72
  • Loading branch information
ncopa committed Apr 5, 2023
1 parent 4895fee commit b23de9d
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ func copyNextOrSkip(src, dest string, info os.FileInfo, opt Options) error {
// with considering existence of parent directory
// and file permission.
func fcopy(src, dest string, info os.FileInfo, opt Options) (err error) {
s, err := os.Open(src)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return
}
defer fclose(s, &err)

if err = os.MkdirAll(filepath.Dir(dest), os.ModePerm); err != nil {
return
Expand All @@ -82,12 +90,6 @@ func fcopy(src, dest string, info os.FileInfo, opt Options) (err error) {
}
chmodfunc(&err)

s, err := os.Open(src)
if err != nil {
return
}
defer fclose(s, &err)

var buf []byte = nil
var w io.Writer = f
var r io.Reader = s
Expand Down Expand Up @@ -146,6 +148,9 @@ func dcopy(srcdir, destdir string, info os.FileInfo, opt Options) (err error) {

contents, err := ioutil.ReadDir(srcdir)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return
}

Expand Down Expand Up @@ -222,6 +227,9 @@ func onsymlink(src, dest string, opt Options) error {
func lcopy(src, dest string) error {
src, err := os.Readlink(src)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return err
}
return os.Symlink(src, dest)
Expand Down

0 comments on commit b23de9d

Please sign in to comment.