// from the source, and directories are created with mode 0o777
// (before umask).
//
-// CopyFS will not overwrite existing files, and returns an error
-// if a file name in fsys already exists in the destination.
+// CopyFS will not overwrite existing files. If a file name in fsys
+// already exists in the destination, CopyFS will return an error
+// such that errors.Is(err, fs.ErrExist) will be true.
//
// Symbolic links in fsys are not supported. A *PathError with Err set
// to ErrInvalid is returned when copying from a symbolic link.
if err != nil {
return err
}
- w, err := OpenFile(newPath, O_CREATE|O_TRUNC|O_WRONLY, 0666|info.Mode()&0777)
+ w, err := OpenFile(newPath, O_CREATE|O_EXCL|O_WRONLY, 0666|info.Mode()&0777)
if err != nil {
return err
}
t.Fatal("comparing two directories:", err)
}
+ // Test whether CopyFS disallows copying for disk filesystem when there is any
+ // existing file in the destination directory.
+ if err := CopyFS(tmpDir, fsys); !errors.Is(err, fs.ErrExist) {
+ t.Errorf("CopyFS should have failed and returned error when there is"+
+ "any existing file in the destination directory (in disk filesystem), "+
+ "got: %v, expected any error that indicates <file exists>", err)
+ }
+
// Test with memory filesystem.
fsys = fstest.MapFS{
"william": {Data: []byte("Shakespeare\n")},
}); err != nil {
t.Fatal("comparing two directories:", err)
}
+
+ // Test whether CopyFS disallows copying for memory filesystem when there is any
+ // existing file in the destination directory.
+ if err := CopyFS(tmpDir, fsys); !errors.Is(err, fs.ErrExist) {
+ t.Errorf("CopyFS should have failed and returned error when there is"+
+ "any existing file in the destination directory (in memory filesystem), "+
+ "got: %v, expected any error that indicates <file exists>", err)
+ }
}
func TestCopyFSWithSymlinks(t *testing.T) {