-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathlosetup.c
112 lines (101 loc) · 2.22 KB
/
losetup.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <sys/types.h>
#include <sys/param.h>
#include <sys/ioctl.h>
#include <sys/loop.h>
#include <sys/fcntl.h>
#include <sys/stat.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "busybox.h"
static char dospfx[] = "\\DosDevices\\";
static void
fixsep(char *path)
{
char *p;
do {
p = strchr(path, '/');
if (p)
*p++ = '\\';
} while ((path = p));
}
int
losetup_main (int argc, char **argv)
{
int newfile, delete;
char *offset, *fssize;
char *rpath;
struct loopsetup loset;
int ch, fd, rv;
newfile = delete = 0;
offset = fssize = NULL;
bzero(&loset, sizeof (struct loopsetup));
while ((ch = getopt(argc, argv, "ndo:s:")) != -1)
switch (ch) {
case 'd':
delete++;
break;
case 'n':
newfile++;
break;
case 'o':
offset = optarg;
loset.offset = strtoll(offset, NULL, 0);
break;
case 's':
fssize = optarg;
loset.size = strtoll(fssize, NULL, 0);
break;
default:
show_usage();
/* NOTREACHED */
}
argc -= optind;
argv += optind;
if (delete) {
if (!argc || offset || fssize || newfile)
show_usage();
loset.loname = basename(*argv++);
fd = open("/dev/loop", O_RDONLY);
if (fd < 0)
perror_msg_and_die("/dev/loop");
rv = ioctl(fd, LOOP_DEL_FILE, &loset);
if (rv < 0)
perror_msg_and_die("ioctl");
return EXIT_SUCCESS;
}
if (argc < 2 || (newfile && !fssize))
show_usage();
loset.loname = *argv++;
if (strncmp(loset.loname, "/dev/", 5) == 0)
loset.loname += 5;
loset.filename = *argv;
fixsep(loset.filename);
if (!fssize) {
struct _stat sb;
if (_stat(loset.filename, &sb) < 0)
perror_msg_and_die(loset.filename);
loset.size = sb.st_size;
}
if (strncmp(loset.filename, dospfx, sizeof (dospfx)-1)) {
rpath = xmalloc(strlen(loset.filename) + sizeof (dospfx));
strcpy(rpath, dospfx);
strcat(rpath, loset.filename);
loset.filename = rpath;
}
if (newfile) {
int fh = devcreate(loset.filename, loset.size);
if (fh == -1)
error_msg_and_die("cannot create file %s\n", loset.filename);
}
fd = open("/dev/loop", O_RDONLY);
if (fd < 0)
perror_msg_and_die("/dev/loop");
rv = ioctl(fd, LOOP_SET_FILE, &loset);
close(fd);
if (rv < 0)
perror_msg_and_die("ioctl");
return EXIT_SUCCESS;
}