Theodore Ts'o | da1a022 | 2000-08-20 21:48:45 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * random_exercise.c --- Test program which exercises an ext2 |
| 3 | * filesystem. It creates a lot of random files in the current |
| 4 | * directory, while holding some files open while they are being |
| 5 | * deleted. This exercises the orphan list code, as well as |
| 6 | * creating lots of fodder for the ext3 journal. |
| 7 | * |
| 8 | * Copyright (C) 2000 Theodore Ts'o. |
| 9 | * |
| 10 | * %Begin-Header% |
| 11 | * This file may be redistributed under the terms of the GNU Public |
| 12 | * License. |
| 13 | * %End-Header% |
| 14 | */ |
| 15 | |
| 16 | #include <unistd.h> |
| 17 | #include <stdio.h> |
| 18 | #include <sys/types.h> |
| 19 | #include <sys/stat.h> |
| 20 | #include <fcntl.h> |
| 21 | |
| 22 | #define MAXFDS 128 |
| 23 | |
| 24 | struct state { |
| 25 | char name[16]; |
| 26 | int state; |
| 27 | }; |
| 28 | |
| 29 | #define STATE_CLEAR 0 |
| 30 | #define STATE_CREATED 1 |
| 31 | #define STATE_DELETED 2 |
| 32 | |
| 33 | struct state state_array[MAXFDS]; |
| 34 | |
| 35 | void clear_state_array() |
| 36 | { |
| 37 | int i; |
| 38 | |
| 39 | for (i = 0; i < MAXFDS; i++) |
| 40 | state_array[i].state = STATE_CLEAR; |
| 41 | } |
| 42 | |
| 43 | int get_random_fd() |
| 44 | { |
| 45 | int fd; |
| 46 | |
| 47 | while (1) { |
| 48 | fd = ((int) random()) % MAXFDS; |
| 49 | if (fd > 2) |
| 50 | return fd; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | void create_random_file() |
| 55 | { |
| 56 | char template[16] = "EX.XXXXXX"; |
| 57 | int fd; |
| 58 | |
| 59 | mktemp(template); |
| 60 | fd = open(template, O_CREAT|O_RDWR, 0600); |
| 61 | if (fd < 0) |
| 62 | return; |
| 63 | printf("Created temp file %s, fd = %d\n", template, fd); |
| 64 | state_array[fd].state = STATE_CREATED; |
| 65 | strcpy(state_array[fd].name, template); |
| 66 | } |
| 67 | |
| 68 | void unlink_file(int fd) |
| 69 | { |
| 70 | char *filename = state_array[fd].name; |
| 71 | |
| 72 | printf("Unlinking %s, fd = %d\n", filename, fd); |
| 73 | |
| 74 | unlink(filename); |
| 75 | state_array[fd].state = STATE_DELETED; |
| 76 | } |
| 77 | |
| 78 | void close_file(int fd) |
| 79 | { |
| 80 | char *filename = state_array[fd].name; |
| 81 | |
| 82 | printf("Closing %s, fd = %d\n", filename, fd); |
| 83 | |
| 84 | close(fd); |
| 85 | state_array[fd].state = STATE_CLEAR; |
| 86 | } |
| 87 | |
| 88 | |
| 89 | main(int argc, char **argv) |
| 90 | { |
| 91 | int i, fd; |
| 92 | |
| 93 | |
| 94 | for (i=0; i < 100000; i++) { |
| 95 | fd = get_random_fd(); |
| 96 | switch (state_array[fd].state) { |
| 97 | case STATE_CLEAR: |
| 98 | create_random_file(); |
| 99 | break; |
| 100 | case STATE_CREATED: |
| 101 | unlink_file(fd); |
| 102 | break; |
| 103 | case STATE_DELETED: |
| 104 | close_file(fd); |
| 105 | break; |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | |