void EvalDir(const void *data, qCtx *ctx, qStr *out, qArgAry *args) { VALID_ARGC("dir", 2, 3); CStr path = (*args)[0]; if (!path) { ctx->ThrowF(out, 601, "Empty directory path is invalid"); return; } if (!safe_fcheck(ctx, path, 'r')) { ctx->ThrowF(out, 601, "Failed to open directory. %y", GetLastError()); return; } CStr body = args->GetAt(1); int mask = 0; if (args->Count() > 2) { mask = ParseInt((*args)[2]); } if (!mask) mask = -1; ScanDir(path, mask, body, ctx, out); }
FILE *safe_fopen(qCtx *ctx, const char *path, const char *mode) { if (safe_fcheck(ctx, path, ((mode && (mode[0] == 'r')) ? 'r' : 'w'))) return fopen(path, mode); else return NULL; }
void EvalFileRename(const void *mode, qCtx *ctx, qStr *out, qArgAry *args) { VALID_ARGC("rename", 2, 3); CStr path = (*args)[0]; CStr path_new = (*args)[1]; if (!safe_fcheck(ctx, path, 'w')) { ctx->ThrowF(out, 632, "Access denied to source"); return; } if (!safe_fcheck(ctx, path_new, 'w')) { ctx->ThrowF(out, 632, "Access denied to destination"); return; } #ifdef WIN32 bool clobber = ParseBool((*args)[2]); if (clobber) { if (!MoveFileEx(path, path_new, MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING)) { ctx->ThrowF(out, 632, "Rename failed, %y", GetLastError()); return; } } else #endif { if (rename(path, path_new)) { if (errno == ENOENT) { ctx->ThrowF(out, 632, "Rename failed, file or path specified not found."); return; } if (errno == EACCES) { ctx->ThrowF(out, 633, "Rename failed, file already exists."); return; } if (errno == EINVAL) { ctx->ThrowF(out, 634, "Rename failed, invalid filename."); return; } ctx->ThrowF(out, 635, "Rename failed."); return; } } }
void EvalFileDelete(const void *mode, qCtx *ctx, qStr *out, qArgAry *args) { CStr path = (*args)[0]; if (path.IsEmpty()) return; if (!safe_fcheck(ctx, path, 'w')) { ctx->ThrowF(out, 632, "Error deleting file, %y", GetLastError()); return; } remove(path); }
void EvalHFile(const void *data, qCtx *ctx, qStr *out, qArgAry *args) { qObjHCtx *hCtx; if ( ctx->GetEnv() && ctx->GetEnv()->GetSessionCtx() ) ctx=ctx->GetEnv()->GetSessionCtx(); if (args->Count() == 1) { if (!safe_fcheck(ctx, (*args)[0])) { ctx->ThrowF(out, 909, "Permission denied"); return; } hCtx = new qObjHCtx(ctx); hCtx->SetPath((*args)[0], true); ctx->MapObj(hCtx, "<hctx>"); } else if (args->Count() == 0) { if (hCtx=GetHCtx(ctx)) hCtx->Eval(ctx, out, args); } }