[Toybox] [patch] add u?mount

Strake strake888 at gmail.com
Sun Aug 18 07:38:42 PDT 2013


>From 999f64b2615cd53504e4ad312f7a72eb2170da5f Mon Sep 17 00:00:00 2001
From: Strake <strake888 at gmail.com>
Date: Sun, 18 Aug 2013 09:29:51 -0500
Subject: [PATCH] add u?mount

---
 toys/pending/mount.c  | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++
 toys/pending/umount.c | 27 +++++++++++++++
 2 files changed, 121 insertions(+)
 create mode 100644 toys/pending/mount.c
 create mode 100644 toys/pending/umount.c

diff --git a/toys/pending/mount.c b/toys/pending/mount.c
new file mode 100644
index 0000000..3b666ea
--- /dev/null
+++ b/toys/pending/mount.c
@@ -0,0 +1,94 @@
+/* mount.c - mount filesystem
+ *
+ * Copyright 2013 Strake <strake888 at gmail.com>
+ *
+ * See http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/mount.html
+
+USE_MOUNT(NEWTOY(mount, "o*t:", TOYFLAG_BIN))
+
+config MOUNT
+  bool "mount"
+  default n
+  help
+    usage: mount -t TYPE DEVICE DIR
+
+    mount the filesystem of TYPE on DEVICE onto DIR.
+*/
+
+#define FOR_mount
+#include "toys.h"
+
+GLOBALS(
+  char *t;
+  struct arg_list *o;
+)
+
+struct optkv {
+  char *k;
+  long v;
+};
+
+struct optkv mount_options[] = {
+  { "bind",         MS_BIND },
+  { "dirsync",      MS_DIRSYNC },
+  { "mandlock",     MS_MANDLOCK },
+  { "move",         MS_MOVE },
+  { "atime",       ~MS_NOATIME },
+  { "dev",         ~MS_NODEV },
+  { "diratime",    ~MS_NODIRATIME },
+  { "exec",        ~MS_NOEXEC },
+  { "suid",        ~MS_NOSUID },
+  { "rdonly",       MS_RDONLY },
+  { "relatime",     MS_RELATIME },
+  { "silent",       MS_SILENT },
+  { "strictatime",  MS_STRICTATIME },
+  { "sync",         MS_SYNC },
+  { 0 }
+};
+
+long findmountopt (char *o) {
+  int ii;
+  long v = 0;
+
+  if (strncmp (o, "no", 2) == 0) {
+    v = -1;
+    o += 2;
+  }
+
+  for (ii = 0; mount_options[ii].k; ii++) if (strcmp (o,
mount_options[ii].k) == 0) return (v ^ mount_options[ii].v);
+  return 0;
+}
+
+void mount_main () {
+  char *fsopts;
+  long mountflags = 0;
+
+  fsopts = 0;
+  for (; TT.o; TT.o = TT.o -> next) {
+    char *o, *p;
+    for (o = TT.o -> arg; o; o = p) {
+      p = strchr (o, ',');
+      if (p) p[0] = 0;
+      long flag = findmountopt (o);
+      if (flag  < 0) mountflags &= flag;
+      if (flag  > 0) mountflags |= flag;
+      if (flag == 0) {
+        if (fsopts) fsopts = xastrcat (fsopts, ",");
+        fsopts = xastrcat (fsopts, o);
+      }
+      if (p) p++[0] = ',';
+    }
+  }
+
+  switch (toys.optc) {
+  case 0:
+    xsendfile (xopen ("/proc/self/mounts", O_RDONLY), 1);
+    break;
+  case 2:
+    if (!(toys.optflags & FLAG_t)) error_exit ("usage: mount -t TYPE
DEVICE DIR");
+    if (mount (toys.optargs[0], toys.optargs[1], TT.t, mountflags,
fsopts) < 0) perror_exit ("can't mount -t %s %s %s", TT.t,
toys.optargs[0], toys.optargs[1]);
+    break;
+  default:
+    error_exit ("usage: mount -t TYPE DEVICE DIR");
+  }
+}
diff --git a/toys/pending/umount.c b/toys/pending/umount.c
new file mode 100644
index 0000000..dd7bc53
--- /dev/null
+++ b/toys/pending/umount.c
@@ -0,0 +1,27 @@
+/* umount.c - umount filesystem
+ *
+ * Copyright 2013 CE Strake <strake888 at gmail.com>
+ *
+ * See http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/umount.html
+
+USE_UMOUNT(NEWTOY(umount, "", TOYFLAG_BIN))
+
+config UMOUNT
+  bool "umount"
+  default n
+  help
+    usage: umount DIR...
+
+    unmount filesystem on DIR.
+*/
+
+#define FOR_umount
+#include "toys.h"
+
+void umount_main () {
+  int ii;
+  for (ii = 0; toys.optargs[ii]; ii++) if (umount (toys.optargs[ii]) < 0) {
+    error_msg ("can't umount %s", toys.optargs[ii]);
+    toys.exitval++;
+  }
+}
-- 
1.7.11.1

 1376836722.0


More information about the Toybox mailing list