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(GOT_ERR_BAD_PATH);
116 if (strncmp(parent_abspath, abspath, len_parent) != 0)
117 return got_error(GOT_ERR_BAD_PATH);
118 if (!got_path_is_root_dir(parent_abspath) && abspath[len_parent] != '/')
119 return got_error(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 return (path[0] == '/' && path[1] == '\0');
141 int
142 got_path_is_current_dir(const char *path)
144 return (path[0] == '.' && path[1] == '\0');
147 int
148 got_path_is_child(const char *child, const char *parent, size_t parent_len)
150 if (parent_len == 0 || got_path_is_root_dir(parent))
151 return 1;
153 if (strncmp(parent, child, parent_len) != 0)
154 return 0;
155 if (child[parent_len] != '/')
156 return 0;
158 return 1;
161 int
162 got_path_cmp(const char *path1, const char *path2)
164 size_t len1 = strlen(path1);
165 size_t len2 = strlen(path2);
166 size_t min_len = MIN(len1, len2);
167 size_t i = 0;
169 /* Leading directory separators are insignificant. */
170 while (path1[0] == '/')
171 path1++;
172 while (path2[0] == '/')
173 path2++;
175 len1 = strlen(path1);
176 len2 = strlen(path2);
177 min_len = MIN(len1, len2);
179 /* Skip over common prefix. */
180 while (i < min_len && path1[i] == path2[i])
181 i++;
183 /* Are the paths exactly equal (besides path separators)? */
184 if (len1 == len2 && i >= min_len)
185 return 0;
187 /* Skip over redundant trailing path seperators. */
188 while (path1[i] == '/' && path1[i + 1] == '/')
189 path1++;
190 while (path2[i] == '/' && path2[i + 1] == '/')
191 path2++;
193 /* Trailing path separators are insignificant. */
194 if (path1[i] == '/' && path1[i + 1] == '\0' && path2[i] == '\0')
195 return 0;
196 if (path2[i] == '/' && path2[i + 1] == '\0' && path1[i] == '\0')
197 return 0;
199 /* Order children in subdirectories directly after their parents. */
200 if (path1[i] == '/' && path2[i] == '\0')
201 return 1;
202 if (path2[i] == '/' && path1[i] == '\0')
203 return -1;
204 if (path1[i] == '/' && path2[i] != '\0')
205 return -1;
206 if (path2[i] == '/' && path1[i] != '\0')
207 return 1;
209 /* Next character following the common prefix determines order. */
210 return (unsigned char)path1[i] < (unsigned char)path2[i] ? -1 : 1;
213 const struct got_error *
214 got_pathlist_insert(struct got_pathlist_entry **inserted,
215 struct got_pathlist_head *pathlist, const char *path, void *data)
217 struct got_pathlist_entry *new, *pe;
219 if (inserted)
220 *inserted = NULL;
222 new = malloc(sizeof(*new));
223 if (new == NULL)
224 return got_error_from_errno("malloc");
225 new->path = path;
226 new->data = data;
228 /*
229 * Many callers will provide paths in a somewhat sorted order while
230 * constructing a path list from inputs such as tree objects or
231 * dirents. Iterating backwards from the tail of the list should
232 * be more efficient than traversing through the entire list each
233 * time an element is inserted.
234 */
235 pe = TAILQ_LAST(pathlist, got_pathlist_head);
236 while (pe) {
237 int cmp = got_path_cmp(pe->path, path);
238 if (cmp == 0) {
239 free(new); /* duplicate */
240 return NULL;
241 } else if (cmp < 0) {
242 TAILQ_INSERT_AFTER(pathlist, pe, new, entry);
243 if (inserted)
244 *inserted = new;
245 return NULL;
247 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
250 TAILQ_INSERT_HEAD(pathlist, new, entry);
251 if (inserted)
252 *inserted = new;
253 return NULL;
256 const struct got_error *
257 got_pathlist_append(struct got_pathlist_entry **pe,
258 struct got_pathlist_head *pathlist, const char *path, void *data)
260 struct got_pathlist_entry *new;
262 new = malloc(sizeof(*new));
263 if (new == NULL)
264 return got_error_from_errno("malloc");
265 new->path = path;
266 new->data = data;
267 TAILQ_INSERT_TAIL(pathlist, new, entry);
268 if (pe)
269 *pe = 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;
438 const struct got_error *
439 got_path_create_file(const char *path, const char *content)
441 const struct got_error *err = NULL;
442 int fd = -1;
444 fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
445 GOT_DEFAULT_FILE_MODE);
446 if (fd == -1) {
447 err = got_error_from_errno2("open", path);
448 goto done;
451 if (content) {
452 int len = dprintf(fd, "%s\n", content);
453 if (len != strlen(content) + 1) {
454 err = got_error_from_errno("dprintf");
455 goto done;
459 done:
460 if (fd != -1 && close(fd) == -1 && err == NULL)
461 err = got_error_from_errno("close");
462 return err;