R/file.R

Defines functions file_resize_off file_resize file_create

Documented in file_create file_resize file_resize_off

################################################################################

#' Create or resize files
#'
#' @param file Path name.
#' @param size Size in bytes.
#'
#' @importFrom bigassertr assert_noexist assert_dir assert_exist stop2
#'
#' @return Input `file`, invisibly.
#' @export
#'
#' @examples
#' tmp <- tempfile()
#' file_create(tmp, 10)
#' file.size(tmp)
#' file_resize(tmp, 5)
#' file.size(tmp)
#' file_resize_off(tmp, 10)
#' file.size(tmp)
#'
file_create <- function(file, size) {
  file <- path.expand(file)
  assert_noexist(file)
  assert_dir(dirname(file))
  file.create(file)
  ff::file.resize(file, size)
  invisible(file)
}

################################################################################

#' @rdname file_create
#' @export
file_resize <- function(file, size) {
  file <- path.expand(file)
  assert_exist(file)
  success <- ff::file.resize(file, size)
  if (!success) stop2("Problem when resizing '%s'.", file)
  invisible(file)
}

################################################################################

#' @rdname file_create
#' @export
file_resize_off <- function(file, size) {
  file_resize(file, file.size(file) + size)
}

################################################################################

Try the rmio package in your browser

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

rmio documentation built on March 18, 2022, 6:09 p.m.