Exemplo n.º 1
0
void integck(void)
{
	pid_t pid;
	int64_t rpt;
	uint64_t z;
	char dir_name[256];

	/* Make our top directory */
	pid = getpid();
	printf("pid is %u\n", (unsigned) pid);
	tests_cat_pid(dir_name, "integck_test_dir_", pid);
	if (chdir(dir_name) != -1) {
		/* Remove it if it is already there */
		tests_clear_dir(".");
		CHECK(chdir("..") != -1);
		CHECK(rmdir(dir_name) != -1);
	}
	initial_free_space = tests_get_free_space();
	log10_initial_free_space = 0;
	for (z = initial_free_space; z >= 10; z /= 10)
		++log10_initial_free_space;
	top_dir = dir_new(NULL, dir_name);

	if (!top_dir)
		return;

	srand(pid);

	create_test_data();

	if (!tests_fs_is_rootfs()) {
		close_open_files();
		tests_remount(); /* Requires root access */
	}

	/* Check everything */
	dir_check(top_dir);
	check_deleted_files();

	for (rpt = 0; tests_repeat_parameter == 0 ||
				rpt < tests_repeat_parameter; ++rpt) {
		update_test_data();

		if (!tests_fs_is_rootfs()) {
			close_open_files();
			tests_remount(); /* Requires root access */
		}

		/* Check everything */
		dir_check(top_dir);
		check_deleted_files();
	}

	/* Tidy up by removing everything */
	close_open_files();
	tests_clear_dir(dir_name);
	CHECK(rmdir(dir_name) != -1);
}
/* Make a directory empty */
void tests_clear_dir(const char *dir_name)
{
	DIR *dir;
	struct dirent *entry;
	char buf[4096];

	dir = opendir(dir_name);
	CHECK(dir != NULL);
	CHECK(getcwd(buf, 4096) != NULL);
	CHECK(chdir(dir_name) != -1);
	for (;;) {
		errno = 0;
		entry = readdir(dir);
		if (entry) {
			if (strcmp(".",entry->d_name) != 0 &&
					strcmp("..",entry->d_name) != 0) {
				if (entry->d_type == DT_DIR) {
					tests_clear_dir(entry->d_name);
					CHECK(rmdir(entry->d_name) != -1);
				} else
					CHECK(unlink(entry->d_name) != -1);
			}
		} else {
			CHECK(errno == 0);
			break;
		}
	}
	CHECK(chdir(buf) != -1);
	CHECK(closedir(dir) != -1);
}
/* Remove a random file of empty sub-directory from the current directory */
int64_t tests_remove_entry(void)
{
	DIR *dir;
	struct dirent *entry;
	unsigned count = 0, pos;
	int64_t result = 0;

	dir = opendir(".");
	CHECK(dir != NULL);
	for (;;) {
		errno = 0;
		entry = readdir(dir);
		if (entry) {
			if (strcmp(".",entry->d_name) != 0 &&
					strcmp("..",entry->d_name) != 0)
				++count;
		} else {
			CHECK(errno == 0);
			break;
		}
	}
	pos = tests_random_no(count);
	count = 0;
	rewinddir(dir);
	for (;;) {
		errno = 0;
		entry = readdir(dir);
		if (!entry) {
			CHECK(errno == 0);
			break;
		}
		if (strcmp(".",entry->d_name) != 0 &&
				strcmp("..",entry->d_name) != 0) {
			if (count == pos) {
				if (entry->d_type == DT_DIR) {
					tests_clear_dir(entry->d_name);
					CHECK(rmdir(entry->d_name) != -1);
					result = TESTS_EMPTY_DIR_SIZE;
				} else {
					struct stat st;
					CHECK(stat(entry->d_name, &st) != -1);
					result = st.st_size;
					CHECK(unlink(entry->d_name) != -1);
				}
			}
			++count;
		}
	}
	CHECK(closedir(dir) != -1);
	return result;
}
Exemplo n.º 4
0
static void rndrm99(void)
{
	int64_t repeat, loop_cnt;
	int64_t size, this_size;
	pid_t pid;
	char dir_name[256];

	size_ptr = &size;
	/* Create a directory to test in */
	pid = getpid();
	tests_cat_pid(dir_name, "rndrm99_test_dir_", pid);
	if (chdir(dir_name) == -1)
		CHECK(mkdir(dir_name, 0777) != -1);
	CHECK(chdir(dir_name) != -1);
	/* Repeat loop */
	repeat = tests_repeat_parameter;
	size = 0;
	for (;;) {
		/* Create and remove sub-dirs and small files, */
		/* but tending to grow */
		printf("\nrndrm99: growing\n");fflush(stdout);
		loop_cnt = 0;
		do {
			if (loop_cnt++ % 2000 == 0)
				display_stats();
			if (tests_random_no(3)) {
				this_size = create_entry(NULL);
				if (!this_size)
					break;
				size += this_size;
			} else {
				this_size = remove_entry();
				size -= this_size;
				if (size < 0)
					size = 0;
				if (!this_size)
					this_size = 1;
			}
		} while (this_size &&
			(tests_size_parameter == 0 ||
			size < tests_size_parameter));
		/* Create and remove sub-dirs and small files, but */
		/* but tending to shrink */
		printf("\nrndrm99: shrinking\n");fflush(stdout);
		loop_cnt = 0;
		do {
			if (loop_cnt++ % 2000 == 0)
				display_stats();
			if (!tests_random_no(3)) {
				this_size = create_entry(NULL);
				size += this_size;
			} else {
				this_size = remove_entry();
				size -= this_size;
				if (size < 0)
					size = 0;
			}
		} while ((tests_size_parameter != 0 &&
			size > tests_size_parameter / 10) ||
			(tests_size_parameter == 0 && size > 100000));
		/* Break if repeat count exceeded */
		if (tests_repeat_parameter > 0 && --repeat <= 0)
			break;
		/* 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);
			printf("\nrndrm99: sleeping\n");fflush(stdout);
			usleep(s);
		}
	}
	printf("\nrndrm99: tidying\n");fflush(stdout);
	display_stats();
	/* Tidy up by removing everything */
	tests_clear_dir(".");
	CHECK(chdir("..") != -1);
	CHECK(rmdir(dir_name) != -1);
	size_ptr = 0;
}
Exemplo n.º 5
0
/* Remove a random file of empty sub-directory from the current directory */
static int64_t remove_entry(void)
{
	DIR *dir;
	struct dirent *entry;
	unsigned count = 0, pos;
	int64_t result = 0;

	before();
	dir = opendir(".");
	CHECK(dir != NULL);
	after("opendir");
	for (;;) {
		errno = 0;
		before();
		entry = readdir(dir);
		if (entry) {
			after("readdir 1");
			if (strcmp(".",entry->d_name) != 0 &&
					strcmp("..",entry->d_name) != 0)
				++count;
		} else {
			CHECK(errno == 0);
			after("readdir 1");
			break;
		}
	}
	pos = tests_random_no(count);
	count = 0;
	before();
	rewinddir(dir);
	after("rewinddir");
	for (;;) {
		errno = 0;
		before();
		entry = readdir(dir);
		if (!entry) {
			CHECK(errno == 0);
			after("readdir 2");
			break;
		}
		after("readdir 2");
		if (strcmp(".",entry->d_name) != 0 &&
				strcmp("..",entry->d_name) != 0) {
			if (count == pos) {
				if (entry->d_type == DT_DIR) {
					before();
					tests_clear_dir(entry->d_name);
					after("tests_clear_dir");
					before();
					CHECK(rmdir(entry->d_name) != -1);
					after("rmdir");
					result = TESTS_EMPTY_DIR_SIZE;
					dirs_removed += 1;
				} else {
					struct stat st;
					before();
					CHECK(stat(entry->d_name, &st) != -1);
					after("stat");
					result = st.st_size;
					before();
					CHECK(unlink(entry->d_name) != -1);
					after("unlink");
					files_removed += 1;
				}
			}
			++count;
		}
	}
	before();
	CHECK(closedir(dir) != -1);
	after("closedir");
	return result;
}