style(9) only; no functional changes
[got-portable.git] / lib / path.c
blobee7bab1688a8a41daaae1df52eb15e13b9bddfa4
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>
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.
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.
19 #include "got_compat.h"
21 #include <sys/queue.h>
22 #include <sys/stat.h>
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <limits.h>
27 #include <libgen.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <dirent.h>
33 #include <paths.h>
35 #include "got_error.h"
36 #include "got_path.h"
38 #ifndef MIN
39 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
40 #endif
42 int
43 got_path_is_absolute(const char *path)
45 return path[0] == '/';
48 /* based on canonpath() from kern_pledge.c */
49 const struct got_error *
50 got_canonpath(const char *input, char *buf, size_t bufsize)
52 const char *p;
53 char *q;
55 /* can't canon relative paths, don't bother */
56 if (!got_path_is_absolute(input)) {
57 if (strlcpy(buf, input, bufsize) >= bufsize)
58 return got_error(GOT_ERR_NO_SPACE);
59 return NULL;
62 p = input;
63 q = buf;
64 while (*p && (q - buf < bufsize)) {
65 if (p[0] == '/' && (p[1] == '/' || p[1] == '\0')) {
66 p += 1;
68 } else if (p[0] == '/' && p[1] == '.' &&
69 (p[2] == '/' || p[2] == '\0')) {
70 p += 2;
72 } else if (p[0] == '/' && p[1] == '.' && p[2] == '.' &&
73 (p[3] == '/' || p[3] == '\0')) {
74 p += 3;
75 if (q != buf) /* "/../" at start of buf */
76 while (*--q != '/')
77 continue;
79 } else {
80 *q++ = *p++;
83 if ((*p == '\0') && (q - buf < bufsize)) {
84 *q = 0;
85 return NULL;
86 } else
87 return got_error(GOT_ERR_NO_SPACE);
90 const struct got_error *
91 got_path_skip_common_ancestor(char **child, const char *parent_abspath,
92 const char *abspath)
94 const struct got_error *err = NULL;
95 size_t len_parent, len, bufsize;
97 *child = NULL;
99 len_parent = strlen(parent_abspath);
100 len = strlen(abspath);
101 if (len_parent >= len)
102 return got_error_path(abspath, GOT_ERR_BAD_PATH);
103 if (strncmp(parent_abspath, abspath, len_parent) != 0)
104 return got_error_path(abspath, GOT_ERR_BAD_PATH);
105 if (!got_path_is_root_dir(parent_abspath) && abspath[len_parent] != '/')
106 return got_error_path(abspath, GOT_ERR_BAD_PATH);
107 while (abspath[len_parent] == '/')
108 abspath++;
109 bufsize = len - len_parent + 1;
110 *child = malloc(bufsize);
111 if (*child == NULL)
112 return got_error_from_errno("malloc");
113 if (strlcpy(*child, abspath + len_parent, bufsize) >= bufsize) {
114 err = got_error_from_errno("strlcpy");
115 free(*child);
116 *child = NULL;
117 return err;
119 return NULL;
122 const struct got_error *
123 got_path_strip(char **out, const char *path, int n)
125 const char *p, *c;
127 p = path;
128 *out = NULL;
130 while (n > 0 && (c = strchr(p, '/')) != NULL) {
131 p = c + 1;
132 n--;
135 if (n > 0)
136 return got_error_fmt(GOT_ERR_BAD_PATH,
137 "can't strip %d path-components from %s", n, path);
139 if ((*out = strdup(p)) == NULL)
140 return got_error_from_errno("strdup");
141 return NULL;
145 got_path_is_root_dir(const char *path)
147 while (*path == '/')
148 path++;
149 return (*path == '\0');
153 got_path_is_child(const char *child, const char *parent, size_t parent_len)
155 if (parent_len == 0 || got_path_is_root_dir(parent))
156 return 1;
158 if (strncmp(parent, child, parent_len) != 0)
159 return 0;
160 if (child[parent_len] != '/')
161 return 0;
163 return 1;
167 got_path_cmp(const char *path1, const char *path2, size_t len1, size_t len2)
169 size_t min_len;
170 size_t i = 0;
172 /* Leading directory separators are insignificant. */
173 while (path1[0] == '/') {
174 path1++;
175 len1--;
177 while (path2[0] == '/') {
178 path2++;
179 len2--;
182 min_len = MIN(len1, len2);
184 /* Skip over common prefix. */
185 while (i < min_len && path1[i] == path2[i])
186 i++;
188 /* Are the paths exactly equal (besides path separators)? */
189 if (len1 == len2 && i >= min_len)
190 return 0;
192 /* Skip over redundant trailing path separators. */
193 while (path1[i] == '/' && path1[i + 1] == '/')
194 path1++;
195 while (path2[i] == '/' && path2[i + 1] == '/')
196 path2++;
198 /* Trailing path separators are insignificant. */
199 if (path1[i] == '/' && path1[i + 1] == '\0' && path2[i] == '\0')
200 return 0;
201 if (path2[i] == '/' && path2[i + 1] == '\0' && path1[i] == '\0')
202 return 0;
204 /* Order children in subdirectories directly after their parents. */
205 if (path1[i] == '/' && path2[i] == '\0')
206 return 1;
207 if (path2[i] == '/' && path1[i] == '\0')
208 return -1;
209 if (path1[i] == '/' && path2[i] != '\0')
210 return -1;
211 if (path2[i] == '/' && path1[i] != '\0')
212 return 1;
214 /* Next character following the common prefix determines order. */
215 return (unsigned char)path1[i] < (unsigned char)path2[i] ? -1 : 1;
219 got_pathlist_cmp(const struct got_pathlist_entry *p1,
220 const struct got_pathlist_entry *p2)
222 return got_path_cmp(p1->path, p2->path, p1->path_len, p2->path_len);
225 const struct got_error *
226 got_pathlist_insert(struct got_pathlist_entry **inserted,
227 struct got_pathlist_head *pathlist, const char *path, void *data)
229 struct got_pathlist_entry *new;
230 size_t path_len = strlen(path);
232 if (inserted)
233 *inserted = NULL;
234 new = malloc(sizeof(*new));
235 if (new == NULL)
236 return got_error_from_errno("malloc");
237 new->path = path;
238 new->path_len = path_len;
239 new->data = data;
240 if (RB_INSERT(got_pathlist_head, pathlist, new)) {
241 free(new);
242 new = NULL;
244 if (inserted)
245 *inserted = new;
246 return NULL;
249 void
250 got_pathlist_free(struct got_pathlist_head *pathlist, int freemask)
252 struct got_pathlist_entry *pe, *temp;
254 RB_FOREACH_SAFE(pe, got_pathlist_head, pathlist, temp) {
255 if (freemask & GOT_PATHLIST_FREE_PATH) {
256 free((char *)pe->path);
257 pe->path = NULL;
259 if (freemask & GOT_PATHLIST_FREE_DATA) {
260 free(pe->data);
261 pe->data = NULL;
263 RB_REMOVE(got_pathlist_head, pathlist, pe);
264 free(pe);
268 static const struct got_error *
269 make_parent_dirs(const char *abspath)
271 const struct got_error *err = NULL;
272 char *parent;
274 err = got_path_dirname(&parent, abspath);
275 if (err)
276 return err;
278 if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
279 if (errno == ENOENT) {
280 err = make_parent_dirs(parent);
281 if (err)
282 goto done;
283 if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
284 err = got_error_from_errno2("mkdir", parent);
285 goto done;
287 } else
288 err = got_error_from_errno2("mkdir", parent);
290 done:
291 free(parent);
292 return err;
295 const struct got_error *
296 got_path_mkdir(const char *abspath)
298 const struct got_error *err = NULL;
300 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
301 if (errno == ENOENT) {
302 err = make_parent_dirs(abspath);
303 if (err)
304 goto done;
305 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1)
306 err = got_error_from_errno2("mkdir", abspath);
307 } else
308 err = got_error_from_errno2("mkdir", abspath);
311 done:
312 return err;
316 got_path_dir_is_empty(const char *dir)
318 DIR *d;
319 struct dirent *dent;
320 int empty = 1;
322 d = opendir(dir);
323 if (d == NULL)
324 return 1;
326 while ((dent = readdir(d)) != NULL) {
327 if (strcmp(dent->d_name, ".") == 0 ||
328 strcmp(dent->d_name, "..") == 0)
329 continue;
331 empty = 0;
332 break;
335 closedir(d);
336 return empty;
339 const struct got_error *
340 got_path_dirname(char **parent, const char *path)
342 char buf[PATH_MAX];
343 char *p;
345 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
346 return got_error(GOT_ERR_NO_SPACE);
348 p = dirname(buf);
349 if (p == NULL)
350 return got_error_from_errno2("dirname", path);
352 if (p[0] == '.' && p[1] == '\0')
353 return got_error_path(path, GOT_ERR_BAD_PATH);
355 *parent = strdup(p);
356 if (*parent == NULL)
357 return got_error_from_errno("strdup");
359 return NULL;
362 const struct got_error *
363 got_path_dirent_type(int *type, const char *path_parent, struct dirent *dent)
365 const struct got_error *err = NULL;
366 char *path_child;
367 struct stat sb;
369 if (dent->d_type != DT_UNKNOWN) {
370 *type = dent->d_type;
371 return NULL;
374 *type = DT_UNKNOWN;
377 * This is a fallback to accommodate filesystems which do not
378 * provide directory entry type information. DT_UNKNOWN directory
379 * entries occur on NFS mounts without "readdir plus" RPC.
382 if (asprintf(&path_child, "%s/%s", path_parent, dent->d_name) == -1)
383 return got_error_from_errno("asprintf");
385 if (lstat(path_child, &sb) == -1) {
386 err = got_error_from_errno2("lstat", path_child);
387 goto done;
390 if (S_ISFIFO(sb.st_mode))
391 *type = DT_FIFO;
392 else if (S_ISCHR(sb.st_mode))
393 *type = DT_CHR;
394 else if (S_ISDIR(sb.st_mode))
395 *type = DT_DIR;
396 else if (S_ISBLK(sb.st_mode))
397 *type = DT_BLK;
398 else if (S_ISLNK(sb.st_mode))
399 *type = DT_LNK;
400 else if (S_ISREG(sb.st_mode))
401 *type = DT_REG;
402 else if (S_ISSOCK(sb.st_mode))
403 *type = DT_SOCK;
404 done:
405 free(path_child);
406 return err;
409 const struct got_error *
410 got_path_basename(char **s, const char *path)
412 char buf[PATH_MAX];
413 char *base;
415 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
416 return got_error(GOT_ERR_NO_SPACE);
418 base = basename(buf);
419 if (base == NULL)
420 return got_error_from_errno2("basename", path);
422 *s = strdup(base);
423 if (*s == NULL)
424 return got_error_from_errno("strdup");
426 return NULL;
429 void
430 got_path_strip_trailing_slashes(char *path)
432 size_t x;
434 x = strlen(path);
435 while (x-- > 0 && path[x] == '/')
436 path[x] = '\0';
439 /* based on findprog() from usr.bin/which/which.c */
440 const struct got_error *
441 got_path_find_prog(char **filename, const char *prog)
443 const struct got_error *err = NULL;
444 const char *path;
445 char *p;
446 int len;
447 struct stat sbuf;
448 char *pathcpy, *dup = NULL;
450 *filename = NULL;
452 path = getenv("PATH");
453 if (path == NULL)
454 path = _PATH_DEFPATH;
456 /* Special case if prog contains '/' */
457 if (strchr(prog, '/')) {
458 if ((stat(prog, &sbuf) == 0) && S_ISREG(sbuf.st_mode) &&
459 access(prog, X_OK) == 0) {
460 *filename = strdup(prog);
461 if (*filename == NULL)
462 return got_error_from_errno("strdup");
464 return NULL;
467 if ((dup = strdup(path)) == NULL)
468 return got_error_from_errno("strdup");
469 pathcpy = dup;
471 while ((p = strsep(&pathcpy, ":")) != NULL) {
472 const char *d;
474 len = strlen(p);
475 while (len > 0 && p[len-1] == '/')
476 p[--len] = '\0'; /* strip trailing '/' */
478 d = p;
479 if (*d == '\0')
480 d = ".";
482 if (asprintf(filename, "%s/%s", d, prog) == -1) {
483 err = got_error_from_errno("asprintf");
484 break;
486 if ((stat(*filename, &sbuf) == 0) && S_ISREG(sbuf.st_mode) &&
487 access(*filename, X_OK) == 0)
488 break;
489 free(*filename);
490 *filename = NULL;
492 free(dup);
493 return err;
496 const struct got_error *
497 got_path_create_file(const char *path, const char *content)
499 const struct got_error *err = NULL;
500 int fd = -1;
502 fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
503 GOT_DEFAULT_FILE_MODE);
504 if (fd == -1) {
505 err = got_error_from_errno2("open", path);
506 goto done;
509 if (content) {
510 int len = dprintf(fd, "%s\n", content);
511 if (len != strlen(content) + 1) {
512 err = got_error_from_errno("dprintf");
513 goto done;
517 done:
518 if (fd != -1 && close(fd) == -1 && err == NULL)
519 err = got_error_from_errno("close");
520 return err;
523 const struct got_error *
524 got_path_move_file(const char *oldpath, const char *newpath)
526 const struct got_error *err;
528 if (rename(oldpath, newpath) != -1)
529 return NULL;
531 if (errno != ENOENT)
532 return got_error_from_errno3("rename", oldpath, newpath);
534 err = make_parent_dirs(newpath);
535 if (err)
536 return err;
538 if (rename(oldpath, newpath) == -1)
539 return got_error_from_errno3("rename", oldpath, newpath);
541 return NULL;
544 RB_GENERATE(got_pathlist_head, got_pathlist_entry, entry, got_pathlist_cmp);