| 1 |
#include <libgen.h> |
| 2 |
|
| 3 |
#include <cstdlib> |
| 4 |
#include <cstring> |
| 5 |
#include <string> |
| 6 |
|
| 7 |
#include "R.h" |
| 8 |
#include "Rinternals.h" |
| 9 |
#include "error.h" |
| 10 |
#include "utils.h" |
| 11 |
|
| 12 |
#include "uv.h" |
| 13 |
|
| 14 |
#undef ERROR |
| 15 |
|
| 16 |
|
| 17 |
// [[export]] |
| 18 |
extern "C" SEXP fs_realize_(SEXP path) { |
| 19 |
SEXP out = PROTECT(Rf_allocVector(STRSXP, Rf_xlength(path))); |
| 20 |
|
| 21 |
for (R_xlen_t i = 0; i < Rf_xlength(out); ++i) { |
| 22 |
uv_fs_t req; |
| 23 |
const char* p = CHAR(STRING_ELT(path, i)); |
| 24 |
uv_fs_realpath(uv_default_loop(), &req, p, NULL); |
| 25 |
stop_for_error(req, "Failed to realize '%s'", p); |
| 26 |
SET_STRING_ELT(out, i, Rf_mkChar((const char*)req.ptr)); |
| 27 |
uv_fs_req_cleanup(&req); |
| 28 |
} |
| 29 |
|
| 30 |
UNPROTECT(1); |
| 31 |
return out; |
| 32 |
} |
| 33 |
|
| 34 |
// [[export]] |
| 35 |
extern "C" SEXP fs_path_(SEXP paths, SEXP ext_sxp) { |
| 36 |
R_xlen_t max_row = 0; |
| 37 |
R_xlen_t max_col = Rf_xlength(paths); |
| 38 |
char buf[PATH_MAX]; |
| 39 |
char* b = buf; |
| 40 |
if (max_col == 0) { |
| 41 |
return Rf_allocVector(STRSXP, 0); |
| 42 |
} |
| 43 |
for (R_xlen_t c = 0; c < max_col; ++c) { |
| 44 |
R_xlen_t len = Rf_xlength(VECTOR_ELT(paths, c)); |
| 45 |
if (len == 0) { |
| 46 |
return Rf_allocVector(STRSXP, 0); |
| 47 |
} |
| 48 |
if (len > max_row) { |
| 49 |
max_row = len; |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
R_xlen_t ext_len = Rf_xlength(ext_sxp); |
| 54 |
if (ext_len == 0) { |
| 55 |
return Rf_allocVector(STRSXP, 0); |
| 56 |
} else if (ext_len > max_row) { |
| 57 |
max_row = ext_len; |
| 58 |
} |
| 59 |
|
| 60 |
SEXP out = PROTECT(Rf_allocVector(STRSXP, max_row)); |
| 61 |
for (R_xlen_t r = 0; r < max_row; ++r) { |
| 62 |
bool has_na = false; |
| 63 |
b = buf; |
| 64 |
for (R_xlen_t c = 0; c < max_col; ++c) { |
| 65 |
R_xlen_t k = Rf_xlength(VECTOR_ELT(paths, c)); |
| 66 |
if (k > 0) { |
| 67 |
SEXP str = STRING_ELT(VECTOR_ELT(paths, c), r % k); |
| 68 |
if (str == NA_STRING) { |
| 69 |
has_na = true; |
| 70 |
break; |
| 71 |
} |
| 72 |
int str_len = LENGTH(str); |
| 73 |
int new_len = b - buf + str_len; |
| 74 |
if (new_len > PATH_MAX) { |
| 75 |
UNPROTECT(1); |
| 76 |
Rf_error( |
| 77 |
"Total path length must be less than PATH_MAX: %i", PATH_MAX); |
| 78 |
} |
| 79 |
|
| 80 |
const char* s = CHAR(str); |
| 81 |
strncpy(b, s, str_len); |
| 82 |
b += str_len; |
| 83 |
|
| 84 |
bool trailing_slash = |
| 85 |
(b > buf) && (*(b - 1) == '/' || *(b - 1) == '\\'); |
| 86 |
if (!(trailing_slash || c == (max_col - 1))) { |
| 87 |
*b++ = '/'; |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
if (has_na) { |
| 92 |
SET_STRING_ELT(out, r, NA_STRING); |
| 93 |
} else { |
| 94 |
const char* ext = CHAR(STRING_ELT(ext_sxp, r % ext_len)); |
| 95 |
if (strlen(ext) > 0) { |
| 96 |
*b++ = '.'; |
| 97 |
strcpy(b, ext); |
| 98 |
b += strlen(ext) + 1; |
| 99 |
} |
| 100 |
*b = '\0'; |
| 101 |
SET_STRING_ELT(out, r, Rf_mkCharCE(buf, CE_UTF8)); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
UNPROTECT(1); |
| 106 |
|
| 107 |
return out; |
| 108 |
} |
| 109 |
|
| 110 |
// Sets the destination with the source and translates slashes if needed |
| 111 |
void set_path( |
| 112 |
char* destination, |
| 113 |
const char* source, |
| 114 |
size_t start = 0, |
| 115 |
size_t size = PATH_MAX) { |
| 116 |
size_t i = 0; |
| 117 |
for (; (i + start) < size && source[i] != '\0'; ++i) { |
| 118 |
if (source[i] == '\\') { |
| 119 |
destination[start + i] = '/'; |
| 120 |
} else { |
| 121 |
destination[start + i] = source[i]; |
| 122 |
} |
| 123 |
} |
| 124 |
destination[start + i] = '\0'; |
| 125 |
} |
| 126 |
|
| 127 |
std::string expand_windows(const char* p) { |
| 128 |
size_t np = strlen(p); |
| 129 |
if (np == 0) { |
| 130 |
return ""; |
| 131 |
} |
| 132 |
if (p[0] != '~') { |
| 133 |
return p; |
| 134 |
} |
| 135 |
|
| 136 |
size_t i = 0; |
| 137 |
for (; i < np; ++i) { |
| 138 |
if (p[i] == '/' || p[i] == '\\') { |
| 139 |
break; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
const char* env; |
| 144 |
char home[PATH_MAX] = {'\0'}; |
| 145 |
size_t n; |
| 146 |
|
| 147 |
if ((env = getenv("R_FS_HOME"))) { |
| 148 |
set_path(home, env); |
| 149 |
} else if ((env = getenv("USERPROFILE"))) { |
| 150 |
set_path(home, env); |
| 151 |
} else { |
| 152 |
env = getenv("HOMEDRIVE"); |
| 153 |
if (env) { |
| 154 |
set_path(home, env); |
| 155 |
} |
| 156 |
env = getenv("HOMEPATH"); |
| 157 |
if (!env) { |
| 158 |
return p; |
| 159 |
} |
| 160 |
set_path(home, env, strlen(home), PATH_MAX); |
| 161 |
} |
| 162 |
|
| 163 |
// # ~user case |
| 164 |
if (i != 1) { |
| 165 |
char* home_str = strdup(home); |
| 166 |
if (home_str == NULL) { |
| 167 |
Rf_error("Allocation Failed"); |
| 168 |
} |
| 169 |
strncpy(home, dirname(home_str), PATH_MAX - 1); |
| 170 |
free(home_str); |
| 171 |
|
| 172 |
// only copy enough characters to i |
| 173 |
n = strlen(home); |
| 174 |
strncat(home, p, i); |
| 175 |
home[n] = '/'; |
| 176 |
} |
| 177 |
if (np > i) { |
| 178 |
n = strlen(home); |
| 179 |
strncat(home, p + i, PATH_MAX - n); |
| 180 |
home[n] = '/'; |
| 181 |
} |
| 182 |
|
| 183 |
return home; |
| 184 |
} |
| 185 |
|
| 186 |
// [[export]] |
| 187 |
extern "C" SEXP fs_expand_(SEXP path_sxp, SEXP windows_sxp) { |
| 188 |
|
| 189 |
SEXP out = PROTECT(Rf_allocVector(STRSXP, Rf_xlength(path_sxp))); |
| 190 |
|
| 191 |
bool windows = LOGICAL(windows_sxp)[0]; |
| 192 |
|
| 193 |
for (R_xlen_t i = 0; i < Rf_xlength(out); ++i) { |
| 194 |
if (STRING_ELT(path_sxp, i) == R_NaString) { |
| 195 |
SET_STRING_ELT(out, i, R_NaString); |
| 196 |
} else { |
| 197 |
const char* p = CHAR(STRING_ELT(path_sxp, i)); |
| 198 |
if (windows) { |
| 199 |
|
| 200 |
BEGIN_CPP |
| 201 |
std::string res = expand_windows(p); |
| 202 |
SET_STRING_ELT(out, i, Rf_mkCharCE(res.c_str(), CE_UTF8)); |
| 203 |
END_CPP |
| 204 |
} else { |
| 205 |
SET_STRING_ELT(out, i, Rf_mkCharCE(R_ExpandFileName(p), CE_UTF8)); |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
UNPROTECT(1); |
| 211 |
return out; |
| 212 |
} |
| 213 |
|
| 214 |
// [[export]] |
| 215 |
extern "C" SEXP fs_tidy_(SEXP path) { |
| 216 |
SEXP out = PROTECT(Rf_allocVector(STRSXP, Rf_xlength(path))); |
| 217 |
|
| 218 |
for (R_xlen_t i = 0; i < Rf_xlength(out); ++i) { |
| 219 |
if (STRING_ELT(path, i) == R_NaString) { |
| 220 |
SET_STRING_ELT(out, i, R_NaString); |
| 221 |
} else { |
| 222 |
|
| 223 |
BEGIN_CPP |
| 224 |
std::string p = path_tidy_(CHAR(STRING_ELT(path, i))); |
| 225 |
SET_STRING_ELT(out, i, Rf_mkCharCE(p.c_str(), CE_UTF8)); |
| 226 |
END_CPP |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
UNPROTECT(1); |
| 231 |
return out; |
| 232 |
} |