convD2M.c (1927B)
1 #include <u.h> 2 #include <libc.h> 3 #include <fcall.h> 4 5 uint 6 sizeD2Mu(Dir *d, int dotu) 7 { 8 char *sv[5]; 9 int i, ns, nstr, fixlen; 10 11 sv[0] = d->name; 12 sv[1] = d->uid; 13 sv[2] = d->gid; 14 sv[3] = d->muid; 15 16 fixlen = STATFIXLEN; 17 nstr = 4; 18 if(dotu){ 19 fixlen = STATFIXLENU; 20 sv[4] = d->ext; 21 nstr = 5; 22 } 23 24 ns = 0; 25 for(i = 0; i < nstr; i++) 26 if(sv[i]) 27 ns += strlen(sv[i]); 28 29 return fixlen + ns; 30 } 31 32 uint 33 sizeD2M(Dir *d) 34 { 35 return sizeD2Mu(d, 0); 36 } 37 38 uint 39 convD2Mu(Dir *d, uchar *buf, uint nbuf, int dotu) 40 { 41 uchar *p, *ebuf; 42 char *sv[5]; 43 int i, ns, nsv[5], ss, nstr, fixlen; 44 45 if(nbuf < BIT16SZ) 46 return 0; 47 48 p = buf; 49 ebuf = buf + nbuf; 50 51 sv[0] = d->name; 52 sv[1] = d->uid; 53 sv[2] = d->gid; 54 sv[3] = d->muid; 55 56 fixlen = STATFIXLEN; 57 nstr = 4; 58 if(dotu){ 59 fixlen = STATFIXLENU; 60 sv[4] = d->ext; 61 nstr = 5; 62 } 63 64 ns = 0; 65 for(i = 0; i < nstr; i++){ 66 if(sv[i]) 67 nsv[i] = strlen(sv[i]); 68 else 69 nsv[i] = 0; 70 ns += nsv[i]; 71 } 72 73 ss = fixlen + ns; 74 75 /* set size befor erroring, so user can know how much is needed */ 76 /* note that length excludes count field itself */ 77 PBIT16(p, ss-BIT16SZ); 78 p += BIT16SZ; 79 80 if(ss > nbuf) 81 return BIT16SZ; 82 83 PBIT16(p, d->type); 84 p += BIT16SZ; 85 PBIT32(p, d->dev); 86 p += BIT32SZ; 87 PBIT8(p, d->qid.type); 88 p += BIT8SZ; 89 PBIT32(p, d->qid.vers); 90 p += BIT32SZ; 91 PBIT64(p, d->qid.path); 92 p += BIT64SZ; 93 PBIT32(p, d->mode); 94 p += BIT32SZ; 95 PBIT32(p, d->atime); 96 p += BIT32SZ; 97 PBIT32(p, d->mtime); 98 p += BIT32SZ; 99 PBIT64(p, d->length); 100 p += BIT64SZ; 101 102 for(i = 0; i < nstr; i++){ 103 ns = nsv[i]; 104 if(p + ns + BIT16SZ > ebuf) 105 return 0; 106 PBIT16(p, ns); 107 p += BIT16SZ; 108 if(ns) 109 memmove(p, sv[i], ns); 110 p += ns; 111 } 112 113 if(dotu){ 114 PBIT32(p, d->uidnum); 115 p += BIT32SZ; 116 PBIT32(p, d->gidnum); 117 p += BIT32SZ; 118 PBIT32(p, d->muidnum); 119 p += BIT32SZ; 120 } 121 122 if(ss != p - buf) 123 return 0; 124 125 return p - buf; 126 } 127 128 uint 129 convD2M(Dir *d, uchar *buf, uint nbuf) 130 { 131 return convD2Mu(d, buf, nbuf, 0); 132 }