2 * Copyright (C) 2004-2005 Kay Sievers <kay.sievers@vrfy.org>
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.
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.
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.
33 int create_path(const char *path)
40 pos = strrchr(p, '/');
41 if (pos == p || pos == NULL)
44 while (pos[-1] == '/')
49 dbg("stat '%s'\n", p);
50 if (stat(p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR)
53 if (create_path (p) != 0)
56 dbg("mkdir '%s'\n", p);
57 return mkdir(p, 0755);
60 int delete_path(const char *path)
67 pos = strrchr(p, '/');
70 pos = strrchr(p, '/');
72 /* don't remove the last one */
73 if ((pos == p) || (pos == NULL))
81 if (errno == ENOTEMPTY)
83 err("rmdir(%s) failed: %s", p, strerror(errno));
86 dbg("removed '%s'", p);
91 /* Reset permissions on the device node, before unlinking it to make sure,
92 * that permisions of possible hard links will be removed too.
94 int unlink_secure(const char *filename)
98 retval = chown(filename, 0, 0);
100 err("chown(%s, 0, 0) failed: %s", filename, strerror(errno));
102 retval = chmod(filename, 0000);
104 err("chmod(%s, 0000) failed: %s", filename, strerror(errno));
106 retval = unlink(filename);
111 err("unlink(%s) failed: %s", filename, strerror(errno));
116 int file_map(const char *filename, char **buf, size_t *bufsize)
121 fd = open(filename, O_RDONLY);
126 if (fstat(fd, &stats) < 0) {
131 *buf = mmap(NULL, stats.st_size, PROT_READ, MAP_SHARED, fd, 0);
132 if (*buf == MAP_FAILED) {
136 *bufsize = stats.st_size;
143 void file_unmap(void *buf, size_t bufsize)
145 munmap(buf, bufsize);
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)
154 for (count = cur; count < buflen; count++) {
155 if (!escape && buf[count] == '\n')
158 if (buf[count] == '\\')