-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfsclient.cpp
More file actions
201 lines (133 loc) · 3.12 KB
/
fsclient.cpp
File metadata and controls
201 lines (133 loc) · 3.12 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
/** @file fsclient.cpp
*
* File System demo client program
*
* @author Copyright Aniesh Chawla, Sept 2006
**/
#include "includes.h"
#include "NDisk.h"
#include "Volume.h"
#include "FS.h"
void demo_dir(FS& fs, Volume& V);
void demo_file(FS& fs, Volume& V);
int main()
{
FS fs;
banner("Trying NDisk");
NDisk ndisk;
if (ndisk.format(50) != DISK_ERROR)
{
Volume C("C", ndisk);
demo_dir(fs, C);
demo_file(fs, C);
}
return 0;
}
void demo_dir(FS& fs, Volume& V)
{
banner("Creating directory foo");
fs.mkdir(V, "/", "foo");
banner("Creating directory bar within foo");
fs.mkdir(V, "/foo", "bar");
banner("Listing /");
fs.ls(V, "/");
banner("Listing /foo");
fs.ls(V, "/foo");
banner("Removing directory bar from foo");
fs.rmdir(V, "/foo", "bar");
banner("Listing /foo");
fs.ls(V, "/foo");
banner("Creating subdirs in /foo");
char buf[16];
for (int i = 0; i < 10; ++i)
{
sprintf(buf, "dir%d", i);
fs.mkdir(V, "/foo", buf);
}
banner("Listing /foo");
fs.ls(V, "/foo");
}
void demo_file(FS& fs, Volume& V)
{
banner("Opening abc");
int fd = fs.open(V, "/", "abc");
if (fd != DISK_ERROR)
{
char buf[256];
banner("Storing text in abc");
fs.write(fd, "Hello World!", 14);
//fs.write(fd, "Hel", 3);
banner("we have completed hello");
fs.seek(fd, 0);
fs.read(fd, buf, 14);
buf[14] = '\0';
printf("text: %s\n", buf);
fs.seek(fd, 0);
fs.write(fd, "Goodbye", 7);
fs.seek(fd, 0);
fs.read(fd, buf, 14);
buf[14] = '\0';
printf("text: %s\n", buf);
banner("Closing abc");
fs.close(fd);
banner("Listing /");
fs.ls(V, "/");
banner("Re-open abc to test write/read/seek");
fd = fs.open(V, "/", "abc");
if (fd != DISK_ERROR)
{
for (int i = 0; i < 1000; ++i)
{
if (fs.write(fd, "abcdefghijklmnopqrstuvwxyz", 26) == DISK_ERROR)
{
error("Error in write()\n");
break;
}
}
if (fs.seek(fd, 0) == DISK_ERROR)
error("Error in seek()\n");
for (int i = 0; i < 1000; ++i)
{
memset(buf, 0, 26);
if (fs.read(fd, buf, 26) != 26)
{
error("Error in read()\n");
break;
}
if (strncmp(buf, "abcdefghijklmnopqrstuvwxyz", 26))
{
error("String error in before success read()\n");
break;
}
}
banner("successfully completed this seeking and comparing");
for (int i = 0; i < 1000; ++i)
{
memset(buf, 0, 26);
if (fs.seek(fd, 26 * (999 - i)) == DISK_ERROR)
{
error("Error in after success seek()\n");
break;
}
if (fs.read(fd, buf, 26) != 26)
{
error("Error in after success read()\n");
break;
}
if (strncmp(buf, "abcdefghijklmnopqrstuvwxyz", 26))
{
error("String error in after success read()\n");
break;
}
}
fs.close(fd);
banner("Listing /");
fs.ls(V, "/");
}
banner("Removing abc");
if (fs.rm(V, "/", "abc") == DISK_ERROR)
error("Error in rm()\n");
banner("Listing /");
fs.ls(V, "/");
}
}