a b/R/basic-utils.R
1
#' Create directory if it doesn't already exist, or
2
#' issue error if it does exist but the minimum access
3
#' requirements are not met, to avoid using directories
4
#' created for another purpose.
5
#' 
6
#' @param file_path Location of directory to check/create
7
#' @param min_access Minimum access permissions ("rwx", "rx" etc.)
8
#' 
9
#' @importFrom checkmate test_directory_exists assert_access
10
#' 
11
12
makeifnot_dir <- function(file_path, min_access) {
13
  # Set up directory if does not already exist
14
  if (checkmate::test_directory_exists(file.path(
15
    file_path
16
  ))) {
17
    # Can we write to it?
18
    checkmate::assert_access(file.path(
19
      file_path, access = min_access
20
    )) 
21
  } else {
22
    # Wrap me in a tryCatch
23
    tryCatch(
24
      dir.create(file_path),
25
      # Convert warning to error
26
      warning = function(w) rlang::abort(w$message)
27
    )
28
  }
29
}