Major coreutils update.
[oweals/busybox.git] / libbb / loop.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Utility routines.
4  *
5  * Copyright (C) 1999,2000,2001 by Erik Andersen <andersee@debian.org>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */
21
22 #include <stdio.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <sys/ioctl.h>
28 #include "libbb.h"
29 #include "loop.h" /* Pull in loop device support */
30
31 extern int del_loop(const char *device)
32 {
33         int fd;
34
35         if ((fd = open(device, O_RDONLY)) < 0) {
36                 bb_perror_msg("%s", device);
37                 return (FALSE);
38         }
39         if (ioctl(fd, LOOP_CLR_FD, 0) < 0) {
40                 bb_perror_msg("ioctl: LOOP_CLR_FD");
41                 return (FALSE);
42         }
43         close(fd);
44         return (TRUE);
45 }
46
47 extern int set_loop(const char *device, const char *file, int offset,
48                                         int *loopro)
49 {
50         struct loop_info loopinfo;
51         int fd, ffd, mode;
52
53         mode = *loopro ? O_RDONLY : O_RDWR;
54         if ((ffd = open(file, mode)) < 0 && !*loopro
55                 && (errno != EROFS || (ffd = open(file, mode = O_RDONLY)) < 0)) {
56                 bb_perror_msg("%s", file);
57                 return 1;
58         }
59         if ((fd = open(device, mode)) < 0) {
60                 close(ffd);
61                 bb_perror_msg("%s", device);
62                 return 1;
63         }
64         *loopro = (mode == O_RDONLY);
65
66         memset(&loopinfo, 0, sizeof(loopinfo));
67         safe_strncpy(loopinfo.lo_name, file, LO_NAME_SIZE);
68
69         loopinfo.lo_offset = offset;
70
71         loopinfo.lo_encrypt_key_size = 0;
72         if (ioctl(fd, LOOP_SET_FD, ffd) < 0) {
73                 bb_perror_msg("ioctl: LOOP_SET_FD");
74                 close(fd);
75                 close(ffd);
76                 return 1;
77         }
78         if (ioctl(fd, LOOP_SET_STATUS, &loopinfo) < 0) {
79                 (void) ioctl(fd, LOOP_CLR_FD, 0);
80                 bb_perror_msg("ioctl: LOOP_SET_STATUS");
81                 close(fd);
82                 close(ffd);
83                 return 1;
84         }
85         close(fd);
86         close(ffd);
87         return 0;
88 }
89
90 extern char *find_unused_loop_device(void)
91 {
92         char dev[20];
93         int i, fd;
94         struct stat statbuf;
95         struct loop_info loopinfo;
96
97         for (i = 0; i <= 7; i++) {
98                 sprintf(dev, LOOP_FORMAT, i);
99                 if (stat(dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) {
100                         if ((fd = open(dev, O_RDONLY)) >= 0) {
101                                 if (ioctl(fd, LOOP_GET_STATUS, &loopinfo) != 0) {
102                                         if (errno == ENXIO) {   /* probably free */
103                                                 close(fd);
104                                                 return strdup(dev);
105                                         }
106                                 }
107                                 close(fd);
108                         }
109                 }
110         }
111         return NULL;
112 }
113
114
115 /* END CODE */
116 /*
117 Local Variables:
118 c-file-style: "linux"
119 c-basic-offset: 4
120 tab-width: 4
121 End:
122 */