akaros/kern/src/ns/convD2M.c
<<
>>
Prefs
   1/* Copyright © 1994-1999 Lucent Technologies Inc.  All rights reserved.
   2 * Portions Copyright © 1997-1999 Vita Nuova Limited
   3 * Portions Copyright © 2000-2007 Vita Nuova Holdings Limited
   4 *                                (www.vitanuova.com)
   5 * Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
   6 *
   7 * Modified for the Akaros operating system:
   8 * Copyright (c) 2013-2014 The Regents of the University of California
   9 * Copyright (c) 2013-2018 Google Inc.
  10 *
  11 * Permission is hereby granted, free of charge, to any person obtaining a copy
  12 * of this software and associated documentation files (the "Software"), to deal
  13 * in the Software without restriction, including without limitation the rights
  14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15 * copies of the Software, and to permit persons to whom the Software is
  16 * furnished to do so, subject to the following conditions:
  17 *
  18 * The above copyright notice and this permission notice shall be included in
  19 * all copies or substantial portions of the Software.
  20 *
  21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
  24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  27 * SOFTWARE. */
  28
  29#include <slab.h>
  30#include <kmalloc.h>
  31#include <kref.h>
  32#include <string.h>
  33#include <stdio.h>
  34#include <assert.h>
  35#include <error.h>
  36#include <cpio.h>
  37#include <pmap.h>
  38#include <smp.h>
  39#include <net/ip.h>
  40
  41unsigned int sizeD2M(struct dir *d)
  42{
  43        char *sv[STAT_NR_STRINGS_AK];
  44        int i, ns;
  45
  46        sv[0] = d->name;
  47        sv[1] = d->uid;
  48        sv[2] = d->gid;
  49        sv[3] = d->muid;
  50        sv[4] = d->ext;
  51
  52        ns = 0;
  53        for (i = 0; i < STAT_NR_STRINGS_AK; i++)
  54                if (sv[i])
  55                        ns += strlen(sv[i]);
  56
  57        return STAT_FIX_LEN_AK + ns;
  58}
  59
  60/* This converts dirs into the Akaros format (P92000.u + timespecs).  So far, we
  61 * haven't needed to convert to a smaller format, such as you'd get if nbuf was
  62 * too small. */
  63unsigned int convD2M(struct dir *d, uint8_t * buf, unsigned int nbuf)
  64{
  65        uint8_t *p, *ebuf;
  66        char *sv[STAT_NR_STRINGS_AK];
  67        int i, ns, nsv[STAT_NR_STRINGS_AK], ss;
  68
  69        if (nbuf < BIT16SZ)
  70                return 0;
  71
  72        p = buf;
  73        ebuf = buf + nbuf;
  74
  75        sv[0] = d->name;
  76        sv[1] = d->uid;
  77        sv[2] = d->gid;
  78        sv[3] = d->muid;
  79        sv[4] = d->ext;
  80
  81        ns = 0;
  82        for (i = 0; i < STAT_NR_STRINGS_AK; i++) {
  83                if (sv[i])
  84                        nsv[i] = strlen(sv[i]);
  85                else
  86                        nsv[i] = 0;
  87                ns += nsv[i];
  88        }
  89
  90        ss = STAT_FIX_LEN_AK + ns;
  91
  92        /* set size befor erroring, so user can know how much is needed */
  93        /* note that length excludes the count field itself */
  94        PBIT16(p, ss - BIT16SZ);
  95        p += BIT16SZ;
  96
  97        if (ss > nbuf)
  98                return BIT16SZ;
  99
 100        PBIT16(p, d->type);             p += BIT16SZ;
 101        PBIT32(p, d->dev);              p += BIT32SZ;
 102        PBIT8(p, d->qid.type);          p += BIT8SZ;
 103        PBIT32(p, d->qid.vers);         p += BIT32SZ;
 104        PBIT64(p, d->qid.path);         p += BIT64SZ;
 105        PBIT32(p, d->mode);             p += BIT32SZ;
 106        PBIT32(p, d->atime.tv_sec);     p += BIT32SZ;
 107        PBIT32(p, d->mtime.tv_sec);     p += BIT32SZ;
 108        PBIT64(p, d->length);           p += BIT64SZ;
 109
 110        for (i = 0; i < STAT_NR_STRINGS_AK; i++) {
 111                ns = nsv[i];
 112                if (p + ns + BIT16SZ > ebuf)
 113                        return 0;
 114                PBIT16(p, ns); p += BIT16SZ;
 115                if (ns)
 116                        memmove(p, sv[i], ns);
 117                p += ns;
 118        }
 119        PBIT32(p, d->n_uid);            p += BIT32SZ;
 120        PBIT32(p, d->n_gid);            p += BIT32SZ;
 121        PBIT32(p, d->n_muid);           p += BIT32SZ;
 122        PBIT64(p, d->atime.tv_sec);     p += BIT64SZ;
 123        PBIT64(p, d->atime.tv_nsec);    p += BIT64SZ;
 124        PBIT64(p, d->btime.tv_sec);     p += BIT64SZ;
 125        PBIT64(p, d->btime.tv_nsec);    p += BIT64SZ;
 126        PBIT64(p, d->ctime.tv_sec);     p += BIT64SZ;
 127        PBIT64(p, d->ctime.tv_nsec);    p += BIT64SZ;
 128        PBIT64(p, d->mtime.tv_sec);     p += BIT64SZ;
 129        PBIT64(p, d->mtime.tv_nsec);    p += BIT64SZ;
 130
 131        if (ss != p - buf)
 132                return 0;
 133
 134        return p - buf;
 135}
 136