Debian Codegraph @

R/fs_perms.R

raw
1 #' Create, modify and view file permissions
2 #'
3 #' `fs_perms()` objects help one create and modify file permissions easily.
4 #' They support both numeric input, octal and symbolic character
5 #' representations. Compared to [octmode] they support symbolic representations
6 #' and display the mode the same format as `ls` on POSIX systems.
7 #'
8 #' @details
9 #' On POSIX systems the permissions are displayed as a 9 character string with
10 #' three sets of three characters. Each set corresponds to the permissions for
11 #' the user, the group and other (or default) users.
12 #'
13 #' If the first character of each set is a "r", the file is readable for those
14 #' users, if a "-", it is not readable.
15 #'
16 #' If the second character of each set is a "w", the file is writable for those
17 #' users, if a "-", it is not writable.
18 #'
19 #' The third character is more complex, and is the first of the following
20 #' characters which apply.
21 #' - 'S' If the character is part of the owner permissions and the file is not
22 #' executable or the directory is not searchable by the owner, and the
23 #' set-user-id bit is set.
24 #' - 'S' If the character is part of the group permissions and the file is not
25 #' executable or the directory is not searchable by the group, and the
26 #' set-group-id bit is set.
27 #' - 'T' If the character is part of the other permissions and the file is not
28 #' executable or the directory is not searchable by others, and the 'sticky'
29 #' (S_ISVTX) bit is set.
30 #' - 's' If the character is part of the owner permissions and the file is
31 #' executable or the directory searchable by the owner, and the set-user-id bit
32 #' is set.
33 #' - 's' If the character is part of the group permissions and the file is
34 #' executable or the directory searchable by the group, and the set-group-id
35 #' bit is set.
36 #' - 't' If the character is part of the other permissions and the file is
37 #' executable or the directory searchable by others, and the ''sticky''
38 #' (S_ISVTX) bit is set.
39 #' - 'x' The file is executable or the directory is searchable.
40 #' - '-' If none of the above apply.
41 #' Most commonly the third character is either 'x' or `-`.
42 #'
43 #' On Windows the permissions are displayed as a 3 character string where the
44 #' third character is only `-` or `x`.
45 #' @param x An object which is to be coerced to a fs_perms object. Can be an
46 #' number or octal character representation, including symbolic
47 #' representations.
48 #' @param ... Additional arguments passed to methods.
49 #' @examples
50 #' # Integer and numeric
51 #' fs_perms(420L)
52 #' fs_perms(c(511, 420))
53 #'
54 #' # Octal
55 #' fs_perms("777")
56 #' fs_perms(c("777", "644"))
57 #'
58 #' # Symbolic
59 #' fs_perms("a+rwx")
60 #' fs_perms(c("a+rwx", "u+rw,go+r"))
61 #'
62 #' # Use the `&` and `|`operators to check for certain permissions
63 #' (fs_perms("777") & "u+r") == "u+r"
64 #' @export
65 #' @name fs_perms
66 as_fs_perms <- function(x, ...) {
67 UseMethod("as_fs_perms")
68 }
69
70 #' @export
71 as_fs_perms.NULL <- function(x, ...) {
72 new_fs_perms(integer())
73 }
74
75 #' @export
76 #' @rdname fs_perms
77 fs_perms <- as_fs_perms
78
79 #' @export
80 print.fs_perms <- function(x, ...) {
81 print(format(x, ...), quote = FALSE)
82
83 invisible(x)
84 }
85
86 #' @export
87 format.fs_perms <- function(x, ...) {
88 vapply(x, function(x) .Call(fs_strmode_, x), character(1))
89 }
90
91 #' @export
92 as.character.fs_perms <- format.fs_perms
93
94 #' @export
95 `[.fs_perms` <- function(x, i, ...) {
96 new_fs_perms(NextMethod("["))
97 }
98
99 #' @export
100 `[[.fs_perms` <- function(x, i, ...) {
101 new_fs_perms(NextMethod("[["))
102 }
103
104 #' @export
105 as_fs_perms.fs_perms <- function(x, ...) x
106
107 #' @export
108 as_fs_perms.character <- function(x, ..., mode = 0) {
109 # matches inputs in rwxrwxrwx mode
110 res <- x
111
112 if (!is_windows()) {
113 is_display_mode <- grepl("^[rwxXst-]{9}$", x)
114 res[is_display_mode] <- display_mode_to_symbolic_mode_posix(res[
115 is_display_mode
116 ])
117 } else {
118 is_display_mode <- grepl("^[rwxXst-]{3}$", x)
119 res[is_display_mode] <- display_mode_to_symbolic_mode_windows(res[
120 is_display_mode
121 ])
122 }
123
124 if (length(x) > 1 && length(mode) > 1 && length(x) != length(mode)) {
125 msg <- sprintf(
126 paste0(
127 "`x` and `mode` must have compatible lengths:\n",
128 "* `x` has length %i\n",
129 "* `mode` has length %i\n"
130 ),
131 length(x),
132 length(mode)
133 )
134
135 stop(fs_error(msg))
136 }
137
138 # Recycled over both res and mode
139 n <- max(length(res), length(mode))
140 out <- integer(n)
141 for (i in seq_len(n)) {
142 out[[i]] <- as.integer(
143 .Call(
144 fs_getmode_,
145 res[[((i + 1) %% length(res)) + 1L]],
146 as.integer(mode[[((i + 1) %% length(mode)) + 1L]])
147 )
148 )
149 }
150 new_fs_perms(out)
151 }
152
153 display_mode_to_symbolic_mode_posix <- function(x) {
154 paste0(
155 "u=",
156 substring(x, 1, 3),
157 ",g=",
158 substring(x, 4, 6),
159 ",o=",
160 substring(x, 7, 9)
161 )
162 }
163 display_mode_to_symbolic_mode_windows <- function(x) {
164 paste0("u=", substring(x, 1, 3))
165 }
166
167 #' @export
168 as_fs_perms.octmode <- function(x, ...) {
169 class(x) <- c("fs_perms", "integer")
170 x
171 }
172
173 #' @export
174 as_fs_perms.numeric <- function(x, ...) {
175 if (all(is.na(x) | x == as.integer(x))) {
176 x <- as.integer(x)
177 return(new_fs_perms(x))
178 }
179 stop("'x' cannot be coerced to class \"fs_perms\"", call. = FALSE)
180 }
181
182 #' @export
183 as_fs_perms.integer <- function(x, ...) {
184 new_fs_perms(x)
185 }
186
187 new_fs_perms <- function(x) {
188 assert("`x` must be an integer", is.integer(x))
189 class(x) <- c("fs_perms", "integer")
190 x
191 }
192 setOldClass(c("fs_perms", "integer"), integer())
193
194 #' @export
195 `!.fs_perms` <- function(a) {
196 new_fs_perms(bitwNot(a))
197 }
198
199 #' @export
200 `&.fs_perms` <- function(a, b) {
201 new_fs_perms(bitwAnd(a, as_fs_perms(b)))
202 }
203
204 #' @export
205 `|.fs_perms` <- function(a, b) {
206 new_fs_perms(bitwOr(a, as_fs_perms(b)))
207 }
208
209 #' @export
210 `==.fs_perms` <- function(a, b) {
211 b <- as_fs_perms(b)
212 unclass(a & b) == unclass(b)
213 }
214
215 pillar_shaft.fs_perms <- function(x, ...) {
216 pillar::new_pillar_shaft_simple(format.fs_perms(x), ...)
217 }
218
219 type_sum.fs_perms <- function(x) {
220 "fs::perms"
221 }
222
223 # All functions below registered in .onLoad
224
225 vec_ptype2.fs_perms.fs_perms <- function(x, y, ...) {
226 x
227 }
228 vec_ptype2.fs_perms.double <- function(x, y, ...) {
229 x
230 }
231 vec_ptype2.double.fs_perms <- function(x, y, ...) {
232 y
233 }
234
235 # Note order of class is the opposite as for ptype2
236 vec_cast.fs_perms.fs_perms <- function(x, to, ...) {
237 x
238 }
239 vec_cast.fs_perms.double <- function(x, to, ...) {
240 as_fs_perms(x)
241 }
242 vec_cast.double.fs_perms <- function(x, to, ...) {
243 unclass(x)
244 }
245
246 vec_ptype2.fs_perms.integer <- function(x, y, ...) {
247 x
248 }
249 vec_ptype2.integer.fs_perms <- function(x, y, ...) {
250 y
251 }
252
253 vec_cast.fs_perms.integer <- function(x, to, ...) {
254 as_fs_perms(x)
255 }
256 vec_cast.integer.fs_perms <- function(x, to, ...) {
257 unclass(x)
258 }
259
260 vec_ptype2.fs_perms.character <- function(x, y, ...) {
261 x
262 }
263 vec_ptype2.character.fs_perms <- function(x, y, ...) {
264 y
265 }
266
267 vec_cast.fs_perms.character <- function(x, to, ...) {
268 as_fs_perms(x)
269 }
270 vec_cast.character.fs_perms <- function(x, to, ...) {
271 as.character(x)
272 }