aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2019-12-05 14:53:43 -0800
committerRob Landley <rob@landley.net>2019-12-06 12:45:29 -0600
commit7eaf5d674f32a0301c44e2f0f433aa7144ad1479 (patch)
tree8b204f90d06f23ac600f1ef09699b670abf025d0 /lib
parent8c6a4db5558f55f176685de8e9ee082f8f7e9ced (diff)
downloadtoybox-7eaf5d674f32a0301c44e2f0f433aa7144ad1479.tar.gz
stat: fix filesystem type on macOS.
(Also show unknown values on Linux in hex rather than just "unknown".)
Diffstat (limited to 'lib')
-rw-r--r--lib/portability.c27
-rw-r--r--lib/portability.h2
2 files changed, 29 insertions, 0 deletions
diff --git a/lib/portability.c b/lib/portability.c
index ae862c97..5428766f 100644
--- a/lib/portability.c
+++ b/lib/portability.c
@@ -548,3 +548,30 @@ int dev_makedev(int major, int minor)
#error
#endif
}
+
+char *fs_type_name(struct statfs *statfs)
+{
+#if defined(__APPLE__)
+ // macOS has an `f_type` field, but assigns values dynamically as filesystems
+ // are registered. They do give you the name directly though, so use that.
+ return statfs->f_fstypename;
+#else
+ char *s = NULL;
+ struct {unsigned num; char *name;} nn[] = {
+ {0xADFF, "affs"}, {0x5346544e, "ntfs"}, {0x1Cd1, "devpts"},
+ {0x137D, "ext"}, {0xEF51, "ext2"}, {0xEF53, "ext3"},
+ {0x1BADFACE, "bfs"}, {0x9123683E, "btrfs"}, {0x28cd3d45, "cramfs"},
+ {0x3153464a, "jfs"}, {0x7275, "romfs"}, {0x01021994, "tmpfs"},
+ {0x3434, "nilfs"}, {0x6969, "nfs"}, {0x9fa0, "proc"},
+ {0x534F434B, "sockfs"}, {0x62656572, "sysfs"}, {0x517B, "smb"},
+ {0x4d44, "msdos"}, {0x4006, "fat"}, {0x43415d53, "smackfs"},
+ {0x73717368, "squashfs"}
+ };
+ int i;
+
+ for (i=0; i<ARRAY_LEN(nn); i++)
+ if (nn[i].num == statfs->f_type) s = nn[i].name;
+ if (!s) sprintf(s = libbuf, "0x%x", statfs->f_type);
+ return s;
+#endif
+}
diff --git a/lib/portability.h b/lib/portability.h
index 24cd1297..d81ddead 100644
--- a/lib/portability.h
+++ b/lib/portability.h
@@ -355,3 +355,5 @@ void xsignal_all_killers(void *handler);
int dev_minor(int dev);
int dev_major(int dev);
int dev_makedev(int major, int minor);
+
+char *fs_type_name(struct statfs *statfs);