akaros/kern/src/ns/convM2kdirent.c
<<
>>
Prefs
   1/* Copyright (c) 2013-2018 Google Inc
   2 * Barret Rhoden <brho@cs.berkeley.edu>
   3 * Ron Minnich <rminnich@google.com>
   4 *
   5 * See LICENSE for details.  */
   6
   7#include <slab.h>
   8#include <kmalloc.h>
   9#include <kref.h>
  10#include <string.h>
  11#include <stdio.h>
  12#include <assert.h>
  13#include <error.h>
  14#include <cpio.h>
  15#include <pmap.h>
  16#include <smp.h>
  17#include <net/ip.h>
  18
  19/* Special akaros edition. */
  20unsigned int convM2kdirent(uint8_t *buf, unsigned int nbuf, struct kdirent *kd,
  21                           char *strs)
  22{
  23        struct dir *dir;
  24        size_t conv_sz, name_sz;
  25
  26        if (nbuf < STAT_FIX_LEN_9P)
  27                return 0;
  28        dir = kmalloc(sizeof(struct dir) + nbuf, MEM_WAIT);
  29        conv_sz = convM2D(buf, nbuf, dir, (char*)&dir[1]);
  30
  31        kd->d_ino = dir->qid.path;
  32        kd->d_off = 0;  /* ignored for 9ns readdir */
  33        kd->d_type = 0; /* TODO: might need this; never used this in the VFS */
  34        name_sz = dir->name ? strlen(dir->name) : 0;
  35        kd->d_reclen = name_sz;
  36        /* Our caller should have made sure kd was big enough... */
  37        memcpy(kd->d_name, dir->name, name_sz);
  38        kd->d_name[name_sz] = 0;
  39
  40        kfree(dir);
  41        return conv_sz;
  42}
  43
  44static int mode_9ns_to_posix(int mode_9ns)
  45{
  46        int mode_posix = 0;
  47
  48        if (mode_9ns & DMDIR)
  49                mode_posix |= __S_IFDIR;
  50        else if (mode_9ns & DMSYMLINK)
  51                mode_posix |= __S_IFLNK;
  52        else
  53                mode_posix |= __S_IFREG;
  54        if (mode_9ns & DMREADABLE)
  55                mode_posix |= __S_READABLE;
  56        if (mode_9ns & DMWRITABLE)
  57                mode_posix |= __S_WRITABLE;
  58        mode_posix |= mode_9ns & 0777;
  59        return mode_posix;
  60}
  61
  62unsigned int convM2kstat(uint8_t * buf, unsigned int nbuf, struct kstat *ks)
  63{
  64        struct dir *dir;
  65        size_t conv_sz, name_sz;
  66
  67        if (nbuf < STAT_FIX_LEN_9P)
  68                return 0;
  69        dir = kmalloc(sizeof(struct dir) + nbuf, MEM_WAIT);
  70        conv_sz = convM2D(buf, nbuf, dir, (char*)&dir[1]);
  71
  72        ks->st_dev = dir->type;
  73        ks->st_ino = dir->qid.path;
  74        ks->st_mode = mode_9ns_to_posix(dir->mode);
  75        ks->st_nlink = dir->mode & DMDIR ? 2 : 1;
  76        ks->st_uid = dir->n_uid;
  77        ks->st_gid = dir->n_gid;
  78        ks->st_rdev = dir->dev;
  79        ks->st_size = dir->length;
  80        ks->st_blksize = 1;
  81        ks->st_blocks = dir->length;
  82        ks->st_atim = dir->atime;
  83        ks->st_mtim = dir->mtime;
  84        ks->st_ctim = dir->ctime;
  85
  86        kfree(dir);
  87        return conv_sz;
  88}
  89