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, '/');
68 if (pos == p || pos == NULL)
73 pos = strrchr(p, '/');
75 /* don't remove the last one */
76 if ((pos == p) || (pos == NULL))
84 if (errno == ENOTEMPTY)
86 err("rmdir(%s) failed: %s", p, strerror(errno));
89 dbg("removed '%s'", p);
94 /* Reset permissions on the device node, before unlinking it to make sure,
95 * that permisions of possible hard links will be removed too.
97 int unlink_secure(const char *filename)
101 retval = chown(filename, 0, 0);
103 err("chown(%s, 0, 0) failed: %s", filename, strerror(errno));
105 retval = chmod(filename, 0000);
107 err("chmod(%s, 0000) failed: %s", filename, strerror(errno));
109 retval = unlink(filename);
114 err("unlink(%s) failed: %s", filename, strerror(errno));
119 int file_map(const char *filename, char **buf, size_t *bufsize)
124 fd = open(filename, O_RDONLY);
129 if (fstat(fd, &stats) < 0) {
134 *buf = mmap(NULL, stats.st_size, PROT_READ, MAP_SHARED, fd, 0);
135 if (*buf == MAP_FAILED) {
139 *bufsize = stats.st_size;
146 void file_unmap(void *buf, size_t bufsize)
148 munmap(buf, bufsize);
151 /* return number of chars until the next newline, skip escaped newline */
152 size_t buf_get_line(const char *buf, size_t buflen, size_t cur)
157 for (count = cur; count < buflen; count++) {
158 if (!escape && buf[count] == '\n')
161 if (buf[count] == '\\')