summaryrefslogtreecommitdiffhomepage
path: root/losetup.c
blob: 2908dbc88883a7341fe881cd34ef64b12e9d1aaa (plain)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
 * losetup.c - setup and control loop devices
 */


#include "internal.h"

const char	losetup_usage[] = "losetup\n"
"\n"
"\tlosetup loop_device				give info\n"
"\tlosetup -d loop_device				delete\n"
"\tlosetup [ -o offset ] loop_device file		setup\n";

/* from mount-2.6d */

/*
 * losetup.c - setup and control loop devices
 */

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <fcntl.h>
#include <unistd.h>
#include <getopt.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <linux/fs.h>
/* #include "loop.h" */

/*
 * include/linux/loop.h
 *
 * Written by Theodore Ts'o, 3/29/93.
 *
 * Copyright 1993 by Theodore Ts'o.  Redistribution of this file is
 * permitted under the GNU Public License.
 */

#define LO_NAME_SIZE	64
#define LO_KEY_SIZE	32
       
struct loop_info {
	int		lo_number;	/* ioctl r/o */
	dev_t		lo_device; 	/* ioctl r/o */
	unsigned long	lo_inode; 	/* ioctl r/o */
	dev_t		lo_rdevice; 	/* ioctl r/o */
	int		lo_offset;
	int		lo_encrypt_type;
	int		lo_encrypt_key_size; 	/* ioctl w/o */
	int		lo_flags;	/* ioctl r/o */
	char		lo_name[LO_NAME_SIZE];
	unsigned char	lo_encrypt_key[LO_KEY_SIZE]; /* ioctl w/o */
	unsigned long	lo_init[2];
	char		reserved[4];
};

/*
 * IOCTL commands --- we will commandeer 0x4C ('L')
 */

#define LOOP_SET_FD	0x4C00
#define LOOP_CLR_FD	0x4C01
#define LOOP_SET_STATUS	0x4C02
#define LOOP_GET_STATUS	0x4C03

/* #include "lomount.h" */

extern int set_loop (const char *, const char *, int, int *);
extern int del_loop (const char *);

static void show_loop(const char *device)
{
	struct	loop_info	loopinfo;
	int			fd;
	
	if ((fd = open(device, O_RDWR)) < 0) {
		perror(device);
		return;
	}
	if (ioctl(fd, LOOP_GET_STATUS, &loopinfo) < 0) {
		perror("Cannot get loop info");
		close(fd);
		return;
	}
	printf("%s: [%04x]:%ld (%s) offset %d\n",
	       device, (unsigned int)loopinfo.lo_device, loopinfo.lo_inode,
	       loopinfo.lo_name, loopinfo.lo_offset);
	close(fd);
}


int set_loop(const char *device, const char *file, int offset, int *loopro)
{
	struct loop_info loopinfo;
	int	fd, ffd, mode;
	
	mode = *loopro ? O_RDONLY : O_RDWR;
	if ((ffd = open (file, mode)) < 0 && !*loopro
	    && (errno != EROFS || (ffd = open (file, mode = O_RDONLY)) < 0)) {
	  perror (file);
	  return 1;
	}
	if ((fd = open (device, mode)) < 0) {
	  close(ffd);
	  perror (device);
	  return 1;
	}
	*loopro = (mode == O_RDONLY);

	memset(&loopinfo, 0, sizeof(loopinfo));
	strncpy(loopinfo.lo_name, file, LO_NAME_SIZE);
	loopinfo.lo_name[LO_NAME_SIZE-1] = 0;

	loopinfo.lo_offset = offset;

	loopinfo.lo_encrypt_key_size = 0;
	if (ioctl(fd, LOOP_SET_FD, ffd) < 0) {
		perror("ioctl: LOOP_SET_FD");
		exit(1);
	}
	if (ioctl(fd, LOOP_SET_STATUS, &loopinfo) < 0) {
		(void) ioctl(fd, LOOP_CLR_FD, 0);
		perror("ioctl: LOOP_SET_STATUS");
		exit(1);
	}
	close(fd);
	close(ffd);
	return 0;
}

int del_loop(const char *device)
{
	int fd;

	if ((fd = open(device, O_RDONLY)) < 0) {
		perror(device);
		exit(1);
	}
	if (ioctl(fd, LOOP_CLR_FD, 0) < 0) {
		perror("ioctl: LOOP_CLR_FD");
		exit(1);
	}
	close(fd);
	return(0);
}


static int losetup_usage_fn(void)
{
	fprintf(stderr, losetup_usage);
	exit(1);
}

int losetup_main(struct FileInfo * i, int argc, char * * argv)
{
	char *offset;
	int delete,off,c;
	int ro = 0;

	delete = off = 0;
	offset = NULL;
	while ((c = getopt(argc,argv,"do:")) != EOF) {
		switch (c) {
			case 'd':
				delete = 1;
				break;
			case 'o':
				offset = optarg;
				break;
			default:
				losetup_usage_fn();
		}
	}
	if (argc == 1) losetup_usage_fn();
	if ((delete && (argc != optind+1 || offset)) ||
	    (!delete && (argc < optind+1 || argc > optind+2)))
		losetup_usage_fn();
	if (argc == optind+1)
		if (delete)
			del_loop(argv[optind]);
		else
			show_loop(argv[optind]);
	else {
		if (offset && sscanf(offset,"%d",&off) != 1)
			losetup_usage_fn();
		set_loop(argv[optind],argv[optind+1],off,&ro);
	}
	return 0;
}