plan9port/src/lib9/convD2M.c

133 lines
1.9 KiB
C
Raw Normal View History

2003-12-06 18:08:52 +00:00
#include <u.h>
#include <libc.h>
#include <fcall.h>
uint
2005-09-13 01:37:14 +00:00
sizeD2Mu(Dir *d, int dotu)
2003-12-06 18:08:52 +00:00
{
2005-09-13 01:37:14 +00:00
char *sv[5];
int i, ns, nstr, fixlen;
2003-12-06 18:08:52 +00:00
sv[0] = d->name;
sv[1] = d->uid;
sv[2] = d->gid;
sv[3] = d->muid;
2005-09-13 01:37:14 +00:00
fixlen = STATFIXLEN;
nstr = 4;
if(dotu){
fixlen = STATFIXLENU;
sv[4] = d->ext;
nstr = 5;
}
2003-12-06 18:08:52 +00:00
ns = 0;
2005-09-13 01:37:14 +00:00
for(i = 0; i < nstr; i++)
2003-12-06 18:08:52 +00:00
if(sv[i])
ns += strlen(sv[i]);
2005-09-13 01:37:14 +00:00
return fixlen + ns;
2003-12-06 18:08:52 +00:00
}
uint
2005-09-13 01:37:14 +00:00
sizeD2M(Dir *d)
{
return sizeD2Mu(d, 0);
}
uint
convD2Mu(Dir *d, uchar *buf, uint nbuf, int dotu)
2003-12-06 18:08:52 +00:00
{
uchar *p, *ebuf;
2005-09-13 01:37:14 +00:00
char *sv[5];
2005-12-29 21:39:51 +00:00
int i, ns, nsv[5], ss, nstr, fixlen;
2003-12-06 18:08:52 +00:00
if(nbuf < BIT16SZ)
return 0;
p = buf;
ebuf = buf + nbuf;
sv[0] = d->name;
sv[1] = d->uid;
sv[2] = d->gid;
sv[3] = d->muid;
2005-09-13 01:37:14 +00:00
fixlen = STATFIXLEN;
nstr = 4;
if(dotu){
fixlen = STATFIXLENU;
sv[4] = d->ext;
nstr = 5;
}
2003-12-06 18:08:52 +00:00
ns = 0;
2005-09-13 01:37:14 +00:00
for(i = 0; i < nstr; i++){
2003-12-06 18:08:52 +00:00
if(sv[i])
nsv[i] = strlen(sv[i]);
else
nsv[i] = 0;
ns += nsv[i];
}
2005-09-13 01:37:14 +00:00
ss = fixlen + ns;
2003-12-06 18:08:52 +00:00
/* set size befor erroring, so user can know how much is needed */
/* note that length excludes count field itself */
PBIT16(p, ss-BIT16SZ);
p += BIT16SZ;
if(ss > nbuf)
return BIT16SZ;
PBIT16(p, d->type);
p += BIT16SZ;
PBIT32(p, d->dev);
p += BIT32SZ;
PBIT8(p, d->qid.type);
p += BIT8SZ;
PBIT32(p, d->qid.vers);
p += BIT32SZ;
PBIT64(p, d->qid.path);
p += BIT64SZ;
PBIT32(p, d->mode);
p += BIT32SZ;
PBIT32(p, d->atime);
p += BIT32SZ;
PBIT32(p, d->mtime);
p += BIT32SZ;
PBIT64(p, d->length);
p += BIT64SZ;
2005-09-13 01:37:14 +00:00
for(i = 0; i < nstr; i++){
2003-12-06 18:08:52 +00:00
ns = nsv[i];
if(p + ns + BIT16SZ > ebuf)
return 0;
PBIT16(p, ns);
p += BIT16SZ;
if(ns)
memmove(p, sv[i], ns);
p += ns;
}
2005-09-13 01:37:14 +00:00
if(dotu){
PBIT32(p, d->uidnum);
p += BIT32SZ;
PBIT32(p, d->gidnum);
p += BIT32SZ;
PBIT32(p, d->muidnum);
p += BIT32SZ;
}
2003-12-06 18:08:52 +00:00
if(ss != p - buf)
return 0;
return p - buf;
}
2005-09-13 01:37:14 +00:00
uint
convD2M(Dir *d, uchar *buf, uint nbuf)
{
return convD2Mu(d, buf, nbuf, 0);
}