| 1 |
#' Query file metadata |
| 2 |
#' |
| 3 |
#' Compared to [file.info()] the full results of a `stat(2)` system call are |
| 4 |
#' returned and some columns are returned as S3 classes to make manipulation |
| 5 |
#' more natural. On systems which do not support all metadata (such as Windows) |
| 6 |
#' default values are used. |
| 7 |
#' @template fs |
| 8 |
#' @param follow If `TRUE`, symbolic links will be followed (recursively) and |
| 9 |
#' the results will be that of the final file rather than the link. |
| 10 |
#' @inheritParams dir_ls |
| 11 |
#' @return A data.frame with metadata for each file. Columns returned are as follows. |
| 12 |
#' \item{path}{The input path, as a [fs_path()] character vector.} |
| 13 |
#' \item{type}{The file type, as a factor of file types.} |
| 14 |
#' \item{size}{The file size, as a [fs_bytes()] numeric vector.} |
| 15 |
#' \item{permissions}{The file permissions, as a [fs_perms()] integer vector.} |
| 16 |
#' \item{modification_time}{The time of last data modification, as a [POSIXct] datetime.} |
| 17 |
#' \item{user}{The file owner name - as a character vector.} |
| 18 |
#' \item{group}{The file group name - as a character vector.} |
| 19 |
#' \item{device_id}{The file device id - as a numeric vector.} |
| 20 |
#' \item{hard_links}{The number of hard links to the file - as a numeric vector.} |
| 21 |
#' \item{special_device_id}{The special device id of the file - as a numeric vector.} |
| 22 |
#' \item{inode}{The inode of the file - as a numeric vector.} |
| 23 |
#' \item{block_size}{The optimal block for the file - as a numeric vector.} |
| 24 |
#' \item{blocks}{The number of blocks allocated for the file - as a numeric vector.} |
| 25 |
#' \item{flags}{The user defined flags for the file - as an integer vector.} |
| 26 |
#' \item{generation}{The generation number for the file - as a numeric vector.} |
| 27 |
#' \item{access_time}{The time of last access - as a [POSIXct] datetime.} |
| 28 |
#' \item{change_time}{The time of last file status change - as a [POSIXct] datetime.} |
| 29 |
#' \item{birth_time}{The time when the inode was created - as a [POSIXct] datetime.} |
| 30 |
#' @seealso [dir_info()] to display file information for files in a given |
| 31 |
#' directory. |
| 32 |
#' @examples |
| 33 |
#' \dontshow{.old_wd <- setwd(tempdir())} |
| 34 |
#' write.csv(mtcars, "mtcars.csv") |
| 35 |
#' file_info("mtcars.csv") |
| 36 |
#' |
| 37 |
#' # Files in the working directory modified more than 20 days ago |
| 38 |
#' files <- file_info(dir_ls()) |
| 39 |
#' files$path[difftime(Sys.time(), files$modification_time, units = "days") > 20] |
| 40 |
#' |
| 41 |
#' # Cleanup |
| 42 |
#' file_delete("mtcars.csv") |
| 43 |
#' \dontshow{setwd(.old_wd)} |
| 44 |
#' @export |
| 45 |
file_info <- function(path, fail = TRUE, follow = FALSE) { |
| 46 |
old <- path_expand(path) |
| 47 |
|
| 48 |
res <- .Call(fs_stat_, old, fail) |
| 49 |
|
| 50 |
res$path <- path_tidy(path) |
| 51 |
|
| 52 |
res$type <- factor(res$type, levels = file_types, labels = names(file_types)) |
| 53 |
|
| 54 |
# TODO: convert to UTC times? |
| 55 |
res$access_time <- .POSIXct(res$access_time) |
| 56 |
res$modification_time <- .POSIXct(res$modification_time) |
| 57 |
res$change_time <- .POSIXct(res$change_time) |
| 58 |
res$birth_time <- .POSIXct(res$birth_time) |
| 59 |
|
| 60 |
important <- c( |
| 61 |
"path", |
| 62 |
"type", |
| 63 |
"size", |
| 64 |
"permissions", |
| 65 |
"modification_time", |
| 66 |
"user", |
| 67 |
"group" |
| 68 |
) |
| 69 |
res <- res[c(important, setdiff(names(res), important))] |
| 70 |
|
| 71 |
is_symlink <- !is.na(res$type) & res$type == "symlink" |
| 72 |
while (follow && any(is_symlink)) { |
| 73 |
lpath <- link_path(path[is_symlink]) |
| 74 |
lpath <- ifelse( |
| 75 |
is_absolute_path(lpath), |
| 76 |
lpath, |
| 77 |
path(path_dir(path[is_symlink]), lpath) |
| 78 |
) |
| 79 |
res[is_symlink, ] <- file_info(lpath, fail = fail, follow = FALSE) |
| 80 |
is_symlink <- !is.na(res$type) & res$type == "symlink" |
| 81 |
} |
| 82 |
|
| 83 |
res |
| 84 |
as_tibble(res) |
| 85 |
} |
| 86 |
|
| 87 |
#' @export |
| 88 |
#' @rdname file_info |
| 89 |
file_size <- function(path, fail = TRUE) { |
| 90 |
res <- file_info(path, fail) |
| 91 |
|
| 92 |
stats::setNames(res$size, path) |
| 93 |
} |
| 94 |
|
| 95 |
file_types <- c( |
| 96 |
"any" = -1, |
| 97 |
"block_device" = 0L, |
| 98 |
"character_device" = 1L, |
| 99 |
"directory" = 2L, |
| 100 |
"FIFO" = 3L, |
| 101 |
"symlink" = 4L, |
| 102 |
"file" = 5L, |
| 103 |
"socket" = 6L |
| 104 |
) |
| 105 |
|
| 106 |
#' Change file permissions |
| 107 |
#' @template fs |
| 108 |
#' @param mode A character representation of the mode, in either hexidecimal or symbolic format. |
| 109 |
#' @details **Cross-compatibility warning:** File permissions differ on Windows |
| 110 |
#' from POSIX systems. Windows does not use an executable bit, so attempting |
| 111 |
#' to change this will have no effect. Windows also does not have user |
| 112 |
#' groups, so only the user permissions (`u`) are relevant. |
| 113 |
#' @export |
| 114 |
#' @examples |
| 115 |
#' \dontshow{.old_wd <- setwd(tempdir())} |
| 116 |
#' file_create("foo", mode = "000") |
| 117 |
#' file_chmod("foo", "777") |
| 118 |
#' file_info("foo")$permissions |
| 119 |
#' |
| 120 |
#' file_chmod("foo", "u-x") |
| 121 |
#' file_info("foo")$permissions |
| 122 |
#' |
| 123 |
#' file_chmod("foo", "a-wrx") |
| 124 |
#' file_info("foo")$permissions |
| 125 |
#' |
| 126 |
#' file_chmod("foo", "u+wr") |
| 127 |
#' file_info("foo")$permissions |
| 128 |
#' |
| 129 |
#' # It is also vectorized |
| 130 |
#' files <- c("foo", file_create("bar", mode = "000")) |
| 131 |
#' file_chmod(files, "a+rwx") |
| 132 |
#' file_info(files)$permissions |
| 133 |
#' |
| 134 |
#' file_chmod(files, c("644", "600")) |
| 135 |
#' file_info(files)$permissions |
| 136 |
#' \dontshow{setwd(.old_wd)} |
| 137 |
file_chmod <- function(path, mode) { |
| 138 |
assert_no_missing(path) |
| 139 |
mode <- as_fs_perms(mode, mode = file_info(path)$permissions) |
| 140 |
|
| 141 |
old <- path_expand(path) |
| 142 |
|
| 143 |
.Call(fs_chmod_, old, as.integer(mode)) |
| 144 |
|
| 145 |
invisible(path_tidy(path)) |
| 146 |
} |
| 147 |
|
| 148 |
#' Change owner or group of a file |
| 149 |
#' @template fs |
| 150 |
#' @param user_id The user id of the new owner, specified as a numeric ID or |
| 151 |
#' name. The R process must be privileged to change this. |
| 152 |
#' @param group_id The group id of the new owner, specified as a numeric ID or |
| 153 |
#' name. |
| 154 |
#' @export |
| 155 |
file_chown <- function(path, user_id = NULL, group_id = NULL) { |
| 156 |
assert_no_missing(path) |
| 157 |
|
| 158 |
old <- path_expand(path) |
| 159 |
|
| 160 |
if (is.null(user_id)) { |
| 161 |
user_id <- -1 |
| 162 |
} |
| 163 |
|
| 164 |
if (is.null(group_id)) { |
| 165 |
group_id <- -1 |
| 166 |
} |
| 167 |
|
| 168 |
if (is.character(user_id)) { |
| 169 |
user_id <- .Call(fs_getpwnam_, user_id) |
| 170 |
} |
| 171 |
|
| 172 |
if (is.character(group_id)) { |
| 173 |
group_id <- .Call(fs_getgrnam_, group_id) |
| 174 |
} |
| 175 |
|
| 176 |
.Call(fs_chown_, old, as.integer(user_id), as.integer(group_id)) |
| 177 |
|
| 178 |
invisible(path_tidy(path)) |
| 179 |
} |
| 180 |
|
| 181 |
#' Open files or directories |
| 182 |
#' |
| 183 |
#' @template fs |
| 184 |
#' @return The directories that were opened (invisibly). |
| 185 |
#' @importFrom utils browseURL |
| 186 |
#' @inheritParams utils::browseURL |
| 187 |
#' @export |
| 188 |
file_show <- function(path = ".", browser = getOption("browser")) { |
| 189 |
assert_no_missing(path) |
| 190 |
|
| 191 |
old <- path_expand(path) |
| 192 |
|
| 193 |
for (p in path) { |
| 194 |
browseURL(p, browser = browser) |
| 195 |
} |
| 196 |
|
| 197 |
invisible(path_tidy(path)) |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
#' Move or rename files |
| 202 |
#' |
| 203 |
#' Compared to [file.rename] `file_move()` always fails if it is unable to move |
| 204 |
#' a file, rather than signaling a Warning and returning an error code. |
| 205 |
#' @template fs |
| 206 |
#' @param new_path New file path. If `new_path` is existing directory, the file |
| 207 |
#' will be moved into that directory; otherwise it will be moved/renamed to |
| 208 |
#' the full path. |
| 209 |
#' |
| 210 |
#' Should either be the same length as `path`, or a single directory. |
| 211 |
#' @return The new path (invisibly). |
| 212 |
#' @examples |
| 213 |
#' \dontshow{.old_wd <- setwd(tempdir())} |
| 214 |
#' file_create("foo") |
| 215 |
#' file_move("foo", "bar") |
| 216 |
#' file_exists(c("foo", "bar")) |
| 217 |
#' file_delete("bar") |
| 218 |
#' \dontshow{setwd(.old_wd)} |
| 219 |
#' @export |
| 220 |
file_move <- function(path, new_path) { |
| 221 |
assert_no_missing(path) |
| 222 |
assert_no_missing(new_path) |
| 223 |
|
| 224 |
old <- path_expand(path) |
| 225 |
new <- path_expand(new_path) |
| 226 |
|
| 227 |
is_directory <- file_exists(new) & is_dir(new) |
| 228 |
|
| 229 |
if (length(new) == 1 && is_directory[[1]]) { |
| 230 |
new <- rep(new, length(path)) |
| 231 |
} |
| 232 |
assert( |
| 233 |
"Length of `path` must equal length of `new_path`", |
| 234 |
length(old) == length(new) |
| 235 |
) |
| 236 |
|
| 237 |
new[is_directory] <- path(new[is_directory], basename(old)) |
| 238 |
|
| 239 |
.Call(fs_move_, old, new) |
| 240 |
|
| 241 |
invisible(path_tidy(new)) |
| 242 |
} |
| 243 |
|
| 244 |
#' Change file access and modification times |
| 245 |
#' |
| 246 |
#' Like the touch POSIX utility this will create the file if it does not |
| 247 |
#' exist. |
| 248 |
#' |
| 249 |
#' @template fs |
| 250 |
#' |
| 251 |
#' @param access_time,modification_time The times to set, inputs will be |
| 252 |
#' coerced to [POSIXct] objects. |
| 253 |
#' @examples |
| 254 |
#' \dontshow{.old_wd <- setwd(tempdir())} |
| 255 |
#' file_touch("foo", "2018-01-01") |
| 256 |
#' file_info("foo")[c("access_time", "modification_time", "change_time", "birth_time")] |
| 257 |
#' \dontshow{setwd(.old_wd)} |
| 258 |
#' @export |
| 259 |
file_touch <- function( |
| 260 |
path, |
| 261 |
access_time = Sys.time(), |
| 262 |
modification_time = access_time |
| 263 |
) { |
| 264 |
assert_no_missing(path) |
| 265 |
|
| 266 |
access_time <- as.POSIXct(access_time) |
| 267 |
modification_time <- as.POSIXct(modification_time) |
| 268 |
|
| 269 |
path <- path_expand(path) |
| 270 |
|
| 271 |
.Call(fs_create_, path, 420L) |
| 272 |
.Call(fs_touch_, path, access_time, modification_time) |
| 273 |
|
| 274 |
invisible(path_tidy(path)) |
| 275 |
} |