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 <fcntl.h>
24 #include <limits.h>
25 #include <libgen.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <dirent.h>
31 #include <paths.h>
33 #include "got_error.h"
34 #include "got_path.h"
36 #ifndef MIN
37 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
38 #endif
40 int
41 got_path_is_absolute(const char *path)
42 {
43 return path[0] == '/';
44 }
46 char *
47 got_path_get_absolute(const char *relpath)
48 {
49 char cwd[PATH_MAX];
50 char *abspath;
52 if (getcwd(cwd, sizeof(cwd)) == NULL)
53 return NULL;
55 if (asprintf(&abspath, "%s/%s/", cwd, relpath) == -1)
56 return NULL;
58 return abspath;
59 }
61 /* based on canonpath() from kern_pledge.c */
62 const struct got_error *
63 got_canonpath(const char *input, char *buf, size_t bufsize)
64 {
65 const char *p;
66 char *q;
68 /* can't canon relative paths, don't bother */
69 if (!got_path_is_absolute(input)) {
70 if (strlcpy(buf, input, bufsize) >= bufsize)
71 return got_error(GOT_ERR_NO_SPACE);
72 return NULL;
73 }
75 p = input;
76 q = buf;
77 while (*p && (q - buf < bufsize)) {
78 if (p[0] == '/' && (p[1] == '/' || p[1] == '\0')) {
79 p += 1;
81 } else if (p[0] == '/' && p[1] == '.' &&
82 (p[2] == '/' || p[2] == '\0')) {
83 p += 2;
85 } else if (p[0] == '/' && p[1] == '.' && p[2] == '.' &&
86 (p[3] == '/' || p[3] == '\0')) {
87 p += 3;
88 if (q != buf) /* "/../" at start of buf */
89 while (*--q != '/')
90 continue;
92 } else {
93 *q++ = *p++;
94 }
95 }
96 if ((*p == '\0') && (q - buf < bufsize)) {
97 *q = 0;
98 return NULL;
99 } else
100 return got_error(GOT_ERR_NO_SPACE);
103 const struct got_error *
104 got_path_skip_common_ancestor(char **child, const char *parent_abspath,
105 const char *abspath)
107 const struct got_error *err = NULL;
108 size_t len_parent, len, bufsize;
110 *child = NULL;
112 len_parent = strlen(parent_abspath);
113 len = strlen(abspath);
114 if (len_parent >= len)
115 return got_error_path(abspath, GOT_ERR_BAD_PATH);
116 if (strncmp(parent_abspath, abspath, len_parent) != 0)
117 return got_error_path(abspath, GOT_ERR_BAD_PATH);
118 if (!got_path_is_root_dir(parent_abspath) && abspath[len_parent] != '/')
119 return got_error_path(abspath, GOT_ERR_BAD_PATH);
120 while (abspath[len_parent] == '/')
121 abspath++;
122 bufsize = len - len_parent + 1;
123 *child = malloc(bufsize);
124 if (*child == NULL)
125 return got_error_from_errno("malloc");
126 if (strlcpy(*child, abspath + len_parent, bufsize) >= bufsize) {
127 err = got_error_from_errno("strlcpy");
128 free(*child);
129 *child = NULL;
130 return err;
132 return NULL;
135 int
136 got_path_is_root_dir(const char *path)
138 while (*path == '/')
139 path++;
140 return (*path == '\0');
143 int
144 got_path_is_current_dir(const char *path)
146 return (path[0] == '.' && path[1] == '\0');
149 int
150 got_path_is_child(const char *child, const char *parent, size_t parent_len)
152 if (parent_len == 0 || got_path_is_root_dir(parent))
153 return 1;
155 if (strncmp(parent, child, parent_len) != 0)
156 return 0;
157 if (child[parent_len] != '/')
158 return 0;
160 return 1;
163 int
164 got_path_cmp(const char *path1, const char *path2, size_t len1, size_t len2)
166 size_t min_len;
167 size_t i = 0;
169 /* Leading directory separators are insignificant. */
170 while (path1[0] == '/') {
171 path1++;
172 len1--;
174 while (path2[0] == '/') {
175 path2++;
176 len2--;
179 min_len = MIN(len1, len2);
181 /* Skip over common prefix. */
182 while (i < min_len && path1[i] == path2[i])
183 i++;
185 /* Are the paths exactly equal (besides path separators)? */
186 if (len1 == len2 && i >= min_len)
187 return 0;
189 /* Skip over redundant trailing path seperators. */
190 while (path1[i] == '/' && path1[i + 1] == '/')
191 path1++;
192 while (path2[i] == '/' && path2[i + 1] == '/')
193 path2++;
195 /* Trailing path separators are insignificant. */
196 if (path1[i] == '/' && path1[i + 1] == '\0' && path2[i] == '\0')
197 return 0;
198 if (path2[i] == '/' && path2[i + 1] == '\0' && path1[i] == '\0')
199 return 0;
201 /* Order children in subdirectories directly after their parents. */
202 if (path1[i] == '/' && path2[i] == '\0')
203 return 1;
204 if (path2[i] == '/' && path1[i] == '\0')
205 return -1;
206 if (path1[i] == '/' && path2[i] != '\0')
207 return -1;
208 if (path2[i] == '/' && path1[i] != '\0')
209 return 1;
211 /* Next character following the common prefix determines order. */
212 return (unsigned char)path1[i] < (unsigned char)path2[i] ? -1 : 1;
215 const struct got_error *
216 got_pathlist_insert(struct got_pathlist_entry **inserted,
217 struct got_pathlist_head *pathlist, const char *path, void *data)
219 struct got_pathlist_entry *new, *pe;
221 if (inserted)
222 *inserted = NULL;
224 new = malloc(sizeof(*new));
225 if (new == NULL)
226 return got_error_from_errno("malloc");
227 new->path = path;
228 new->path_len = strlen(path);
229 new->data = data;
231 /*
232 * Many callers will provide paths in a somewhat sorted order while
233 * constructing a path list from inputs such as tree objects or
234 * dirents. Iterating backwards from the tail of the list should
235 * be more efficient than traversing through the entire list each
236 * time an element is inserted.
237 */
238 pe = TAILQ_LAST(pathlist, got_pathlist_head);
239 while (pe) {
240 int cmp = got_path_cmp(pe->path, new->path,
241 pe->path_len, new->path_len);
242 if (cmp == 0) {
243 free(new); /* duplicate */
244 return NULL;
245 } else if (cmp < 0) {
246 TAILQ_INSERT_AFTER(pathlist, pe, new, entry);
247 if (inserted)
248 *inserted = new;
249 return NULL;
251 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
254 TAILQ_INSERT_HEAD(pathlist, new, entry);
255 if (inserted)
256 *inserted = new;
257 return NULL;
260 const struct got_error *
261 got_pathlist_append(struct got_pathlist_head *pathlist,
262 const char *path, void *data)
264 struct got_pathlist_entry *new;
266 new = malloc(sizeof(*new));
267 if (new == NULL)
268 return got_error_from_errno("malloc");
269 new->path = path;
270 new->path_len = strlen(path);
271 new->data = data;
272 TAILQ_INSERT_TAIL(pathlist, new, entry);
273 return NULL;
276 void
277 got_pathlist_free(struct got_pathlist_head *pathlist)
279 struct got_pathlist_entry *pe;
281 while ((pe = TAILQ_FIRST(pathlist)) != NULL) {
282 TAILQ_REMOVE(pathlist, pe, entry);
283 free(pe);
287 static const struct got_error *
288 make_parent_dirs(const char *abspath)
290 const struct got_error *err = NULL;
291 char *parent;
293 err = got_path_dirname(&parent, abspath);
294 if (err)
295 return err;
297 if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
298 if (errno == ENOENT) {
299 err = make_parent_dirs(parent);
300 if (err)
301 goto done;
302 if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
303 err = got_error_from_errno2("mkdir", parent);
304 goto done;
306 } else
307 err = got_error_from_errno2("mkdir", parent);
309 done:
310 free(parent);
311 return err;
314 const struct got_error *
315 got_path_mkdir(const char *abspath)
317 const struct got_error *err = NULL;
319 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
320 if (errno == ENOENT) {
321 err = make_parent_dirs(abspath);
322 if (err)
323 goto done;
324 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1)
325 err = got_error_from_errno2("mkdir", abspath);
326 } else
327 err = got_error_from_errno2("mkdir", abspath);
330 done:
331 return err;
334 int
335 got_path_dir_is_empty(const char *dir)
337 DIR *d;
338 struct dirent *dent;
339 int empty = 1;
341 d = opendir(dir);
342 if (d == NULL)
343 return 1;
345 while ((dent = readdir(d)) != NULL) {
346 if (strcmp(dent->d_name, ".") == 0 ||
347 strcmp(dent->d_name, "..") == 0)
348 continue;
350 empty = 0;
351 break;
354 closedir(d);
355 return empty;
358 const struct got_error *
359 got_path_dirname(char **parent, const char *path)
361 char *p;
363 p = dirname(path);
364 if (p == NULL)
365 return got_error_from_errno2("dirname", path);
367 if (p[0] == '.' && p[1] == '\0')
368 return got_error_path(path, GOT_ERR_BAD_PATH);
370 *parent = strdup(p);
371 if (*parent == NULL)
372 return got_error_from_errno("strdup");
374 return NULL;
377 const struct got_error *
378 got_path_dirent_type(int *type, const char *path_parent, struct dirent *dent)
380 const struct got_error *err = NULL;
381 char *path_child;
382 struct stat sb;
384 if (dent->d_type != DT_UNKNOWN) {
385 *type = dent->d_type;
386 return NULL;
389 *type = DT_UNKNOWN;
391 /*
392 * This is a fallback to accommodate filesystems which do not
393 * provide directory entry type information. DT_UNKNOWN directory
394 * entries occur on NFS mounts without "readdir plus" RPC.
395 */
397 if (asprintf(&path_child, "%s/%s", path_parent, dent->d_name) == -1)
398 return got_error_from_errno("asprintf");
400 if (lstat(path_child, &sb) == -1) {
401 err = got_error_from_errno2("lstat", path_child);
402 goto done;
405 if (S_ISFIFO(sb.st_mode))
406 *type = DT_FIFO;
407 else if (S_ISCHR(sb.st_mode))
408 *type = DT_CHR;
409 else if (S_ISDIR(sb.st_mode))
410 *type = DT_DIR;
411 else if (S_ISBLK(sb.st_mode))
412 *type = DT_BLK;
413 else if (S_ISLNK(sb.st_mode))
414 *type = DT_LNK;
415 else if (S_ISREG(sb.st_mode))
416 *type = DT_REG;
417 else if (S_ISSOCK(sb.st_mode))
418 *type = DT_SOCK;
419 done:
420 free(path_child);
421 return err;
424 const struct got_error *
425 got_path_basename(char **s, const char *path)
427 char *base;
429 base = basename(path);
430 if (base == NULL)
431 return got_error_from_errno2("basename", path);
433 *s = strdup(base);
434 if (*s == NULL)
435 return got_error_from_errno("strdup");
437 return NULL;
440 void
441 got_path_strip_trailing_slashes(char *path)
443 size_t x;
445 x = strlen(path);
446 while (x-- > 0 && path[x] == '/')
447 path[x] = '\0';
450 /* based on findprog() from usr.sbin/which/which.c */
451 const struct got_error *
452 got_path_find_prog(char **filename, const char *prog)
454 const struct got_error *err = NULL;
455 char *p;
456 int len;
457 struct stat sbuf;
458 char *path, *pathcpy;
460 *filename = NULL;
462 path = getenv("PATH");
463 if (path == NULL)
464 path = _PATH_DEFPATH;
466 /* Special case if prog contains '/' */
467 if (strchr(prog, '/')) {
468 if ((stat(prog, &sbuf) == 0) && S_ISREG(sbuf.st_mode) &&
469 access(prog, X_OK) == 0) {
470 *filename = strdup(prog);
471 if (*filename == NULL)
472 return got_error_from_errno("strdup");
474 return NULL;
477 if ((path = strdup(path)) == NULL)
478 return got_error_from_errno("strdup");
479 pathcpy = path;
481 while ((p = strsep(&pathcpy, ":")) != NULL) {
482 if (*p == '\0')
483 p = ".";
485 len = strlen(p);
486 while (len > 0 && p[len-1] == '/')
487 p[--len] = '\0'; /* strip trailing '/' */
489 if (asprintf(filename, "%s/%s", p, prog) == -1) {
490 err = got_error_from_errno("asprintf");
491 break;
493 if ((stat(*filename, &sbuf) == 0) && S_ISREG(sbuf.st_mode) &&
494 access(*filename, X_OK) == 0)
495 break;
496 free(*filename);
497 *filename = NULL;
498 continue;
500 free(path);
501 return err;
504 const struct got_error *
505 got_path_create_file(const char *path, const char *content)
507 const struct got_error *err = NULL;
508 int fd = -1;
510 fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
511 GOT_DEFAULT_FILE_MODE);
512 if (fd == -1) {
513 err = got_error_from_errno2("open", path);
514 goto done;
517 if (content) {
518 int len = dprintf(fd, "%s\n", content);
519 if (len != strlen(content) + 1) {
520 err = got_error_from_errno("dprintf");
521 goto done;
525 done:
526 if (fd != -1 && close(fd) == -1 && err == NULL)
527 err = got_error_from_errno("close");
528 return err;