1 rizwank 1.1 /*--------------------------------------------------------------------------
2 Trivial virtual file system for cabinet conformance test.
3 --------------------------------------------------------------------------*/
4
5 #include "tvfs.h"
6 #include <string.h>
7
8 /* This is extremely cheezy. Everything is preallocated per file. */
9
10 #define MAXFILES 10
11 #define MAXHANDLES 10
12 #define MAXFNAME 255
13 #define MAXFLEN 65536
14
15 struct tvfs_file {
16 char fname[MAXFNAME];
17 int bytes_used;
18 char buf[MAXFLEN];
19 };
20 static struct tvfs_file *files[MAXFILES];
21 int nfiles = 0;
22 rizwank 1.1
23 struct tvfs_fcb {
24 int pos;
25 int inode;
26 };
27 static struct tvfs_fcb *handles[MAXHANDLES];
28 int nhandles = 0;
29
30 int tvfs_create(const char *fname, const char *buf, int len)
31 {
32 int inode;
33 struct tvfs_file *f;
34 if (nfiles >= MAXFILES)
35 return -1;
36
37 inode = nfiles++;
38 f = calloc(sizeof(struct tvfs_file));
39 strcpy(f->fname, fname);
40 f->len = len;
41 if (buf)
42 memcpy(f->buf, buf, len);
43 rizwank 1.1 files[inode] = f;
44 return inode;
45 }
46
47 int tvfs_open(const char *fname, int flags, int mode)
48 {
49 int h;
50 int inode;
51 /* Existing file? */
52 for (inode=0; inode<nfiles; inode++) {
53 if (!files[inode])
54 continue;
55 if (strcmp(files[inode].fname, fname))
56 break;
57 }
58 if (inode == nfiles) {
59 /* File did not exist */
60 if ((flags & O_CREAT) == 0) {
61 /* ENOENT */
62 return -1;
63 }
64 rizwank 1.1 inode = tvfs_create(fname, 0, 0);
65 }
66 h = nhandles++;
67 handles[h].inode = inode;
68 handles[h].pos = 0;
69 return h;
70 }
71
72 unsigned int tvfs_read(int h, void *buf, unsigned int len)
73 {
74 int inode = handles[h].inode;
75 int pos = handles[h].pos;
76
77 /* FIXME: handle edge cases */
78 memcpy(buf, files[inode].buf, len);
79 handles[h].pos += len;
80
81 return len;
82 }
|