Blob


1 /*
2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 * Copyright (c) 2015 Theo de Raadt <deraadt@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/queue.h>
19 #include <sys/stat.h>
21 #include <errno.h>
22 #include <limits.h>
23 #include <libgen.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <dirent.h>
30 #include "got_error.h"
31 #include "got_path.h"
33 #ifndef MIN
34 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
35 #endif
37 int
38 got_path_is_absolute(const char *path)
39 {
40 return path[0] == '/';
41 }
43 char *
44 got_path_get_absolute(const char *relpath)
45 {
46 char cwd[PATH_MAX];
47 char *abspath;
49 if (getcwd(cwd, sizeof(cwd)) == NULL)
50 return NULL;
52 if (asprintf(&abspath, "%s/%s/", cwd, relpath) == -1)
53 return NULL;
55 return abspath;
56 }
58 char *
59 got_path_normalize(const char *path)
60 {
61 char *resolved;
63 resolved = realpath(path, NULL);
64 if (resolved == NULL)
65 return NULL;
67 if (!got_path_is_absolute(resolved)) {
68 char *abspath = got_path_get_absolute(resolved);
69 free(resolved);
70 resolved = abspath;
71 }
73 return resolved;
74 }
76 /* based on canonpath() from kern_pledge.c */
77 const struct got_error *
78 got_canonpath(const char *input, char *buf, size_t bufsize)
79 {
80 const char *p;
81 char *q;
83 /* can't canon relative paths, don't bother */
84 if (!got_path_is_absolute(input)) {
85 if (strlcpy(buf, input, bufsize) >= bufsize)
86 return got_error(GOT_ERR_NO_SPACE);
87 return NULL;
88 }
90 p = input;
91 q = buf;
92 while (*p && (q - buf < bufsize)) {
93 if (p[0] == '/' && (p[1] == '/' || p[1] == '\0')) {
94 p += 1;
96 } else if (p[0] == '/' && p[1] == '.' &&
97 (p[2] == '/' || p[2] == '\0')) {
98 p += 2;
100 } else if (p[0] == '/' && p[1] == '.' && p[2] == '.' &&
101 (p[3] == '/' || p[3] == '\0')) {
102 p += 3;
103 if (q != buf) /* "/../" at start of buf */
104 while (*--q != '/')
105 continue;
107 } else {
108 *q++ = *p++;
111 if ((*p == '\0') && (q - buf < bufsize)) {
112 *q = 0;
113 return NULL;
114 } else
115 return got_error(GOT_ERR_NO_SPACE);
118 const struct got_error *
119 got_path_skip_common_ancestor(char **child, const char *parent_abspath,
120 const char *abspath)
122 const struct got_error *err = NULL;
123 size_t len_parent, len, bufsize;
125 *child = NULL;
127 len_parent = strlen(parent_abspath);
128 len = strlen(abspath);
129 if (len_parent >= len)
130 return got_error(GOT_ERR_BAD_PATH);
131 if (strncmp(parent_abspath, abspath, len_parent) != 0)
132 return got_error(GOT_ERR_BAD_PATH);
133 if (!got_path_is_root_dir(parent_abspath) && abspath[len_parent] != '/')
134 return got_error(GOT_ERR_BAD_PATH);
135 while (abspath[len_parent] == '/')
136 abspath++;
137 bufsize = len - len_parent + 1;
138 *child = malloc(bufsize);
139 if (*child == NULL)
140 return got_error_prefix_errno("malloc");
141 if (strlcpy(*child, abspath + len_parent, bufsize) >= bufsize) {
142 err = got_error_prefix_errno("strlcpy");
143 free(*child);
144 *child = NULL;
145 return err;
147 return NULL;
150 int
151 got_path_is_root_dir(const char *path)
153 return (path[0] == '/' && path[1] == '\0');
156 int
157 got_path_is_current_dir(const char *path)
159 return (path[0] == '.' && path[1] == '\0');
162 int
163 got_path_is_child(const char *child, const char *parent, size_t parent_len)
165 if (parent_len == 0 || got_path_is_root_dir(parent))
166 return 1;
168 if (strncmp(parent, child, parent_len) != 0)
169 return 0;
170 if (child[parent_len] != '/')
171 return 0;
173 return 1;
176 int
177 got_path_cmp(const char *path1, const char *path2)
179 size_t len1 = strlen(path1);
180 size_t len2 = strlen(path2);
181 size_t min_len = MIN(len1, len2);
182 size_t i = 0;
184 /* Leading directory separators are insignificant. */
185 while (path1[0] == '/')
186 path1++;
187 while (path2[0] == '/')
188 path2++;
190 len1 = strlen(path1);
191 len2 = strlen(path2);
192 min_len = MIN(len1, len2);
194 /* Skip over common prefix. */
195 while (i < min_len && path1[i] == path2[i])
196 i++;
198 /* Are the paths exactly equal (besides path separators)? */
199 if (len1 == len2 && i >= min_len)
200 return 0;
202 /* Skip over redundant trailing path seperators. */
203 while (path1[i] == '/' && path1[i + 1] == '/')
204 path1++;
205 while (path2[i] == '/' && path2[i + 1] == '/')
206 path2++;
208 /* Trailing path separators are insignificant. */
209 if (path1[i] == '/' && path1[i + 1] == '\0' && path2[i] == '\0')
210 return 0;
211 if (path2[i] == '/' && path2[i + 1] == '\0' && path1[i] == '\0')
212 return 0;
214 /* Order children in subdirectories directly after their parents. */
215 if (path1[i] == '/' && path2[i] == '\0')
216 return 1;
217 if (path2[i] == '/' && path1[i] == '\0')
218 return -1;
219 if (path1[i] == '/' && path2[i] != '\0')
220 return -1;
221 if (path2[i] == '/' && path1[i] != '\0')
222 return 1;
224 /* Next character following the common prefix determines order. */
225 return (unsigned char)path1[i] < (unsigned char)path2[i] ? -1 : 1;
228 const struct got_error *
229 got_pathlist_insert(struct got_pathlist_entry **inserted,
230 struct got_pathlist_head *pathlist, const char *path, void *data)
232 struct got_pathlist_entry *new, *pe;
234 if (inserted)
235 *inserted = NULL;
237 new = malloc(sizeof(*new));
238 if (new == NULL)
239 return got_error_prefix_errno("malloc");
240 new->path = path;
241 new->data = data;
243 /*
244 * Many callers will provide paths in a somewhat sorted order while
245 * constructing a path list from inputs such as tree objects or
246 * dirents. Iterating backwards from the tail of the list should
247 * be more efficient than traversing through the entire list each
248 * time an element is inserted.
249 */
250 pe = TAILQ_LAST(pathlist, got_pathlist_head);
251 while (pe) {
252 int cmp = got_path_cmp(pe->path, path);
253 if (cmp == 0) {
254 free(new); /* duplicate */
255 return NULL;
256 } else if (cmp < 0) {
257 TAILQ_INSERT_AFTER(pathlist, pe, new, entry);
258 if (inserted)
259 *inserted = new;
260 return NULL;
262 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
265 TAILQ_INSERT_HEAD(pathlist, new, entry);
266 if (inserted)
267 *inserted = new;
268 return NULL;
271 void
272 got_pathlist_free(struct got_pathlist_head *pathlist)
274 struct got_pathlist_entry *pe;
276 while ((pe = TAILQ_FIRST(pathlist)) != NULL) {
277 TAILQ_REMOVE(pathlist, pe, entry);
278 free(pe);
282 static const struct got_error *
283 make_parent_dirs(const char *abspath)
285 const struct got_error *err = NULL;
286 char *parent;
288 err = got_path_dirname(&parent, abspath);
289 if (err)
290 return err;
292 if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
293 if (errno == ENOENT) {
294 err = make_parent_dirs(parent);
295 if (err)
296 goto done;
297 if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
298 err = got_error_prefix_errno2("mkdir", parent);
299 goto done;
301 } else
302 err = got_error_prefix_errno2("mkdir", parent);
304 done:
305 free(parent);
306 return err;
309 const struct got_error *
310 got_path_mkdir(const char *abspath)
312 const struct got_error *err = NULL;
314 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
315 if (errno == ENOENT) {
316 err = make_parent_dirs(abspath);
317 if (err)
318 goto done;
319 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1)
320 err = got_error_prefix_errno2("mkdir", abspath);
321 } else
322 err = got_error_prefix_errno2("mkdir", abspath);
325 done:
326 return err;
329 int
330 got_path_dir_is_empty(const char *dir)
332 DIR *d;
333 struct dirent *dent;
334 int empty = 1;
336 d = opendir(dir);
337 if (d == NULL)
338 return 1;
340 while ((dent = readdir(d)) != NULL) {
341 if (strcmp(dent->d_name, ".") == 0 ||
342 strcmp(dent->d_name, "..") == 0)
343 continue;
345 empty = 0;
346 break;
349 closedir(d);
350 return empty;
353 const struct got_error *
354 got_path_dirname(char **parent, const char *path)
356 char *p;
358 p = dirname(path);
359 if (p == NULL)
360 return got_error_prefix_errno2("dirname", path);
362 if (p[0] == '.' && p[1] == '\0')
363 return got_error(GOT_ERR_BAD_PATH);
365 *parent = strdup(p);
366 if (*parent == NULL)
367 return got_error_prefix_errno("strdup");
369 return NULL;
372 void
373 got_path_strip_trailing_slashes(char *path)
375 int x;
377 while (path[x = strlen(path) - 1] == '/')
378 path[x] = '\0';