2 * Copyright (C) 2004-2008 Kay Sievers <kay.sievers@vrfy.org>
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.
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.
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/>.
32 int create_path(struct udev *udev, const char *path)
34 char p[UTIL_PATH_SIZE];
39 util_strlcpy(p, path, sizeof(p));
40 pos = strrchr(p, '/');
41 if (pos == p || pos == NULL)
44 while (pos[-1] == '/')
48 dbg(udev, "stat '%s'\n", p);
49 if (stat(p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR)
52 if (create_path(udev, p) != 0)
55 dbg(udev, "mkdir '%s'\n", p);
56 udev_selinux_setfscreatecon(udev, p, S_IFDIR|0755);
58 udev_selinux_resetfscreatecon(udev);
63 if (stat(p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR)
68 int delete_path(struct udev *udev, const char *path)
70 char p[UTIL_PATH_SIZE];
75 pos = strrchr(p, '/');
76 if (pos == p || pos == NULL)
81 pos = strrchr(p, '/');
83 /* don't remove the last one */
84 if ((pos == p) || (pos == NULL))
92 if (errno == ENOTEMPTY)
94 err(udev, "rmdir(%s) failed: %s\n", p, strerror(errno));
97 dbg(udev, "removed '%s'\n", p);
102 /* Reset permissions on the device node, before unlinking it to make sure,
103 * that permisions of possible hard links will be removed too.
105 int unlink_secure(struct udev *udev, const char *filename)
109 retval = chown(filename, 0, 0);
111 err(udev, "chown(%s, 0, 0) failed: %s\n", filename, strerror(errno));
113 retval = chmod(filename, 0000);
115 err(udev, "chmod(%s, 0000) failed: %s\n", filename, strerror(errno));
117 retval = unlink(filename);
122 err(udev, "unlink(%s) failed: %s\n", filename, strerror(errno));
127 int file_map(const char *filename, char **buf, size_t *bufsize)
132 fd = open(filename, O_RDONLY);
137 if (fstat(fd, &stats) < 0) {
142 *buf = mmap(NULL, stats.st_size, PROT_READ, MAP_SHARED, fd, 0);
143 if (*buf == MAP_FAILED) {
147 *bufsize = stats.st_size;
154 void file_unmap(void *buf, size_t bufsize)
156 munmap(buf, bufsize);
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)
165 for (count = cur; count < buflen; count++) {
166 if (!escape && buf[count] == '\n')
169 if (buf[count] == '\\')