aruu

[MIRROR] Actual replacement for Busybox | suckless
git clone https://git.daat.foo/aruu.git
Log | Files | Refs | README | LICENSE

diskutil_obsd.c (4148B)


      1 /* see LICENSE file for copyright and license details */
      2 #include "../diskutil.h"
      3 #include "../util.h"
      4 
      5 #if defined(__OpenBSD__) && !defined(ARUU_FORCE_POSIX)
      6 #include <errno.h>
      7 #include <stdio.h>
      8 #include <stdlib.h>
      9 #include <string.h>
     10 #include <sys/types.h>
     11 #include <sys/disklabel.h>
     12 #include <sys/dkio.h>
     13 #include <sys/ioctl.h>
     14 #include <sys/mount.h>
     15 #include <sys/param.h>
     16 #include <sys/sysctl.h>
     17 
     18 void
     19 blockdev_sys_size(struct BlockDev *dev)
     20 {
     21   struct disklabel dl;
     22 
     23   if (ioctl(dev->fd, DIOCGDINFO, &dl) >= 0) {
     24     dev->size     = (uint64_t)dl.d_secsize * DL_GETDSIZE(&dl);
     25     dev->sec_size = dl.d_secsize;
     26   }
     27 }
     28 
     29 void
     30 blockdev_sys_reread(struct BlockDev *dev)
     31 {
     32   (void)dev;
     33 }
     34 
     35 struct BlockDevInfo *
     36 blockdev_get_list(void)
     37 {
     38   char                 disknames[1024];
     39   char                 devpath[128];
     40   char                *tok;
     41   char                *ptr;
     42   char                *colon;
     43   struct BlockDevInfo *head = NULL;
     44   struct BlockDevInfo *tail = NULL;
     45   struct BlockDevInfo *info;
     46   struct BlockDevInfo *part;
     47   struct BlockDevInfo *p;
     48   struct BlockDev      dev;
     49   size_t               len;
     50   int                  mib[2];
     51   char                 c;
     52   struct statfs       *mntbuf;
     53   int                  mntsize;
     54   int                  i;
     55 
     56   mib[0] = CTL_HW;
     57   mib[1] = HW_DISKNAMES;
     58   len    = sizeof(disknames);
     59   if (sysctl(mib, 2, disknames, &len, NULL, 0) < 0)
     60     return NULL;
     61 
     62   ptr = disknames;
     63   while ((tok = strsep(&ptr, ",")) != NULL) {
     64     colon = strchr(tok, ':');
     65     if (colon)
     66       *colon = '\0';
     67     if (!*tok)
     68       continue;
     69 
     70     info = ecalloc(1, sizeof(*info));
     71 
     72     strlcpy(info->name, tok, sizeof(info->name));
     73     snprintf(info->path, sizeof(info->path), "%s/r%sc", ARUU_PATH_DEV, tok);
     74     strlcpy(info->type, "disk", sizeof(info->type));
     75 
     76     snprintf(devpath, sizeof(devpath), "%s/r%sc", ARUU_PATH_DEV, tok);
     77     if (blockdev_open(&dev, devpath, 0) == 0) {
     78       info->size = dev.size;
     79       blockdev_close(&dev);
     80     }
     81 
     82     for (c = 'a'; c <= 'p'; c++) {
     83       if (c == 'c')
     84         continue;
     85       snprintf(devpath, sizeof(devpath), "%s/r%s%c", ARUU_PATH_DEV, tok, c);
     86       if (blockdev_open(&dev, devpath, 0) == 0) {
     87         part = ecalloc(1, sizeof(*part));
     88         snprintf(part->name, sizeof(part->name), "%s%c", tok, c);
     89         snprintf(part->path, sizeof(part->path), "%s/r%s%c", ARUU_PATH_DEV, tok, c);
     90         strlcpy(part->type, "part", sizeof(part->type));
     91         part->size = dev.size;
     92         p          = info->parts;
     93         if (!p) {
     94           info->parts = part;
     95         } else {
     96           while (p->next)
     97             p = p->next;
     98           p->next = part;
     99         }
    100         blockdev_close(&dev);
    101       }
    102     }
    103 
    104     if (!head) {
    105       head = info;
    106     } else {
    107       tail->next = info;
    108     }
    109     tail = info;
    110   }
    111 
    112   mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
    113   for (i = 0; i < mntsize; i++) {
    114     char  *frompath = mntbuf[i].f_mntfromname;
    115     char  *devname  = frompath;
    116     size_t dev_len  = strlen(ARUU_PATH_DEV);
    117     if (strncmp(frompath, ARUU_PATH_DEV, dev_len) == 0 && frompath[dev_len] == '/')
    118       devname = frompath + dev_len + 1;
    119     struct BlockDevInfo *curr;
    120     for (curr = head; curr; curr = curr->next) {
    121       if (strcmp(curr->name, devname) == 0) {
    122         strlcpy(curr->mountpoint, mntbuf[i].f_mntonname, sizeof(curr->mountpoint));
    123         strlcpy(curr->fstype, mntbuf[i].f_fstypename, sizeof(curr->fstype));
    124       }
    125       struct BlockDevInfo *part;
    126       for (part = curr->parts; part; part = part->next) {
    127         if (strcmp(part->name, devname) == 0) {
    128           strlcpy(part->mountpoint, mntbuf[i].f_mntonname, sizeof(part->mountpoint));
    129           strlcpy(part->fstype, mntbuf[i].f_fstypename, sizeof(part->fstype));
    130         }
    131       }
    132     }
    133   }
    134 
    135   return head;
    136 }
    137 
    138 int
    139 blockdev_mount(const char *dev, const char *dir, const char *fstype, int flags, const char *data)
    140 {
    141   (void)dev;
    142   (void)dir;
    143   (void)fstype;
    144   (void)flags;
    145   (void)data;
    146   errno = ENOSYS;
    147   return -1;
    148 }
    149 
    150 int
    151 blockdev_unmount(const char *dir)
    152 {
    153   (void)dir;
    154   errno = ENOSYS;
    155   return -1;
    156 }
    157 
    158 int
    159 blockdev_poweroff(const char *disk)
    160 {
    161   (void)disk;
    162   errno = ENOSYS;
    163   return -1;
    164 }
    165 #endif