Blob


1 /*
2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 * Copyright (c) 2015 Theo de Raadt <deraadt@openbsd.org>
4 * Copyright (c) 1997 Todd C. Miller <millert@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
19 #include <sys/queue.h>
20 #include <sys/stat.h>
22 #include <errno.h>
23 #include <limits.h>
24 #include <libgen.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <dirent.h>
30 #include <paths.h>
32 #include "got_error.h"
33 #include "got_path.h"
35 #ifndef MIN
36 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
37 #endif
39 int
40 got_path_is_absolute(const char *path)
41 {
42 return path[0] == '/';
43 }
45 char *
46 got_path_get_absolute(const char *relpath)
47 {
48 char cwd[PATH_MAX];
49 char *abspath;
51 if (getcwd(cwd, sizeof(cwd)) == NULL)
52 return NULL;
54 if (asprintf(&abspath, "%s/%s/", cwd, relpath) == -1)
55 return NULL;
57 return abspath;
58 }
60 char *
61 got_path_normalize(const char *path)
62 {
63 char *resolved;
65 resolved = realpath(path, NULL);
66 if (resolved == NULL)
67 return NULL;
69 if (!got_path_is_absolute(resolved)) {
70 char *abspath = got_path_get_absolute(resolved);
71 free(resolved);
72 resolved = abspath;
73 }
75 return resolved;
76 }
78 /* based on canonpath() from kern_pledge.c */
79 const struct got_error *
80 got_canonpath(const char *input, char *buf, size_t bufsize)
81 {
82 const char *p;
83 char *q;
85 /* can't canon relative paths, don't bother */
86 if (!got_path_is_absolute(input)) {
87 if (strlcpy(buf, input, bufsize) >= bufsize)
88 return got_error(GOT_ERR_NO_SPACE);
89 return NULL;
90 }
92 p = input;
93 q = buf;
94 while (*p && (q - buf < bufsize)) {
95 if (p[0] == '/' && (p[1] == '/' || p[1] == '\0')) {
96 p += 1;
98 } else if (p[0] == '/' && p[1] == '.' &&
99 (p[2] == '/' || p[2] == '\0')) {
100 p += 2;
102 } else if (p[0] == '/' && p[1] == '.' && p[2] == '.' &&
103 (p[3] == '/' || p[3] == '\0')) {
104 p += 3;
105 if (q != buf) /* "/../" at start of buf */
106 while (*--q != '/')
107 continue;
109 } else {
110 *q++ = *p++;
113 if ((*p == '\0') && (q - buf < bufsize)) {
114 *q = 0;
115 return NULL;
116 } else
117 return got_error(GOT_ERR_NO_SPACE);
120 const struct got_error *
121 got_path_skip_common_ancestor(char **child, const char *parent_abspath,
122 const char *abspath)
124 const struct got_error *err = NULL;
125 size_t len_parent, len, bufsize;
127 *child = NULL;
129 len_parent = strlen(parent_abspath);
130 len = strlen(abspath);
131 if (len_parent >= len)
132 return got_error(GOT_ERR_BAD_PATH);
133 if (strncmp(parent_abspath, abspath, len_parent) != 0)
134 return got_error(GOT_ERR_BAD_PATH);
135 if (!got_path_is_root_dir(parent_abspath) && abspath[len_parent] != '/')
136 return got_error(GOT_ERR_BAD_PATH);
137 while (abspath[len_parent] == '/')
138 abspath++;
139 bufsize = len - len_parent + 1;
140 *child = malloc(bufsize);
141 if (*child == NULL)
142 return got_error_from_errno("malloc");
143 if (strlcpy(*child, abspath + len_parent, bufsize) >= bufsize) {
144 err = got_error_from_errno("strlcpy");
145 free(*child);
146 *child = NULL;
147 return err;
149 return NULL;
152 int
153 got_path_is_root_dir(const char *path)
155 return (path[0] == '/' && path[1] == '\0');
158 int
159 got_path_is_current_dir(const char *path)
161 return (path[0] == '.' && path[1] == '\0');
164 int
165 got_path_is_child(const char *child, const char *parent, size_t parent_len)
167 if (parent_len == 0 || got_path_is_root_dir(parent))
168 return 1;
170 if (strncmp(parent, child, parent_len) != 0)
171 return 0;
172 if (child[parent_len] != '/')
173 return 0;
175 return 1;
178 int
179 got_path_cmp(const char *path1, const char *path2)
181 size_t len1 = strlen(path1);
182 size_t len2 = strlen(path2);
183 size_t min_len = MIN(len1, len2);
184 size_t i = 0;
186 /* Leading directory separators are insignificant. */
187 while (path1[0] == '/')
188 path1++;
189 while (path2[0] == '/')
190 path2++;
192 len1 = strlen(path1);
193 len2 = strlen(path2);
194 min_len = MIN(len1, len2);
196 /* Skip over common prefix. */
197 while (i < min_len && path1[i] == path2[i])
198 i++;
200 /* Are the paths exactly equal (besides path separators)? */
201 if (len1 == len2 && i >= min_len)
202 return 0;
204 /* Skip over redundant trailing path seperators. */
205 while (path1[i] == '/' && path1[i + 1] == '/')
206 path1++;
207 while (path2[i] == '/' && path2[i + 1] == '/')
208 path2++;
210 /* Trailing path separators are insignificant. */
211 if (path1[i] == '/' && path1[i + 1] == '\0' && path2[i] == '\0')
212 return 0;
213 if (path2[i] == '/' && path2[i + 1] == '\0' && path1[i] == '\0')
214 return 0;
216 /* Order children in subdirectories directly after their parents. */
217 if (path1[i] == '/' && path2[i] == '\0')
218 return 1;
219 if (path2[i] == '/' && path1[i] == '\0')
220 return -1;
221 if (path1[i] == '/' && path2[i] != '\0')
222 return -1;
223 if (path2[i] == '/' && path1[i] != '\0')
224 return 1;
226 /* Next character following the common prefix determines order. */
227 return (unsigned char)path1[i] < (unsigned char)path2[i] ? -1 : 1;
230 const struct got_error *
231 got_pathlist_insert(struct got_pathlist_entry **inserted,
232 struct got_pathlist_head *pathlist, const char *path, void *data)
234 struct got_pathlist_entry *new, *pe;
236 if (inserted)
237 *inserted = NULL;
239 new = malloc(sizeof(*new));
240 if (new == NULL)
241 return got_error_from_errno("malloc");
242 new->path = path;
243 new->data = data;
245 /*
246 * Many callers will provide paths in a somewhat sorted order while
247 * constructing a path list from inputs such as tree objects or
248 * dirents. Iterating backwards from the tail of the list should
249 * be more efficient than traversing through the entire list each
250 * time an element is inserted.
251 */
252 pe = TAILQ_LAST(pathlist, got_pathlist_head);
253 while (pe) {
254 int cmp = got_path_cmp(pe->path, path);
255 if (cmp == 0) {
256 free(new); /* duplicate */
257 return NULL;
258 } else if (cmp < 0) {
259 TAILQ_INSERT_AFTER(pathlist, pe, new, entry);
260 if (inserted)
261 *inserted = new;
262 return NULL;
264 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
267 TAILQ_INSERT_HEAD(pathlist, new, entry);
268 if (inserted)
269 *inserted = new;
270 return NULL;
273 void
274 got_pathlist_free(struct got_pathlist_head *pathlist)
276 struct got_pathlist_entry *pe;
278 while ((pe = TAILQ_FIRST(pathlist)) != NULL) {
279 TAILQ_REMOVE(pathlist, pe, entry);
280 free(pe);
284 static const struct got_error *
285 make_parent_dirs(const char *abspath)
287 const struct got_error *err = NULL;
288 char *parent;
290 err = got_path_dirname(&parent, abspath);
291 if (err)
292 return err;
294 if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
295 if (errno == ENOENT) {
296 err = make_parent_dirs(parent);
297 if (err)
298 goto done;
299 if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
300 err = got_error_from_errno2("mkdir", parent);
301 goto done;
303 } else
304 err = got_error_from_errno2("mkdir", parent);
306 done:
307 free(parent);
308 return err;
311 const struct got_error *
312 got_path_mkdir(const char *abspath)
314 const struct got_error *err = NULL;
316 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
317 if (errno == ENOENT) {
318 err = make_parent_dirs(abspath);
319 if (err)
320 goto done;
321 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1)
322 err = got_error_from_errno2("mkdir", abspath);
323 } else
324 err = got_error_from_errno2("mkdir", abspath);
327 done:
328 return err;
331 int
332 got_path_dir_is_empty(const char *dir)
334 DIR *d;
335 struct dirent *dent;
336 int empty = 1;
338 d = opendir(dir);
339 if (d == NULL)
340 return 1;
342 while ((dent = readdir(d)) != NULL) {
343 if (strcmp(dent->d_name, ".") == 0 ||
344 strcmp(dent->d_name, "..") == 0)
345 continue;
347 empty = 0;
348 break;
351 closedir(d);
352 return empty;
355 const struct got_error *
356 got_path_dirname(char **parent, const char *path)
358 char *p;
360 p = dirname(path);
361 if (p == NULL)
362 return got_error_from_errno2("dirname", path);
364 if (p[0] == '.' && p[1] == '\0')
365 return got_error(GOT_ERR_BAD_PATH);
367 *parent = strdup(p);
368 if (*parent == NULL)
369 return got_error_from_errno("strdup");
371 return NULL;
374 void
375 got_path_strip_trailing_slashes(char *path)
377 int x;
379 while (path[x = strlen(path) - 1] == '/')
380 path[x] = '\0';
383 /* based on findprog() from usr.sbin/which/which.c */
384 const struct got_error *
385 got_path_find_prog(char **filename, const char *prog)
387 char *p;
388 int len;
389 struct stat sbuf;
390 char *path, *pathcpy;
392 *filename = NULL;
394 path = getenv("PATH");
395 if (path == NULL)
396 path = _PATH_DEFPATH;
398 /* Special case if prog contains '/' */
399 if (strchr(prog, '/')) {
400 if ((stat(prog, &sbuf) == 0) && S_ISREG(sbuf.st_mode) &&
401 access(prog, X_OK) == 0) {
402 *filename = strdup(prog);
403 if (*filename == NULL)
404 return got_error_from_errno("strdup");
406 return NULL;
409 if ((path = strdup(path)) == NULL)
410 return got_error_from_errno("strdup");
411 pathcpy = path;
413 while ((p = strsep(&pathcpy, ":")) != NULL) {
414 if (*p == '\0')
415 p = ".";
417 len = strlen(p);
418 while (len > 0 && p[len-1] == '/')
419 p[--len] = '\0'; /* strip trailing '/' */
421 if (asprintf(filename, "%s/%s", p, prog) == -1) {
422 free(path);
423 return got_error_from_errno("asprintf");
425 if ((stat(*filename, &sbuf) == 0) && S_ISREG(sbuf.st_mode) &&
426 access(*filename, X_OK) == 0) {
427 free(path);
428 return NULL;
430 free(*filename);
431 *filename = NULL;
432 continue;
434 free(path);
435 return NULL;