static void copy_helper(abts_case *tc, const char *from, const char * to, apr_fileperms_t perms, int append, apr_pool_t *p) { apr_status_t rv; apr_status_t dest_rv; apr_finfo_t copy; apr_finfo_t orig; apr_finfo_t dest; dest_rv = apr_stat(&dest, to, APR_FINFO_SIZE, p); if (!append) { rv = apr_file_copy(from, to, perms, p); } else { rv = apr_file_append(from, to, perms, p); } APR_ASSERT_SUCCESS(tc, "Error copying file", rv); rv = apr_stat(&orig, from, APR_FINFO_SIZE, p); APR_ASSERT_SUCCESS(tc, "Couldn't stat original file", rv); rv = apr_stat(©, to, APR_FINFO_SIZE, p); APR_ASSERT_SUCCESS(tc, "Couldn't stat copy file", rv); if (!append) { ABTS_ASSERT(tc, "File size differs", orig.size == copy.size); } else { ABTS_ASSERT(tc, "File size differs", ((dest_rv == APR_SUCCESS) ? dest.size : 0) + orig.size == copy.size); } }
int lua_apr_file_copy(lua_State *L) { const char *source, *target; apr_fileperms_t permissions; apr_status_t status; source = luaL_checkstring(L, 1); target = luaL_checkstring(L, 2); permissions = check_permissions(L, 3, 1); status = apr_file_copy(source, target, permissions, to_pool(L)); return push_status(L, status); }
static inline bool performCopy(const String &source, const String &dest) { if (stappler::filesystem::exists(dest)) { stappler::filesystem::remove(dest); } #if SPDEFAULT if (!stappler::filesystem::exists(dest)) { std::ofstream destStream(dest, std::ios::binary); auto f = openForReading(source); if (f && destStream.is_open()) { if (io::read(f, destStream) > 0) { return true; } } destStream.close(); } return false; #else return apr_file_copy(source.c_str(), dest.c_str(), APR_FPROT_FILE_SOURCE_PERMS, apr::AllocStack::get().top()) == APR_SUCCESS; #endif }
/* copy a file from one to another. */ void MVM_file_copy(MVMThreadContext *tc, MVMString *src, MVMString *dest) { apr_status_t rv; char *a, *b, *afull, *bfull; MVMuint32 len; apr_pool_t *tmp_pool; /* need a temporary pool */ if ((rv = apr_pool_create(&tmp_pool, POOL(tc))) != APR_SUCCESS) { MVM_exception_throw_apr_error(tc, rv, "Failed to copy file: "); } afull = MVM_file_get_full_path(tc, tmp_pool, a = MVM_string_utf8_encode_C_string(tc, src)); bfull = MVM_file_get_full_path(tc, tmp_pool, b = MVM_string_utf8_encode_C_string(tc, dest)); free(a); free(b); if ((rv = apr_file_copy((const char *)afull, (const char *)bfull, 0, tmp_pool)) != APR_SUCCESS) { apr_pool_destroy(tmp_pool); MVM_exception_throw_apr_error(tc, rv, "Failed to copy file: "); } apr_pool_destroy(tmp_pool); }
void nx_expr_proc__xm_fileop_file_copy(nx_expr_eval_ctx_t *eval_ctx, nx_module_t *module, nx_expr_arg_list_t *args) { nx_expr_arg_t *src, *dst; nx_value_t srcval, dstval; nx_exception_t e; apr_status_t rv; apr_pool_t *pool; ASSERT(module != NULL); ASSERT(args != NULL); src = NX_DLIST_FIRST(args); ASSERT(src != NULL); ASSERT(src->expr != NULL); dst = NX_DLIST_NEXT(src, link); ASSERT(dst != NULL); ASSERT(dst->expr != NULL); nx_expr_evaluate(eval_ctx, &srcval, src->expr); if ( srcval.defined != TRUE ) { throw_msg("'src' is undef"); } if ( srcval.type != NX_VALUE_TYPE_STRING ) { nx_value_kill(&srcval); throw_msg("string type required for 'src'"); } try { nx_expr_evaluate(eval_ctx, &dstval, dst->expr); } catch(e) { nx_value_kill(&srcval); rethrow(e); } if ( dstval.defined != TRUE ) { nx_value_kill(&srcval); throw_msg("'dst' is undef"); } if ( dstval.type != NX_VALUE_TYPE_STRING ) { nx_value_kill(&dstval); nx_value_kill(&srcval); throw_msg("string type required for 'dst'"); } pool = nx_pool_create_core(); if ( (rv = apr_file_copy(srcval.string->buf, dstval.string->buf, APR_FILE_SOURCE_PERMS, pool)) != APR_SUCCESS ) { log_aprerror(rv, "failed to copy file from '%s' to '%s'", srcval.string->buf, dstval.string->buf); } apr_pool_destroy(pool); nx_value_kill(&srcval); nx_value_kill(&dstval); }
SWITCH_DECLARE(switch_status_t) switch_file_copy(const char *from_path, const char *to_path, switch_fileperms_t perms, switch_memory_pool_t *pool) { return apr_file_copy(from_path, to_path, perms, pool); }
/* * Let's do it. We end up doing a lot of file opening and closing, * but what do we care? This application isn't run constantly. */ int main(int argc, const char * const argv[]) { apr_file_t *fpw = NULL; char record[MAX_STRING_LEN]; char line[MAX_STRING_LEN]; char *password = NULL; char *pwfilename = NULL; char *user = NULL; char tn[] = "htpasswd.tmp.XXXXXX"; char *dirname; char *scratch, cp[MAX_STRING_LEN]; int found = 0; int i; int alg = ALG_APMD5; int mask = 0; apr_pool_t *pool; int existing_file = 0; #if APR_CHARSET_EBCDIC apr_status_t rv; apr_xlate_t *to_ascii; #endif apr_app_initialize(&argc, &argv, NULL); atexit(terminate); apr_pool_create(&pool, NULL); apr_file_open_stderr(&errfile, pool); #if APR_CHARSET_EBCDIC rv = apr_xlate_open(&to_ascii, "ISO-8859-1", APR_DEFAULT_CHARSET, pool); if (rv) { apr_file_printf(errfile, "apr_xlate_open(to ASCII)->%d" NL, rv); exit(1); } rv = apr_SHA1InitEBCDIC(to_ascii); if (rv) { apr_file_printf(errfile, "apr_SHA1InitEBCDIC()->%d" NL, rv); exit(1); } rv = apr_MD5InitEBCDIC(to_ascii); if (rv) { apr_file_printf(errfile, "apr_MD5InitEBCDIC()->%d" NL, rv); exit(1); } #endif /*APR_CHARSET_EBCDIC*/ check_args(pool, argc, argv, &alg, &mask, &user, &pwfilename, &password); #if defined(WIN32) || defined(NETWARE) if (alg == ALG_CRYPT) { alg = ALG_APMD5; apr_file_printf(errfile, "Automatically using MD5 format." NL); } #endif #if (!(defined(WIN32) || defined(TPF) || defined(NETWARE))) if (alg == ALG_PLAIN) { apr_file_printf(errfile,"Warning: storing passwords as plain text " "might just not work on this platform." NL); } #endif /* * Only do the file checks if we're supposed to frob it. */ if (!(mask & APHTP_NOFILE)) { existing_file = exists(pwfilename, pool); if (existing_file) { /* * Check that this existing file is readable and writable. */ if (!accessible(pool, pwfilename, APR_READ | APR_APPEND)) { apr_file_printf(errfile, "%s: cannot open file %s for " "read/write access" NL, argv[0], pwfilename); exit(ERR_FILEPERM); } } else { /* * Error out if -c was omitted for this non-existant file. */ if (!(mask & APHTP_NEWFILE)) { apr_file_printf(errfile, "%s: cannot modify file %s; use '-c' to create it" NL, argv[0], pwfilename); exit(ERR_FILEPERM); } /* * As it doesn't exist yet, verify that we can create it. */ if (!accessible(pool, pwfilename, APR_CREATE | APR_WRITE)) { apr_file_printf(errfile, "%s: cannot create file %s" NL, argv[0], pwfilename); exit(ERR_FILEPERM); } } } /* * All the file access checks (if any) have been made. Time to go to work; * try to create the record for the username in question. If that * fails, there's no need to waste any time on file manipulations. * Any error message text is returned in the record buffer, since * the mkrecord() routine doesn't have access to argv[]. */ if (!(mask & APHTP_DELUSER)) { i = mkrecord(user, record, sizeof(record) - 1, password, alg); if (i != 0) { apr_file_printf(errfile, "%s: %s" NL, argv[0], record); exit(i); } if (mask & APHTP_NOFILE) { printf("%s" NL, record); exit(0); } } /* * We can access the files the right way, and we have a record * to add or update. Let's do it.. */ if (apr_temp_dir_get((const char**)&dirname, pool) != APR_SUCCESS) { apr_file_printf(errfile, "%s: could not determine temp dir" NL, argv[0]); exit(ERR_FILEPERM); } dirname = apr_psprintf(pool, "%s/%s", dirname, tn); if (apr_file_mktemp(&ftemp, dirname, 0, pool) != APR_SUCCESS) { apr_file_printf(errfile, "%s: unable to create temporary file %s" NL, argv[0], dirname); exit(ERR_FILEPERM); } /* * If we're not creating a new file, copy records from the existing * one to the temporary file until we find the specified user. */ if (existing_file && !(mask & APHTP_NEWFILE)) { if (apr_file_open(&fpw, pwfilename, APR_READ | APR_BUFFERED, APR_OS_DEFAULT, pool) != APR_SUCCESS) { apr_file_printf(errfile, "%s: unable to read file %s" NL, argv[0], pwfilename); exit(ERR_FILEPERM); } while (apr_file_gets(line, sizeof(line), fpw) == APR_SUCCESS) { char *colon; strcpy(cp, line); scratch = cp; while (apr_isspace(*scratch)) { ++scratch; } if (!*scratch || (*scratch == '#')) { putline(ftemp, line); continue; } /* * See if this is our user. */ colon = strchr(scratch, ':'); if (colon != NULL) { *colon = '\0'; } else { /* * If we've not got a colon on the line, this could well * not be a valid htpasswd file. * We should bail at this point. */ apr_file_printf(errfile, "%s: The file %s does not appear " "to be a valid htpasswd file." NL, argv[0], pwfilename); apr_file_close(fpw); exit(ERR_INVALID); } if (strcmp(user, scratch) != 0) { putline(ftemp, line); continue; } else { if (!(mask & APHTP_DELUSER)) { /* We found the user we were looking for. * Add him to the file. */ apr_file_printf(errfile, "Updating "); putline(ftemp, record); found++; } else { /* We found the user we were looking for. * Delete them from the file. */ apr_file_printf(errfile, "Deleting "); found++; } } } apr_file_close(fpw); } if (!found && !(mask & APHTP_DELUSER)) { apr_file_printf(errfile, "Adding "); putline(ftemp, record); } else if (!found && (mask & APHTP_DELUSER)) { apr_file_printf(errfile, "User %s not found" NL, user); exit(0); } apr_file_printf(errfile, "password for user %s" NL, user); /* The temporary file has all the data, just copy it to the new location. */ if (apr_file_copy(dirname, pwfilename, APR_FILE_SOURCE_PERMS, pool) != APR_SUCCESS) { apr_file_printf(errfile, "%s: unable to update file %s" NL, argv[0], pwfilename); exit(ERR_FILEPERM); } apr_file_close(ftemp); return 0; }
int main(int argc, const char * const argv[]) { apr_file_t *f; apr_status_t rv; char tn[] = "htdigest.tmp.XXXXXX"; char *dirname; char user[MAX_STRING_LEN]; char realm[MAX_STRING_LEN]; char line[MAX_STRING_LEN]; char l[MAX_STRING_LEN]; char w[MAX_STRING_LEN]; char x[MAX_STRING_LEN]; int found; apr_app_initialize(&argc, &argv, NULL); atexit(terminate); apr_pool_create(&cntxt, NULL); apr_file_open_stderr(&errfile, cntxt); #if APR_CHARSET_EBCDIC rv = apr_xlate_open(&to_ascii, "ISO8859-1", APR_DEFAULT_CHARSET, cntxt); if (rv) { apr_file_printf(errfile, "apr_xlate_open(): %s (%d)\n", apr_strerror(rv, line, sizeof(line)), rv); exit(1); } #endif apr_signal(SIGINT, (void (*)(int)) interrupted); if (argc == 5) { if (strcmp(argv[1], "-c")) usage(); rv = apr_file_open(&f, argv[2], APR_WRITE | APR_CREATE, APR_OS_DEFAULT, cntxt); if (rv != APR_SUCCESS) { char errmsg[120]; apr_file_printf(errfile, "Could not open passwd file %s for writing: %s\n", argv[2], apr_strerror(rv, errmsg, sizeof errmsg)); exit(1); } apr_file_printf(errfile, "Adding password for %s in realm %s.\n", argv[4], argv[3]); add_password(argv[4], argv[3], f); apr_file_close(f); exit(0); } else if (argc != 4) usage(); if (apr_temp_dir_get((const char**)&dirname, cntxt) != APR_SUCCESS) { apr_file_printf(errfile, "%s: could not determine temp dir\n", argv[0]); exit(1); } dirname = apr_psprintf(cntxt, "%s/%s", dirname, tn); if (apr_file_mktemp(&tfp, dirname, 0, cntxt) != APR_SUCCESS) { apr_file_printf(errfile, "Could not open temp file %s.\n", dirname); exit(1); } if (apr_file_open(&f, argv[1], APR_READ, APR_OS_DEFAULT, cntxt) != APR_SUCCESS) { apr_file_printf(errfile, "Could not open passwd file %s for reading.\n", argv[1]); apr_file_printf(errfile, "Use -c option to create new one.\n"); cleanup_tempfile_and_exit(1); } apr_cpystrn(user, argv[3], sizeof(user)); apr_cpystrn(realm, argv[2], sizeof(realm)); found = 0; while (!(get_line(line, MAX_STRING_LEN, f))) { if (found || (line[0] == '#') || (!line[0])) { putline(tfp, line); continue; } strcpy(l, line); getword(w, l, ':'); getword(x, l, ':'); if (strcmp(user, w) || strcmp(realm, x)) { putline(tfp, line); continue; } else { apr_file_printf(errfile, "Changing password for user %s in realm %s\n", user, realm); add_password(user, realm, tfp); found = 1; } } if (!found) { apr_file_printf(errfile, "Adding user %s in realm %s\n", user, realm); add_password(user, realm, tfp); } apr_file_close(f); /* The temporary file has all the data, just copy it to the new location. */ if (apr_file_copy(dirname, argv[1], APR_FILE_SOURCE_PERMS, cntxt) != APR_SUCCESS) { apr_file_printf(errfile, "%s: unable to update file %s\n", argv[0], argv[1]); } apr_file_close(tfp); return 0; }