void test_1(void) { int fd; pid_t pid; uint64_t i; uint64_t block; uint64_t actual_size; char name[256]; char old[16]; char buf[16]; off_t old_len; char dir_name[256]; /* Create a directory to test in */ pid = getpid(); tests_cat_pid(dir_name, "test_1_test_dir_", pid); if (chdir(dir_name) == -1) CHECK(mkdir(dir_name, 0777) != -1); CHECK(chdir(dir_name) != -1); /* Create a file that fills half the free space on the file system */ tests_create_file("big_file", tests_get_big_file_size(1,2)); CHECK(tests_count_files_in_dir(".") == 1); fd = open("big_file", O_RDWR | tests_maybe_sync_flag()); CHECK(fd != -1); CHECK(read(fd, old, 5) == 5); CHECK(lseek(fd, 0, SEEK_SET) != (off_t) -1); CHECK(write(fd,"start", 5) == 5); CHECK(lseek(fd,0,SEEK_END) != (off_t) -1); CHECK(write(fd, "end", 3) == 3); tests_maybe_sync(fd); /* Delete the file while it is still open */ tests_delete_file("big_file"); CHECK(tests_count_files_in_dir(".") == 0); /* Create files to file up the file system */ for (block = 1000000, i = 1; ; block /= 10) { while (i != 0) { sprintf(name, "fill_up_%llu", i); actual_size = tests_create_file(name, block); if (actual_size != 0) ++i; if (actual_size != block) break; } if (block == 1) break; } /* Check the big file */ CHECK(lseek(fd, 0, SEEK_SET) != (off_t) -1); CHECK(read(fd, buf, 5) == 5); CHECK(strncmp(buf, "start", 5) == 0); CHECK(lseek(fd, -3, SEEK_END) != (off_t) -1); CHECK(read(fd, buf, 3) == 3); CHECK(strncmp(buf, "end", 3) == 0); /* Check the other files and delete them */ i -= 1; CHECK(tests_count_files_in_dir(".") == i); for (; i > 0; --i) { sprintf(name, "fill_up_%llu", i); tests_check_filled_file(name); tests_delete_file(name); } CHECK(tests_count_files_in_dir(".") == 0); /* Check the big file again */ CHECK(lseek(fd, 0, SEEK_SET) != (off_t) -1); CHECK(read(fd, buf, 5) == 5); CHECK(strncmp(buf, "start", 5) == 0); CHECK(lseek(fd, -3, SEEK_END) != (off_t) -1); CHECK(read(fd, buf, 3) == 3); CHECK(strncmp(buf, "end", 3) == 0); CHECK(lseek(fd, 0, SEEK_SET) != (off_t) -1); CHECK(write(fd,old, 5) == 5); old_len = lseek(fd, -3, SEEK_END); CHECK(old_len != (off_t) -1); CHECK(ftruncate(fd,old_len) != -1); tests_check_filled_file_fd(fd); /* Close the big file*/ CHECK(close(fd) != -1); CHECK(tests_count_files_in_dir(".") == 0); CHECK(chdir("..") != -1); CHECK(rmdir(dir_name) != -1); }
static void orph(void) { pid_t pid; unsigned i, j, k, n; int fd, done, full; int64_t repeat; ssize_t sz; char dir_name[256]; int fds[MAX_ORPHANS]; /* Create a directory to test in */ pid = getpid(); tests_cat_pid(dir_name, "orph_test_dir_", pid); if (chdir(dir_name) == -1) CHECK(mkdir(dir_name, 0777) != -1); CHECK(chdir(dir_name) != -1); repeat = tests_repeat_parameter; for (;;) { full = 0; done = 0; n = 0; while (n + 100 < MAX_ORPHANS && !done) { /* Make 100 more orphans */ for (i = 0; i < 100; i++) { fd = tests_create_orphan(n + i); if (fd < 0) { done = 1; if (errno == ENOSPC) full = 1; else if (errno != EMFILE) CHECK(0); errno = 0; break; } fds[n + i] = fd; } if (!full) { /* Write to orphans just created */ k = i; for (i = 0; i < k; i++) { if (tests_write_fragment_file(n + i, fds[n+i], 0, 1000) != 1000) { /* * Out of space, so close * remaining files */ for (j = i; j < k; j++) CHECK(close(fds[n + j]) != -1); done = 1; break; } } } if (!done) CHECK(tests_count_files_in_dir(".") == 0); n += i; } /* Check the data in the files */ for (i = 0; i < n; i++) tests_check_fragment_file_fd(i, fds[i]); if (!full && n) { /* Ensure the file system is full */ n -= 1; do { sz = write(fds[n], fds, 4096); if (sz == -1 && errno == ENOSPC) { errno = 0; break; } CHECK(sz >= 0); } while (sz == 4096); CHECK(close(fds[n]) != -1); } /* Check the data in the files */ for (i = 0; i < n; i++) tests_check_fragment_file_fd(i, fds[i]); /* Sleep */ if (tests_sleep_parameter > 0) { unsigned us = tests_sleep_parameter * 1000; unsigned rand_divisor = RAND_MAX / us; unsigned s = (us / 2) + (rand() / rand_divisor); usleep(s); } /* Close orphans */ for (i = 0; i < n; i++) CHECK(close(fds[i]) != -1); /* Break if repeat count exceeded */ if (tests_repeat_parameter > 0 && --repeat <= 0) break; } CHECK(tests_count_files_in_dir(".") == 0); CHECK(chdir("..") != -1); CHECK(rmdir(dir_name) != -1); }