2 * udev_utils_file.c - files operations
4 * Copyright (C) 2004-2005 Kay Sievers <kay.sievers@vrfy.org>
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.
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.
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.
33 #include "udev_libc_wrapper.h"
36 #include "udev_utils.h"
39 int create_path(const char *path)
46 pos = strrchr(p, '/');
47 if (pos == p || pos == NULL)
50 while (pos[-1] == '/')
55 dbg("stat '%s'\n", p);
56 if (stat (p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR)
59 if (create_path (p) != 0)
62 dbg("mkdir '%s'\n", p);
63 return mkdir(p, 0755);
66 int delete_path(const char *path)
73 pos = strrchr(p, '/');
76 pos = strrchr(p, '/');
78 /* don't remove the last one */
79 if ((pos == p) || (pos == NULL))
87 if (errno == ENOTEMPTY)
89 err("rmdir(%s) failed: %s", p, strerror(errno));
92 dbg("removed '%s'", p);
97 /* Reset permissions on the device node, before unlinking it to make sure,
98 * that permisions of possible hard links will be removed too.
100 int unlink_secure(const char *filename)
104 retval = chown(filename, 0, 0);
106 err("chown(%s, 0, 0) failed: %s", filename, strerror(errno));
108 retval = chmod(filename, 0000);
110 err("chmod(%s, 0000) failed: %s", filename, strerror(errno));
112 retval = unlink(filename);
117 err("unlink(%s) failed: %s", filename, strerror(errno));
122 int file_map(const char *filename, char **buf, size_t *bufsize)
127 fd = open(filename, O_RDONLY);
132 if (fstat(fd, &stats) < 0) {
137 *buf = mmap(NULL, stats.st_size, PROT_READ, MAP_SHARED, fd, 0);
138 if (*buf == MAP_FAILED) {
142 *bufsize = stats.st_size;
149 void file_unmap(void *buf, size_t bufsize)
151 munmap(buf, bufsize);
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)
160 for (count = cur; count < buflen; count++) {
161 if (!escape && buf[count] == '\n')
164 if (buf[count] == '\\')