annotate toys/other/losetup.c @ 778:14aabcd31fd9

Add losetup. (Who knows, it might even work.)
author Rob Landley <rob@landley.net>
date Sun, 30 Dec 2012 21:35:01 -0600
parents
children 6cc69be43c42
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);
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
64 int rc = 0, lfd = -1, ffd;
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) {
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
78 if (0 <= (i = ioctl(cfd, LOOP_CTL_GET_FREE)))
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 rc = 1;
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
93 goto done;
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
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
97 // Skip -j filtered devices
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
98 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
99 goto done;
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
100
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
101 // 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
102 if (flags & (FLAG_c|FLAG_d)) {
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
103 if (ioctl(lfd, (flags & FLAG_c) ? LOOP_SET_CAPACITY : LOOP_CLR_FD, 0)) {
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 rc = 1;
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
106 goto done;
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
107 }
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
108 // Associate file with this device?
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
109 } else if (file) {
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
110 char *s = xrealpath(file);
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
111
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
112 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
113 loop->lo_offset = TT.offset;
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
114 loop->lo_sizelimit = TT.size;
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
115 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
116 s[LO_NAME_SIZE-1] = 0;
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
117 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
118 if (flags & FLAG_s) printf("%s", device);
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
119 free(s);
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
120 } 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
121 else {
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
122 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
123 loop->lo_file_name);
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
124 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
125 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
126 xputc('\n');
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
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
129 done:
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
130 if (file) close(ffd);
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
131 if (lfd != -1) close(lfd);
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
132 toys.exitval |= rc;
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
133 }
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
134
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
135 // 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
136 static int dash_a(struct dirtree *node)
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 char *s = node->name;
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
139
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
140 // 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
141 if (*s == '/') return DIRTREE_RECURSE;
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
142 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
143
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
144 s = dirtree_path(node, 0);
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
145 loopback_setup(s, 0);
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
146 free(s);
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 return 0;
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
149 }
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 void losetup_main(void)
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 char **s;
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 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
156
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
157 if (TT.jfile) {
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
158 struct stat st;
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
159
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
160 xstat(TT.jfile, &st);
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
161 TT.jdev = st.st_dev;
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
162 TT.jino = st.st_ino;
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
163 }
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
164
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
165 // With just device, display current association
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
166 // -a, -f substitute for device
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
167 // -j substitute for device
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
168
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
169 // 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
170 // existing association: cd
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 // -f(dc FILE)
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
173
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
174 if (toys.optflags & FLAG_f) {
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
175 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
176 loopback_setup(NULL, *toys.optargs);
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
177 } 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
178 if (toys.optc) error_exit("bad args");
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
179 dirtree_read("/dev", dash_a);
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
180 // Do we need one DEVICE argument?
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
181 } else {
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
182 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
183
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
184 if (!toys.optc || (file && toys.optc>1)) {
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
185 if (CFG_HELP) toys.exithelp++;
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
186 perror_exit("needs 1 arg");
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
187 }
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
188 for (s = toys.optargs; *s; s++) loopback_setup(*s, file);
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
189 }
14aabcd31fd9 Add losetup. (Who knows, it might even work.)
Rob Landley <rob@landley.net>
parents:
diff changeset
190 }