int main(POSSIBLY_UNUSED(int argc), char **argv) { char tdir[PATH_MAX]; struct fast_log_buf *fb; timer_t timer; time_t t; EXPECT_ZERO(utility_ctx_init(argv[0])); /* for g_fast_log_mgr */ t = mt_time() + 600; EXPECT_ZERO(mt_set_alarm(t, "ostor_unit timed out", &timer)); fb = fast_log_create(g_fast_log_mgr, "main"); EXPECT_NOT_ERRPTR(fb); EXPECT_ZERO(get_tempdir(tdir, sizeof(tdir), 0755)); EXPECT_ZERO(register_tempdir_for_cleanup(tdir)); EXPECT_ZERO(ostoru_test_open_close(tdir)); EXPECT_ZERO(get_tempdir(tdir, sizeof(tdir), 0755)); EXPECT_ZERO(register_tempdir_for_cleanup(tdir)); EXPECT_ZERO(ostoru_simple_test(tdir, fb, 100)); EXPECT_ZERO(get_tempdir(tdir, sizeof(tdir), 0755)); EXPECT_ZERO(register_tempdir_for_cleanup(tdir)); EXPECT_ZERO(ostoru_simple_test(tdir, fb, 1)); EXPECT_ZERO(get_tempdir(tdir, sizeof(tdir), 0755)); EXPECT_ZERO(register_tempdir_for_cleanup(tdir)); EXPECT_ZERO(ostoru_threaded_test(tdir, 10)); EXPECT_ZERO(mt_deactivate_alarm(timer)); fast_log_free(fb); process_ctx_shutdown(); return EXIT_SUCCESS; }
int __archive_mktemp(const char *tmpdir) { static const char num[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' }; struct archive_string temp_name; struct stat st; int fd; char *tp, *ep; fd = -1; archive_string_init(&temp_name); if (tmpdir == NULL) { if (get_tempdir(&temp_name) != ARCHIVE_OK) goto exit_tmpfile; } else archive_strcpy(&temp_name, tmpdir); if (temp_name.s[temp_name.length-1] == '/') { temp_name.s[temp_name.length-1] = '\0'; temp_name.length --; } if (stat(temp_name.s, &st) < 0) goto exit_tmpfile; if (!S_ISDIR(st.st_mode)) { errno = ENOTDIR; goto exit_tmpfile; } archive_strcat(&temp_name, "/libarchive_"); tp = temp_name.s + archive_strlen(&temp_name); archive_strcat(&temp_name, "XXXXXXXXXX"); ep = temp_name.s + archive_strlen(&temp_name); do { char *p; p = tp; archive_random(p, ep - p); while (p < ep) { int d = *((unsigned char *)p) % sizeof(num); *p++ = num[d]; } fd = open(temp_name.s, O_CREAT | O_EXCL | O_RDWR | O_CLOEXEC, 0600); } while (fd < 0 && errno == EEXIST); if (fd < 0) goto exit_tmpfile; __archive_ensure_cloexec_flag(fd); unlink(temp_name.s); exit_tmpfile: archive_string_free(&temp_name); return (fd); }
int __archive_mktemp(const char *tmpdir) { struct archive_string temp_name; int fd = -1; archive_string_init(&temp_name); if (tmpdir == NULL) { if (get_tempdir(&temp_name) != ARCHIVE_OK) goto exit_tmpfile; } else { archive_strcpy(&temp_name, tmpdir); if (temp_name.s[temp_name.length-1] != '/') archive_strappend_char(&temp_name, '/'); } archive_strcat(&temp_name, "libarchive_XXXXXX"); fd = mkstemp(temp_name.s); if (fd < 0) goto exit_tmpfile; __archive_ensure_cloexec_flag(fd); unlink(temp_name.s); exit_tmpfile: archive_string_free(&temp_name); return (fd); }
static char *new_temp_dir() { char *buf, *base = get_tempdir(); #ifdef HAVE_MKDTEMP buf = file_in_dir(NULL, base, "mtpaintXXXXXX", PATHBUF); if (!buf) return (NULL); if (mkdtemp(buf)) { chmod(buf, 0755); return (buf); } #else buf = tempnam(base, "mttmp"); if (!buf) return (NULL); #ifdef WIN32 /* No mkdtemp() in MinGW */ /* tempnam() may use Unix path separator */ reseparate(buf); if (!mkdir(buf)) return (buf); #else if (!mkdir(buf, 0755)) return (buf); #endif #endif free(buf); return (NULL); }
static void stest_parse_argv(int argc, char **argv, const char **test_usage) { int ret, c; while ((c = getopt(argc, argv, "d:fh")) != -1) { switch (c) { case 'd': snprintf(g_test_dir, sizeof(g_test_dir), "%s", optarg); break; case 'f': g_daemonize = 0; break; case 'h': stest_usage(argv[0], test_usage, EXIT_SUCCESS); break; case '?': stest_usage(argv[0], test_usage, EXIT_FAILURE); break; } } if (!g_test_dir[0]) { ret = get_tempdir(g_test_dir, sizeof(g_test_dir), 0775); if (ret) { fprintf(stderr, "get_tempdir failed with error %d!\n", ret); exit(EXIT_FAILURE); } register_tempdir_for_cleanup(g_test_dir); } }
int main(void) { char dir_src[PATH_MAX]; char dir_dst[PATH_MAX]; EXPECT_ZERO(get_tempdir(dir_src, PATH_MAX, 0755)); EXPECT_ZERO(register_tempdir_for_cleanup(dir_src)); EXPECT_ZERO(get_tempdir(dir_dst, PATH_MAX, 0755)); EXPECT_ZERO(register_tempdir_for_cleanup(dir_dst)); /* test rename /src/a -> /dst/m, a as file, m as file */ EXPECT_ZERO(do_touch2(dir_src, "a")); EXPECT_ZERO(do_touch2(dir_dst, "m")); EXPECT_ZERO(do_rename(dir_src, "a", dir_dst, "m")); /* test rename /src/b -> /dst/n, b as file, n as dir */ EXPECT_ZERO(do_touch2(dir_src, "b")); EXPECT_ZERO(do_test_mkdir(dir_dst, "n")); EXPECT_EQ(do_rename(dir_src, "b", dir_dst, "n"), -EISDIR); /* test rename /src/c -> /dst/o, c as dir, o as file */ EXPECT_ZERO(do_test_mkdir(dir_src, "c")); EXPECT_ZERO(do_touch2(dir_dst, "o")); EXPECT_EQ(do_rename(dir_src, "c", dir_dst, "o"), -ENOTDIR); /* test rename /src/d -> /dst/p, d as dir, p as dir */ EXPECT_ZERO(do_test_mkdir(dir_src, "d")); EXPECT_ZERO(do_test_mkdir(dir_dst, "p")); EXPECT_ZERO(do_rename(dir_src, "d", dir_dst, "p")); /* test rename /src/e -> /dst/q, e as file, q as nonexistent */ EXPECT_ZERO(do_touch2(dir_src, "e")); EXPECT_ZERO(do_rename(dir_src, "e", dir_dst, "q")); /* test rename /src/f -> /dst/r, f as dir, r as nonexistent */ EXPECT_ZERO(do_test_mkdir(dir_src, "f")); EXPECT_ZERO(do_rename(dir_src, "f", dir_dst, "r")); return 0; }
int main(POSSIBLY_UNUSED(int argc), char **argv) { char tempdir[PATH_MAX]; srand(time(NULL)); EXPECT_ZERO(get_tempdir(tempdir, sizeof(tempdir), 0770)); EXPECT_ZERO(register_tempdir_for_cleanup(tempdir)); EXPECT_ZERO(test_signal_handler(argv[0], tempdir, SIGSEGV)); EXPECT_ZERO(test_signal_handler(argv[0], tempdir, SIGBUS)); EXPECT_ZERO(test_signal_handler(argv[0], tempdir, SIGILL)); EXPECT_ZERO(test_signal_handler(argv[0], tempdir, SIGFPE)); EXPECT_ZERO(test_signal_handler(argv[0], tempdir, SIGABRT)); EXPECT_ZERO(test_signal_handler(argv[0], tempdir, SIGINT)); return EXIT_SUCCESS; }
int main(void) { char tempdir[PATH_MAX]; char glitch_log_path[PATH_MAX]; struct logc lc; EXPECT_ZERO(get_tempdir(tempdir, PATH_MAX, 0700)); EXPECT_ZERO(register_tempdir_for_cleanup(tempdir)); snprintf(glitch_log_path, sizeof(glitch_log_path), "%s/glitch_log.txt", tempdir); memset(&lc, 0, sizeof(struct logc)); lc.glitch_log_path = glitch_log_path; EXPECT_ZERO(test_stderr_output(tempdir)); configure_glitch_log(&lc); EXPECT_ZERO(test_log_output(lc.glitch_log_path)); return EXIT_SUCCESS; }
int main(POSSIBLY_UNUSED(int argc), char **argv) { char tempdir[PATH_MAX]; srand(time(NULL)); g_fast_log_mgr = fast_log_mgr_init(g_fast_log_dumpers); EXPECT_NOT_EQ(g_fast_log_mgr, NULL); EXPECT_ZERO(get_tempdir(tempdir, sizeof(tempdir), 0770)); EXPECT_ZERO(register_tempdir_for_cleanup(tempdir)); EXPECT_ZERO(test_signal_handler(argv[0], tempdir, SIGSEGV)); EXPECT_ZERO(test_signal_handler(argv[0], tempdir, SIGBUS)); EXPECT_ZERO(test_signal_handler(argv[0], tempdir, SIGILL)); EXPECT_ZERO(test_signal_handler(argv[0], tempdir, SIGFPE)); EXPECT_ZERO(test_signal_handler(argv[0], tempdir, SIGABRT)); EXPECT_ZERO(test_signal_handler(argv[0], tempdir, SIGINT)); fast_log_mgr_release(g_fast_log_mgr); return EXIT_SUCCESS; }
static void open_temp_glitch_log(void) { int ret; char tempdir[PATH_MAX]; ret = get_tempdir(tempdir, PATH_MAX, 0755); if (ret) return; snprintf(g_glitch_log_fname, PATH_MAX, "%s/glitch_log.tmp.txt", tempdir); RETRY_ON_EINTR(g_glitch_log_fd, open(g_glitch_log_fname, O_CREAT | O_RDWR, 0644)); if (g_glitch_log_fd == -1) { g_glitch_log_fname[0] = '\0'; remove_tempdir(tempdir); return; } register_tempdir_for_cleanup(tempdir); }
int main(void) { char *bigbuf, tempdir[PATH_MAX]; int next_id = 0; EXPECT_ZERO(get_tempdir(tempdir, sizeof(tempdir), 0700)); EXPECT_ZERO(register_tempdir_for_cleanup(tempdir)); EXPECT_ZERO(read_then_write_file(tempdir, &next_id, "", 0)); EXPECT_ZERO(read_then_write_file(tempdir, &next_id, "abc", 123)); EXPECT_ZERO(read_then_write_file(tempdir, &next_id, "abracadabra", 5)); EXPECT_ZERO(test_copy_fd_to_fd(tempdir, &next_id, "")); EXPECT_ZERO(test_copy_fd_to_fd(tempdir, &next_id, "foo bar")); bigbuf = malloc(BIGBUF_SZ); memset(bigbuf, 'm', BIGBUF_SZ - 1); bigbuf[BIGBUF_SZ - 1] = '\0'; EXPECT_NOT_EQUAL(bigbuf, NULL); EXPECT_ZERO(test_copy_fd_to_fd(tempdir, &next_id, bigbuf)); free(bigbuf); return EXIT_SUCCESS; }
FILE *make_temp_file(const char *name_orig, const char **name_result) { if (tempsubdir == NULL) tempsubdir = make_tempsubdir(get_tempdir()); assert(obstack_object_size(&file_obst) == 0); obstack_printf(&file_obst, "%s/%s", tempsubdir, name_orig); obstack_1grow(&file_obst, '\0'); char *name = obstack_finish(&file_obst); FILE *out = fopen(name, "w"); if (out == NULL) { position_t const pos = { name, 0, 0, 0 }; errorf(&pos, "could not create temporary file: %s", strerror(errno)); panic("abort"); } ARR_APP1(char*, temp_files, name); *name_result = name; return out; }
/* Save the user configuration. */ unsigned config_save(void) { char *tmpprefix = NULL, *tmppath = NULL; struct config_save_status status; int i; int ret = 0; if (read_only) return 1; asprintf(&tmpprefix, "%s/%s", get_tempdir(), CONF_PATH_NAME); if ((tmppath = new_tempfile(tmpprefix)) == NULL) goto cleanup; status.fp = fopen(tmppath, "w"); if (!status.fp) goto cleanup; memset(status.done, 0, sizeof(status.done)); config_file_walk(config_save_cb, config_save_junk_cb, (void *)&status); /* Set variables that were missing from the configuration file. */ for (i = 0; i < ARRAY_SIZE(confmap); i++) { if (!status.done[i]) config_save_cb(confmap[i].key, NULL, &status); } file_close(status.fp, __FILE_POS__); if (io_file_cp(tmppath, path_conf)) unlink(tmppath); ret = 1; cleanup: mem_free(tmpprefix); mem_free(tmppath); return ret; }