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
)
39 strlcpy(p
, path
, sizeof(p
));
40 pos
= strrchr(p
, '/');
41 if (pos
== p
|| pos
== NULL
)
44 while (pos
[-1] == '/')
49 if (stat(p
, &stats
) == 0 && (stats
.st_mode
& S_IFMT
) == S_IFDIR
)
52 if (create_path (p
) != 0)
56 if (mkdir(p
, 0755) == 0)
59 if (stat(p
, &stats
) == 0 && (stats
.st_mode
& S_IFMT
) == S_IFDIR
)
64 int delete_path(const char *path
)
71 pos
= strrchr(p
, '/');
72 if (pos
== p
|| pos
== NULL
)
77 pos
= strrchr(p
, '/');
79 /* don't remove the last one */
80 if ((pos
== p
) || (pos
== NULL
))
88 if (errno
== ENOTEMPTY
)
90 err("rmdir(%s) failed: %s", p
, strerror(errno
));
93 dbg("removed '%s'", p
);
98 /* Reset permissions on the device node, before unlinking it to make sure,
99 * that permisions of possible hard links will be removed too.
101 int unlink_secure(const char *filename
)
105 retval
= chown(filename
, 0, 0);
107 err("chown(%s, 0, 0) failed: %s", filename
, strerror(errno
));
109 retval
= chmod(filename
, 0000);
111 err("chmod(%s, 0000) failed: %s", filename
, strerror(errno
));
113 retval
= unlink(filename
);
118 err("unlink(%s) failed: %s", filename
, strerror(errno
));
123 int file_map(const char *filename
, char **buf
, size_t *bufsize
)
128 fd
= open(filename
, O_RDONLY
);
133 if (fstat(fd
, &stats
) < 0) {
138 *buf
= mmap(NULL
, stats
.st_size
, PROT_READ
, MAP_SHARED
, fd
, 0);
139 if (*buf
== MAP_FAILED
) {
143 *bufsize
= stats
.st_size
;
150 void file_unmap(void *buf
, size_t bufsize
)
152 munmap(buf
, bufsize
);
155 /* return number of chars until the next newline, skip escaped newline */
156 size_t buf_get_line(const char *buf
, size_t buflen
, size_t cur
)
161 for (count
= cur
; count
< buflen
; count
++) {
162 if (!escape
&& buf
[count
] == '\n')
165 if (buf
[count
] == '\\')