annotate toys/other/losetup.c @ 1322:b91284c2e569 draft

Make "losetup /dev/loop0 filename" work. Sigh. Implement the complex cases and you screw up the simple cases you already tested...
author Rob Landley <rob@landley.net>
date Thu, 29 May 2014 06:29:12 -0500
parents 34ac05521d94
children 43bed82c87df
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
778
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
1 /* losetup.c - Loopback setup
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
2 *
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
3 * Copyright 2012 Rob Landley <rob@landley.net>
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
4 *
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
5 * No standard. (Sigh.)
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
6
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
7 USE_LOSETUP(NEWTOY(losetup, ">2S(sizelimit)#s(show)ro#j:fdca[!afj]", TOYFLAG_SBIN))
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
8
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
9 config LOSETUP
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
10 bool "losetup"
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
11 default y
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
12 help
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
13 usage: losetup [-cdrs] [-o OFFSET] [-S SIZE] {-d DEVICE...|-j FILE|-af|{DEVICE FILE}}
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
14
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
15 Associate a loopback device with a file, or show current file (if any)
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
16 associated with a loop device.
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
17
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
18 Instead of a device:
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
19 -a Iterate through all loopback devices
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
20 -f Find first unused loop device (may create one)
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
21 -j Iterate through all loopback devices associated with FILE
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
22
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
23 existing:
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
24 -c Check capacity (file size changed)
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
25 -d Detach loopback device
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
26
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
27 new:
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
28 -s Show device name (alias --show)
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
29 -o Start assocation at OFFSET into FILE
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
30 -r Read only
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
31 -S Limit SIZE of loopback association (alias --sizelimit)
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
32 */
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
33
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
34 #define FOR_losetup
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
35 #include "toys.h"
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
36 #include <linux/loop.h>
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
37
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
38 GLOBALS(
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
39 char *jfile;
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
40 long offset;
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
41 long size;
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
42
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
43 int openflags;
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
44 dev_t jdev;
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
45 ino_t jino;
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
46 )
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
47
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
48 /*
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
49 todo: basic /dev file association
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
50 associate DEV FILE
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
51 #-a
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
52 cdfjosS
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
53 allocate new loop device:
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
54 /dev/loop-control
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
55 https://lkml.org/lkml/2011/7/26/148
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
56 */
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
57
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
58 // -f: *device is NULL
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
59
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
60 // Perform requested operation on one device. Returns 1 if handled, 0 if error
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
61 static void loopback_setup(char *device, char *file)
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
62 {
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
63 struct loop_info64 *loop = (void *)(toybuf+32);
788
a94ca6ee780f Silence deeply stupid gcc warning. (First non-declaration line of function: if (file) ffd = open(); at end of of function: if (file) close(ffd); "file" is an argument to the function and nothing else assigns to it. gcc warning on that close, "ffd may be used uninitialized!" _HOW_?)
Rob Landley <rob@landley.net>
parents: 780
diff changeset
64 int lfd = -1, ffd = ffd;
778
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
65 unsigned flags = toys.optflags;
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
66
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
67 // Open file (ffd) and loop device (lfd)
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
68
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
69 if (file) ffd = xopen(file, TT.openflags);
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
70 if (!device) {
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
71 int i, cfd = open("/dev/loop-control", O_RDWR);
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
72
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
73 // We assume /dev is devtmpfs so device creation has no lag. Otherwise
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
74 // just preallocate loop devices and stay within them.
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
75
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
76 // mount -o loop depends on found device being at the start of toybuf.
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
77 if (cfd != -1) {
799
6b8cc11d517c Some older build environments don't have LOOP_CTL_GET_FREE or LOOP_SET_CAPACITY. Substitute constants so they can get the rest of losetup without a build break.
Rob Landley <rob@landley.net>
parents: 788
diff changeset
78 if (0 <= (i = ioctl(cfd, 0x4C82))) // LOOP_CTL_GET_FREE
778
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
79 sprintf(device = toybuf, "/dev/loop%d", i);
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
80 close(cfd);
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
81 }
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
82 }
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
83
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
84 if (device) lfd = open(device, TT.openflags);
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
85
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
86 // Stat the loop device to see if there's a current association.
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
87 memset(loop, 0, sizeof(struct loop_info64));
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
88 if (-1 == lfd || ioctl(lfd, LOOP_GET_STATUS64, loop)) {
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
89 if (errno == ENXIO && (flags & (FLAG_a|FLAG_j))) return;
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
90 if (errno != ENXIO || !file) {
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
91 perror_msg("%s", device ? device : "-f");
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
92 goto done;
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
93 }
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
94 }
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
95
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
96 // Skip -j filtered devices
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
97 if (TT.jfile && (loop->lo_device != TT.jdev || loop->lo_inode != TT.jino))
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
98 goto done;
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
99
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
100 // Check size of file or delete existing association
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
101 if (flags & (FLAG_c|FLAG_d)) {
799
6b8cc11d517c Some older build environments don't have LOOP_CTL_GET_FREE or LOOP_SET_CAPACITY. Substitute constants so they can get the rest of losetup without a build break.
Rob Landley <rob@landley.net>
parents: 788
diff changeset
102 // The constant is LOOP_SET_CAPACITY
6b8cc11d517c Some older build environments don't have LOOP_CTL_GET_FREE or LOOP_SET_CAPACITY. Substitute constants so they can get the rest of losetup without a build break.
Rob Landley <rob@landley.net>
parents: 788
diff changeset
103 if (ioctl(lfd, (flags & FLAG_c) ? 0x4C07 : LOOP_CLR_FD, 0)) {
778
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
104 perror_msg("%s", device);
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
105 goto done;
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
106 }
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
107 // Associate file with this device?
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
108 } else if (file) {
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
109 char *s = xrealpath(file);
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
110
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
111 if (ioctl(lfd, LOOP_SET_FD, ffd)) perror_exit("%s=%s", device, file);
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
112 loop->lo_offset = TT.offset;
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
113 loop->lo_sizelimit = TT.size;
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
114 strncpy((char *)loop->lo_file_name, s, LO_NAME_SIZE);
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
115 s[LO_NAME_SIZE-1] = 0;
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
116 if (ioctl(lfd, LOOP_SET_STATUS64, loop)) perror_exit("%s=%s", device, file);
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
117 if (flags & FLAG_s) printf("%s", device);
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
118 free(s);
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
119 } else if (flags & FLAG_f) printf("%s", device);
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
120 else {
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
121 xprintf("%s: [%04llx]:%llu (%s)", device, loop->lo_device, loop->lo_inode,
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
122 loop->lo_file_name);
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
123 if (loop->lo_offset) xprintf(", offset %llu", loop->lo_offset);
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
124 if (loop->lo_sizelimit) xprintf(", sizelimit %llu", loop->lo_sizelimit);
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
125 xputc('\n');
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
126 }
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
127
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
128 done:
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
129 if (file) close(ffd);
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
130 if (lfd != -1) close(lfd);
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
131 }
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
132
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
133 // Perform an action on all currently existing loop devices
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
134 static int dash_a(struct dirtree *node)
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
135 {
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
136 char *s = node->name;
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
137
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
138 // Initial /dev node needs to recurse down one level, then only loop[0-9]*
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
139 if (*s == '/') return DIRTREE_RECURSE;
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
140 if (strncmp(s, "loop", 4) || !isdigit(s[4])) return 0;
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
141
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
142 s = dirtree_path(node, 0);
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
143 loopback_setup(s, 0);
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
144 free(s);
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
145
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
146 return 0;
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
147 }
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
148
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
149 void losetup_main(void)
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
150 {
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
151 char **s;
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
152
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
153 TT.openflags = (toys.optflags & FLAG_r) ? O_RDONLY : O_RDWR;
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
154
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
155 if (TT.jfile) {
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
156 struct stat st;
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
157
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
158 xstat(TT.jfile, &st);
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
159 TT.jdev = st.st_dev;
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
160 TT.jino = st.st_ino;
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
161 }
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
162
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
163 // With just device, display current association
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
164 // -a, -f substitute for device
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
165 // -j substitute for device
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
166
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
167 // new association: S size o offset rs - need a file
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
168 // existing association: cd
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
169
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
170 // -f(dc FILE)
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
171
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
172 if (toys.optflags & FLAG_f) {
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
173 if (toys.optc > 1) perror_exit("max 1 arg");
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
174 loopback_setup(NULL, *toys.optargs);
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
175 } else if (toys.optflags & (FLAG_a|FLAG_j)) {
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
176 if (toys.optc) error_exit("bad args");
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
177 dirtree_read("/dev", dash_a);
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
178 // Do we need one DEVICE argument?
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
179 } else {
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
180 char *file = (toys.optflags & (FLAG_d|FLAG_c)) ? NULL : toys.optargs[1];
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
181
1322
b91284c2e569 Make "losetup /dev/loop0 filename" work.
Rob Landley <rob@landley.net>
parents: 858
diff changeset
182 if (!toys.optc || (file && toys.optc != 2)) {
858
34ac05521d94 Move guts of help command into show_help() in lib/help.c, with config TOYBOX_HELP controlling infrastructure.
Rob Landley <rob@landley.net>
parents: 799
diff changeset
183 toys.exithelp++;
1322
b91284c2e569 Make "losetup /dev/loop0 filename" work.
Rob Landley <rob@landley.net>
parents: 858
diff changeset
184 perror_exit("needs %d arg%s", 1+!!file, file ? "s" : "");
778
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
185 }
1322
b91284c2e569 Make "losetup /dev/loop0 filename" work.
Rob Landley <rob@landley.net>
parents: 858
diff changeset
186 for (s = toys.optargs; *s; s++) {
b91284c2e569 Make "losetup /dev/loop0 filename" work.
Rob Landley <rob@landley.net>
parents: 858
diff changeset
187 loopback_setup(*s, file);
b91284c2e569 Make "losetup /dev/loop0 filename" work.
Rob Landley <rob@landley.net>
parents: 858
diff changeset
188 if (file) break;
b91284c2e569 Make "losetup /dev/loop0 filename" work.
Rob Landley <rob@landley.net>
parents: 858
diff changeset
189 }
778
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
190 }
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
191 }