summaryrefslogtreecommitdiffstats
path: root/tests/progs/random_exercise.c
blob: 38217ab59f868d8428c23f9a98fa0139db2dae3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
 * random_exercise.c --- Test program which exercises an ext2
 * 	filesystem.  It creates a lot of random files in the current
 * 	directory, while holding some files open while they are being
 * 	deleted.  This exercises the orphan list code, as well as
 * 	creating lots of fodder for the ext3 journal.
 *
 * Copyright (C) 2000 Theodore Ts'o.
 *
 * %Begin-Header%
 * This file may be redistributed under the terms of the GNU Public
 * License.
 * %End-Header%
 */

#include "config.h"
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#define MAXFDS	128

struct state {
	char	name[16];
	int	state;
	int	isdir;
};

#define STATE_CLEAR	0
#define STATE_CREATED	1
#define STATE_DELETED	2

struct state state_array[MAXFDS];

#define DATA_SIZE 65536

char data_buffer[DATA_SIZE];

void clear_state_array()
{
	int	i;

	for (i = 0; i < MAXFDS; i++)
		state_array[i].state = STATE_CLEAR;
}

int get_random_fd()
{
	int	fd;

	while (1) {
		fd = ((int) random()) % MAXFDS;
		if (fd > 2)
			return fd;
	}
}

unsigned int get_inode_num(int fd)
{
	struct stat st;

	if (fstat(fd, &st) < 0) {
		perror("fstat");
		return 0;
	}
	return st.st_ino;
}


void create_random_file()
{
	char template[16] = "EX.XXXXXX";
	int	fd;
	int	isdir = 0;
	int	size;

	mktemp(template);
	isdir = random() & 1;
	if (isdir) {
		if (mkdir(template, 0700) < 0)
			return;
		fd = open(template, O_RDONLY, 0600);
		printf("Created temp directory %s, fd = %d\n",
		       template, fd);
	} else {
		size = random() & (DATA_SIZE-1);
		fd = open(template, O_CREAT|O_RDWR, 0600);
		write(fd, data_buffer, size);
		printf("Created temp file %s, fd = %d, size=%d\n",
		       template, fd, size);
	}
	state_array[fd].isdir = isdir;
	if (fd < 0)
		return;
	state_array[fd].isdir = isdir;
	state_array[fd].state = STATE_CREATED;
	strcpy(state_array[fd].name, template);
}

void truncate_file(int fd)
{
	int	size;

	size = random() & (DATA_SIZE-1);

	if (state_array[fd].isdir)
		return;

	ftruncate(fd, size);
	printf("Truncating temp file %s, fd = %d, ino=%u, size=%d\n",
	       state_array[fd].name, fd, get_inode_num(fd), size);
}


void unlink_file(int fd)
{
	char *filename = state_array[fd].name;

	printf("Deleting %s, fd = %d, ino = %u\n", filename, fd,
	       get_inode_num(fd));

	if (state_array[fd].isdir)
		rmdir(filename);
	else
		unlink(filename);
	state_array[fd].state = STATE_DELETED;
}

void close_file(int fd)
{
	char *filename = state_array[fd].name;

	printf("Closing %s, fd = %d, ino = %u\n", filename, fd,
	       get_inode_num(fd));

	close(fd);
	state_array[fd].state = STATE_CLEAR;
}


main(int argc, char **argv)
{
	int	i, fd;

	memset(data_buffer, 0, sizeof(data_buffer));
	sprintf(data_buffer, "This is a test file created by the "
		"random_exerciser program\n");

	for (i=0; i < 100000; i++) {
		fd = get_random_fd();
		switch (state_array[fd].state) {
		case STATE_CLEAR:
			create_random_file();
			break;
		case STATE_CREATED:
			if ((state_array[fd].isdir == 0) &&
			    (random() & 2))
				truncate_file(fd);
			else
				unlink_file(fd);
			break;
		case STATE_DELETED:
			close_file(fd);
			break;
		}
	}
}