Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
script3r
GitHub Repository: script3r/os161
Path: blob/master/user/testbin/fileonlytest/fileonlytest.c
734 views
1
/*
2
* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
3
* The President and Fellows of Harvard College.
4
*
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
7
* are met:
8
* 1. Redistributions of source code must retain the above copyright
9
* notice, this list of conditions and the following disclaimer.
10
* 2. Redistributions in binary form must reproduce the above copyright
11
* notice, this list of conditions and the following disclaimer in the
12
* documentation and/or other materials provided with the distribution.
13
* 3. Neither the name of the University nor the names of its contributors
14
* may be used to endorse or promote products derived from this software
15
* without specific prior written permission.
16
*
17
* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
18
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
21
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
* SUCH DAMAGE.
28
*/
29
30
/*
31
* rmtest.c
32
*
33
* Tests file system synchronization by deleting an open file and
34
* then attempting to read it.
35
*
36
* This should run correctly when the file system assignment is complete.
37
*/
38
39
#include <stdlib.h>
40
#include <stdio.h>
41
#include <string.h>
42
#include <unistd.h>
43
#include <err.h>
44
45
// 23 Mar 2012 : GWA : BUFFER_COUNT must be even.
46
47
#define BUFFER_COUNT 128
48
#define BUFFER_SIZE 128
49
50
static int writebuf[BUFFER_SIZE];
51
static int readbuf[BUFFER_SIZE];
52
53
int
54
main(int argc, char **argv)
55
{
56
57
// 23 Mar 2012 : GWA : Assume argument passing is *not* supported.
58
59
(void) argc;
60
(void) argv;
61
int i, j;
62
int fh, len;
63
off_t pos, target;
64
65
const char * filename = "fileonlytest.dat";
66
67
// 23 Mar 2012 : GWA : Test that open works.
68
69
printf("Opening %s\n", filename);
70
71
fh = open(filename, O_RDWR|O_CREAT|O_TRUNC);
72
if (fh < 0) {
73
err(1, "create failed");
74
}
75
76
printf("Writing %d bytes.\n", BUFFER_SIZE * BUFFER_COUNT);
77
78
// 23 Mar 2012 : GWA : Do the even-numbered writes. Test read() and
79
// lseek(SEEK_END).
80
81
for (i = 0; i < BUFFER_COUNT / 2; i++) {
82
for (j = 0; j < BUFFER_SIZE; j++) {
83
writebuf[j] = i * 2 * j;
84
}
85
len = write(fh, writebuf, sizeof(writebuf));
86
if (len != sizeof(writebuf)) {
87
err(1, "write failed");
88
}
89
90
// 23 Mar 2012 : GWA : Use lseek() to skip the odd guys.
91
92
target = (i + 1) * 2 * sizeof(writebuf);
93
pos = lseek(fh, sizeof(writebuf), SEEK_END);
94
if (pos != target) {
95
err(1, "(even) lseek failed: %llu != %llu", pos, target);
96
}
97
}
98
99
target = 0;
100
pos = lseek(fh, target, SEEK_SET);
101
if (pos != target) {
102
err(1, "(reset) lseek failed: %llu != %llu", pos, target);
103
}
104
105
// 23 Mar 2012 : GWA : Do the odd-numbered writes. Test write() and
106
// lseek(SEEK_CUR).
107
108
for (i = 0; i < BUFFER_COUNT / 2; i++) {
109
110
// 23 Mar 2012 : GWA : Use lseek() to skip the even guys.
111
112
target = ((i * 2) + 1) * sizeof(writebuf);
113
pos = lseek(fh, sizeof(writebuf), SEEK_CUR);
114
if (pos != target) {
115
err(1, "(odd) lseek failed: %llu != %llu", pos, target);
116
}
117
118
for (j = 0; j < BUFFER_SIZE; j++) {
119
writebuf[j] = ((i * 2) + 1) * j;
120
}
121
len = write(fh, writebuf, sizeof(writebuf));
122
if (len != sizeof(writebuf)) {
123
err(1, "write failed");
124
}
125
}
126
127
// 23 Mar 2012 : GWA : Read the test data back and make sure what we wrote
128
// ended up where we wrote it. Tests read() and lseek(SEEK_SET).
129
130
printf("Verifying write.\n");
131
132
for (i = BUFFER_COUNT - 1; i >= 0; i--) {
133
target = i * sizeof(writebuf);
134
pos = lseek(fh, target, SEEK_SET);
135
if (pos != target) {
136
err(1, "(verify) lseek failed: %llu != %llu", pos, target);
137
}
138
len = read(fh, readbuf, sizeof(readbuf));
139
if (len != sizeof(readbuf)) {
140
err(1, "read failed");
141
}
142
for (j = BUFFER_SIZE - 1; j >= 0; j--) {
143
if (readbuf[j] != i * j) {
144
err(1, "read mismatch: pos=%llu, readbuf[j]=%d, i*j=%d, i=%d, j=%d", pos, readbuf[j], i * j, i, j);
145
}
146
}
147
}
148
149
// 23 Mar 2012 : GWA : Close the file.
150
151
printf("Closing %s\n", filename);
152
close(fh);
153
154
// 23 Mar 2012 : GWA : Make sure the file is actually closed.
155
156
pos = lseek(fh, (off_t) 0, SEEK_SET);
157
if (pos == 0) {
158
err(1, "seek after close succeeded");
159
}
160
161
// 23 Mar 2012 : GWA : FIXME : Spin until exit() works.
162
163
printf("Spinning in case exit() doesn't work.\n");
164
while (1) {};
165
166
return 0;
167
}
168
169