Skip to content

Commit

Permalink
Added a recursive= option when listing files.
Browse files Browse the repository at this point in the history
  • Loading branch information
LTLA committed Nov 6, 2024
1 parent 9a97a63 commit b7ff3c2
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 9 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: SewerRat
Version: 0.3.0
Date: 2024-11-05
Version: 0.3.1
Date: 2024-11-06
Title: Client for the SewerRat API
Description:
Search metadata files across a shared filesystem via the SewerRat API.
Expand Down
17 changes: 12 additions & 5 deletions R/listFiles.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#'
#' @param path String containing the path to the registered (sub)directory.
#' @param url String containing the URL of the SewerRat REST API.
#' @param recursive Logical scalar indicating whether to list files recursively.
#' @param forceRemote Logical scalar indicating whether to force remote access,
#' even if \code{path} is on the same filesystem as the caller.
#'
Expand Down Expand Up @@ -36,15 +37,21 @@
#'
#' @export
#' @import httr2
listFiles <- function(path, url, forceRemote=FALSE) {
listFiles <- function(path, url, recursive=TRUE, forceRemote=FALSE) {
if (!forceRemote && file.exists(path)) {
.quick_list(path)
if (recursive) {
list.files(path, recursive=TRUE, all.files=TRUE)
} else {
current <- list.files(path, all.files=TRUE, no..=TRUE)
dirs <- list.dirs(path, recursive=TRUE, full.names=FALSE)
is.dir <- current %in% dirs
current[is.dir] <- paste0(current[is.dir], "/")
current
}
} else {
req <- request(paste0(url, "/list?path=", URLencode(path, reserved=TRUE), "&recursive=true"))
req <- request(paste0(url, "/list?path=", URLencode(path, reserved=TRUE), "&recursive=", if (recursive) "true" else "false"))
req <- handle_error(req)
res <- req_perform(req)
unlist(resp_body_json(res))
}
}

.quick_list <- function(path) list.files(path, recursive=TRUE, all.files=TRUE)
2 changes: 1 addition & 1 deletion R/retrieveDirectory.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ retrieveDirectory <- function(path, url, cache=NULL, forceRemote=FALSE, overwrit
listing <- resp_body_json(res)

# Removing files that no longer exist.
existing <- .quick_list(final)
existing <- list.files(final, recursive=TRUE, all.files=TRUE)
unlink(file.path(final, setdiff(existing, listing)))

if (concurrent == 1L) {
Expand Down
4 changes: 3 additions & 1 deletion man/listFiles.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions tests/testthat/test-list.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ register(mydir, "metadata.json", url=info$url)
test_that("listing works as expected", {
expect_identical(sort(listFiles(mydir, url=info$url)), sort(c("diet/metadata.json", "metadata.json")))
expect_identical(sort(listFiles(paste0(mydir, "/diet"), url=info$url)), "metadata.json")
expect_identical(sort(listFiles(mydir, url=info$url, recursive=FALSE)), sort(c("diet/", "metadata.json")))

# Forcing remote access.
expect_identical(sort(listFiles(mydir, url=info$url, forceRemote=TRUE)), sort(c("diet/metadata.json", "metadata.json")))
expect_identical(sort(listFiles(mydir, url=info$url, forceRemote=TRUE, recursive=FALSE)), sort(c("diet/", "metadata.json")))
})

test_that("listRegisteredDirectories works as expected", {
Expand Down

0 comments on commit b7ff3c2

Please sign in to comment.