diff --git a/DESCRIPTION b/DESCRIPTION index a25c34b..932f177 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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. diff --git a/R/listFiles.R b/R/listFiles.R index 79bdc73..72cfe41 100644 --- a/R/listFiles.R +++ b/R/listFiles.R @@ -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. #' @@ -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) diff --git a/R/retrieveDirectory.R b/R/retrieveDirectory.R index eb9bf8c..a39de11 100644 --- a/R/retrieveDirectory.R +++ b/R/retrieveDirectory.R @@ -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) { diff --git a/man/listFiles.Rd b/man/listFiles.Rd index b4dc89b..1657f98 100644 --- a/man/listFiles.Rd +++ b/man/listFiles.Rd @@ -4,13 +4,15 @@ \alias{listFiles} \title{List files in a registered directory} \usage{ -listFiles(path, url, forceRemote = FALSE) +listFiles(path, url, recursive = TRUE, forceRemote = FALSE) } \arguments{ \item{path}{String containing the path to the registered (sub)directory.} \item{url}{String containing the URL of the SewerRat REST API.} +\item{recursive}{Logical scalar indicating whether to list files recursively.} + \item{forceRemote}{Logical scalar indicating whether to force remote access, even if \code{path} is on the same filesystem as the caller.} } diff --git a/tests/testthat/test-list.R b/tests/testthat/test-list.R index 5a784e4..7bf52db 100644 --- a/tests/testthat/test-list.R +++ b/tests/testthat/test-list.R @@ -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", {