| 1 |
|
| 2 |
#include <cstring> |
| 3 |
|
| 4 |
#include "R.h" |
| 5 |
#include "Rinternals.h" |
| 6 |
#include "error.h" |
| 7 |
#include "utils.h" |
| 8 |
|
| 9 |
#include "uv.h" |
| 10 |
|
| 11 |
#undef ERROR |
| 12 |
|
| 13 |
// [[export]] |
| 14 |
extern "C" SEXP fs_link_create_hard_(SEXP path, SEXP new_path) { |
| 15 |
for (R_xlen_t i = 0; i < Rf_xlength(new_path); ++i) { |
| 16 |
uv_fs_t req; |
| 17 |
const char* p = CHAR(STRING_ELT(path, i)); |
| 18 |
const char* n = CHAR(STRING_ELT(new_path, i)); |
| 19 |
uv_fs_link(uv_default_loop(), &req, p, n, NULL); |
| 20 |
stop_for_error2(req, "Failed to link '%s' to '%s'", p, n); |
| 21 |
uv_fs_req_cleanup(&req); |
| 22 |
} |
| 23 |
|
| 24 |
return R_NilValue; |
| 25 |
} |
| 26 |
|
| 27 |
// [[export]] |
| 28 |
extern "C" SEXP fs_link_create_symbolic_(SEXP path, SEXP new_path) { |
| 29 |
for (R_xlen_t i = 0; i < Rf_xlength(new_path); ++i) { |
| 30 |
uv_fs_t req; |
| 31 |
const char* p = CHAR(STRING_ELT(path, i)); |
| 32 |
const char* n = CHAR(STRING_ELT(new_path, i)); |
| 33 |
|
| 34 |
int flags = 0; |
| 35 |
// windows 10 sort of supports file links for non-elevated users, and libuv |
| 36 |
// will try the flag, https://github.com/libuv/libuv/issues/1157. But most |
| 37 |
// users do not _yet_ have this enabled. In the meantime we will always use |
| 38 |
// the directory symlinks instead. |
| 39 |
#ifdef __WIN32 |
| 40 |
flags = UV_FS_SYMLINK_JUNCTION; |
| 41 |
#endif |
| 42 |
uv_fs_symlink(uv_default_loop(), &req, p, n, flags, NULL); |
| 43 |
if (req.result == UV_EEXIST && get_dirent_type(n) == UV_DIRENT_LINK) { |
| 44 |
// check that the link points to where we want to point to |
| 45 |
uv_fs_t l_req; |
| 46 |
uv_fs_readlink(uv_default_loop(), &l_req, n, NULL); |
| 47 |
stop_for_error(l_req, "Failed to read link '%s'", n); |
| 48 |
if (strcmp(path_tidy_((const char*)l_req.ptr).c_str(), p) == 0) { |
| 49 |
uv_fs_req_cleanup(&req); |
| 50 |
uv_fs_req_cleanup(&l_req); |
| 51 |
continue; |
| 52 |
} |
| 53 |
uv_fs_req_cleanup(&l_req); |
| 54 |
} |
| 55 |
stop_for_error2(req, "Failed to link '%s' to '%s'", p, n); |
| 56 |
uv_fs_req_cleanup(&req); |
| 57 |
} |
| 58 |
|
| 59 |
return R_NilValue; |
| 60 |
} |
| 61 |
|
| 62 |
// [[export]] |
| 63 |
extern "C" SEXP fs_readlink_(SEXP path) { |
| 64 |
SEXP out = PROTECT(Rf_allocVector(STRSXP, Rf_xlength(path))); |
| 65 |
Rf_setAttrib(out, R_NamesSymbol, path); |
| 66 |
for (R_xlen_t i = 0; i < Rf_xlength(path); ++i) { |
| 67 |
uv_fs_t req; |
| 68 |
const char* p = CHAR(STRING_ELT(path, i)); |
| 69 |
uv_fs_readlink(uv_default_loop(), &req, p, NULL); |
| 70 |
stop_for_error(req, "Failed to read link '%s'", p); |
| 71 |
SET_STRING_ELT(out, i, Rf_mkCharCE((const char*)req.ptr, CE_UTF8)); |
| 72 |
uv_fs_req_cleanup(&req); |
| 73 |
} |
| 74 |
|
| 75 |
UNPROTECT(1); |
| 76 |
return out; |
| 77 |
} |