Fixes stat() #include
[akaros.git] / user / ndblib / convM2D.c
1 /* 
2  * This file is part of the UCB release of Plan 9. It is subject to the license
3  * terms in the LICENSE file found in the top-level directory of this
4  * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
5  * part of the UCB release of Plan 9, including this file, may be copied,
6  * modified, propagated, or distributed except according to the terms contained
7  * in the LICENSE file.
8  */
9 #include <stdlib.h>
10
11 #include <stdio.h>
12 #include <parlib.h>
13 #include <unistd.h>
14 #include <signal.h>
15 #include <fcntl.h>
16 #include <iplib.h>
17 #include <dir.h>
18 #include <ndb.h>
19 #include <fcall.h>
20
21 int
22 statcheck(uint8_t *buf, unsigned int nbuf)
23 {
24         uint8_t *ebuf;
25         int i;
26
27         ebuf = buf + nbuf;
28
29         if(nbuf < STATFIXLEN || nbuf != BIT16SZ + GBIT16(buf))
30                 return -1;
31
32         buf += STATFIXLEN - 4 * BIT16SZ;
33
34         for(i = 0; i < 4; i++){
35                 if(buf + BIT16SZ > ebuf)
36                         return -1;
37                 buf += BIT16SZ + GBIT16(buf);
38         }
39
40         if(buf != ebuf)
41                 return -1;
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 }