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>
35 #include "got_error.h"
39 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
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
)
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
);
64 while (*p
&& (q
- buf
< bufsize
)) {
65 if (p
[0] == '/' && (p
[1] == '/' || p
[1] == '\0')) {
68 } else if (p
[0] == '/' && p
[1] == '.' &&
69 (p
[2] == '/' || p
[2] == '\0')) {
72 } else if (p
[0] == '/' && p
[1] == '.' && p
[2] == '.' &&
73 (p
[3] == '/' || p
[3] == '\0')) {
75 if (q
!= buf
) /* "/../" at start of buf */
83 if ((*p
== '\0') && (q
- buf
< bufsize
)) {
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
,
94 const struct got_error
*err
= NULL
;
95 size_t len_parent
, len
, bufsize
;
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
] == '/')
109 bufsize
= len
- len_parent
+ 1;
110 *child
= malloc(bufsize
);
112 return got_error_from_errno("malloc");
113 if (strlcpy(*child
, abspath
+ len_parent
, bufsize
) >= bufsize
) {
114 err
= got_error_from_errno("strlcpy");
122 const struct got_error
*
123 got_path_strip(char **out
, const char *path
, int n
)
130 while (n
> 0 && (c
= strchr(p
, '/')) != NULL
) {
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");
145 got_path_is_root_dir(const char *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
))
158 if (strncmp(parent
, child
, parent_len
) != 0)
160 if (child
[parent_len
] != '/')
167 got_path_cmp(const char *path1
, const char *path2
, size_t len1
, size_t len2
)
172 /* Leading directory separators are insignificant. */
173 while (path1
[0] == '/') {
177 while (path2
[0] == '/') {
182 min_len
= MIN(len1
, len2
);
184 /* Skip over common prefix. */
185 while (i
< min_len
&& path1
[i
] == path2
[i
])
188 /* Are the paths exactly equal (besides path separators)? */
189 if (len1
== len2
&& i
>= min_len
)
192 /* Skip over redundant trailing path seperators. */
193 while (path1
[i
] == '/' && path1
[i
+ 1] == '/')
195 while (path2
[i
] == '/' && path2
[i
+ 1] == '/')
198 /* Trailing path separators are insignificant. */
199 if (path1
[i
] == '/' && path1
[i
+ 1] == '\0' && path2
[i
] == '\0')
201 if (path2
[i
] == '/' && path2
[i
+ 1] == '\0' && path1
[i
] == '\0')
204 /* Order children in subdirectories directly after their parents. */
205 if (path1
[i
] == '/' && path2
[i
] == '\0')
207 if (path2
[i
] == '/' && path1
[i
] == '\0')
209 if (path1
[i
] == '/' && path2
[i
] != '\0')
211 if (path2
[i
] == '/' && path1
[i
] != '\0')
214 /* Next character following the common prefix determines order. */
215 return (unsigned char)path1
[i
] < (unsigned char)path2
[i
] ? -1 : 1;
218 const struct got_error
*
219 got_pathlist_insert(struct got_pathlist_entry
**inserted
,
220 struct got_pathlist_head
*pathlist
, const char *path
, void *data
)
222 struct got_pathlist_entry
*new, *pe
;
223 size_t path_len
= strlen(path
);
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.
235 pe
= TAILQ_LAST(pathlist
, got_pathlist_head
);
237 int cmp
= got_path_cmp(pe
->path
, path
, pe
->path_len
, path_len
);
239 return NULL
; /* duplicate */
242 pe
= TAILQ_PREV(pe
, got_pathlist_head
, entry
);
245 new = malloc(sizeof(*new));
247 return got_error_from_errno("malloc");
249 new->path_len
= path_len
;
252 TAILQ_INSERT_AFTER(pathlist
, pe
, new, entry
);
254 TAILQ_INSERT_HEAD(pathlist
, new, entry
);
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));
268 return got_error_from_errno("malloc");
270 new->path_len
= strlen(path
);
272 TAILQ_INSERT_TAIL(pathlist
, new, entry
);
277 got_pathlist_free(struct got_pathlist_head
*pathlist
, int freemask
)
279 struct got_pathlist_entry
*pe
;
281 while ((pe
= TAILQ_FIRST(pathlist
)) != NULL
) {
282 if (freemask
& GOT_PATHLIST_FREE_PATH
) {
283 free((char *)pe
->path
);
286 if (freemask
& GOT_PATHLIST_FREE_DATA
) {
290 TAILQ_REMOVE(pathlist
, pe
, entry
);
295 static const struct got_error
*
296 make_parent_dirs(const char *abspath
)
298 const struct got_error
*err
= NULL
;
301 err
= got_path_dirname(&parent
, abspath
);
305 if (mkdir(parent
, GOT_DEFAULT_DIR_MODE
) == -1) {
306 if (errno
== ENOENT
) {
307 err
= make_parent_dirs(parent
);
310 if (mkdir(parent
, GOT_DEFAULT_DIR_MODE
) == -1) {
311 err
= got_error_from_errno2("mkdir", parent
);
315 err
= got_error_from_errno2("mkdir", parent
);
322 const struct got_error
*
323 got_path_mkdir(const char *abspath
)
325 const struct got_error
*err
= NULL
;
327 if (mkdir(abspath
, GOT_DEFAULT_DIR_MODE
) == -1) {
328 if (errno
== ENOENT
) {
329 err
= make_parent_dirs(abspath
);
332 if (mkdir(abspath
, GOT_DEFAULT_DIR_MODE
) == -1)
333 err
= got_error_from_errno2("mkdir", abspath
);
335 err
= got_error_from_errno2("mkdir", abspath
);
343 got_path_dir_is_empty(const char *dir
)
353 while ((dent
= readdir(d
)) != NULL
) {
354 if (strcmp(dent
->d_name
, ".") == 0 ||
355 strcmp(dent
->d_name
, "..") == 0)
366 const struct got_error
*
367 got_path_dirname(char **parent
, const char *path
)
372 if (strlcpy(buf
, path
, sizeof(buf
)) >= sizeof(buf
))
373 return got_error(GOT_ERR_NO_SPACE
);
377 return got_error_from_errno2("dirname", path
);
379 if (p
[0] == '.' && p
[1] == '\0')
380 return got_error_path(path
, GOT_ERR_BAD_PATH
);
384 return got_error_from_errno("strdup");
389 const struct got_error
*
390 got_path_dirent_type(int *type
, const char *path_parent
, struct dirent
*dent
)
392 const struct got_error
*err
= NULL
;
396 if (dent
->d_type
!= DT_UNKNOWN
) {
397 *type
= dent
->d_type
;
404 * This is a fallback to accommodate filesystems which do not
405 * provide directory entry type information. DT_UNKNOWN directory
406 * entries occur on NFS mounts without "readdir plus" RPC.
409 if (asprintf(&path_child
, "%s/%s", path_parent
, dent
->d_name
) == -1)
410 return got_error_from_errno("asprintf");
412 if (lstat(path_child
, &sb
) == -1) {
413 err
= got_error_from_errno2("lstat", path_child
);
417 if (S_ISFIFO(sb
.st_mode
))
419 else if (S_ISCHR(sb
.st_mode
))
421 else if (S_ISDIR(sb
.st_mode
))
423 else if (S_ISBLK(sb
.st_mode
))
425 else if (S_ISLNK(sb
.st_mode
))
427 else if (S_ISREG(sb
.st_mode
))
429 else if (S_ISSOCK(sb
.st_mode
))
436 const struct got_error
*
437 got_path_basename(char **s
, const char *path
)
442 if (strlcpy(buf
, path
, sizeof(buf
)) >= sizeof(buf
))
443 return got_error(GOT_ERR_NO_SPACE
);
445 base
= basename(buf
);
447 return got_error_from_errno2("basename", path
);
451 return got_error_from_errno("strdup");
457 got_path_strip_trailing_slashes(char *path
)
462 while (x
-- > 0 && path
[x
] == '/')
466 /* based on findprog() from usr.bin/which/which.c */
467 const struct got_error
*
468 got_path_find_prog(char **filename
, const char *prog
)
470 const struct got_error
*err
= NULL
;
475 char *pathcpy
, *dup
= NULL
;
479 path
= getenv("PATH");
481 path
= _PATH_DEFPATH
;
483 /* Special case if prog contains '/' */
484 if (strchr(prog
, '/')) {
485 if ((stat(prog
, &sbuf
) == 0) && S_ISREG(sbuf
.st_mode
) &&
486 access(prog
, X_OK
) == 0) {
487 *filename
= strdup(prog
);
488 if (*filename
== NULL
)
489 return got_error_from_errno("strdup");
494 if ((dup
= strdup(path
)) == NULL
)
495 return got_error_from_errno("strdup");
498 while ((p
= strsep(&pathcpy
, ":")) != NULL
) {
502 while (len
> 0 && p
[len
-1] == '/')
503 p
[--len
] = '\0'; /* strip trailing '/' */
509 if (asprintf(filename
, "%s/%s", d
, prog
) == -1) {
510 err
= got_error_from_errno("asprintf");
513 if ((stat(*filename
, &sbuf
) == 0) && S_ISREG(sbuf
.st_mode
) &&
514 access(*filename
, X_OK
) == 0)
523 const struct got_error
*
524 got_path_create_file(const char *path
, const char *content
)
526 const struct got_error
*err
= NULL
;
529 fd
= open(path
, O_RDWR
| O_CREAT
| O_EXCL
| O_NOFOLLOW
| O_CLOEXEC
,
530 GOT_DEFAULT_FILE_MODE
);
532 err
= got_error_from_errno2("open", path
);
537 int len
= dprintf(fd
, "%s\n", content
);
538 if (len
!= strlen(content
) + 1) {
539 err
= got_error_from_errno("dprintf");
545 if (fd
!= -1 && close(fd
) == -1 && err
== NULL
)
546 err
= got_error_from_errno("close");
550 const struct got_error
*
551 got_path_move_file(const char *oldpath
, const char *newpath
)
553 const struct got_error
*err
;
555 if (rename(oldpath
, newpath
) != -1)
559 return got_error_from_errno3("rename", oldpath
, newpath
);
561 err
= make_parent_dirs(newpath
);
565 if (rename(oldpath
, newpath
) == -1)
566 return got_error_from_errno3("rename", oldpath
, newpath
);