Skip to content

file_chmod doesn't work with an empty vector and a single mode #471

Open
@plietar

Description

@plietar

It is quite common to call file_chmod with a vector of files and a single mode, which mostly works.
As a corner case however, the vector of files can be empty in which case an out-of-bounds is thrown. I would expect this to be allowed, and be a no-op.

fs::file_chmod(character(0), "644")
#> Error in `[[.default`(mode, ((i + 1)%%length(mode)) + 1L) :
#>  subscript out of bounds

traceback()
#> 9: NextMethod("[[")
#> 8: unlist(list(...))
#> 7: assert("`x` must be an integer", is.integer(x))
#> 6: new_fs_perms(NextMethod("[["))
#> 5: `[[.fs_perms`(mode, ((i + 1)%%length(mode)) + 1L)
#> 4: mode[[((i + 1)%%length(mode)) + 1L]]
#> 3: as_fs_perms.character(mode, mode = file_info(path)$permissions)
#> 2: as_fs_perms(mode, mode = file_info(path)$permissions)
#> 1: fs::file_chmod(character(0), "644")

Similarly, if a single file path is given but the mode is empty, an out-of-bounds error is thrown. In this case I do expect an error, but it seems like it should get caught earlier with a nicer error message.

fs::file_chmod(c("hello.txt"), character(0))
#> Error in res[[((i + 1)%%length(res)) + 1L]] : subscript out of bounds

traceback()
#> 3: as_fs_perms.character(mode, mode = file_info(path)$permissions)
#> 2: as_fs_perms(mode, mode = file_info(path)$permissions)
#> 1: fs::file_chmod(c("hello.txt"), character(0))

Conversely, the function accepts a single file path with a vector of modes, and uses the first element of the vector:

fs::file_chmod("hello.txt", c("644", "755"))
fs::file_info("hello.txt")$permissions
#> [1] rw-r--r--

This behaviour seems nonsensical and in my opinion should throw an error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions