aruu

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

filesystem_linux.c (2903B)


      1 #ifndef _DEFAULT_SOURCE
      2 #define _DEFAULT_SOURCE
      3 #endif
      4 #include "../filesystem.h"
      5 #include "../paths.h"
      6 #include "../util.h"
      7 
      8 #if defined(__linux__) && !defined(ARUU_FORCE_POSIX)
      9 #include <mntent.h>
     10 #include <stdio.h>
     11 #include <stdlib.h>
     12 #include <string.h>
     13 #include <sys/statfs.h>
     14 #include <sys/statvfs.h>
     15 #include <sys/sysmacros.h>
     16 
     17 int
     18 fs_get_status(const char *path, struct FilesystemStatus *status)
     19 {
     20   struct statvfs s;
     21 
     22   memset(status, 0, sizeof(*status));
     23   if (statvfs(path, &s) < 0)
     24     return -1;
     25   status->block_size   = s.f_frsize;
     26   status->total_blocks = s.f_blocks;
     27   status->free_blocks  = s.f_bfree;
     28   return 0;
     29 }
     30 
     31 int
     32 fs_get_mounts(struct FilesystemMount **mounts, size_t *count)
     33 {
     34   struct FilesystemMount *list = NULL;
     35   struct mntent          *me;
     36   FILE                   *fp;
     37   size_t                  n = 0;
     38 
     39   *mounts = NULL;
     40   *count  = 0;
     41   fp      = setmntent(ARUU_LINUX_PATH_PROC_MOUNTS, "r");
     42   if (!fp)
     43     return -1;
     44 
     45   while ((me = getmntent(fp)) != NULL) {
     46     list           = erealloc(list, (n + 1) * sizeof(*list));
     47     list[n].source = estrdup(me->mnt_fsname);
     48     list[n].type   = estrdup(me->mnt_type);
     49     list[n].path   = estrdup(me->mnt_dir);
     50     n++;
     51   }
     52   endmntent(fp);
     53 
     54   *mounts = list;
     55   *count  = n;
     56   return 0;
     57 }
     58 
     59 void
     60 fs_free_mounts(struct FilesystemMount *mounts, size_t count)
     61 {
     62   size_t i;
     63 
     64   for (i = 0; i < count; i++) {
     65     free(mounts[i].source);
     66     free(mounts[i].type);
     67     free(mounts[i].path);
     68   }
     69   free(mounts);
     70 }
     71 
     72 unsigned int
     73 fs_devmajor(dev_t dev)
     74 {
     75   return major(dev);
     76 }
     77 
     78 unsigned int
     79 fs_devminor(dev_t dev)
     80 {
     81   return minor(dev);
     82 }
     83 
     84 dev_t
     85 fs_makedev(unsigned int ma, unsigned int mi)
     86 {
     87   return makedev(ma, mi);
     88 }
     89 
     90 int
     91 fs_get_info(const char *path, int fd, struct FilesystemInfo *info)
     92 {
     93   struct statfs st;
     94 
     95   if ((path ? statfs(path, &st) : fstatfs(fd, &st)) < 0)
     96     return -1;
     97   info->id =
     98       (unsigned long long)st.f_fsid.__val[0] | ((unsigned long long)st.f_fsid.__val[1] << 32);
     99   info->namelen = st.f_namelen;
    100   info->type    = st.f_type;
    101   info->bsize   = st.f_bsize;
    102   info->frsize  = st.f_frsize;
    103   info->blocks  = st.f_blocks;
    104   info->bfree   = st.f_bfree;
    105   info->bavail  = st.f_bavail;
    106   info->files   = st.f_files;
    107   info->ffree   = st.f_ffree;
    108   return 0;
    109 }
    110 
    111 const char *
    112 fs_type_name(long type)
    113 {
    114   switch (type) {
    115     case 0x9fa0:
    116       return "proc";
    117     case 0x62656567:
    118       return "beegfs";
    119     case 0xef53:
    120       return "ext2/ext3";
    121     case 0x1cd1:
    122       return "devpctld";
    123     case 0xf15f:
    124       return "ecryptfs";
    125     case 0x65737566:
    126       return "fuse";
    127     case 0x4d44:
    128       return "msdos";
    129     case 0x564c:
    130       return "novell";
    131     case 0x6969:
    132       return "nfs";
    133     case 0x534f4654:
    134       return "overlay";
    135     case 0x73717368:
    136       return "squashfs";
    137     case 0x01021994:
    138       return "tmpfs";
    139     case 0x858458f6:
    140       return "ramfs";
    141     default:
    142       return "unknown";
    143   }
    144 }
    145 #endif