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