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 /* based on canonpath() from kern_pledge.c */
47 const struct got_error *
48 got_canonpath(const char *input, char *buf, size_t bufsize)
49 {
50 const char *p;
51 char *q;
53 /* can't canon relative paths, don't bother */
54 if (!got_path_is_absolute(input)) {
55 if (strlcpy(buf, input, bufsize) >= bufsize)
56 return got_error(GOT_ERR_NO_SPACE);
57 return NULL;
58 }
60 p = input;
61 q = buf;
62 while (*p && (q - buf < bufsize)) {
63 if (p[0] == '/' && (p[1] == '/' || p[1] == '\0')) {
64 p += 1;
66 } else if (p[0] == '/' && p[1] == '.' &&
67 (p[2] == '/' || p[2] == '\0')) {
68 p += 2;
70 } else if (p[0] == '/' && p[1] == '.' && p[2] == '.' &&
71 (p[3] == '/' || p[3] == '\0')) {
72 p += 3;
73 if (q != buf) /* "/../" at start of buf */
74 while (*--q != '/')
75 continue;
77 } else {
78 *q++ = *p++;
79 }
80 }
81 if ((*p == '\0') && (q - buf < bufsize)) {
82 *q = 0;
83 return NULL;
84 } else
85 return got_error(GOT_ERR_NO_SPACE);
86 }
88 const struct got_error *
89 got_path_skip_common_ancestor(char **child, const char *parent_abspath,
90 const char *abspath)
91 {
92 const struct got_error *err = NULL;
93 size_t len_parent, len, bufsize;
95 *child = NULL;
97 len_parent = strlen(parent_abspath);
98 len = strlen(abspath);
99 if (len_parent >= len)
100 return got_error_path(abspath, GOT_ERR_BAD_PATH);
101 if (strncmp(parent_abspath, abspath, len_parent) != 0)
102 return got_error_path(abspath, GOT_ERR_BAD_PATH);
103 if (!got_path_is_root_dir(parent_abspath) && abspath[len_parent] != '/')
104 return got_error_path(abspath, GOT_ERR_BAD_PATH);
105 while (abspath[len_parent] == '/')
106 abspath++;
107 bufsize = len - len_parent + 1;
108 *child = malloc(bufsize);
109 if (*child == NULL)
110 return got_error_from_errno("malloc");
111 if (strlcpy(*child, abspath + len_parent, bufsize) >= bufsize) {
112 err = got_error_from_errno("strlcpy");
113 free(*child);
114 *child = NULL;
115 return err;
117 return NULL;
120 int
121 got_path_is_root_dir(const char *path)
123 while (*path == '/')
124 path++;
125 return (*path == '\0');
128 int
129 got_path_is_current_dir(const char *path)
131 return (path[0] == '.' && path[1] == '\0');
134 int
135 got_path_is_child(const char *child, const char *parent, size_t parent_len)
137 if (parent_len == 0 || got_path_is_root_dir(parent))
138 return 1;
140 if (strncmp(parent, child, parent_len) != 0)
141 return 0;
142 if (child[parent_len] != '/')
143 return 0;
145 return 1;
148 int
149 got_path_cmp(const char *path1, const char *path2, size_t len1, size_t len2)
151 size_t min_len;
152 size_t i = 0;
154 /* Leading directory separators are insignificant. */
155 while (path1[0] == '/') {
156 path1++;
157 len1--;
159 while (path2[0] == '/') {
160 path2++;
161 len2--;
164 min_len = MIN(len1, len2);
166 /* Skip over common prefix. */
167 while (i < min_len && path1[i] == path2[i])
168 i++;
170 /* Are the paths exactly equal (besides path separators)? */
171 if (len1 == len2 && i >= min_len)
172 return 0;
174 /* Skip over redundant trailing path seperators. */
175 while (path1[i] == '/' && path1[i + 1] == '/')
176 path1++;
177 while (path2[i] == '/' && path2[i + 1] == '/')
178 path2++;
180 /* Trailing path separators are insignificant. */
181 if (path1[i] == '/' && path1[i + 1] == '\0' && path2[i] == '\0')
182 return 0;
183 if (path2[i] == '/' && path2[i + 1] == '\0' && path1[i] == '\0')
184 return 0;
186 /* Order children in subdirectories directly after their parents. */
187 if (path1[i] == '/' && path2[i] == '\0')
188 return 1;
189 if (path2[i] == '/' && path1[i] == '\0')
190 return -1;
191 if (path1[i] == '/' && path2[i] != '\0')
192 return -1;
193 if (path2[i] == '/' && path1[i] != '\0')
194 return 1;
196 /* Next character following the common prefix determines order. */
197 return (unsigned char)path1[i] < (unsigned char)path2[i] ? -1 : 1;
200 const struct got_error *
201 got_pathlist_insert(struct got_pathlist_entry **inserted,
202 struct got_pathlist_head *pathlist, const char *path, void *data)
204 struct got_pathlist_entry *new, *pe;
206 if (inserted)
207 *inserted = NULL;
209 new = malloc(sizeof(*new));
210 if (new == NULL)
211 return got_error_from_errno("malloc");
212 new->path = path;
213 new->path_len = strlen(path);
214 new->data = data;
216 /*
217 * Many callers will provide paths in a somewhat sorted order while
218 * constructing a path list from inputs such as tree objects or
219 * dirents. Iterating backwards from the tail of the list should
220 * be more efficient than traversing through the entire list each
221 * time an element is inserted.
222 */
223 pe = TAILQ_LAST(pathlist, got_pathlist_head);
224 while (pe) {
225 int cmp = got_path_cmp(pe->path, new->path,
226 pe->path_len, new->path_len);
227 if (cmp == 0) {
228 free(new); /* duplicate */
229 return NULL;
230 } else if (cmp < 0) {
231 TAILQ_INSERT_AFTER(pathlist, pe, new, entry);
232 if (inserted)
233 *inserted = new;
234 return NULL;
236 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
239 TAILQ_INSERT_HEAD(pathlist, new, entry);
240 if (inserted)
241 *inserted = new;
242 return NULL;
245 const struct got_error *
246 got_pathlist_append(struct got_pathlist_head *pathlist,
247 const char *path, void *data)
249 struct got_pathlist_entry *new;
251 new = malloc(sizeof(*new));
252 if (new == NULL)
253 return got_error_from_errno("malloc");
254 new->path = path;
255 new->path_len = strlen(path);
256 new->data = data;
257 TAILQ_INSERT_TAIL(pathlist, new, entry);
258 return NULL;
261 void
262 got_pathlist_free(struct got_pathlist_head *pathlist)
264 struct got_pathlist_entry *pe;
266 while ((pe = TAILQ_FIRST(pathlist)) != NULL) {
267 TAILQ_REMOVE(pathlist, pe, entry);
268 free(pe);
272 static const struct got_error *
273 make_parent_dirs(const char *abspath)
275 const struct got_error *err = NULL;
276 char *parent;
278 err = got_path_dirname(&parent, abspath);
279 if (err)
280 return err;
282 if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
283 if (errno == ENOENT) {
284 err = make_parent_dirs(parent);
285 if (err)
286 goto done;
287 if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
288 err = got_error_from_errno2("mkdir", parent);
289 goto done;
291 } else
292 err = got_error_from_errno2("mkdir", parent);
294 done:
295 free(parent);
296 return err;
299 const struct got_error *
300 got_path_mkdir(const char *abspath)
302 const struct got_error *err = NULL;
304 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
305 if (errno == ENOENT) {
306 err = make_parent_dirs(abspath);
307 if (err)
308 goto done;
309 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1)
310 err = got_error_from_errno2("mkdir", abspath);
311 } else
312 err = got_error_from_errno2("mkdir", abspath);
315 done:
316 return err;
319 int
320 got_path_dir_is_empty(const char *dir)
322 DIR *d;
323 struct dirent *dent;
324 int empty = 1;
326 d = opendir(dir);
327 if (d == NULL)
328 return 1;
330 while ((dent = readdir(d)) != NULL) {
331 if (strcmp(dent->d_name, ".") == 0 ||
332 strcmp(dent->d_name, "..") == 0)
333 continue;
335 empty = 0;
336 break;
339 closedir(d);
340 return empty;
343 const struct got_error *
344 got_path_dirname(char **parent, const char *path)
346 char buf[PATH_MAX];
347 char *p;
349 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
350 return got_error(GOT_ERR_NO_SPACE);
352 p = dirname(buf);
353 if (p == NULL)
354 return got_error_from_errno2("dirname", path);
356 if (p[0] == '.' && p[1] == '\0')
357 return got_error_path(path, GOT_ERR_BAD_PATH);
359 *parent = strdup(p);
360 if (*parent == NULL)
361 return got_error_from_errno("strdup");
363 return NULL;
366 const struct got_error *
367 got_path_dirent_type(int *type, const char *path_parent, struct dirent *dent)
369 const struct got_error *err = NULL;
370 char *path_child;
371 struct stat sb;
373 if (dent->d_type != DT_UNKNOWN) {
374 *type = dent->d_type;
375 return NULL;
378 *type = DT_UNKNOWN;
380 /*
381 * This is a fallback to accommodate filesystems which do not
382 * provide directory entry type information. DT_UNKNOWN directory
383 * entries occur on NFS mounts without "readdir plus" RPC.
384 */
386 if (asprintf(&path_child, "%s/%s", path_parent, dent->d_name) == -1)
387 return got_error_from_errno("asprintf");
389 if (lstat(path_child, &sb) == -1) {
390 err = got_error_from_errno2("lstat", path_child);
391 goto done;
394 if (S_ISFIFO(sb.st_mode))
395 *type = DT_FIFO;
396 else if (S_ISCHR(sb.st_mode))
397 *type = DT_CHR;
398 else if (S_ISDIR(sb.st_mode))
399 *type = DT_DIR;
400 else if (S_ISBLK(sb.st_mode))
401 *type = DT_BLK;
402 else if (S_ISLNK(sb.st_mode))
403 *type = DT_LNK;
404 else if (S_ISREG(sb.st_mode))
405 *type = DT_REG;
406 else if (S_ISSOCK(sb.st_mode))
407 *type = DT_SOCK;
408 done:
409 free(path_child);
410 return err;
413 const struct got_error *
414 got_path_basename(char **s, const char *path)
416 char buf[PATH_MAX];
417 char *base;
419 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
420 return got_error(GOT_ERR_NO_SPACE);
422 base = basename(buf);
423 if (base == NULL)
424 return got_error_from_errno2("basename", path);
426 *s = strdup(base);
427 if (*s == NULL)
428 return got_error_from_errno("strdup");
430 return NULL;
433 void
434 got_path_strip_trailing_slashes(char *path)
436 size_t x;
438 x = strlen(path);
439 while (x-- > 0 && path[x] == '/')
440 path[x] = '\0';
443 /* based on findprog() from usr.sbin/which/which.c */
444 const struct got_error *
445 got_path_find_prog(char **filename, const char *prog)
447 const struct got_error *err = NULL;
448 char *p;
449 int len;
450 struct stat sbuf;
451 char *path, *pathcpy;
453 *filename = NULL;
455 path = getenv("PATH");
456 if (path == NULL)
457 path = _PATH_DEFPATH;
459 /* Special case if prog contains '/' */
460 if (strchr(prog, '/')) {
461 if ((stat(prog, &sbuf) == 0) && S_ISREG(sbuf.st_mode) &&
462 access(prog, X_OK) == 0) {
463 *filename = strdup(prog);
464 if (*filename == NULL)
465 return got_error_from_errno("strdup");
467 return NULL;
470 if ((path = strdup(path)) == NULL)
471 return got_error_from_errno("strdup");
472 pathcpy = path;
474 while ((p = strsep(&pathcpy, ":")) != NULL) {
475 if (*p == '\0')
476 p = ".";
478 len = strlen(p);
479 while (len > 0 && p[len-1] == '/')
480 p[--len] = '\0'; /* strip trailing '/' */
482 if (asprintf(filename, "%s/%s", p, 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;
491 continue;
493 free(path);
494 return err;
497 const struct got_error *
498 got_path_create_file(const char *path, const char *content)
500 const struct got_error *err = NULL;
501 int fd = -1;
503 fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
504 GOT_DEFAULT_FILE_MODE);
505 if (fd == -1) {
506 err = got_error_from_errno2("open", path);
507 goto done;
510 if (content) {
511 int len = dprintf(fd, "%s\n", content);
512 if (len != strlen(content) + 1) {
513 err = got_error_from_errno("dprintf");
514 goto done;
518 done:
519 if (fd != -1 && close(fd) == -1 && err == NULL)
520 err = got_error_from_errno("close");
521 return err;