akaros/tools/compilers/gcc-glibc/glibc-2.19-akaros/sysdeps/akaros/convD2M.c
<<
>>
Prefs
   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
  10#include <string.h>
  11#include <fcall.h>
  12
  13unsigned int sizeD2M(struct dir *d)
  14{
  15        char *sv[4];
  16        int i, ns;
  17
  18        sv[0] = d->name;
  19        sv[1] = d->uid;
  20        sv[2] = d->gid;
  21        sv[3] = d->muid;
  22
  23        ns = 0;
  24        for (i = 0; i < 4; i++)
  25                if (sv[i])
  26                        ns += strlen(sv[i]);
  27
  28        return STATFIXLEN + ns;
  29}
  30
  31unsigned int convD2M(struct dir *d, uint8_t * buf, unsigned int nbuf)
  32{
  33        uint8_t *p, *ebuf;
  34        char *sv[4];
  35        int i, ns, nsv[4], ss;
  36
  37        if (nbuf < BIT16SZ)
  38                return 0;
  39
  40        p = buf;
  41        ebuf = buf + nbuf;
  42
  43        sv[0] = d->name;
  44        sv[1] = d->uid;
  45        sv[2] = d->gid;
  46        sv[3] = d->muid;
  47
  48        ns = 0;
  49        for (i = 0; i < 4; i++) {
  50                if (sv[i])
  51                        nsv[i] = strlen(sv[i]);
  52                else
  53                        nsv[i] = 0;
  54                ns += nsv[i];
  55        }
  56
  57        ss = STATFIXLEN + ns;
  58
  59        /* set size before erroring, so user can know how much is needed */
  60        /* note that length excludes count field itself */
  61        PBIT16(p, ss - BIT16SZ);
  62        p += BIT16SZ;
  63
  64        if (ss > nbuf)
  65                return BIT16SZ;
  66
  67        PBIT16(p, d->type);
  68        p += BIT16SZ;
  69        PBIT32(p, d->dev);
  70        p += BIT32SZ;
  71        PBIT8(p, d->qid.type);
  72        p += BIT8SZ;
  73        PBIT32(p, d->qid.vers);
  74        p += BIT32SZ;
  75        PBIT64(p, d->qid.path);
  76        p += BIT64SZ;
  77        PBIT32(p, d->mode);
  78        p += BIT32SZ;
  79        PBIT32(p, d->atime);
  80        p += BIT32SZ;
  81        PBIT32(p, d->mtime);
  82        p += BIT32SZ;
  83        PBIT64(p, d->length);
  84        p += BIT64SZ;
  85
  86        for (i = 0; i < 4; i++) {
  87                ns = nsv[i];
  88                if (p + ns + BIT16SZ > ebuf)
  89                        return 0;
  90                PBIT16(p, ns);
  91                p += BIT16SZ;
  92                if (ns)
  93                        memmove(p, sv[i], ns);
  94                p += ns;
  95        }
  96
  97        if (ss != p - buf)
  98                return 0;
  99
 100        return p - buf;
 101}
 102