Debian Codegraph @

src/file.cc

raw symbols SCIP
1 #ifndef __STDC_FORMAT_MACROS
2 #define __STDC_FORMAT_MACROS 1
3 #endif
4
5 #include <cstdio>
6 #include <string>
7 #include <vector>
8 #include <inttypes.h>
9
10 #include "error.h"
11
12 #ifndef __WIN32
13 #include <grp.h>
14 #include <pwd.h>
15 #endif
16
17 #include <R.h>
18 #include <Rinternals.h>
19
20 #include "getmode.h"
21 #include "uv.h"
22
23 #undef ERROR
24
25 // [[export]]
26 extern "C" SEXP fs_move_(SEXP path, SEXP new_path) {
27 for (R_xlen_t i = 0; i < Rf_xlength(new_path); ++i) {
28 uv_fs_t req;
29 const char* p = CHAR(STRING_ELT(path, i));
30 const char* n = CHAR(STRING_ELT(new_path, i));
31 int res = uv_fs_rename(uv_default_loop(), &req, p, n, NULL);
32
33 // Rename does not work across partitions, so we need to instead copy, then
34 // remove the file.
35 if (res == UV_EXDEV) {
36 uv_fs_req_cleanup(&req);
37
38 uv_fs_copyfile(uv_default_loop(), &req, p, n, 0, NULL);
39 stop_for_error2(req, "Failed to copy '%s' to '%s'", p, n);
40 uv_fs_req_cleanup(&req);
41
42 uv_fs_unlink(uv_default_loop(), &req, p, NULL);
43 stop_for_error(req, "Failed to remove '%s'", p);
44 uv_fs_req_cleanup(&req);
45 continue;
46 }
47
48 stop_for_error2(req, "Failed to move '%s' to '%s'", p, n);
49 uv_fs_req_cleanup(&req);
50 }
51
52 return R_NilValue;
53 }
54
55 // [[export]]
56 extern "C" SEXP fs_create_(SEXP path_sxp, SEXP mode_sxp) {
57
58 unsigned short mode = INTEGER(mode_sxp)[0];
59
60 for (R_xlen_t i = 0; i < Rf_xlength(path_sxp); ++i) {
61 uv_fs_t req;
62 const char* p = CHAR(STRING_ELT(path_sxp, i));
63 int fd = uv_fs_open(
64 uv_default_loop(), &req, p, UV_FS_O_CREAT | UV_FS_O_WRONLY, mode, NULL);
65 stop_for_error(req, "Failed to open '%s'", p);
66
67 uv_fs_close(uv_default_loop(), &req, fd, NULL);
68 stop_for_error(req, "Failed to close '%s'", p);
69
70 uv_fs_req_cleanup(&req);
71 }
72
73 return R_NilValue;
74 }
75
76 // [[export]]
77 extern "C" SEXP fs_stat_(SEXP path, SEXP fail_sxp) {
78 bool fail = LOGICAL(fail_sxp)[0];
79 // typedef struct {
80 // uint64_t st_dev;
81 // uint64_t st_mode;
82 // uint64_t st_nlink;
83 // uint64_t st_uid;
84 // uint64_t st_gid;
85 // uint64_t st_rdev;
86 // uint64_t st_ino;
87 // uint64_t st_size;
88 // uint64_t st_blksize;
89 // uint64_t st_blocks;
90 // uint64_t st_flags;
91 // uint64_t st_gen;
92 // uv_timespec_t st_atim;
93 // uv_timespec_t st_mtim;
94 // uv_timespec_t st_ctim;
95 // uv_timespec_t st_birthtim;
96
97 R_xlen_t n = Rf_xlength(path);
98
99 SEXP out = PROTECT(Rf_allocVector(VECSXP, 18));
100 SEXP names = PROTECT(Rf_allocVector(STRSXP, 18));
101
102 SET_STRING_ELT(names, 0, Rf_mkChar("path"));
103 SET_VECTOR_ELT(out, 0, Rf_duplicate(path));
104
105 SET_STRING_ELT(names, 1, Rf_mkChar("device_id"));
106 SET_VECTOR_ELT(out, 1, Rf_allocVector(REALSXP, n));
107
108 SET_STRING_ELT(names, 2, Rf_mkChar("type"));
109 SET_VECTOR_ELT(out, 2, Rf_allocVector(INTSXP, n));
110
111 SET_STRING_ELT(names, 3, Rf_mkChar("permissions"));
112 SET_VECTOR_ELT(out, 3, Rf_allocVector(INTSXP, n));
113 SEXP class_perm_sxp = PROTECT(Rf_allocVector(STRSXP, 2));
114 SET_STRING_ELT(class_perm_sxp, 0, Rf_mkChar("fs_perms"));
115 SET_STRING_ELT(class_perm_sxp, 1, Rf_mkChar("integer"));
116 Rf_classgets(VECTOR_ELT(out, 3), class_perm_sxp);
117 UNPROTECT(1);
118
119 SET_STRING_ELT(names, 4, Rf_mkChar("hard_links"));
120 SET_VECTOR_ELT(out, 4, Rf_allocVector(REALSXP, n));
121
122 SET_STRING_ELT(names, 5, Rf_mkChar("user"));
123 SET_VECTOR_ELT(out, 5, Rf_allocVector(STRSXP, n));
124
125 SET_STRING_ELT(names, 6, Rf_mkChar("group"));
126 SET_VECTOR_ELT(out, 6, Rf_allocVector(STRSXP, n));
127
128 SET_STRING_ELT(names, 7, Rf_mkChar("special_device_id"));
129 SET_VECTOR_ELT(out, 7, Rf_allocVector(REALSXP, n));
130
131 SET_STRING_ELT(names, 8, Rf_mkChar("inode"));
132 SET_VECTOR_ELT(out, 8, Rf_allocVector(REALSXP, n));
133
134 SET_STRING_ELT(names, 9, Rf_mkChar("size"));
135 SET_VECTOR_ELT(out, 9, Rf_allocVector(REALSXP, n));
136
137 SEXP class_sxp = PROTECT(Rf_allocVector(STRSXP, 2));
138 SET_STRING_ELT(class_sxp, 0, Rf_mkChar("fs_bytes"));
139 SET_STRING_ELT(class_sxp, 1, Rf_mkChar("numeric"));
140 Rf_classgets(VECTOR_ELT(out, 9), class_sxp);
141 UNPROTECT(1);
142
143 SET_STRING_ELT(names, 10, Rf_mkChar("block_size"));
144 SET_VECTOR_ELT(out, 10, Rf_allocVector(REALSXP, n));
145
146 SET_STRING_ELT(names, 11, Rf_mkChar("blocks"));
147 SET_VECTOR_ELT(out, 11, Rf_allocVector(REALSXP, n));
148
149 SET_STRING_ELT(names, 12, Rf_mkChar("flags"));
150 SET_VECTOR_ELT(out, 12, Rf_allocVector(INTSXP, n));
151
152 SET_STRING_ELT(names, 13, Rf_mkChar("generation"));
153 SET_VECTOR_ELT(out, 13, Rf_allocVector(REALSXP, n));
154
155 SET_STRING_ELT(names, 14, Rf_mkChar("access_time"));
156 SET_VECTOR_ELT(out, 14, Rf_allocVector(REALSXP, n));
157
158 SET_STRING_ELT(names, 15, Rf_mkChar("modification_time"));
159 SET_VECTOR_ELT(out, 15, Rf_allocVector(REALSXP, n));
160
161 SET_STRING_ELT(names, 16, Rf_mkChar("change_time"));
162 SET_VECTOR_ELT(out, 16, Rf_allocVector(REALSXP, n));
163
164 SET_STRING_ELT(names, 17, Rf_mkChar("birth_time"));
165 SET_VECTOR_ELT(out, 17, Rf_allocVector(REALSXP, n));
166
167 R_xlen_t i;
168 for (i = 0; i < Rf_xlength(path); ++i) {
169 uv_fs_t req;
170 const char* p = CHAR(STRING_ELT(path, i));
171 int res = uv_fs_lstat(uv_default_loop(), &req, p, NULL);
172
173 bool is_na = STRING_ELT(path, i) == NA_STRING;
174 bool doesnt_exist = res == UV_ENOENT || res == UV_ENOTDIR;
175 bool has_error =
176 !fail && !doesnt_exist && warn_for_error(req, "Failed to stat '%s'", p);
177
178 if (is_na || doesnt_exist || has_error) {
179 REAL(VECTOR_ELT(out, 1))[i] = NA_REAL;
180 INTEGER(VECTOR_ELT(out, 2))[i] = NA_INTEGER;
181 INTEGER(VECTOR_ELT(out, 2))[i] = NA_INTEGER;
182 INTEGER(VECTOR_ELT(out, 3))[i] = NA_INTEGER;
183 REAL(VECTOR_ELT(out, 4))[i] = NA_REAL;
184 SET_STRING_ELT(VECTOR_ELT(out, 5), i, NA_STRING);
185 SET_STRING_ELT(VECTOR_ELT(out, 6), i, NA_STRING);
186 REAL(VECTOR_ELT(out, 7))[i] = NA_REAL;
187 REAL(VECTOR_ELT(out, 8))[i] = NA_REAL;
188 REAL(VECTOR_ELT(out, 9))[i] = NA_REAL;
189 REAL(VECTOR_ELT(out, 10))[i] = NA_REAL;
190 REAL(VECTOR_ELT(out, 11))[i] = NA_REAL;
191 INTEGER(VECTOR_ELT(out, 12))[i] = NA_INTEGER;
192 REAL(VECTOR_ELT(out, 13))[i] = NA_REAL;
193 REAL(VECTOR_ELT(out, 14))[i] = NA_REAL;
194 REAL(VECTOR_ELT(out, 15))[i] = NA_REAL;
195 REAL(VECTOR_ELT(out, 16))[i] = NA_REAL;
196 REAL(VECTOR_ELT(out, 17))[i] = NA_REAL;
197 continue;
198 }
199 stop_for_error(req, "Failed to stat '%s'", p);
200
201 uv_stat_t st = req.statbuf;
202 REAL(VECTOR_ELT(out, 1))[i] = st.st_dev;
203 int type;
204 switch (st.st_mode & S_IFMT) {
205 case S_IFBLK:
206 type = 0;
207 break;
208 case S_IFCHR:
209 type = 1;
210 break;
211 case S_IFDIR:
212 type = 2;
213 break;
214 case S_IFIFO:
215 type = 3;
216 break;
217 case S_IFLNK:
218 type = 4;
219 break;
220 case S_IFREG:
221 type = 5;
222 break;
223 #ifndef __WIN32
224 case S_IFSOCK:
225 type = 6;
226 break;
227 #endif
228 default:
229 type = NA_INTEGER;
230 break;
231 }
232 INTEGER(VECTOR_ELT(out, 2))[i] = type;
233 INTEGER(VECTOR_ELT(out, 3))[i] = st.st_mode;
234 REAL(VECTOR_ELT(out, 4))[i] = st.st_nlink;
235
236 #ifdef __WIN32
237 SET_STRING_ELT(VECTOR_ELT(out, 5), i, NA_STRING);
238 #else
239 passwd* pwd;
240 if ((pwd = getpwuid(st.st_uid)) != NULL) {
241 SET_STRING_ELT(VECTOR_ELT(out, 5), i, Rf_mkCharCE(pwd->pw_name, CE_UTF8));
242 } else {
243 char buf[20];
244 snprintf(buf, sizeof(buf), "%" PRIu64, st.st_uid);
245 SET_STRING_ELT(VECTOR_ELT(out, 5), i, Rf_mkCharCE(buf, CE_UTF8));
246 }
247 #endif
248
249 #ifdef __WIN32
250 SET_STRING_ELT(VECTOR_ELT(out, 6), i, NA_STRING);
251 #else
252 group* grp;
253 if ((grp = getgrgid(st.st_gid)) != NULL) {
254 SET_STRING_ELT(VECTOR_ELT(out, 6), i, Rf_mkCharCE(grp->gr_name, CE_UTF8));
255 } else {
256 char buf[20];
257 snprintf(buf, sizeof(buf), "%" PRIu64, st.st_gid);
258 SET_STRING_ELT(VECTOR_ELT(out, 6), i, Rf_mkCharCE(buf, CE_UTF8));
259 }
260 #endif
261
262 REAL(VECTOR_ELT(out, 7))[i] = st.st_rdev;
263 REAL(VECTOR_ELT(out, 8))[i] = st.st_ino;
264 REAL(VECTOR_ELT(out, 9))[i] = st.st_size;
265 REAL(VECTOR_ELT(out, 10))[i] = st.st_blksize;
266 REAL(VECTOR_ELT(out, 11))[i] = st.st_blocks;
267 INTEGER(VECTOR_ELT(out, 12))[i] = st.st_flags;
268 REAL(VECTOR_ELT(out, 13))[i] = st.st_gen;
269
270 REAL(VECTOR_ELT(out, 14))
271 [i] = (st.st_atim.tv_sec + 1e-9 * st.st_atim.tv_nsec);
272
273 REAL(VECTOR_ELT(out, 15))
274 [i] = (st.st_mtim.tv_sec + 1e-9 * st.st_mtim.tv_nsec);
275
276 REAL(VECTOR_ELT(out, 16))
277 [i] = (st.st_ctim.tv_sec + 1e-9 * st.st_ctim.tv_nsec);
278
279 REAL(VECTOR_ELT(out, 17))
280 [i] = (st.st_birthtim.tv_sec + 1e-9 * st.st_birthtim.tv_nsec);
281 uv_fs_req_cleanup(&req);
282 }
283 Rf_setAttrib(out, R_NamesSymbol, names);
284 Rf_setAttrib(out, R_ClassSymbol, Rf_mkString("data.frame"));
285
286 SEXP row_names = PROTECT(Rf_allocVector(INTSXP, 2));
287 INTEGER(row_names)[0] = NA_INTEGER;
288 INTEGER(row_names)[1] = -i;
289 Rf_setAttrib(out, R_RowNamesSymbol, row_names);
290 UNPROTECT(1);
291
292 UNPROTECT(2);
293 return out;
294 }
295
296 // [[export]]
297 extern "C" SEXP fs_exists_(SEXP path_sxp, SEXP name_sxp) {
298
299 SEXP out = PROTECT(Rf_allocVector(LGLSXP, Rf_xlength(path_sxp)));
300 Rf_setAttrib(out, R_NamesSymbol, Rf_duplicate(name_sxp));
301
302 for (R_xlen_t i = 0; i < Rf_xlength(path_sxp); ++i) {
303 uv_fs_t req;
304 const char* p = CHAR(STRING_ELT(path_sxp, i));
305 int res = uv_fs_stat(uv_default_loop(), &req, p, NULL);
306 LOGICAL(out)[i] = static_cast<int>(res == 0);
307 uv_fs_req_cleanup(&req);
308 }
309
310 UNPROTECT(1);
311 return out;
312 }
313
314 // [[export]]
315 extern "C" SEXP fs_access_(SEXP path_sxp, SEXP mode_sxp) {
316
317 int mode = INTEGER(mode_sxp)[0];
318
319 SEXP out = PROTECT(Rf_allocVector(LGLSXP, Rf_xlength(path_sxp)));
320 Rf_setAttrib(out, R_NamesSymbol, Rf_duplicate(path_sxp));
321
322 for (R_xlen_t i = 0; i < Rf_xlength(path_sxp); ++i) {
323 uv_fs_t req;
324 const char* p = CHAR(STRING_ELT(path_sxp, i));
325 int res = uv_fs_access(uv_default_loop(), &req, p, mode, NULL);
326 LOGICAL(out)[i] = res == 0;
327 uv_fs_req_cleanup(&req);
328 }
329
330 UNPROTECT(1);
331 return out;
332 }
333
334 // [[export]]
335 extern "C" SEXP fs_chmod_(SEXP path_sxp, SEXP mode_sxp) {
336 for (R_xlen_t i = 0; i < Rf_xlength(path_sxp); ++i) {
337 uv_fs_t req;
338 const char* p = CHAR(STRING_ELT(path_sxp, i));
339 mode_t m = INTEGER(mode_sxp)[i];
340 uv_fs_chmod(uv_default_loop(), &req, p, m, NULL);
341 stop_for_error(req, "Failed to chmod '%s'", p);
342 uv_fs_req_cleanup(&req);
343 }
344
345 return R_NilValue;
346 }
347
348 // [[export]]
349 extern "C" SEXP fs_unlink_(SEXP path) {
350 for (R_xlen_t i = 0; i < Rf_xlength(path); ++i) {
351 uv_fs_t req;
352 const char* p = CHAR(STRING_ELT(path, i));
353 uv_fs_unlink(uv_default_loop(), &req, p, NULL);
354 stop_for_error(req, "Failed to remove '%s'", p);
355 uv_fs_req_cleanup(&req);
356 }
357
358 return R_NilValue;
359 }
360
361 // [[export]]
362 extern "C" SEXP
363 fs_copyfile_(SEXP path_sxp, SEXP new_path_sxp, SEXP overwrite_sxp) {
364
365 bool overwrite = LOGICAL(overwrite_sxp)[0];
366
367 for (R_xlen_t i = 0; i < Rf_xlength(path_sxp); ++i) {
368 uv_fs_t req;
369 const char* p = CHAR(STRING_ELT(path_sxp, i));
370 const char* n = CHAR(STRING_ELT(new_path_sxp, i));
371 uv_fs_copyfile(
372 uv_default_loop(),
373 &req,
374 p,
375 n,
376 !overwrite ? UV_FS_COPYFILE_EXCL : 0,
377 NULL);
378 stop_for_error2(req, "Failed to copy '%s' to '%s'", p, n);
379 uv_fs_req_cleanup(&req);
380 }
381
382 return R_NilValue;
383 }
384
385 // [[export]]
386 extern "C" SEXP fs_chown_(SEXP path_sxp, SEXP uid_sxp, SEXP gid_sxp) {
387 int uid = INTEGER(uid_sxp)[0];
388 int gid = INTEGER(gid_sxp)[0];
389
390 for (R_xlen_t i = 0; i < Rf_xlength(path_sxp); ++i) {
391 uv_fs_t req;
392 const char* p = CHAR(STRING_ELT(path_sxp, i));
393 uv_fs_chown(uv_default_loop(), &req, p, uid, gid, NULL);
394 stop_for_error(req, "Failed to chown '%s'", p);
395 uv_fs_req_cleanup(&req);
396 }
397
398 return R_NilValue;
399 }
400
401 // [[export]]
402 extern "C" SEXP fs_touch_(SEXP path, SEXP atime_sxp, SEXP mtime_sxp) {
403
404 double atime = REAL(atime_sxp)[0];
405 double mtime = REAL(mtime_sxp)[0];
406
407 for (R_xlen_t i = 0; i < Rf_xlength(path); ++i) {
408 uv_fs_t req;
409 const char* p = CHAR(STRING_ELT(path, i));
410 uv_fs_utime(uv_default_loop(), &req, p, atime, mtime, NULL);
411 stop_for_error(req, "Failed to touch '%s'", p);
412 uv_fs_req_cleanup(&req);
413 }
414
415 return R_NilValue;
416 }