Debian Codegraph @

src/dir.cc

raw symbols SCIP
1 #include <cstring>
2 #include <limits>
3 #include <string>
4 #include <sys/stat.h>
5
6 #include "getmode.h"
7 #include "uv.h"
8
9 #undef ERROR
10
11 #include "CollectorList.h"
12 #include "R.h"
13 #include "Rinternals.h"
14 #include "error.h"
15 #include "utils.h"
16
17 // [[export]]
18 extern "C" SEXP fs_mkdir_(SEXP path, SEXP mode_sxp) {
19 mode_t m = INTEGER(mode_sxp)[0];
20
21 R_xlen_t n = Rf_xlength(path);
22 for (R_xlen_t i = 0; i < n; ++i) {
23 uv_fs_t req;
24 const char* p = CHAR(STRING_ELT(path, i));
25
26 int fd = uv_fs_mkdir(uv_default_loop(), &req, p, 0x777, NULL);
27
28 if (fd == UV_EEXIST) {
29 // Fail silently if the directory already exists
30
31 uv_dirent_type_t t = get_dirent_type(p);
32 if (t == UV_DIRENT_DIR || t == UV_DIRENT_LINK) {
33 uv_fs_req_cleanup(&req);
34 continue;
35 }
36 } else if (fd == UV_EPERM && i < n - 1) {
37 // Fail silently if we do not have permissions to create the directory and
38 // it is not the last directory we are trying to create. (In this case we
39 // assume the directory already exists).
40
41 uv_fs_req_cleanup(&req);
42 continue;
43 }
44
45 stop_for_error(req, "Failed to make directory '%s'", p);
46
47 uv_fs_t chmod_req;
48 uv_fs_chmod(uv_default_loop(), &chmod_req, p, m, NULL);
49 stop_for_error(
50 chmod_req, "Failed to set permissions for directory '%s'", p);
51 }
52
53 return R_NilValue;
54 }
55
56 // [[export]]
57 extern "C" SEXP fs_rmdir_(SEXP path) {
58 for (R_xlen_t i = 0; i < Rf_xlength(path); ++i) {
59 uv_fs_t req;
60 const char* p = CHAR(STRING_ELT(path, i));
61 uv_fs_rmdir(uv_default_loop(), &req, p, NULL);
62 stop_for_error(req, "Failed to remove '%s'", p);
63
64 uv_fs_req_cleanup(&req);
65 }
66
67 return R_NilValue;
68 }
69
70 void dir_map(
71 SEXP fun,
72 const char* path,
73 bool all,
74 int file_type,
75 int recurse,
76 CollectorList* value,
77 bool fail) {
78
79 BEGIN_CPP
80
81 if (recurse < 0) {
82 recurse = std::numeric_limits<int>::max();
83 }
84
85 uv_fs_t req;
86 uv_fs_scandir(uv_default_loop(), &req, path, 0, NULL);
87
88 if (!fail && warn_for_error(req, "Failed to search directory '%s'", path)) {
89 return;
90 }
91
92 stop_for_error(req, "Failed to search directory '%s'", path);
93
94 uv_dirent_t e;
95 for (int next_res = uv_fs_scandir_next(&req, &e); next_res != UV_EOF;
96 next_res = uv_fs_scandir_next(&req, &e)) {
97 if (!all && e.name[0] == '.') {
98 continue;
99 }
100
101 std::string name;
102
103 // If path is '.', just return the name
104 if (strcmp(path, ".") == 0) {
105 name = e.name;
106 }
107 // If path already ends with '/' just concatenate them.
108 else if (path[strlen(path) - 1] == '/') {
109 name = std::string(path) + e.name;
110 } else {
111 name = std::string(path) + '/' + e.name;
112 }
113 uv_dirent_type_t entry_type = get_dirent_type(name.c_str(), e.type, fail);
114 if (file_type == -1 || (((1 << (entry_type)) & file_type) > 0)) {
115 SEXP call = PROTECT(Rf_lang2(fun, Rf_mkString(name.c_str())));
116 SEXP res = PROTECT(Rf_eval(call, R_GlobalEnv));
117 value->push_back(res);
118 UNPROTECT(2);
119 }
120
121 if (recurse > 0 && entry_type == UV_DIRENT_DIR) {
122 dir_map(fun, name.c_str(), all, file_type, recurse - 1, value, fail);
123 }
124 if (next_res != UV_EOF) {
125
126 if (!fail && warn_for_error(req, "Failed to open directory '%s'", path)) {
127 continue;
128 }
129 stop_for_error(req, "Failed to open directory '%s'", path);
130 }
131 }
132 uv_fs_req_cleanup(&req);
133
134 END_CPP
135 }
136
137 // [[export]]
138 extern "C" SEXP fs_dir_map_(
139 SEXP path_sxp,
140 SEXP fun_sxp,
141 SEXP all_sxp,
142 SEXP type_sxp,
143 SEXP recurse_sxp,
144 SEXP fail_sxp) {
145
146 CollectorList out;
147 for (R_xlen_t i = 0; i < Rf_xlength(path_sxp); ++i) {
148 const char* p = CHAR(STRING_ELT(path_sxp, i));
149 dir_map(
150 fun_sxp,
151 p,
152 LOGICAL(all_sxp)[0],
153 INTEGER(type_sxp)[0],
154 INTEGER(recurse_sxp)[0],
155 &out,
156 LOGICAL(fail_sxp)[0]);
157 }
158 return out;
159 }