R/volume-fs.R

Defines functions db_volume_list_files_recursive db_volume_download_dir db_volume_upload_dir db_volume_action is_valid_volume_path db_volume_dir_exists db_volume_recursive_delete_contents db_volume_dir_delete db_volume_dir_create db_volume_file_exists db_volume_write db_volume_list_all_contents db_volume_list db_volume_delete db_volume_read

Documented in db_volume_delete db_volume_dir_create db_volume_dir_delete db_volume_dir_exists db_volume_download_dir db_volume_file_exists db_volume_list db_volume_list_files_recursive db_volume_read db_volume_recursive_delete_contents db_volume_upload_dir db_volume_write

#' Volume FileSystem Read
#'
#' Return the contents of a file within a volume (up to 5GiB).
#'
#' @param path Absolute path of the file in the Files API, omitting the initial
#' slash.
#' @param destination Path to write downloaded file to.
#' @param progress If `TRUE`, show progress bar for file operations (default: `TRUE` for uploads/downloads, `FALSE` for other operations)
#' @inheritParams db_dbfs_create
#' @inheritParams db_sql_warehouse_create
#'
#' @family Volumes FileSystem API
#'
#' @export
#' @returns If `perform_request = TRUE`, returns endpoint-specific API output. If `FALSE`, returns an `httr2_request`.
db_volume_read <- function(
  path,
  destination,
  host = db_host(),
  token = db_token(),
  perform_request = TRUE,
  progress = TRUE
) {
  db_volume_action(
    path = path,
    destination = destination,
    action = "GET",
    type = "files",
    host = host,
    token = token,
    perform_request = perform_request,
    progress = progress
  )
}

#' Volume FileSystem Delete
#'
#' @inheritParams auth_params
#' @inheritParams db_volume_read
#' @inheritParams db_sql_warehouse_create
#'
#' @family Volumes FileSystem API
#'
#' @export
#' @returns If `perform_request = TRUE`, returns endpoint-specific API output. If `FALSE`, returns an `httr2_request`.
db_volume_delete <- function(
  path,
  host = db_host(),
  token = db_token(),
  perform_request = TRUE
) {
  db_volume_action(
    path = path,
    action = "DELETE",
    type = "files",
    host = host,
    token = token,
    perform_request = perform_request,
    progress = FALSE
  )
}

#' Volume FileSystem List Directory Contents
#'
#' @param page_size Maximum number of directory entries per page, from 0 to
#' 1000. `NULL` uses the API default; 0 requests the maximum page size.
#' @param page_token Continuation token from a previous response, or `NULL`
#' for the first page.
#' @inheritParams auth_params
#' @inheritParams db_volume_read
#' @inheritParams db_sql_warehouse_create
#'
#' @family Volumes FileSystem API
#'
#' @export
#' @returns If `perform_request = TRUE`, returns one API response page, including
#' `contents` and `next_page_token` when present. If `FALSE`, returns an
#' `httr2_request`. Directory download and recursive-delete helpers fetch all pages.
db_volume_list <- function(
  path,
  host = db_host(),
  token = db_token(),
  perform_request = TRUE,
  page_size = NULL,
  page_token = NULL
) {
  if (!is.null(page_size) && (
    !rlang::is_scalar_integerish(page_size, finite = TRUE) || page_size < 0 || page_size > 1000
  )) {
    cli::cli_abort("{.arg page_size} must be NULL or a whole number from 0 to 1000.")
  }
  if (!is.null(page_token) && (!rlang::is_string(page_token) || !nzchar(page_token))) {
    cli::cli_abort("{.arg page_token} must be NULL or a non-empty string.")
  }

  req <- db_volume_action(
    path = path,
    action = "GET",
    type = "directories",
    host = host,
    token = token,
    perform_request = FALSE,
    progress = FALSE
  ) |>
    httr2::req_url_query(page_size = page_size, page_token = page_token)

  if (perform_request) {
    db_perform_request(req)
  } else {
    req
  }
}

db_volume_list_all_contents <- function(path, host, token) {
  pages <- list()
  page_token <- NULL
  seen_tokens <- character()

  repeat {
    page <- tryCatch(
      db_volume_list(path, host = host, token = token, page_token = page_token),
      error = function(e) cli::cli_abort(
        "Unable to list all contents of {.path {path}}.",
        parent = e,
        class = "brickster_volume_listing_error"
      )
    )
    pages[[length(pages) + 1L]] <- page$contents %||% list()
    page_token <- page$next_page_token
    if (is.null(page_token) || !nzchar(page_token)) break
    if (page_token %in% seen_tokens) {
      cli::cli_abort(
        "Directory listing returned a repeated page token for {.path {path}}.",
        class = "brickster_volume_listing_error"
      )
    }
    seen_tokens <- c(seen_tokens, page_token)
  }

  purrr::list_flatten(pages)
}


#' Volume FileSystem Write
#'
#' Upload a file to volume filesystem.
#'
#' @param file Path to a file on local system, takes precedent over `path`.
#' @param overwrite Flag (Default: `FALSE`) that specifies whether to overwrite
#' existing files.
#' @inheritParams db_volume_read
#' @inheritParams auth_params
#'
#' @details
#' Uploads a file of up to 5 GiB.
#'
#' @family Volumes FileSystem API
#'
#' @export
#' @returns If `perform_request = TRUE`, returns endpoint-specific API output. If `FALSE`, returns an `httr2_request`.
db_volume_write <- function(
  path,
  file = NULL,
  overwrite = FALSE,
  host = db_host(),
  token = db_token(),
  perform_request = TRUE,
  progress = TRUE
) {
  if (is.null(file)) {
    cli::cli_abort(c(
      "Nothing to upload:",
      "x" = "{.arg file} must be specified."
    ))
  }

  db_volume_action(
    path = path,
    file = file,
    overwrite = overwrite,
    action = "PUT",
    type = "files",
    host = host,
    token = token,
    perform_request = perform_request,
    progress = progress
  )
}


#' Volume FileSystem File Status
#'
#' @inheritParams db_volume_read
#' @inheritParams auth_params
#' @inheritParams db_dbfs_create
#' @inheritParams db_sql_warehouse_create
#'
#' @family Volumes FileSystem API
#'
#' @export
#' @returns If `perform_request = TRUE`, returns endpoint-specific API output. If `FALSE`, returns an `httr2_request`.
db_volume_file_exists <- function(
  path,
  host = db_host(),
  token = db_token(),
  perform_request = TRUE
) {
  db_volume_action(
    path = path,
    action = "HEAD",
    type = "files",
    host = host,
    token = token,
    perform_request = perform_request,
    progress = FALSE
  )
}

#' Volume FileSystem Create Directory
#'
#' @inheritParams auth_params
#' @inheritParams db_volume_read
#' @inheritParams db_sql_warehouse_create
#'
#' @family Volumes FileSystem API
#'
#' @export
#' @returns If `perform_request = TRUE`, returns endpoint-specific API output. If `FALSE`, returns an `httr2_request`.
db_volume_dir_create <- function(
  path,
  host = db_host(),
  token = db_token(),
  perform_request = TRUE
) {
  db_volume_action(
    path = path,
    action = "PUT",
    type = "directories",
    host = host,
    token = token,
    perform_request = perform_request,
    progress = FALSE
  )
}

#' Volume FileSystem Delete Directory
#'
#' @param recursive If `TRUE`, recursively delete directory contents (default: `FALSE`)
#'   after listing every page of each directory. Listing failures stop traversal
#'   before deleting that directory's contents; earlier directories may already
#'   have been processed.
#' @param verbose If `TRUE`, announce each file/directory deletion (default: `FALSE`)
#' @inheritParams auth_params
#' @inheritParams db_volume_read
#' @inheritParams db_sql_warehouse_create
#'
#' @family Volumes FileSystem API
#'
#' @export
#' @returns If `perform_request = TRUE`, returns a logical success flag.
#' If `FALSE`, returns an `httr2_request` to delete only the directory itself.
#' Directory contents are not listed or deleted, even when `recursive = TRUE`.
db_volume_dir_delete <- function(
  path,
  recursive = FALSE,
  verbose = FALSE,
  host = db_host(),
  token = db_token(),
  perform_request = TRUE
) {
  if (recursive && perform_request) {
    # Recursively delete contents first
    db_volume_recursive_delete_contents(
      path,
      host = host,
      token = token,
      verbose = verbose
    )
  }

  if (verbose && perform_request) {
    cli::cli_inform("Deleting directory: {.path {path}}")
  }

  db_volume_action(
    path = path,
    action = "DELETE",
    type = "directories",
    host = host,
    token = token,
    perform_request = perform_request,
    progress = FALSE
  )
}

#' Recursively delete all contents of a volume directory
#' @keywords internal
db_volume_recursive_delete_contents <- function(
  path,
  host,
  token,
  verbose = FALSE
) {
  tryCatch(
    {
      # List directory contents
      contents <- db_volume_list_all_contents(path, host = host, token = token)

      if (!is.null(contents) && length(contents) > 0) {
        # Delete all files and subdirectories
        for (item in contents) {
          item_path <- fs::path(path, item$name)

          if (isTRUE(item$is_directory)) {
            # Recursively delete subdirectory and all its contents
            db_volume_dir_delete(
              item_path,
              recursive = TRUE,
              verbose = verbose,
              host = host,
              token = token
            )
          } else {
            # Delete file
            if (verbose) {
              cli::cli_inform("Deleting file: {.path {item_path}}")
            }
            db_volume_delete(item_path, host = host, token = token)
          }
        }
      }
    },
    error = function(e) {
      if (inherits(e, "brickster_volume_listing_error")) stop(e)
    }
  )
}


#' Volume FileSystem Check Directory Exists
#'
#' @inheritParams auth_params
#' @inheritParams db_volume_read
#' @inheritParams db_sql_warehouse_create
#'
#' @family Volumes FileSystem API
#'
#' @export
#' @returns If `perform_request = TRUE`, returns endpoint-specific API output. If `FALSE`, returns an `httr2_request`.
db_volume_dir_exists <- function(
  path,
  host = db_host(),
  token = db_token(),
  perform_request = TRUE
) {
  db_volume_action(
    path = path,
    action = "HEAD",
    type = "directories",
    host = host,
    token = token,
    perform_request = perform_request,
    progress = FALSE
  )
}


is_valid_volume_path <- function(path) {
  if (!startsWith(path, "/Volumes/")) {
    cli::cli_abort("{.arg path} must start with {.path /Volumes/}")
  }
  path
}


db_volume_action <- function(
  path,
  file = NULL,
  overwrite = NULL,
  destination = NULL,
  action = c("HEAD", "PUT", "DELETE", "GET"),
  type = c("directories", "files"),
  host = db_host(),
  token = db_token(),
  perform_request = TRUE,
  progress = TRUE
) {
  path <- is_valid_volume_path(path)
  # Escape literal path characters while preserving directory separators.
  encoded_path <- gsub("%2F", "/", curl::curl_escape(path), fixed = TRUE)
  action <- match.arg(action)
  type <- match.arg(type)

  req <- db_request(
    endpoint = paste0("fs/", type, encoded_path),
    method = action,
    version = "2.0",
    host = host,
    token = token
  )

  if (!is.null(overwrite)) {
    req <- req |>
      httr2::req_url_query(overwrite = ifelse(overwrite, "true", "false"))
  }

  if (type == "files" && action %in% c("GET", "PUT")) {
    if (action == "PUT") {
      req <- httr2::req_body_file(req, file)
    }

    # show progress when uploading and downloading files
    if (progress) {
      # Use httr2 progress with custom formatting
      req <- req |>
        httr2::req_progress(
          type = ifelse(action == "GET", "down", "up")
        )
    }
  } else if (type == "files" && action == "PUT") {
    # Add body file even without progress
    req <- httr2::req_body_file(req, file)
  }

  if (perform_request) {
    resp <- req |>
      httr2::req_error(is_error = function(resp) {
        httr2::resp_status(resp) == 500
      }) |>
      httr2::req_perform(path = destination) |>
      httr2::resp_check_status()

    if (action == "HEAD") {
      return(httr2::resp_status(resp) == 200)
    }

    if (action %in% c("PUT", "DELETE")) {
      return(httr2::resp_status(resp) == 204)
    }

    # GET on files is used for downloading - useful to return location
    if (action == "GET") {
      if (type == "directories") {
        return(httr2::resp_body_json(resp))
      } else {
        return(destination)
      }
    }
  } else {
    req
  }
}

#' Upload Directory to Volume in Parallel
#'
#' Upload all files from a local directory to a volume directory using parallel requests.
#'
#' @param local_dir Path to local directory containing files to upload
#' @param volume_dir Volume directory path (must start with /Volumes/)
#' @param overwrite Flag to overwrite existing files (default: `TRUE`)
#' @param recursive If `TRUE`, recursively include subdirectories (default: `TRUE`).
#' If `FALSE`, only top-level files are transferred.
#' @inheritParams auth_params
#' @inheritParams db_sql_warehouse_create
#'
#' @returns TRUE if all uploads successful
#' @family Volumes FileSystem API
#'
#' @export
db_volume_upload_dir <- function(
  local_dir,
  volume_dir,
  overwrite = TRUE,
  recursive = TRUE,
  host = db_host(),
  token = db_token()
) {
  # Validate inputs
  if (!fs::dir_exists(local_dir)) {
    cli::cli_abort("Local directory does not exist: {.path {local_dir}}")
  }

  volume_dir <- is_valid_volume_path(volume_dir)

  # Create volume directory
  db_volume_dir_create(volume_dir, host = host, token = token)

  local_files <- fs::dir_ls(
    local_dir,
    recurse = recursive,
    type = "file"
  )
  relative_files <- if (recursive) {
    fs::path_rel(local_files, start = local_dir)
  } else {
    fs::path_file(local_files)
  }
  volume_files <- fs::path(volume_dir, relative_files)
  volume_subdirs <- setdiff(unique(fs::path_dir(volume_files)), fs::path(volume_dir))
  purrr::walk(volume_subdirs, db_volume_dir_create, host = host, token = token)

  requests <- purrr::map2(
    local_files,
    volume_files,
    function(local_file, volume_file) {
      # Create upload request (no individual progress for parallel uploads)
      db_volume_action(
        path = volume_file,
        file = local_file,
        overwrite = overwrite,
        action = "PUT",
        type = "files",
        host = host,
        token = token,
        perform_request = FALSE,
        progress = FALSE
      )
    }
  )

  if (length(requests) == 0) {
    cli::cli_warn("No files found in directory: {.path {local_dir}}")
    return(TRUE)
  }

  # Execute parallel uploads with styled progress bars
  httr2::req_perform_parallel(
    requests,
    on_error = "stop",
    progress = list(
      clear = FALSE,
      type = "iterator",
      format = "Uploading {cli::pb_bar} {cli::pb_percent} [{cli::pb_elapsed}]",
      format_done = "{cli::col_green('\\u2714')} Data uploaded [{cli::pb_elapsed}]",
      format_failed = "Data upload failed [{cli::pb_elapsed}]"
    )
  )

  TRUE
}

#' Download Directory from Volume in Parallel
#'
#' Download files from a volume directory to a local directory using parallel requests.
#'
#' @param volume_dir Volume directory path (must start with /Volumes/)
#' @param local_dir Path to local directory where files will be downloaded
#' @param overwrite Flag to overwrite existing local files (default: `TRUE`)
#' @param recursive If `TRUE`, recursively include subdirectories (default: `TRUE`).
#' If `FALSE`, only top-level files are transferred.
#' @inheritParams auth_params
#' @inheritParams db_sql_warehouse_create
#'
#' @returns TRUE if all downloads successful
#' @family Volumes FileSystem API
#'
#' @export
db_volume_download_dir <- function(
  volume_dir,
  local_dir,
  overwrite = TRUE,
  recursive = TRUE,
  host = db_host(),
  token = db_token()
) {
  volume_dir <- is_valid_volume_path(volume_dir)

  if (fs::file_exists(local_dir) && !fs::dir_exists(local_dir)) {
    cli::cli_abort("{.arg local_dir} exists and is not a directory: {.path {local_dir}}")
  }

  fs::dir_create(local_dir, recurse = TRUE)

  volume_files <- db_volume_list_files_recursive(
    path = volume_dir,
    recurse = recursive,
    host = host,
    token = token
  )

  if (length(volume_files) == 0) {
    cli::cli_warn("No files found in volume directory: {.path {volume_dir}}")
    return(TRUE)
  }

  local_files <- purrr::map_chr(volume_files, function(volume_file) {
    if (recursive) {
      relative_path <- fs::path_rel(volume_file, start = volume_dir)
      fs::path(local_dir, relative_path)
    } else {
      fs::path(local_dir, fs::path_file(volume_file))
    }
  })

  # Ensure target directories exist before download.
  local_dirs <- unique(fs::path_dir(local_files))
  purrr::walk(local_dirs, fs::dir_create, recurse = TRUE)

  if (!overwrite) {
    existing_files <- local_files[fs::file_exists(local_files)]
    if (length(existing_files) > 0) {
      cli::cli_abort(c(
        "Local files already exist:",
        "x" = "{length(existing_files)} file(s) already exist. Set {.code overwrite = TRUE} to replace them.",
        "i" = "Example existing file: {.path {existing_files[[1]]}}"
      ))
    }
  }

  requests <- purrr::map(
    volume_files,
    db_volume_action,
    action = "GET",
    type = "files",
    host = host,
    token = token,
    perform_request = FALSE,
    progress = FALSE
  )

  httr2::req_perform_parallel(
    requests,
    paths = local_files,
    on_error = "stop",
    progress = list(
      clear = FALSE,
      type = "iterator",
      format = "Downloading {cli::pb_bar} {cli::pb_percent} [{cli::pb_elapsed}]",
      format_done = "{cli::col_green('\\u2714')} Data downloaded [{cli::pb_elapsed}]",
      format_failed = "Data download failed [{cli::pb_elapsed}]"
    )
  )

  TRUE
}

#' Recursively collect file paths from a volume directory
#' @keywords internal
db_volume_list_files_recursive <- function(
  path,
  recurse = TRUE,
  host,
  token
) {
  contents <- db_volume_list_all_contents(path = path, host = host, token = token)

  if (is.null(contents) || length(contents) == 0) {
    return(character(0))
  }

  files <- character(0)

  for (item in contents) {
    item_path <- fs::path(path, item$name)

    if (isTRUE(item$is_directory)) {
      if (recurse) {
        files <- c(
          files,
          db_volume_list_files_recursive(
            path = item_path,
            recurse = recurse,
            host = host,
            token = token
          )
        )
      }
    } else {
      files <- c(files, item_path)
    }
  }

  files
}

Try the brickster package in your browser

Any scripts or data that you put into this service are public.

brickster documentation built on Oct. 9, 2026, 5:07 p.m.