fillcard.c
5.89 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
int maxcid;
static void
do_fill_card(BBCHandle h)
{
int rv;
int cid;
int errs = 0;
char fname[32];
char bbname[32];
time_t curtime;
int len, rlen;
int cardsize, freeblks, reservedblks, badblks;
/*
* start with a random length file full of zeros
*/
len = rand() % 64;
if (len == 0) len++;
rlen = len * BLK_SIZE;
strcpy(fname, "zerofile");
mkzerofile(fname, rlen);
/*
* set RTC to test fix (only meaningful in direct mode talking to BB)
*/
curtime = time(NULL);
if ((rv = BBCSetTime(h, curtime)) < 0) {
printf("%s: BBCSetTime returns %d\n", pname, rv);
return;
}
cid = 500;
if ((rv = storecont(h, fname, cid)) < 0) {
printf("%s: storecont %s fails: %d\n", pname, fname, rv);
return;
}
len = 0;
for (cid = 0; ; cid++) {
rv = BBCStats(h, &cardsize, &reservedblks, &freeblks, &badblks);
printf("%s: BBCStats returns %d, cardsize %d, freeblks %d\n", pname, rv, cardsize, freeblks);
if (rv < 0) return;
if (freeblks == 0) {
printf("%s: card is full ... check files\n", pname);
break;
}
len += BLK_SIZE*32;
if (len > cardsize*1024/8)
len = BLK_SIZE*32;
//rlen = (cid & 1) ? len : len*4;
rlen = len*32;
sprintf(fname, "testfile.%d", cid);
printf("%s: create content file %s, len %d\n", pname, fname, rlen);
mkfile(fname, rlen);
if ((rv = storecont(h, fname, cid)) < 0) {
printf("%s: storecont %s fails: %d\n", pname, fname, rv);
if (rv == BBC_NOSPACE || rv == BBC_BADHANDLE || rv == BBC_ERROR)
break;
return;
}
// Check file is written correctly
sprintf(fname, "testfile.%d", cid);
sprintf(bbname, "%08x.app", cid);
printf("comparing %s and %s\n", fname, bbname);
rv = cmpfile(h, fname, bbname);
if (rv != 0) {
errs++;
printf("%s: miscompare files %s, %s\n", pname, fname, bbname);
}
}
maxcid = cid;
/*
* Now delete all the ones with odd CIDs to create holes
*/
printf("%s: delete half the content files\n", pname);
for (cid = 1; cid < maxcid; cid += 2) {
if ((rv = BBCRemoveContent(h, cid)) < 0) {
printf("%s: remove content id %x fails: %d\n", pname, cid, rv);
return;
}
}
printf("%s: check remaining content files\n", pname);
for (cid = 0; cid < maxcid; cid += 2) {
sprintf(fname, "testfile.%d", cid);
sprintf(bbname, "%08x.app", cid);
printf("comparing %s and %s\n", fname, bbname);
rv = cmpfile(h, fname, bbname);
if (rv != 0) {
errs++;
printf("%s: miscompare files %s, %s\n", pname, fname, bbname);
}
}
sprintf(bbname, "%08x.app", 500);
strcpy(fname, "zerofile");
rv = cmpfile(h, fname, bbname);
if (rv != 0) {
errs++;
printf("%s: miscompare files %s, %s\n", pname, fname, bbname);
}
if (errs > 0) {
printf("%s: total %d miscompares, exiting\n", pname, errs);
return;
}
/*
* Now try to fill up the remaining space with one file
*/
rv = BBCStats(h, &cardsize, &reservedblks, &freeblks, &badblks);
printf("%s: BBCStats returns %d, cardsize %d, freeblks %d\n", pname, rv, cardsize, freeblks);
if (rv < 0) return;
/*
* First try one that is one block too big to fit
*/
len = BB_RND_BLKS(freeblks*1024+1);
sprintf(fname, "testfile.%d", maxcid);
printf("%s: create content file %s, len %d\n", pname, fname, len);
mkfile(fname, len);
if ((rv = storecont(h, fname, maxcid)) < 0) {
printf("%s: storecont %s fails (EXPECTED): %d\n", pname, fname, rv);
} else {
printf("%s: storecont %s succeeds (should have failed): %d\n", pname, fname, rv);
return;
}
/* Now try the exact size */
len = BB_RND_BLKS(freeblks*1024);
printf("%s: create content file %s, len %d\n", pname, fname, len);
mkfile(fname, len);
if ((rv = storecont(h, fname, maxcid)) < 0) {
printf("%s: storecont %s fails: %d\n", pname, fname, rv);
return;
}
rv = BBCStats(h, &cardsize, &reservedblks, &freeblks, &badblks);
printf("%s: BBCStats returns %d, cardsize %d, freeblks %d\n", pname, rv, cardsize, freeblks);
if (rv < 0) return;
printf("%s: card is full, check all remaining content files\n", pname);
for (cid = 0; cid < maxcid; cid += 2) {
sprintf(fname, "testfile.%d", cid);
sprintf(bbname, "%08x.app", cid);
printf("comparing %s and %s\n", fname, bbname);
rv = cmpfile(h, fname, bbname);
if (rv != 0) {
errs++;
printf("%s: miscompare files %s, %s\n", pname, fname, bbname);
}
}
if (errs > 0) {
printf("%s: total %d miscompares, exiting\n", pname, errs);
return;
}
sprintf(bbname, "%08x.app", 500);
strcpy(fname, "zerofile");
printf("%s: check zero file %s\n", pname, fname);
rv = cmpfile(h, fname, bbname);
if (rv != 0) {
errs++;
printf("%s: miscompare files %s, %s\n", pname, fname, bbname);
}
sprintf(fname, "testfile.%d", maxcid);
sprintf(bbname, "%08x.app", maxcid);
printf("%s: check last big file %s\n", pname, fname);
rv = cmpfile(h, fname, bbname);
if (rv != 0) {
errs++;
printf("%s: miscompare files %s, %s\n", pname, fname, bbname);
}
if (errs > 0) {
printf("%s: total %d miscompares, exiting\n", pname, errs);
return;
}
return;
}
static void
do_check_files(BBCHandle h)
{
int cid;
int rv;
char fname[32];
char bbname[32];
int errs = 0;
printf("%s: check all content files\n", pname);
for (cid = 0; cid < maxcid; cid += 2) {
sprintf(fname, "testfile.%d", cid);
sprintf(bbname, "%08x.app", cid);
printf("comparing %s and %s\n", fname, bbname);
rv = cmpfile(h, fname, bbname);
if (rv != 0) {
errs++;
printf("%s: miscompare files %s, %s\n", pname, fname, bbname);
}
}
sprintf(bbname, "%08x.app", 500);
strcpy(fname, "zerofile");
printf("%s: check zero file %s\n", pname, fname);
rv = cmpfile(h, fname, bbname);
if (rv != 0) {
errs++;
printf("%s: miscompare files %s, %s\n", pname, fname, bbname);
}
sprintf(fname, "testfile.%d", maxcid);
sprintf(bbname, "%08x.app", maxcid);
printf("%s: check last big file %s\n", pname, fname);
rv = cmpfile(h, fname, bbname);
if (rv != 0) {
errs++;
printf("%s: miscompare files %s, %s\n", pname, fname, bbname);
}
if (errs > 0) {
printf("%s: total %d miscompares, exiting\n", pname, errs);
return;
}
}