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 strlcpy(p, path, sizeof(p));
41 pos = strrchr(p, '/');
42 if (pos == p || pos == NULL)
45 while (pos[-1] == '/')
50 if (stat(p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR)
53 if (create_path(p) != 0)
57 selinux_setfscreatecon(p, NULL, S_IFDIR|0755);
59 selinux_resetfscreatecon();
64 if (stat(p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR)
69 int delete_path(const char *path)
76 pos = strrchr(p, '/');
77 if (pos == p || pos == NULL)
82 pos = strrchr(p, '/');
84 /* don't remove the last one */
85 if ((pos == p) || (pos == NULL))
93 if (errno == ENOTEMPTY)
95 err("rmdir(%s) failed: %s", p, strerror(errno));
98 dbg("removed '%s'", p);
103 /* Reset permissions on the device node, before unlinking it to make sure,
104 * that permisions of possible hard links will be removed too.
106 int unlink_secure(const char *filename)
110 retval = chown(filename, 0, 0);
112 err("chown(%s, 0, 0) failed: %s", filename, strerror(errno));
114 retval = chmod(filename, 0000);
116 err("chmod(%s, 0000) failed: %s", filename, strerror(errno));
118 retval = unlink(filename);
123 err("unlink(%s) failed: %s", filename, strerror(errno));
128 int file_map(const char *filename, char **buf, size_t *bufsize)
133 fd = open(filename, O_RDONLY);
138 if (fstat(fd, &stats) < 0) {
143 *buf = mmap(NULL, stats.st_size, PROT_READ, MAP_SHARED, fd, 0);
144 if (*buf == MAP_FAILED) {
148 *bufsize = stats.st_size;
155 void file_unmap(void *buf, size_t bufsize)
157 munmap(buf, bufsize);
160 /* return number of chars until the next newline, skip escaped newline */
161 size_t buf_get_line(const char *buf, size_t buflen, size_t cur)
166 for (count = cur; count < buflen; count++) {
167 if (!escape && buf[count] == '\n')
170 if (buf[count] == '\\')