-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsfs_test.c
169 lines (147 loc) · 5.17 KB
/
sfs_test.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sfs_api.h"
/* The maximum file name length. We assume that filenames can contain
* upper-case letters and periods ('.') characters. Feel free to
* change this if your implementation differs.
*/
#define MAX_FNAME_LENGTH 12 /* Assume at most 12 characters (8.3) */
/* The maximum number of files to attempt to open or create. NOTE: we
* do not _require_ that you support this many files. This is just to
* test the behavior of your code.
*/
#define MAX_FD 100
/* The maximum number of bytes we'll try to write to a file. If you
* support much shorter or larger files for some reason, feel free to
* reduce this value.
*/
#define MAX_BYTES 30000 /* Maximum file size I'll try to create */
#define MIN_BYTES 10000 /* Minimum file size */
/* Just a random test string.
*/
//static char test_str[] = "I am the new tester. I am the replacement for much maligned old tester!\n";
/* rand_name() - return a randomly-generated, but legal, file name.
*
* This function creates a filename of the form xxxxxxxx.xxx, where
* each 'x' is a random upper-case letter (A-Z). Feel free to modify
* this function if your implementation requires shorter filenames, or
* supports longer or different file name conventions.
*
* The return value is a pointer to the new string, which may be
* released by a call to free() when you are done using the string.
*/
char *rand_name()
{
char fname[MAX_FNAME_LENGTH];
int i;
for (i = 0; i < MAX_FNAME_LENGTH; i++) {
if (i != 8) {
fname[i] = 'A' + (rand() % 26);
}
else {
fname[i] = '.';
}
}
fname[i] = '\0';
return (strdup(fname));
}
/* The main testing program
*/
int
main(int argc, char **argv)
{
int i, j, k;
int chunksize;
char *buffer;
// char fixedbuf[1024];
int fds[MAX_FD];
char *names[MAX_FD];
int filesize[MAX_FD];
// int nopen; /* Number of files simultaneously open */
// int ncreate; /* Number of files created in directory */
int error_count = 0;
int tmp;
mksfs(1); /* Initialize the file system. */
/* First we open five files and attempt to write data to them.
*/
for (i = 0; i < 5; i++) {
names[i] = rand_name();
fds[i] = sfs_open(names[i]);
if (fds[i] < 0) {
fprintf(stderr, "ERROR: creating first test file %s\n", names[i]);
error_count++;
}
tmp = sfs_open(names[i]);
if (tmp >= 0 && tmp != fds[i]) {
fprintf(stderr, "ERROR: file %s was opened twice\n", names[i]);
error_count++;
}
filesize[i] = (rand() % (MAX_BYTES-MIN_BYTES)) + MIN_BYTES;
}
for (i = 0; i < 5; i++) {
for (j = i + 1; j < 2; j++) {
if (fds[i] == fds[j]) {
fprintf(stderr, "Warning: the file descriptors probably shouldn't be the same?\n");
}
}
}
printf("Five files created with zero length:\n");
sfs_ls();
printf("\n");
for (i = 0; i < 5; i++) {
for (j = 0; j < filesize[i]; j += chunksize) {
if ((filesize[i] - j) < 10) {
chunksize = filesize[i] - j;
}
else {
chunksize = (rand() % (filesize[i] - j)) + 1;
}
if ((buffer = malloc(chunksize)) == NULL) {
fprintf(stderr, "ABORT: Out of memory!\n");
exit(-1);
}
for (k = 0; k < chunksize; k++) {
buffer[k] = (char) (j+k);
}
sfs_write(fds[i], buffer, chunksize);
free(buffer);
}
}
for (i = 0; i < 5; i++)
sfs_close(fds[i]);
sfs_ls();
for (i = 0; i < 5; i++)
fds[i] = sfs_open(names[i]);
printf("Reopened the files again.. the read/write pointers should be set to front\n");
for (i = 0; i < 5; i++) {
for (j = 0; j < filesize[i]; j += chunksize) {
if ((filesize[i] - j) < 10) {
chunksize = filesize[i] - j;
}
else {
chunksize = (rand() % (filesize[i] - j)) + 1;
}
if ((buffer = malloc(chunksize)) == NULL) {
fprintf(stderr, "ABORT: Out of memory!\n");
exit(-1);
}
sfs_read(fds[i], buffer, chunksize);
for (k = 0; k < chunksize; k++) {
if (buffer[k] != (char)(j+k)) {
fprintf(stderr, "ERROR: data error at offset %d in file %s (%i,%i)\n",
j+k, names[i], buffer[k], (char)(j+k));
error_count++;
break;
}
}
free(buffer);
}
}
fprintf(stderr, "Test program exiting with %d errors\n", error_count);
/**for (i = 0; i < MAX_FILE; i++) {
char * file = FileAllocationTable_getFullFile(root.table[i], fat);
printf("%s\n", file);
}**/
return (error_count);
}