File: delete.R

package info (click to toggle)
r-cran-fs 1.6.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 744 kB
  • sloc: cpp: 1,288; ansic: 530; sh: 13; makefile: 2
file content (92 lines) | stat: -rw-r--r-- 2,381 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#' Delete files, directories, or links
#'
#' @description
#' `file_delete()` and `link_delete()` delete file and links. Compared to
#' [file.remove] they always fail if they cannot delete the object rather than
#' changing return value or signalling a warning. If any inputs are
#' directories, they are passed to `dir_delete()`, so `file_delete()` can
#' therefore be used to delete any filesystem object.
#'
#' `dir_delete()` will first delete the contents of the directory, then remove
#' the directory. Compared to [unlink] it will always throw an error if the
#' directory cannot be deleted rather than being silent or signalling a warning.
#' @template fs
#' @export
#' @return The deleted paths (invisibly).
#' @name delete
#' @examples
#' \dontshow{.old_wd <- setwd(tempdir())}
#' # create a directory, with some files and a link to it
#' dir_create("dir")
#' files <- file_create(path("dir", letters[1:5]))
#' link <- link_create(path_abs("dir"), "link")
#'
#' # All files created
#' dir_exists("dir")
#' file_exists(files)
#' link_exists("link")
#' file_exists(link_path("link"))
#'
#' # Delete a file
#' file_delete(files[1])
#' file_exists(files[1])
#'
#' # Delete the directory (which deletes the files as well)
#' dir_delete("dir")
#' file_exists(files)
#' dir_exists("dir")
#'
#' # The link still exists, but what it points to does not.
#' link_exists("link")
#' dir_exists(link_path("link"))
#'
#' # Delete the link
#' link_delete("link")
#' link_exists("link")
#' \dontshow{setwd(.old_wd)}
file_delete <- function(path) {
  assert_no_missing(path)

  old <- path_expand(path)

  dirs <- is_dir(old)
  dir_delete(old[dirs])

  .Call(fs_unlink_, old[!dirs])

  invisible(path_tidy(path))
}

#' @rdname delete
#' @export
dir_delete <- function(path) {
  assert_no_missing(path)

  old <- path_expand(path)

  dirs <- dir_ls(old, type = "directory", recurse = TRUE, all = TRUE)
  files <- dir_ls(old,
    type = c("unknown", "file", "symlink", "FIFO", "socket", "character_device", "block_device"),
    recurse = TRUE,
    all = TRUE)
  .Call(fs_unlink_, files)
  .Call(fs_rmdir_, rev(c(old, dirs)))

  invisible(path_tidy(path))
}

#' @rdname delete
#' @export
link_delete <- function(path) {
  assert_no_missing(path)

  assert("`path` must be a link",
    all(is_link(path))
  )

  old <- path_expand(path)

  .Call(fs_unlink_, old)

  invisible(path_tidy(path))
}