xref: /plan9/sys/src/cmd/unix/u9fs/convM2D.c (revision 9a747e4fd48b9f4522c70c07e8f882a15030f964)
1 #include	<plan9.h>
2 #include	<fcall.h>
3 
4 int
statcheck(uchar * buf,uint nbuf)5 statcheck(uchar *buf, uint nbuf)
6 {
7 	uchar *ebuf;
8 	int i;
9 
10 	ebuf = buf + nbuf;
11 
12 	buf += STATFIXLEN - 4 * BIT16SZ;
13 
14 	for(i = 0; i < 4; i++){
15 		if(buf + BIT16SZ > ebuf)
16 			return -1;
17 		buf += BIT16SZ + GBIT16(buf);
18 	}
19 
20 	if(buf != ebuf)
21 		return -1;
22 
23 	return 0;
24 }
25 
26 static char nullstring[] = "";
27 
28 uint
convM2D(uchar * buf,uint nbuf,Dir * d,char * strs)29 convM2D(uchar *buf, uint nbuf, Dir *d, char *strs)
30 {
31 	uchar *p, *ebuf;
32 	char *sv[4];
33 	int i, ns;
34 
35 	p = buf;
36 	ebuf = buf + nbuf;
37 
38 	p += BIT16SZ;	/* ignore size */
39 	d->type = GBIT16(p);
40 	p += BIT16SZ;
41 	d->dev = GBIT32(p);
42 	p += BIT32SZ;
43 	d->qid.type = GBIT8(p);
44 	p += BIT8SZ;
45 	d->qid.vers = GBIT32(p);
46 	p += BIT32SZ;
47 	d->qid.path = GBIT64(p);
48 	p += BIT64SZ;
49 	d->mode = GBIT32(p);
50 	p += BIT32SZ;
51 	d->atime = GBIT32(p);
52 	p += BIT32SZ;
53 	d->mtime = GBIT32(p);
54 	p += BIT32SZ;
55 	d->length = GBIT64(p);
56 	p += BIT64SZ;
57 
58 	d->name = nil;
59 	d->uid = nil;
60 	d->gid = nil;
61 	d->muid = nil;
62 
63 	for(i = 0; i < 4; i++){
64 		if(p + BIT16SZ > ebuf)
65 			return 0;
66 		ns = GBIT16(p);
67 		p += BIT16SZ;
68 		if(p + ns > ebuf)
69 			return 0;
70 		if(strs){
71 			sv[i] = strs;
72 			memmove(strs, p, ns);
73 			strs += ns;
74 			*strs++ = '\0';
75 		}
76 		p += ns;
77 	}
78 
79 	if(strs){
80 		d->name = sv[0];
81 		d->uid = sv[1];
82 		d->gid = sv[2];
83 		d->muid = sv[3];
84 	}else{
85 		d->name = nullstring;
86 		d->uid = nullstring;
87 		d->gid = nullstring;
88 		d->muid = nullstring;
89 	}
90 
91 	return p - buf;
92 }
93