Mercurial > hg > toybox
comparison toys/pending/mount.c @ 1323:c3061b237c4c draft
First stab at mount, very incomplete.
author | Rob Landley <rob@landley.net> |
---|---|
date | Thu, 29 May 2014 06:49:59 -0500 |
parents | |
children | 9fd2bcedbeb5 |
comparison
equal
deleted
inserted
replaced
1322:b91284c2e569 | 1323:c3061b237c4c |
---|---|
1 /* mount.c - mount filesystems | |
2 * | |
3 * Copyright 2014 Rob Landley <rob@landley.net> | |
4 * | |
5 * See http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/mount.html | |
6 * Note: -hV is bad spec, haven't implemented -FsLU yet | |
7 * no mtab (/proc/mounts does it) so -n is NOP. | |
8 | |
9 USE_MOUNT(NEWTOY(mount, ">2afnrvwt:o*[-rw]", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_STAYROOT)) | |
10 | |
11 config MOUNT | |
12 bool "mount" | |
13 default n | |
14 help | |
15 usage: mount [-afFrsvw] [-t TYPE] [-o OPTIONS...] [[DEVICE] DIR] | |
16 | |
17 Mount new filesystem(s) on directories. With no arguments, display existing | |
18 mounts. | |
19 | |
20 -a mount all entries in /etc/fstab (with -t, only entries of that TYPE) | |
21 -f fake it (don't actually mount) | |
22 -r read only (same as -o ro) | |
23 -w read/write (default, same as -o rw) | |
24 -t specify filesystem type | |
25 -v verbose | |
26 | |
27 OPTIONS is a comma separated list of options, which can also be supplied | |
28 as --longopts. | |
29 | |
30 | |
31 | |
32 config MOUNT_AUTODETECT | |
33 help | |
34 usage: mount | |
35 | |
36 This mount autodetects loopback mounts (a file on a directory) and | |
37 bind mounts (file on file, directory on directory), so you don't need | |
38 to say --bind or --loop. | |
39 */ | |
40 | |
41 #define FOR_mount | |
42 #include "toys.h" | |
43 | |
44 GLOBALS( | |
45 struct arg_list *optlist; | |
46 char *type; | |
47 | |
48 unsigned long flags; | |
49 char *opts; | |
50 ) | |
51 | |
52 static void do_mount(char *dev, char *dir, char *type, unsigned long flags, char *opts) | |
53 { | |
54 FILE *fp = 0; | |
55 int rc = EINVAL; | |
56 | |
57 if (toys.optflags & FLAG_f) return; | |
58 | |
59 if (!TT.type) fp = xfopen("/proc/filesystems", "r"); | |
60 | |
61 for (;;) { | |
62 char *buf = 0; | |
63 | |
64 // If type wasn't specified, try all of them in order. | |
65 if (fp) { | |
66 size_t i; | |
67 | |
68 if (getline(&buf, &i, fp)<0) break; | |
69 type = buf; | |
70 // skip nodev devices | |
71 if (!isspace(*type)) { | |
72 free(buf); | |
73 continue; | |
74 } | |
75 // trim whitespace | |
76 while (isspace(*type)) type++; | |
77 i = strlen(type); | |
78 if (i) type[i-1] = 0; | |
79 } | |
80 rc = mount(dev, dir, type, flags, opts); | |
81 if (!fp || (rc && errno != EINVAL)) break; | |
82 free(buf); | |
83 } | |
84 if (fp) fclose(fp); | |
85 | |
86 if (rc) perror_msg("'%s' on '%s'", dev, dir); | |
87 } | |
88 | |
89 void mount_main(void) | |
90 { | |
91 if (toys.optflags & FLAG_a) { | |
92 fprintf(stderr, "not yet\n"); | |
93 return; | |
94 } | |
95 | |
96 // show mounts | |
97 if (!toys.optc) { | |
98 struct mtab_list *mtl = xgetmountlist(0), *m; | |
99 | |
100 for (mtl = xgetmountlist(0); mtl && (m = dlist_pop(&mtl)); free(m)) { | |
101 char *s = 0; | |
102 | |
103 if (TT.type && strcmp(TT.type, m->type)) continue; | |
104 if (*m->device == '/') s = xabspath(m->device, 0); | |
105 xprintf("%s on %s type %s (%s)\n", | |
106 s ? s : m->device, m->dir, m->type, m->opts); | |
107 free(s); | |
108 } | |
109 | |
110 // one argument: from fstab, remount, subtree | |
111 } else if (toys.optc == 1) { | |
112 fprintf(stderr, "not yet\n"); | |
113 return; | |
114 // two arguments | |
115 } else do_mount(toys.optargs[0], toys.optargs[1], TT.type, 0, ""); | |
116 } |