[Toybox] [PATCH 3/3] Add support for -o x-mount.mkdir[=0755]

Isaac Dunham ibid.ag at gmail.com
Sat Mar 28 13:38:14 PDT 2015


Add support for -o x-mount.mkdir[=0755]

This is a feature of recent versions of util-linux, which allows
creating a missing directory on mount.
Probably it should actually be integrated into mount_filesystem()
and made to check the existence/type of the source and mountpoint.

Creating a chmod 0000 directory and using it as a mountpoint actually
works, so we need something other than 0 for a magic "don't create a
directory" value.
So we use S_IFLNK as a magic flag, since we don't want to create a link.
---
 toys/lsb/mount.c | 27 ++++++++++++++++++++++-----
 1 file changed, 22 insertions(+), 5 deletions(-)

diff --git a/toys/lsb/mount.c b/toys/lsb/mount.c
index 913a434..1711123 100644
--- a/toys/lsb/mount.c
+++ b/toys/lsb/mount.c
@@ -77,7 +77,7 @@ GLOBALS(
 // TODO mount image.img sub (auto-loopback) then umount image.img
 
 // Strip flags out of comma separated list of options, return flags,.
-static long flag_opts(char *new, long flags, char **more)
+static long flag_opts(char *new, long flags, char **more, mode_t *mode)
 {
   struct {
     char *name;
@@ -126,7 +126,11 @@ static long flag_opts(char *new, long flags, char **more)
         *more = xrealloc(*more, i + strlen(new) + 2);
         if (i) (*more)[i++] = ',';
         strcpy(i+*more, new);
-      } //TODO: handle x-mount.mkdir[=mode] option
+      } else if (!strncmp(new, "x-mount.mkdir", 13)) {
+        // handle x-mount.mkdir[=mode] option
+        if (new[13] == '=') *mode = (mode_t)strtol(new + 14, NULL, 8);
+        else *mode = 0755;
+      }
     }
 
     if (!comma) break;
@@ -251,6 +255,7 @@ void mount_main(void)
 {
   char *opts = 0, *dev = 0, *dir = 0, **ss;
   long flags = MS_SILENT;
+  mode_t dirmode = S_IFLNK;
   struct arg_list *o;
   struct mtab_list *mtl, *mtl2 = 0, *mm, *remount;
 
@@ -332,9 +337,15 @@ void mount_main(void)
  
       // user only counts from fstab, not opts.
       if (!mmm) {
+        dirmode = S_IFLNK;
         TT.okuser = comma_scan(mm->opts, "user", 1);
-        aflags = flag_opts(mm->opts, flags, &aopts);
-        aflags = flag_opts(opts, aflags, &aopts);
+        aflags = flag_opts(mm->opts, flags, &aopts, &dirmode);
+        aflags = flag_opts(opts, aflags, &aopts, &dirmode);
+
+        if (!getuid() && !(dirmode & S_IFLNK)) {
+          mkpathat(AT_FDCWD, mm->dir, dirmode, 2);
+          mkdir(dir, dirmode);
+        }
 
         mount_filesystem(mm->device, mm->dir, mm->type, aflags, aopts);
       } // TODO else if (getuid()) error_msg("already there") ?
@@ -365,8 +376,14 @@ void mount_main(void)
   // two arguments
   } else {
     char *more = 0;
+    long aflags = flag_opts(opts, flags, &more, &dirmode);
+
+    if (!getuid() && !(dirmode & S_IFLNK)) { 
+      mkpathat(AT_FDCWD, dir, dirmode, 2);
+      mkdir(dir, dirmode);
+    }
 
-    mount_filesystem(dev, dir, TT.type, flag_opts(opts, flags, &more), more);
+    mount_filesystem(dev, dir, TT.type, aflags, more);
     if (CFG_TOYBOX_FREE) free(more);
   }
 }
-- 
2.1.4


 1427575094.0


More information about the Toybox mailing list