system/corennnnn
Revisión | e520d036165b36cf5c4cb305f9cec7d183977b61 (tree) |
---|---|
Tiempo | 2008-11-21 03:38:36 |
Autor | Jay Freeman (saurik) <saurik@saur...> |
Commiter | Jay Freeman (saurik) |
Added support for loop@/path/to/filename to init's mount.
In keeping with the pattern of mtd@partition, I have added loop@path as a way to specify a loopback device. This way you can do things like mount directories in /system using cramfs from a file otherwise on /system (just one example oof how I'm using it). I specifically went with loop@ rather than adding this feature as a flag as the flags system is designed to set bits in the flags argument to mount: using loop@ fit the model in a much simpler manner and actually feels "correct".
This is a better version of the previously submitted 4045 that also refactors the mtd@ case. The reason for this is that I received comments that I should check for errors and return errors rather that do work in the case of success and fall through, but the mtd@ case wasn't doing that either and it became awkward to design the function so that it was half in one style of error handling and half in another. I also made certain to use inequality comparisons for Unix's -1 error returns rather than checking for -1, refactored my large if statement so as not to have danling parentheses, and disassocited the loop device on mount failure.
@@ -29,6 +29,7 @@ | ||
29 | 29 | #include <stdlib.h> |
30 | 30 | #include <sys/mount.h> |
31 | 31 | #include <sys/resource.h> |
32 | +#include <linux/loop.h> | |
32 | 33 | |
33 | 34 | #include "init.h" |
34 | 35 | #include "keywords.h" |
@@ -231,7 +232,7 @@ static struct { | ||
231 | 232 | int do_mount(int nargs, char **args) |
232 | 233 | { |
233 | 234 | char tmp[64]; |
234 | - char *source; | |
235 | + char *source, *target, *system; | |
235 | 236 | char *options = NULL; |
236 | 237 | unsigned flags = 0; |
237 | 238 | int n, i; |
@@ -249,15 +250,70 @@ int do_mount(int nargs, char **args) | ||
249 | 250 | options = args[n]; |
250 | 251 | } |
251 | 252 | |
253 | + system = args[1]; | |
252 | 254 | source = args[2]; |
255 | + target = args[3]; | |
256 | + | |
253 | 257 | if (!strncmp(source, "mtd@", 4)) { |
254 | 258 | n = mtd_name_to_number(source + 4); |
255 | - if (n >= 0) { | |
256 | - sprintf(tmp, "/dev/block/mtdblock%d", n); | |
257 | - source = tmp; | |
259 | + if (n < 0) { | |
260 | + return -1; | |
261 | + } | |
262 | + | |
263 | + sprintf(tmp, "/dev/block/mtdblock%d", n); | |
264 | + | |
265 | + if (mount(tmp, target, system, flags, options) < 0) { | |
266 | + return -1; | |
267 | + } | |
268 | + | |
269 | + return 0; | |
270 | + } else if (!strncmp(source, "loop@", 5)) { | |
271 | + int mode, loop, fd; | |
272 | + struct loop_info info; | |
273 | + | |
274 | + mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR; | |
275 | + fd = open(source + 5, mode); | |
276 | + if (fd < 0) { | |
277 | + return -1; | |
258 | 278 | } |
279 | + | |
280 | + for (n = 0; ; n++) { | |
281 | + sprintf(tmp, "/dev/block/loop%d", n); | |
282 | + loop = open(tmp, mode); | |
283 | + if (loop < 0) { | |
284 | + return -1; | |
285 | + } | |
286 | + | |
287 | + /* if it is a blank loop device */ | |
288 | + if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) { | |
289 | + /* if it becomes our loop device */ | |
290 | + if (ioctl(loop, LOOP_SET_FD, fd) >= 0) { | |
291 | + close(fd); | |
292 | + | |
293 | + if (mount(tmp, target, system, flags, options) < 0) { | |
294 | + ioctl(loop, LOOP_CLR_FD, 0); | |
295 | + close(loop); | |
296 | + return -1; | |
297 | + } | |
298 | + | |
299 | + close(loop); | |
300 | + return 0; | |
301 | + } | |
302 | + } | |
303 | + | |
304 | + close(loop); | |
305 | + } | |
306 | + | |
307 | + close(fd); | |
308 | + ERROR("out of loopback devices"); | |
309 | + return -1; | |
310 | + } else { | |
311 | + if (mount(source, target, system, flags, options) < 0) { | |
312 | + return -1; | |
313 | + } | |
314 | + | |
315 | + return 0; | |
259 | 316 | } |
260 | - return mount(source, args[3], args[1], flags, options); | |
261 | 317 | } |
262 | 318 | |
263 | 319 | int do_setkey(int nargs, char **args) |