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 char *
62 got_path_normalize(const char *path)
63 {
64 char *resolved;
66 resolved = realpath(path, NULL);
67 if (resolved == NULL)
68 return NULL;
70 if (!got_path_is_absolute(resolved)) {
71 char *abspath = got_path_get_absolute(resolved);
72 free(resolved);
73 resolved = abspath;
74 }
76 return resolved;
77 }
79 /* based on canonpath() from kern_pledge.c */
80 const struct got_error *
81 got_canonpath(const char *input, char *buf, size_t bufsize)
82 {
83 const char *p;
84 char *q;
86 /* can't canon relative paths, don't bother */
87 if (!got_path_is_absolute(input)) {
88 if (strlcpy(buf, input, bufsize) >= bufsize)
89 return got_error(GOT_ERR_NO_SPACE);
90 return NULL;
91 }
93 p = input;
94 q = buf;
95 while (*p && (q - buf < bufsize)) {
96 if (p[0] == '/' && (p[1] == '/' || p[1] == '\0')) {
97 p += 1;
99 } else if (p[0] == '/' && p[1] == '.' &&
100 (p[2] == '/' || p[2] == '\0')) {
101 p += 2;
103 } else if (p[0] == '/' && p[1] == '.' && p[2] == '.' &&
104 (p[3] == '/' || p[3] == '\0')) {
105 p += 3;
106 if (q != buf) /* "/../" at start of buf */
107 while (*--q != '/')
108 continue;
110 } else {
111 *q++ = *p++;
114 if ((*p == '\0') && (q - buf < bufsize)) {
115 *q = 0;
116 return NULL;
117 } else
118 return got_error(GOT_ERR_NO_SPACE);
121 const struct got_error *
122 got_path_skip_common_ancestor(char **child, const char *parent_abspath,
123 const char *abspath)
125 const struct got_error *err = NULL;
126 size_t len_parent, len, bufsize;
128 *child = NULL;
130 len_parent = strlen(parent_abspath);
131 len = strlen(abspath);
132 if (len_parent >= len)
133 return got_error(GOT_ERR_BAD_PATH);
134 if (strncmp(parent_abspath, abspath, len_parent) != 0)
135 return got_error(GOT_ERR_BAD_PATH);
136 if (!got_path_is_root_dir(parent_abspath) && abspath[len_parent] != '/')
137 return got_error(GOT_ERR_BAD_PATH);
138 while (abspath[len_parent] == '/')
139 abspath++;
140 bufsize = len - len_parent + 1;
141 *child = malloc(bufsize);
142 if (*child == NULL)
143 return got_error_from_errno("malloc");
144 if (strlcpy(*child, abspath + len_parent, bufsize) >= bufsize) {
145 err = got_error_from_errno("strlcpy");
146 free(*child);
147 *child = NULL;
148 return err;
150 return NULL;
153 int
154 got_path_is_root_dir(const char *path)
156 return (path[0] == '/' && path[1] == '\0');
159 int
160 got_path_is_current_dir(const char *path)
162 return (path[0] == '.' && path[1] == '\0');
165 int
166 got_path_is_child(const char *child, const char *parent, size_t parent_len)
168 if (parent_len == 0 || got_path_is_root_dir(parent))
169 return 1;
171 if (strncmp(parent, child, parent_len) != 0)
172 return 0;
173 if (child[parent_len] != '/')
174 return 0;
176 return 1;
179 int
180 got_path_cmp(const char *path1, const char *path2)
182 size_t len1 = strlen(path1);
183 size_t len2 = strlen(path2);
184 size_t min_len = MIN(len1, len2);
185 size_t i = 0;
187 /* Leading directory separators are insignificant. */
188 while (path1[0] == '/')
189 path1++;
190 while (path2[0] == '/')
191 path2++;
193 len1 = strlen(path1);
194 len2 = strlen(path2);
195 min_len = MIN(len1, len2);
197 /* Skip over common prefix. */
198 while (i < min_len && path1[i] == path2[i])
199 i++;
201 /* Are the paths exactly equal (besides path separators)? */
202 if (len1 == len2 && i >= min_len)
203 return 0;
205 /* Skip over redundant trailing path seperators. */
206 while (path1[i] == '/' && path1[i + 1] == '/')
207 path1++;
208 while (path2[i] == '/' && path2[i + 1] == '/')
209 path2++;
211 /* Trailing path separators are insignificant. */
212 if (path1[i] == '/' && path1[i + 1] == '\0' && path2[i] == '\0')
213 return 0;
214 if (path2[i] == '/' && path2[i + 1] == '\0' && path1[i] == '\0')
215 return 0;
217 /* Order children in subdirectories directly after their parents. */
218 if (path1[i] == '/' && path2[i] == '\0')
219 return 1;
220 if (path2[i] == '/' && path1[i] == '\0')
221 return -1;
222 if (path1[i] == '/' && path2[i] != '\0')
223 return -1;
224 if (path2[i] == '/' && path1[i] != '\0')
225 return 1;
227 /* Next character following the common prefix determines order. */
228 return (unsigned char)path1[i] < (unsigned char)path2[i] ? -1 : 1;
231 const struct got_error *
232 got_pathlist_insert(struct got_pathlist_entry **inserted,
233 struct got_pathlist_head *pathlist, const char *path, void *data)
235 struct got_pathlist_entry *new, *pe;
237 if (inserted)
238 *inserted = NULL;
240 new = malloc(sizeof(*new));
241 if (new == NULL)
242 return got_error_from_errno("malloc");
243 new->path = path;
244 new->data = data;
246 /*
247 * Many callers will provide paths in a somewhat sorted order while
248 * constructing a path list from inputs such as tree objects or
249 * dirents. Iterating backwards from the tail of the list should
250 * be more efficient than traversing through the entire list each
251 * time an element is inserted.
252 */
253 pe = TAILQ_LAST(pathlist, got_pathlist_head);
254 while (pe) {
255 int cmp = got_path_cmp(pe->path, path);
256 if (cmp == 0) {
257 free(new); /* duplicate */
258 return NULL;
259 } else if (cmp < 0) {
260 TAILQ_INSERT_AFTER(pathlist, pe, new, entry);
261 if (inserted)
262 *inserted = new;
263 return NULL;
265 pe = TAILQ_PREV(pe, got_pathlist_head, entry);
268 TAILQ_INSERT_HEAD(pathlist, new, entry);
269 if (inserted)
270 *inserted = new;
271 return NULL;
274 void
275 got_pathlist_free(struct got_pathlist_head *pathlist)
277 struct got_pathlist_entry *pe;
279 while ((pe = TAILQ_FIRST(pathlist)) != NULL) {
280 TAILQ_REMOVE(pathlist, pe, entry);
281 free(pe);
285 static const struct got_error *
286 make_parent_dirs(const char *abspath)
288 const struct got_error *err = NULL;
289 char *parent;
291 err = got_path_dirname(&parent, abspath);
292 if (err)
293 return err;
295 if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
296 if (errno == ENOENT) {
297 err = make_parent_dirs(parent);
298 if (err)
299 goto done;
300 if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
301 err = got_error_from_errno2("mkdir", parent);
302 goto done;
304 } else
305 err = got_error_from_errno2("mkdir", parent);
307 done:
308 free(parent);
309 return err;
312 const struct got_error *
313 got_path_mkdir(const char *abspath)
315 const struct got_error *err = NULL;
317 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
318 if (errno == ENOENT) {
319 err = make_parent_dirs(abspath);
320 if (err)
321 goto done;
322 if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1)
323 err = got_error_from_errno2("mkdir", abspath);
324 } else
325 err = got_error_from_errno2("mkdir", abspath);
328 done:
329 return err;
332 int
333 got_path_dir_is_empty(const char *dir)
335 DIR *d;
336 struct dirent *dent;
337 int empty = 1;
339 d = opendir(dir);
340 if (d == NULL)
341 return 1;
343 while ((dent = readdir(d)) != NULL) {
344 if (strcmp(dent->d_name, ".") == 0 ||
345 strcmp(dent->d_name, "..") == 0)
346 continue;
348 empty = 0;
349 break;
352 closedir(d);
353 return empty;
356 const struct got_error *
357 got_path_dirname(char **parent, const char *path)
359 char *p;
361 p = dirname(path);
362 if (p == NULL)
363 return got_error_from_errno2("dirname", path);
365 if (p[0] == '.' && p[1] == '\0')
366 return got_error(GOT_ERR_BAD_PATH);
368 *parent = strdup(p);
369 if (*parent == NULL)
370 return got_error_from_errno("strdup");
372 return NULL;
375 void
376 got_path_strip_trailing_slashes(char *path)
378 int x;
380 while (path[x = strlen(path) - 1] == '/')
381 path[x] = '\0';
384 /* based on findprog() from usr.sbin/which/which.c */
385 const struct got_error *
386 got_path_find_prog(char **filename, const char *prog)
388 char *p;
389 int len;
390 struct stat sbuf;
391 char *path, *pathcpy;
393 *filename = NULL;
395 path = getenv("PATH");
396 if (path == NULL)
397 path = _PATH_DEFPATH;
399 /* Special case if prog contains '/' */
400 if (strchr(prog, '/')) {
401 if ((stat(prog, &sbuf) == 0) && S_ISREG(sbuf.st_mode) &&
402 access(prog, X_OK) == 0) {
403 *filename = strdup(prog);
404 if (*filename == NULL)
405 return got_error_from_errno("strdup");
407 return NULL;
410 if ((path = strdup(path)) == NULL)
411 return got_error_from_errno("strdup");
412 pathcpy = path;
414 while ((p = strsep(&pathcpy, ":")) != NULL) {
415 if (*p == '\0')
416 p = ".";
418 len = strlen(p);
419 while (len > 0 && p[len-1] == '/')
420 p[--len] = '\0'; /* strip trailing '/' */
422 if (asprintf(filename, "%s/%s", p, prog) == -1) {
423 free(path);
424 return got_error_from_errno("asprintf");
426 if ((stat(*filename, &sbuf) == 0) && S_ISREG(sbuf.st_mode) &&
427 access(*filename, X_OK) == 0) {
428 free(path);
429 return NULL;
431 free(*filename);
432 *filename = NULL;
433 continue;
435 free(path);
436 return NULL;
439 const struct got_error *
440 got_path_create_file(const char *path, const char *content)
442 const struct got_error *err = NULL;
443 int fd = -1;
445 fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
446 GOT_DEFAULT_FILE_MODE);
447 if (fd == -1) {
448 err = got_error_from_errno2("open", path);
449 goto done;
452 if (content) {
453 int len = dprintf(fd, "%s\n", content);
454 if (len != strlen(content) + 1) {
455 err = got_error_from_errno("dprintf");
456 goto done;
460 done:
461 if (fd != -1 && close(fd) == -1 && err == NULL)
462 err = got_error_from_errno("close");
463 return err;