Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
plietar opened this issue Sep 10, 2024 · 0 comments
Open

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

plietar opened this issue Sep 10, 2024 · 0 comments

Comments

@plietar
Copy link

plietar commented Sep 10, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant