1 rizwank 1.1 /*--------------------------------------------------------------------------
2 Trivial virtual file system for cabinet conformance test.
|
3 rizwank 1.2 (C) 2005 Dan Kegel and Rizwan Kassim
4 LGPL License
|
5 rizwank 1.1 --------------------------------------------------------------------------*/
6
7 #include "tvfs.h"
8 #include <string.h>
9
|
10 rizwank 1.9 #define MAXFILES 20
11 #define MAXHANDLES 20
|
12 rizwank 1.1 #define MAXFNAME 255
|
13 rizwank 1.9 #define MAXFLEN 65536*2
|
14 rizwank 1.1
|
15 rizwank 1.4 #include <fcntl.h>
|
16 rizwank 1.7 #include <stdlib.h>
|
17 rizwank 1.4 #include <stdio.h>
18 #include <malloc.h>
|
19 rizwank 1.8 #include <stdio.h>
20 #include <stdarg.h>
21
|
22 rizwank 1.4 #define SEEK_SET 0
23 #define SEEK_CUR 1
24 #define SEEK_END 2
|
25 rizwank 1.2
|
26 rizwank 1.8 #ifndef _O_BINARY
27 #define _O_BINARY 0
28 #endif
29
|
30 rizwank 1.12 /*
31 #define TVFS_MAIN
32 */
33
|
34 rizwank 1.8 #ifndef STANDALONE
|
35 rizwank 1.11 #include "wine/test.h"
36 #define ok2 ok
|
37 rizwank 1.8 #else
|
38 rizwank 1.12 #include "standalone.h"
39 #undef START_TEST
|
40 rizwank 1.8 #endif
41
|
42 rizwank 1.1 struct tvfs_file {
|
43 rizwank 1.8 char fname[MAXFNAME];
44 int bytes_used;
45 char buf[MAXFLEN];
|
46 rizwank 1.1 };
47 static struct tvfs_file *files[MAXFILES];
48
49 struct tvfs_fcb {
|
50 rizwank 1.8 int pos;
51 int inode;
|
52 rizwank 1.1 };
53 static struct tvfs_fcb *handles[MAXHANDLES];
|
54 rizwank 1.8
|
55 rizwank 1.1 int nhandles = 0;
|
56 rizwank 1.8 int nfiles = 0;
|
57 rizwank 1.1
|
58 rizwank 1.2 /* tvfs_create does NOT correspond to _creat - it is an internal function
|
59 rizwank 1.10 use to put a file directly into our virtual file system. */
|
60 rizwank 1.2
|
61 rizwank 1.1 int tvfs_create(const char *fname, const char *buf, int len)
62 {
|
63 rizwank 1.8 int inode;
64 struct tvfs_file *f;
65
66 trace("tvfs_create called with %s, %d, %d\n", fname, buf, len);
67
68 if (nfiles >= MAXFILES)
69 return -1;
70 inode = nfiles++;
|
71 rizwank 1.4
|
72 rizwank 1.8 f = malloc(sizeof(struct tvfs_file));
73 strcpy(f->fname, fname);
74 f->bytes_used = len;
75
76 if (buf)
77 memcpy(f->buf, buf, len);
78
79 files[inode] = f;
80 return inode;
|
81 rizwank 1.1 }
82
83 int tvfs_open(const char *fname, int flags, int mode)
84 {
|
85 rizwank 1.4
|
86 rizwank 1.8 /* mode and flags are not handled */
87 int h;
88 int inode;
89 struct tvfs_fcb *handler;
90
91 trace("tvfs_open called with %s, %d, %d\n", fname, flags, mode);
92
93 /* Existing file? */
94 for (inode=0; inode<nfiles; inode++) {
95 if (!files[inode])
96 continue;
97 if (!strcmp(files[inode]->fname, fname))
98 break;
99 }
100
101 if (inode == nfiles) {
102 /* File did not exist */
103 if ((flags & O_CREAT) == 0) {
104 /* ENOENT */
105 trace("tvfs_open returning -1\n");
106 return -1;
107 rizwank 1.8 }
108
109 inode = tvfs_create(fname, 0, 0);
110 }
|
111 rizwank 1.9
|
112 rizwank 1.8 handler = malloc(sizeof(struct tvfs_fcb));
113 handler->inode = inode;
114 handler->pos=0;
|
115 rizwank 1.9 h = nhandles++;
|
116 rizwank 1.8 handles[h] = handler;
117
118 trace("tvfs_open returning with %d\n", h);
119
120 return h;
|
121 rizwank 1.1 }
|
122 rizwank 1.2
|
123 rizwank 1.1 unsigned int tvfs_read(int h, void *buf, unsigned int len)
124 {
|
125 rizwank 1.8 int inode = handles[h]->inode;
126 int pos = handles[h]->pos;
127 int size = files[inode]->bytes_used;
128
129 trace("tvfs_read called with %d, %d, %d\n", h, buf, len);
130
131 /* Edge Case 1 : Request beyond boundary of file */
132 if (pos + len > size) {
133 len = size-pos;
134 }
135
136 memcpy(buf, files[inode]->buf+pos, len);
137 handles[h]->pos += len;
|
138 rizwank 1.1
|
139 rizwank 1.8 return len;
|
140 rizwank 1.1 }
|
141 rizwank 1.2
|
142 rizwank 1.8 void tvfs_free()
143 {
144 int inode;
|
145 rizwank 1.9 int handle;
|
146 rizwank 1.4
|
147 rizwank 1.8 trace("tvfs_free\n");
148
149 nfiles=0;
150 nhandles=0;
|
151 rizwank 1.9
|
152 rizwank 1.4 for (inode=0; inode<nfiles; inode++) {
|
153 rizwank 1.8 if (!files[inode])
154 continue;
155 free(files[inode]);
156 }
|
157 rizwank 1.9
158 for (handle=0; handle<nhandles; handle++) {
159 if (!handles[handle])
160 continue;
161 free(handles[handle]);
162 }
163
|
164 rizwank 1.8 }
|
165 rizwank 1.2
|
166 rizwank 1.4 /* Compare given file with given contents, return 0 on equal, else nonzero */
|
167 rizwank 1.8 int tvfs_compare(const char *fname, const char *buf, int len)
168 {
169
170 int inode;
171
172 trace("tvfs_compare called with %s, %d, %d\n", fname, buf, len);
173
174 for (inode=0; inode<nfiles; inode++) {
175 if (!files[inode])
176 continue;
177 if (!strcmp(files[inode]->fname, fname))
178 break;
179 }
|
180 rizwank 1.9
|
181 rizwank 1.7 if (inode == nfiles) {
|
182 rizwank 1.8 /* File did not exist */
183 trace("tvfs_compare returning -1 (FAILURE)\n");
|
184 rizwank 1.7 return -1;
185 }
186
|
187 rizwank 1.8 return (memcmp(files[inode]->buf,buf,len));
|
188 rizwank 1.10 /* does not check for out of bound */
|
189 rizwank 1.4 }
|
190 rizwank 1.2
|
191 rizwank 1.8 long tvfs_lseek(int h, long whence, int whither )
192 {
|
193 rizwank 1.2
|
194 rizwank 1.8 int inode = handles[h]->inode;
195 int size = files[inode]->bytes_used;
196 trace("tvfs_lseek called with %d, %d, %d\n", h, whither, whence);
197
198 /* if (whence > size)
199 whence = size;*/
|
200 rizwank 1.10 /* Legit lseek does NOT do boundary checking */
|
201 rizwank 1.8
202 switch(whither) {
|
203 rizwank 1.12 case SEEK_SET: {
204 handles[h]->pos = whence;
205 break;
206 }
207 case SEEK_CUR: {
208 handles[h]->pos += whence;
209 break;
210 }
211 case SEEK_END: {
212 handles[h]->pos = size+whence;
213 break;
214 }
215 case 5: {
216 handles[h]->pos = size+whence;
217 break;
218 }
219 default:
220 {
221 trace("lseek was called with an invalid whither %d\n",whither);
222 return -1;
223 }
|
224 rizwank 1.8 }
225 return handles[h]->pos;
|
226 rizwank 1.4 }
227
|
228 rizwank 1.12 int tvfs_close(int h)
229 {
|
230 rizwank 1.8 int inode = handles[h]->inode;
231 trace("tvfs_close called with %d\n", h);
|
232 rizwank 1.12 if (!files[inode]) {
233 return -1;
|
234 rizwank 1.8 }
235 free(handles[h]);
236 /* Currently does NOT enabled open to reuse this handle */
237 return 0;
|
238 rizwank 1.12 }
|
239 rizwank 1.4
240 unsigned int tvfs_write(int h, void *buf, unsigned int len)
241 {
|
242 rizwank 1.8
243 int inode = handles[h]->inode;
244 int pos = handles[h]->pos;
245 trace("tvfs_write called with %d, %d, %d\n", h, buf, len);
246 memcpy(files[inode]->buf+pos, buf, len);
247 files[inode]->bytes_used += len;
|
248 rizwank 1.9 handles[h]->pos += len;
249
|
250 rizwank 1.8 return len;
251 /* return -1 to simulate diskfull or some other error */
|
252 rizwank 1.4 }
|
253 rizwank 1.8
254
|
255 rizwank 1.11 START_TEST(tvfs){
256 printf("dummy!");
257 }
|
258 rizwank 1.4
|
259 rizwank 1.2
260 #ifdef TVFS_MAIN
|
261 rizwank 1.4
|
262 rizwank 1.3 const static char name_test_txt[] = "test.txt";
263 const static char file_test_txt[] = "This is a test. Don't Panic!";
|
264 rizwank 1.4 const static int size_test_txt = sizeof(file_test_txt);
|
265 rizwank 1.2
|
266 rizwank 1.13 /* If TVFSMain is defined, this compiles a test of the TVFS suite */
|
267 rizwank 1.12 main()
268 {
|
269 rizwank 1.8 int result;
270 int active_handler;
|
271 rizwank 1.2
|
272 rizwank 1.8 char *filebuf;
273
274 char dummy_filename[] = "dummy.txt";
275 char bad_filename[] = "chicken.txt";
276
277 filebuf = malloc(MAXFLEN);
278
|
279 rizwank 1.10 trace("Testing TVFS implementation, creating %s\n",name_test_txt);
|
280 rizwank 1.8 result = tvfs_create( dummy_filename, file_test_txt, size_test_txt);
281 result = tvfs_create( name_test_txt, file_test_txt,size_test_txt);
|
282 rizwank 1.10 trace("Created virtual file with inode %d\n",result);
|
283 rizwank 1.8
|
284 rizwank 1.10 trace("Attempting to open non-existent file\n");
|
285 rizwank 1.8 result = tvfs_open(bad_filename, _O_BINARY, 0 );
|
286 rizwank 1.10 trace("Result code %d\n",result);
287 trace("Attempting to open existent file\n");
|
288 rizwank 1.8 result = tvfs_open(name_test_txt, _O_BINARY, 0 );
|
289 rizwank 1.10 trace("Result code %d\n",result);
|
290 rizwank 1.8
291 active_handler = result;
292
293 memset (filebuf,0,MAXFLEN);
294
|
295 rizwank 1.10 trace("Testing reading from file %s\n",name_test_txt);
|
296 rizwank 1.8 result = tvfs_read(active_handler, filebuf, 9);
|
297 rizwank 1.10 trace("Read _%s_\n", filebuf);
|
298 rizwank 1.8 if ( strcmp(filebuf,"This is a"))
|
299 rizwank 1.10 trace("File read check failed!\n");
|
300 rizwank 1.8
|
301 rizwank 1.10 trace("Testing sequential reading from file %s\n",name_test_txt);
|
302 rizwank 1.8 result = tvfs_read(active_handler, filebuf, 12);
|
303 rizwank 1.10 trace("Read _%s_\n", filebuf);
|
304 rizwank 1.8 if ( strcmp(filebuf," test. Don't"))
|
305 rizwank 1.10 trace("File read check failed!\n");
|
306 rizwank 1.8
|
307 rizwank 1.10 trace("Testing edge reading from file %s\n",name_test_txt);
|
308 rizwank 1.8 result = tvfs_read(active_handler, filebuf, 42);
|
309 rizwank 1.10 trace("Read %d bytes - _%s_\n", result, filebuf);
|
310 rizwank 1.8 if ( result != 8 )
|
311 rizwank 1.10 trace("File read check failed!\n");
|
312 rizwank 1.8
|
313 rizwank 1.10 trace("Testing direct file compare and lseek of %s\n", name_test_txt);
314 trace("Seeking to 0 (not needed) - ");
|
315 rizwank 1.8 tvfs_lseek( active_handler, SEEK_SET, 0);
|
316 rizwank 1.10 trace("Comparing file\n");
|
317 rizwank 1.8 result = tvfs_compare( name_test_txt , file_test_txt, size_test_txt);
318 if (result)
|
319 rizwank 1.10 trace ("File compare failed!\n");
|
320 rizwank 1.8
321
|
322 rizwank 1.10 trace("Closing %s\n",name_test_txt);
|
323 rizwank 1.8 tvfs_close(active_handler);
324 if (result)
|
325 rizwank 1.10 trace ("File close failed!\n");
|
326 rizwank 1.8
327 tvfs_free();
328 free(filebuf);
329
|
330 rizwank 1.2 }
331
332 #endif
|