chiark / gitweb /
update RELEASE-NOTES + TODO
[elogind.git] / udev_utils_file.c
1 /*
2  * udev_utils_file.c - files operations
3  *
4  * Copyright (C) 2004-2005 Kay Sievers <kay.sievers@vrfy.org>
5  *
6  *      This program is free software; you can redistribute it and/or modify it
7  *      under the terms of the GNU General Public License as published by the
8  *      Free Software Foundation version 2 of the License.
9  * 
10  *      This program is distributed in the hope that it will be useful, but
11  *      WITHOUT ANY WARRANTY; without even the implied warranty of
12  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  *      General Public License for more details.
14  * 
15  *      You should have received a copy of the GNU General Public License along
16  *      with this program; if not, write to the Free Software Foundation, Inc.,
17  *      675 Mass Ave, Cambridge, MA 02139, USA.
18  *
19  */
20
21
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <stddef.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <errno.h>
28 #include <ctype.h>
29 #include <dirent.h>
30 #include <sys/stat.h>
31 #include <sys/mman.h>
32
33 #include "udev_libc_wrapper.h"
34 #include "udev.h"
35 #include "logging.h"
36 #include "udev_utils.h"
37 #include "list.h"
38
39 int create_path(const char *path)
40 {
41         char p[PATH_SIZE];
42         char *pos;
43         struct stat stats;
44
45         strcpy (p, path);
46         pos = strrchr(p, '/');
47         if (pos == p || pos == NULL)
48                 return 0;
49
50         while (pos[-1] == '/')
51                 pos--;
52
53         pos[0] = '\0';
54
55         dbg("stat '%s'\n", p);
56         if (stat (p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR)
57                 return 0;
58
59         if (create_path (p) != 0)
60                 return -1;
61
62         dbg("mkdir '%s'\n", p);
63         return mkdir(p, 0755);
64 }
65
66 int delete_path(const char *path)
67 {
68         char p[PATH_SIZE];
69         char *pos;
70         int retval;
71
72         strcpy (p, path);
73         pos = strrchr(p, '/');
74         while (1) {
75                 *pos = '\0';
76                 pos = strrchr(p, '/');
77
78                 /* don't remove the last one */
79                 if ((pos == p) || (pos == NULL))
80                         break;
81
82                 /* remove if empty */
83                 retval = rmdir(p);
84                 if (errno == ENOENT)
85                         retval = 0;
86                 if (retval) {
87                         if (errno == ENOTEMPTY)
88                                 return 0;
89                         err("rmdir(%s) failed: %s", p, strerror(errno));
90                         break;
91                 }
92                 dbg("removed '%s'", p);
93         }
94         return 0;
95 }
96
97 /* Reset permissions on the device node, before unlinking it to make sure,
98  * that permisions of possible hard links will be removed too.
99  */
100 int unlink_secure(const char *filename)
101 {
102         int retval;
103
104         retval = chown(filename, 0, 0);
105         if (retval)
106                 err("chown(%s, 0, 0) failed: %s", filename, strerror(errno));
107
108         retval = chmod(filename, 0000);
109         if (retval)
110                 err("chmod(%s, 0000) failed: %s", filename, strerror(errno));
111
112         retval = unlink(filename);
113         if (errno == ENOENT)
114                 retval = 0;
115
116         if (retval)
117                 err("unlink(%s) failed: %s", filename, strerror(errno));
118
119         return retval;
120 }
121
122 int file_map(const char *filename, char **buf, size_t *bufsize)
123 {
124         struct stat stats;
125         int fd;
126
127         fd = open(filename, O_RDONLY);
128         if (fd < 0) {
129                 return -1;
130         }
131
132         if (fstat(fd, &stats) < 0) {
133                 close(fd);
134                 return -1;
135         }
136
137         *buf = mmap(NULL, stats.st_size, PROT_READ, MAP_SHARED, fd, 0);
138         if (*buf == MAP_FAILED) {
139                 close(fd);
140                 return -1;
141         }
142         *bufsize = stats.st_size;
143
144         close(fd);
145
146         return 0;
147 }
148
149 void file_unmap(void *buf, size_t bufsize)
150 {
151         munmap(buf, bufsize);
152 }
153
154 /* return number of chars until the next newline, skip escaped newline */
155 size_t buf_get_line(const char *buf, size_t buflen, size_t cur)
156 {
157         int escape = 0;
158         size_t count;
159
160         for (count = cur; count < buflen; count++) {
161                 if (!escape && buf[count] == '\n')
162                         break;
163
164                 if (buf[count] == '\\')
165                         escape = 1;
166                 else
167                         escape = 0;
168         }
169
170         return count - cur;
171 }