Skip to content

Commit

Permalink
Allow warning on error rather than exiting
Browse files Browse the repository at this point in the history
Fixes #105
Fixes #121
  • Loading branch information
jimhester committed Jul 23, 2018
1 parent 1528958 commit 103a19e
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 10 deletions.
8 changes: 8 additions & 0 deletions R/zzz.R
@@ -1,3 +1,7 @@
fs_default_options <- list(
fs.should_error = TRUE
)

# nocov start
.onLoad <- function(...) {
register_s3_method("pillar", "pillar_shaft", "fs_path")
Expand All @@ -11,6 +15,10 @@
register_s3_method("pillar", "type_sum", "fs_perms")
register_s3_method("testthat", "compare", "fs_perms")

op <- options()
toset <- !(names(fs_default_options) %in% names(op))
if(any(toset)) options(fs_default_options[toset])

invisible()
}

Expand Down
10 changes: 8 additions & 2 deletions src/dir.cc
Expand Up @@ -54,7 +54,10 @@ void dir_map(
CollectorList* value) {
uv_fs_t req;
uv_fs_scandir(uv_default_loop(), &req, path, 0, NULL);
stop_for_error(req, "Failed to search directory '%s'", path);
if (stop_for_error(req, "Failed to search directory '%s'", path) == false) {
uv_fs_req_cleanup(&req);
return;
}

uv_dirent_t e;
for (int next_res = uv_fs_scandir_next(&req, &e); next_res != UV_EOF;
Expand Down Expand Up @@ -84,7 +87,10 @@ void dir_map(
dir_map(fun, name.c_str(), all, file_type, true, value);
}
if (next_res != UV_EOF) {
stop_for_error(req, "Failed to search directory '%s'", path);
if (stop_for_error(req, "Failed to search directory '%s'", path) ==
false) {
break;
}
}
}
uv_fs_req_cleanup(&req);
Expand Down
20 changes: 15 additions & 5 deletions src/error.cc
Expand Up @@ -2,23 +2,29 @@

#define BUFSIZE 8192

SEXP error_condition(uv_fs_t req, const char* loc, const char* format, ...) {
bool error_condition(uv_fs_t req, const char* loc, const char* format, ...) {
SEXP condition, c, signalConditionFun, out;
va_list ap;

if (req.result >= 0) {
return R_NilValue;
return true;
}
int err = req.result;
uv_fs_req_cleanup(&req);

const char* nms[] = {"message", ""};
PROTECT(condition = Rf_mkNamed(VECSXP, nms));

int should_error = Rf_asLogical(Rf_GetOption1(Rf_install("fs.should_error")));

PROTECT(c = Rf_allocVector(STRSXP, 4));
SET_STRING_ELT(c, 0, Rf_mkChar(uv_err_name(err)));
SET_STRING_ELT(c, 1, Rf_mkChar("fs_error"));
SET_STRING_ELT(c, 2, Rf_mkChar("error"));
if (should_error) {
SET_STRING_ELT(c, 2, Rf_mkChar("error"));
} else {
SET_STRING_ELT(c, 2, Rf_mkChar("warning"));
}
SET_STRING_ELT(c, 3, Rf_mkChar("condition"));

char buf[BUFSIZE];
Expand All @@ -32,12 +38,16 @@ SEXP error_condition(uv_fs_t req, const char* loc, const char* format, ...) {
SET_VECTOR_ELT(condition, 0, Rf_mkString(buf));
Rf_setAttrib(condition, R_ClassSymbol, c);
Rf_setAttrib(condition, Rf_mkString("location"), Rf_mkString(loc));
signalConditionFun = Rf_findFun(Rf_install("stop"), R_BaseEnv);
if (should_error) {
signalConditionFun = Rf_findFun(Rf_install("stop"), R_BaseEnv);
} else {
signalConditionFun = Rf_findFun(Rf_install("warning"), R_BaseEnv);
}

SEXP call = PROTECT(Rf_lang2(signalConditionFun, condition));
PROTECT(out = Rf_eval(call, R_GlobalEnv));

UNPROTECT(4);

return out;
return false;
}
2 changes: 1 addition & 1 deletion src/error.h
Expand Up @@ -20,7 +20,7 @@ extern "C" {
#define stop_for_error2(req, format, one, two) \
error_condition(req, __FILE__ ":" STRING(__LINE__), format, one, two)

SEXP error_condition(uv_fs_t req, const char* loc, const char* format, ...);
bool error_condition(uv_fs_t req, const char* loc, const char* format, ...);

#ifdef __cplusplus
}
Expand Down
9 changes: 7 additions & 2 deletions src/link.cc
Expand Up @@ -40,7 +40,9 @@ void link_create_symbolic_(CharacterVector path, CharacterVector new_path) {
// check that the link points to where we want to point to
uv_fs_t l_req;
uv_fs_readlink(uv_default_loop(), &l_req, n, NULL);
stop_for_error(l_req, "Failed to read link '%s'", n);
if (stop_for_error(l_req, "Failed to read link '%s'", n) == false) {
continue;
}
if (strcmp(path_tidy_((const char*)l_req.ptr).c_str(), p) == 0) {
uv_fs_req_cleanup(&req);
uv_fs_req_cleanup(&l_req);
Expand All @@ -61,7 +63,10 @@ CharacterVector readlink_(CharacterVector path) {
uv_fs_t req;
const char* p = CHAR(STRING_ELT(path, i));
uv_fs_readlink(uv_default_loop(), &req, p, NULL);
stop_for_error(req, "Failed to read link '%s'", p);
if (stop_for_error(req, "Failed to read link '%s'", p) == false) {
SET_STRING_ELT(out, i, NA_STRING);
continue;
}
SET_STRING_ELT(out, i, Rf_mkCharCE((const char*)req.ptr, CE_UTF8));
uv_fs_req_cleanup(&req);
}
Expand Down

0 comments on commit 103a19e

Please sign in to comment.