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 /* Reset permissions on the device node, before unlinking it to make sure,
67 * that permisions of possible hard links will be removed too.
69 int unlink_secure(const char *filename)
73 retval = chown(filename, 0, 0);
75 dbg("chown(%s, 0, 0) failed with error '%s'", filename, strerror(errno));
77 retval = chmod(filename, 0000);
79 dbg("chmod(%s, 0000) failed with error '%s'", filename, strerror(errno));
81 retval = unlink(filename);
86 dbg("unlink(%s) failed with error '%s'", filename, strerror(errno));
91 int file_map(const char *filename, char **buf, size_t *bufsize)
96 fd = open(filename, O_RDONLY);
101 if (fstat(fd, &stats) < 0) {
106 *buf = mmap(NULL, stats.st_size, PROT_READ, MAP_SHARED, fd, 0);
107 if (*buf == MAP_FAILED) {
111 *bufsize = stats.st_size;
118 void file_unmap(void *buf, size_t bufsize)
120 munmap(buf, bufsize);
123 /* return number of chars until the next newline, skip escaped newline */
124 size_t buf_get_line(const char *buf, size_t buflen, size_t cur)
129 for (count = cur; count < buflen; count++) {
130 if (!escape && buf[count] == '\n')
133 if (buf[count] == '\\')