Skip to content

Commit

Permalink
Merge pull request #103 from ncopa/rm-while-copy
Browse files Browse the repository at this point in the history
fix files/symlinks/dirs deleted while copy
  • Loading branch information
otiai10 authored Apr 5, 2023
2 parents 06e3b6d + b23de9d commit a112cf0
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 6 deletions.
43 changes: 43 additions & 0 deletions all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
Expand Down Expand Up @@ -86,6 +87,48 @@ func TestCopy(t *testing.T) {
err = os.Chmod(dest, 0o755)
Expect(t, err).ToBe(nil)
})
When(t, "file is deleted while copying", func(t *testing.T) {
src := t.TempDir()
dest := t.TempDir()

file := filepath.Join(src, "file")
f, err := os.Create(file)
Expect(t, err).ToBe(nil)
f.Close()

opt := Options{Skip: func(info os.FileInfo, src, dest string) (bool, error) {
os.Remove(src)
return false, nil
}}
err = Copy(src, dest, opt)
Expect(t, err).ToBe(nil)
})
When(t, "symlink is deleted while copying", func(t *testing.T) {
src := t.TempDir()
dest := t.TempDir()

Expect(t, os.Symlink(".", filepath.Join(src, "symlink"))).ToBe(nil)

opt := Options{Skip: func(info os.FileInfo, src, dest string) (bool, error) {
os.Remove(src)
return false, nil
}}
err = Copy(src, dest, opt)
Expect(t, err).ToBe(nil)
})
When(t, "directory is deleted while copying", func(t *testing.T) {
src := t.TempDir()
dest := t.TempDir()

Expect(t, os.Mkdir(filepath.Join(src, "dir"), 0755)).ToBe(nil)

opt := Options{Skip: func(info os.FileInfo, src, dest string) (bool, error) {
os.Remove(src)
return false, nil
}}
err = Copy(src, dest, opt)
Expect(t, err).ToBe(nil)
})
}

func TestCopy_NamedPipe(t *testing.T) {
Expand Down
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 a112cf0

Please sign in to comment.