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