Skip to content

dir_copy() is not idempotent when overwrite = F #431

Open
@thomascwells

Description

@thomascwells

The results of a second execution of dir_copy() when overwrite = F are not as I expected. I expected an error message, but instead it copied the source directory into the target, but nested one level deeper than during the first execution.

Here's an example

library(fs)

# create test source directory
dir_create("a/b")
file_create("a/b/test.txt")
dir_tree("a")

# a
# └── b
#     └── test.txt

# first copy: contents of 'a' are copied to 'c' = c/b/test.txt
# this is what I expected
dir_copy("a", "c", overwrite = F)
dir_tree("c")

# c
# └── b
#     └── test.txt

# second copy: 'a' and it's contents are copied to 'c' = c/a/b/test.txt
# expected: error about overwriting
# this is the step that seems incorrect to me
dir_copy("a", "c", overwrite = F)
dir_tree("c")

# c
# ├── a
# │   └── b
# │       └── test.txt
# └── b
#     └── test.txt

# third copy: error about overwriting
dir_copy("a", "c", overwrite = F)
# Error: [EEXIST] Failed to copy 'a/b/test.txt' to 'c/a/b/test.txt': file already exists

# test with overwrite = T

# first copy - same results as before: d/b/test.txt
dir_copy("a", "d", overwrite = T)
dir_tree("d")

# d
# └── b
#     └── test.txt

# second copy - expected results: silent overwrite
dir_copy("a", "d", overwrite = T)
dir_tree("d")

# d
# └── b
#     └── test.txt

# cleanup
dir_delete("a")
dir_delete("c")
dir_delete("d")

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions