Fix up reads.
[akaros.git] / kern / src / ns / convM2D.c
1 // INFERNO
2 #include <vfs.h>
3 #include <kfs.h>
4 #include <slab.h>
5 #include <kmalloc.h>
6 #include <kref.h>
7 #include <string.h>
8 #include <stdio.h>
9 #include <assert.h>
10 #include <error.h>
11 #include <cpio.h>
12 #include <pmap.h>
13 #include <smp.h>
14 #include <ip.h>
15
16 int statcheck(uint8_t * buf, unsigned int nbuf)
17 {
18         uint8_t *ebuf;
19         int i;
20
21         ebuf = buf + nbuf;
22
23         if (nbuf < STATFIXLEN || nbuf != BIT16SZ + GBIT16(buf)){
24                 printk("nbuf %d, STATFIXLEN %d ", nbuf, STATFIXLEN);
25                 printk("BIT16SZ %d, GBIT16(buf) %d ", 
26                         BIT16SZ, GBIT16(buf));
27                 printk("This is bad!\n");
28                 return -1;
29         }
30
31         buf += STATFIXLEN - 4 * BIT16SZ;
32
33         for (i = 0; i < 4; i++) {
34                 if (buf + BIT16SZ > ebuf)
35                         return -1;
36                 buf += BIT16SZ + GBIT16(buf);
37         }
38
39         if (buf != ebuf){
40                 return -1;
41         }
42
43         return 0;
44 }
45
46 static char nullstring[] = "";
47
48 unsigned int
49 convM2D(uint8_t * buf, unsigned int nbuf, struct dir *d, char *strs)
50 {
51         uint8_t *p, *ebuf;
52         char *sv[4];
53         int i, ns;
54
55         if (nbuf < STATFIXLEN)
56                 return 0;
57
58         p = buf;
59         ebuf = buf + nbuf;
60
61         p += BIT16SZ;   /* ignore size */
62         d->type = GBIT16(p);
63         p += BIT16SZ;
64         d->dev = GBIT32(p);
65         p += BIT32SZ;
66         d->qid.type = GBIT8(p);
67         p += BIT8SZ;
68         d->qid.vers = GBIT32(p);
69         p += BIT32SZ;
70         d->qid.path = GBIT64(p);
71         p += BIT64SZ;
72         d->mode = GBIT32(p);
73         p += BIT32SZ;
74         d->atime = GBIT32(p);
75         p += BIT32SZ;
76         d->mtime = GBIT32(p);
77         p += BIT32SZ;
78         d->length = GBIT64(p);
79         p += BIT64SZ;
80
81         for (i = 0; i < 4; i++) {
82                 if (p + BIT16SZ > ebuf)
83                         return 0;
84                 ns = GBIT16(p);
85                 p += BIT16SZ;
86                 if (p + ns > ebuf)
87                         return 0;
88                 if (strs) {
89                         sv[i] = strs;
90                         memmove(strs, p, ns);
91                         strs += ns;
92                         *strs++ = '\0';
93                 }
94                 p += ns;
95         }
96
97         if (strs) {
98                 d->name = sv[0];
99                 d->uid = sv[1];
100                 d->gid = sv[2];
101                 d->muid = sv[3];
102         } else {
103                 d->name = nullstring;
104                 d->uid = nullstring;
105                 d->gid = nullstring;
106                 d->muid = nullstring;
107         }
108
109         return p - buf;
110 }