Blame


1 7b19e0f1 2017-11-05 stsp /*
2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 e6eac3b8 2018-06-17 stsp * Copyright (c) 2015 Theo de Raadt <deraadt@openbsd.org>
4 0ee7065d 2019-05-13 stsp * Copyright (c) 1997 Todd C. Miller <millert@openbsd.org>
5 7b19e0f1 2017-11-05 stsp *
6 7b19e0f1 2017-11-05 stsp * Permission to use, copy, modify, and distribute this software for any
7 7b19e0f1 2017-11-05 stsp * purpose with or without fee is hereby granted, provided that the above
8 7b19e0f1 2017-11-05 stsp * copyright notice and this permission notice appear in all copies.
9 7b19e0f1 2017-11-05 stsp *
10 7b19e0f1 2017-11-05 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 7b19e0f1 2017-11-05 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 7b19e0f1 2017-11-05 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 7b19e0f1 2017-11-05 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 7b19e0f1 2017-11-05 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 7b19e0f1 2017-11-05 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 7b19e0f1 2017-11-05 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 7b19e0f1 2017-11-05 stsp */
18 4fccd2fe 2023-03-08 thomas
19 4fccd2fe 2023-03-08 thomas #include "got_compat.h"
20 7b19e0f1 2017-11-05 stsp
21 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
22 0cd1c46a 2019-03-11 stsp #include <sys/stat.h>
23 e08cc72d 2019-02-05 stsp
24 0cd1c46a 2019-03-11 stsp #include <errno.h>
25 2c7829a4 2019-06-17 stsp #include <fcntl.h>
26 4027f31a 2017-11-04 stsp #include <limits.h>
27 0cd1c46a 2019-03-11 stsp #include <libgen.h>
28 4027f31a 2017-11-04 stsp #include <stdlib.h>
29 4027f31a 2017-11-04 stsp #include <unistd.h>
30 4027f31a 2017-11-04 stsp #include <stdio.h>
31 4027f31a 2017-11-04 stsp #include <string.h>
32 3c45a30a 2019-05-12 jcs #include <dirent.h>
33 0ee7065d 2019-05-13 stsp #include <paths.h>
34 dd038bc6 2021-09-21 thomas.ad
35 9d31a1d8 2018-03-11 stsp #include "got_error.h"
36 324d37e7 2019-05-11 stsp #include "got_path.h"
37 1411938b 2018-02-12 stsp
38 e0159033 2019-01-08 stsp #ifndef MIN
39 e0159033 2019-01-08 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
40 e0159033 2019-01-08 stsp #endif
41 e0159033 2019-01-08 stsp
42 4027f31a 2017-11-04 stsp int
43 4027f31a 2017-11-04 stsp got_path_is_absolute(const char *path)
44 4027f31a 2017-11-04 stsp {
45 4027f31a 2017-11-04 stsp return path[0] == '/';
46 4027f31a 2017-11-04 stsp }
47 4027f31a 2017-11-04 stsp
48 f7d20e89 2018-06-17 stsp /* based on canonpath() from kern_pledge.c */
49 f7d20e89 2018-06-17 stsp const struct got_error *
50 e6eac3b8 2018-06-17 stsp got_canonpath(const char *input, char *buf, size_t bufsize)
51 e6eac3b8 2018-06-17 stsp {
52 e6eac3b8 2018-06-17 stsp const char *p;
53 e6eac3b8 2018-06-17 stsp char *q;
54 e6eac3b8 2018-06-17 stsp
55 e6eac3b8 2018-06-17 stsp /* can't canon relative paths, don't bother */
56 e6eac3b8 2018-06-17 stsp if (!got_path_is_absolute(input)) {
57 e6eac3b8 2018-06-17 stsp if (strlcpy(buf, input, bufsize) >= bufsize)
58 f7d20e89 2018-06-17 stsp return got_error(GOT_ERR_NO_SPACE);
59 f7d20e89 2018-06-17 stsp return NULL;
60 e6eac3b8 2018-06-17 stsp }
61 e6eac3b8 2018-06-17 stsp
62 e6eac3b8 2018-06-17 stsp p = input;
63 e6eac3b8 2018-06-17 stsp q = buf;
64 e6eac3b8 2018-06-17 stsp while (*p && (q - buf < bufsize)) {
65 e6eac3b8 2018-06-17 stsp if (p[0] == '/' && (p[1] == '/' || p[1] == '\0')) {
66 e6eac3b8 2018-06-17 stsp p += 1;
67 e6eac3b8 2018-06-17 stsp
68 e6eac3b8 2018-06-17 stsp } else if (p[0] == '/' && p[1] == '.' &&
69 e6eac3b8 2018-06-17 stsp (p[2] == '/' || p[2] == '\0')) {
70 e6eac3b8 2018-06-17 stsp p += 2;
71 e6eac3b8 2018-06-17 stsp
72 e6eac3b8 2018-06-17 stsp } else if (p[0] == '/' && p[1] == '.' && p[2] == '.' &&
73 e6eac3b8 2018-06-17 stsp (p[3] == '/' || p[3] == '\0')) {
74 e6eac3b8 2018-06-17 stsp p += 3;
75 e6eac3b8 2018-06-17 stsp if (q != buf) /* "/../" at start of buf */
76 e6eac3b8 2018-06-17 stsp while (*--q != '/')
77 e6eac3b8 2018-06-17 stsp continue;
78 e6eac3b8 2018-06-17 stsp
79 e6eac3b8 2018-06-17 stsp } else {
80 e6eac3b8 2018-06-17 stsp *q++ = *p++;
81 e6eac3b8 2018-06-17 stsp }
82 e6eac3b8 2018-06-17 stsp }
83 e6eac3b8 2018-06-17 stsp if ((*p == '\0') && (q - buf < bufsize)) {
84 e6eac3b8 2018-06-17 stsp *q = 0;
85 f7d20e89 2018-06-17 stsp return NULL;
86 e6eac3b8 2018-06-17 stsp } else
87 f7d20e89 2018-06-17 stsp return got_error(GOT_ERR_NO_SPACE);
88 e6eac3b8 2018-06-17 stsp }
89 04ca23f4 2018-07-16 stsp
90 04ca23f4 2018-07-16 stsp const struct got_error *
91 04ca23f4 2018-07-16 stsp got_path_skip_common_ancestor(char **child, const char *parent_abspath,
92 04ca23f4 2018-07-16 stsp const char *abspath)
93 04ca23f4 2018-07-16 stsp {
94 04ca23f4 2018-07-16 stsp const struct got_error *err = NULL;
95 04ca23f4 2018-07-16 stsp size_t len_parent, len, bufsize;
96 db5ba8ed 2019-03-26 stsp
97 db5ba8ed 2019-03-26 stsp *child = NULL;
98 04ca23f4 2018-07-16 stsp
99 04ca23f4 2018-07-16 stsp len_parent = strlen(parent_abspath);
100 04ca23f4 2018-07-16 stsp len = strlen(abspath);
101 04ca23f4 2018-07-16 stsp if (len_parent >= len)
102 63f810e6 2020-02-29 stsp return got_error_path(abspath, GOT_ERR_BAD_PATH);
103 04ca23f4 2018-07-16 stsp if (strncmp(parent_abspath, abspath, len_parent) != 0)
104 63f810e6 2020-02-29 stsp return got_error_path(abspath, GOT_ERR_BAD_PATH);
105 eb4304b9 2019-05-09 stsp if (!got_path_is_root_dir(parent_abspath) && abspath[len_parent] != '/')
106 63f810e6 2020-02-29 stsp return got_error_path(abspath, GOT_ERR_BAD_PATH);
107 5e3ce57a 2019-03-26 stsp while (abspath[len_parent] == '/')
108 5e3ce57a 2019-03-26 stsp abspath++;
109 04ca23f4 2018-07-16 stsp bufsize = len - len_parent + 1;
110 04ca23f4 2018-07-16 stsp *child = malloc(bufsize);
111 04ca23f4 2018-07-16 stsp if (*child == NULL)
112 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
113 04ca23f4 2018-07-16 stsp if (strlcpy(*child, abspath + len_parent, bufsize) >= bufsize) {
114 638f9024 2019-05-13 stsp err = got_error_from_errno("strlcpy");
115 04ca23f4 2018-07-16 stsp free(*child);
116 04ca23f4 2018-07-16 stsp *child = NULL;
117 04ca23f4 2018-07-16 stsp return err;
118 d9db2ff9 2022-04-16 thomas }
119 d9db2ff9 2022-04-16 thomas return NULL;
120 d9db2ff9 2022-04-16 thomas }
121 d9db2ff9 2022-04-16 thomas
122 d9db2ff9 2022-04-16 thomas const struct got_error *
123 d9db2ff9 2022-04-16 thomas got_path_strip(char **out, const char *path, int n)
124 d9db2ff9 2022-04-16 thomas {
125 d9db2ff9 2022-04-16 thomas const char *p, *c;
126 d9db2ff9 2022-04-16 thomas
127 d9db2ff9 2022-04-16 thomas p = path;
128 d9db2ff9 2022-04-16 thomas *out = NULL;
129 d9db2ff9 2022-04-16 thomas
130 7472ca4e 2022-04-16 thomas while (n > 0 && (c = strchr(p, '/')) != NULL) {
131 7472ca4e 2022-04-16 thomas p = c + 1;
132 d9db2ff9 2022-04-16 thomas n--;
133 04ca23f4 2018-07-16 stsp }
134 d9db2ff9 2022-04-16 thomas
135 d9db2ff9 2022-04-16 thomas if (n > 0)
136 d9db2ff9 2022-04-16 thomas return got_error_fmt(GOT_ERR_BAD_PATH,
137 7472ca4e 2022-04-16 thomas "can't strip %d path-components from %s", n, path);
138 d9db2ff9 2022-04-16 thomas
139 7472ca4e 2022-04-16 thomas if ((*out = strdup(p)) == NULL)
140 d9db2ff9 2022-04-16 thomas return got_error_from_errno("strdup");
141 04ca23f4 2018-07-16 stsp return NULL;
142 04ca23f4 2018-07-16 stsp }
143 31cedeaf 2018-09-15 stsp
144 31cedeaf 2018-09-15 stsp int
145 31cedeaf 2018-09-15 stsp got_path_is_root_dir(const char *path)
146 31cedeaf 2018-09-15 stsp {
147 b8b3f209 2020-02-29 stsp while (*path == '/')
148 b8b3f209 2020-02-29 stsp path++;
149 b8b3f209 2020-02-29 stsp return (*path == '\0');
150 31cedeaf 2018-09-15 stsp }
151 e0159033 2019-01-08 stsp
152 e0159033 2019-01-08 stsp int
153 8da9e5f4 2019-01-12 stsp got_path_is_child(const char *child, const char *parent, size_t parent_len)
154 8da9e5f4 2019-01-12 stsp {
155 eb4304b9 2019-05-09 stsp if (parent_len == 0 || got_path_is_root_dir(parent))
156 8da9e5f4 2019-01-12 stsp return 1;
157 8da9e5f4 2019-01-12 stsp
158 8da9e5f4 2019-01-12 stsp if (strncmp(parent, child, parent_len) != 0)
159 8da9e5f4 2019-01-12 stsp return 0;
160 8da9e5f4 2019-01-12 stsp if (child[parent_len] != '/')
161 8da9e5f4 2019-01-12 stsp return 0;
162 8da9e5f4 2019-01-12 stsp
163 8da9e5f4 2019-01-12 stsp return 1;
164 8da9e5f4 2019-01-12 stsp }
165 8da9e5f4 2019-01-12 stsp
166 8da9e5f4 2019-01-12 stsp int
167 d572f586 2019-08-02 stsp got_path_cmp(const char *path1, const char *path2, size_t len1, size_t len2)
168 e0159033 2019-01-08 stsp {
169 466d3b32 2019-08-02 stsp size_t min_len;
170 e0159033 2019-01-08 stsp size_t i = 0;
171 e0159033 2019-01-08 stsp
172 e08cc72d 2019-02-05 stsp /* Leading directory separators are insignificant. */
173 466d3b32 2019-08-02 stsp while (path1[0] == '/') {
174 e08cc72d 2019-02-05 stsp path1++;
175 466d3b32 2019-08-02 stsp len1--;
176 466d3b32 2019-08-02 stsp }
177 466d3b32 2019-08-02 stsp while (path2[0] == '/') {
178 e08cc72d 2019-02-05 stsp path2++;
179 466d3b32 2019-08-02 stsp len2--;
180 466d3b32 2019-08-02 stsp }
181 e08cc72d 2019-02-05 stsp
182 e08cc72d 2019-02-05 stsp min_len = MIN(len1, len2);
183 e08cc72d 2019-02-05 stsp
184 e0159033 2019-01-08 stsp /* Skip over common prefix. */
185 e0159033 2019-01-08 stsp while (i < min_len && path1[i] == path2[i])
186 e0159033 2019-01-08 stsp i++;
187 e0159033 2019-01-08 stsp
188 e08cc72d 2019-02-05 stsp /* Are the paths exactly equal (besides path separators)? */
189 e0159033 2019-01-08 stsp if (len1 == len2 && i >= min_len)
190 e0159033 2019-01-08 stsp return 0;
191 e0159033 2019-01-08 stsp
192 e08cc72d 2019-02-05 stsp /* Skip over redundant trailing path seperators. */
193 e08cc72d 2019-02-05 stsp while (path1[i] == '/' && path1[i + 1] == '/')
194 e08cc72d 2019-02-05 stsp path1++;
195 e08cc72d 2019-02-05 stsp while (path2[i] == '/' && path2[i + 1] == '/')
196 e08cc72d 2019-02-05 stsp path2++;
197 e08cc72d 2019-02-05 stsp
198 e08cc72d 2019-02-05 stsp /* Trailing path separators are insignificant. */
199 e08cc72d 2019-02-05 stsp if (path1[i] == '/' && path1[i + 1] == '\0' && path2[i] == '\0')
200 e08cc72d 2019-02-05 stsp return 0;
201 e08cc72d 2019-02-05 stsp if (path2[i] == '/' && path2[i + 1] == '\0' && path1[i] == '\0')
202 e08cc72d 2019-02-05 stsp return 0;
203 e08cc72d 2019-02-05 stsp
204 e0159033 2019-01-08 stsp /* Order children in subdirectories directly after their parents. */
205 e0159033 2019-01-08 stsp if (path1[i] == '/' && path2[i] == '\0')
206 e0159033 2019-01-08 stsp return 1;
207 e0159033 2019-01-08 stsp if (path2[i] == '/' && path1[i] == '\0')
208 e0159033 2019-01-08 stsp return -1;
209 e08cc72d 2019-02-05 stsp if (path1[i] == '/' && path2[i] != '\0')
210 e0159033 2019-01-08 stsp return -1;
211 e08cc72d 2019-02-05 stsp if (path2[i] == '/' && path1[i] != '\0')
212 e0159033 2019-01-08 stsp return 1;
213 e0159033 2019-01-08 stsp
214 e0159033 2019-01-08 stsp /* Next character following the common prefix determines order. */
215 e0159033 2019-01-08 stsp return (unsigned char)path1[i] < (unsigned char)path2[i] ? -1 : 1;
216 e0159033 2019-01-08 stsp }
217 e08cc72d 2019-02-05 stsp
218 e08cc72d 2019-02-05 stsp const struct got_error *
219 7e5c804b 2019-02-05 stsp got_pathlist_insert(struct got_pathlist_entry **inserted,
220 3d8df59c 2019-02-05 stsp struct got_pathlist_head *pathlist, const char *path, void *data)
221 e08cc72d 2019-02-05 stsp {
222 e08cc72d 2019-02-05 stsp struct got_pathlist_entry *new, *pe;
223 b0d7d6cb 2022-05-19 thomas size_t path_len = strlen(path);
224 7e5c804b 2019-02-05 stsp
225 7e5c804b 2019-02-05 stsp if (inserted)
226 7e5c804b 2019-02-05 stsp *inserted = NULL;
227 e08cc72d 2019-02-05 stsp
228 e08cc72d 2019-02-05 stsp /*
229 e08cc72d 2019-02-05 stsp * Many callers will provide paths in a somewhat sorted order while
230 e08cc72d 2019-02-05 stsp * constructing a path list from inputs such as tree objects or
231 e08cc72d 2019-02-05 stsp * dirents. Iterating backwards from the tail of the list should
232 e08cc72d 2019-02-05 stsp * be more efficient than traversing through the entire list each
233 e08cc72d 2019-02-05 stsp * time an element is inserted.
234 e08cc72d 2019-02-05 stsp */
235 e08cc72d 2019-02-05 stsp pe = TAILQ_LAST(pathlist, got_pathlist_head);
236 e08cc72d 2019-02-05 stsp while (pe) {
237 b0d7d6cb 2022-05-19 thomas int cmp = got_path_cmp(pe->path, path, pe->path_len, path_len);
238 b0d7d6cb 2022-05-19 thomas if (cmp == 0)
239 b0d7d6cb 2022-05-19 thomas return NULL; /* duplicate */
240 b0d7d6cb 2022-05-19 thomas else if (cmp < 0)
241 b0d7d6cb 2022-05-19 thomas break;
242 e08cc72d 2019-02-05 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
243 e08cc72d 2019-02-05 stsp }
244 e08cc72d 2019-02-05 stsp
245 b0d7d6cb 2022-05-19 thomas new = malloc(sizeof(*new));
246 b0d7d6cb 2022-05-19 thomas if (new == NULL)
247 b0d7d6cb 2022-05-19 thomas return got_error_from_errno("malloc");
248 b0d7d6cb 2022-05-19 thomas new->path = path;
249 b0d7d6cb 2022-05-19 thomas new->path_len = path_len;
250 b0d7d6cb 2022-05-19 thomas new->data = data;
251 b0d7d6cb 2022-05-19 thomas if (pe)
252 b0d7d6cb 2022-05-19 thomas TAILQ_INSERT_AFTER(pathlist, pe, new, entry);
253 b0d7d6cb 2022-05-19 thomas else
254 b0d7d6cb 2022-05-19 thomas TAILQ_INSERT_HEAD(pathlist, new, entry);
255 7e5c804b 2019-02-05 stsp if (inserted)
256 7e5c804b 2019-02-05 stsp *inserted = new;
257 e08cc72d 2019-02-05 stsp return NULL;
258 e08cc72d 2019-02-05 stsp }
259 72ea6654 2019-07-27 stsp
260 72ea6654 2019-07-27 stsp const struct got_error *
261 adc19d55 2019-07-28 stsp got_pathlist_append(struct got_pathlist_head *pathlist,
262 adc19d55 2019-07-28 stsp const char *path, void *data)
263 72ea6654 2019-07-27 stsp {
264 72ea6654 2019-07-27 stsp struct got_pathlist_entry *new;
265 e08cc72d 2019-02-05 stsp
266 72ea6654 2019-07-27 stsp new = malloc(sizeof(*new));
267 72ea6654 2019-07-27 stsp if (new == NULL)
268 72ea6654 2019-07-27 stsp return got_error_from_errno("malloc");
269 72ea6654 2019-07-27 stsp new->path = path;
270 f2b16ada 2019-08-02 stsp new->path_len = strlen(path);
271 72ea6654 2019-07-27 stsp new->data = data;
272 72ea6654 2019-07-27 stsp TAILQ_INSERT_TAIL(pathlist, new, entry);
273 72ea6654 2019-07-27 stsp return NULL;
274 72ea6654 2019-07-27 stsp }
275 72ea6654 2019-07-27 stsp
276 e08cc72d 2019-02-05 stsp void
277 21c2d8be 2023-01-10 thomas got_pathlist_free(struct got_pathlist_head *pathlist, int freemask)
278 e08cc72d 2019-02-05 stsp {
279 e08cc72d 2019-02-05 stsp struct got_pathlist_entry *pe;
280 e08cc72d 2019-02-05 stsp
281 e08cc72d 2019-02-05 stsp while ((pe = TAILQ_FIRST(pathlist)) != NULL) {
282 21c2d8be 2023-01-10 thomas if (freemask & GOT_PATHLIST_FREE_PATH) {
283 21c2d8be 2023-01-10 thomas free((char *)pe->path);
284 21c2d8be 2023-01-10 thomas pe->path = NULL;
285 21c2d8be 2023-01-10 thomas }
286 21c2d8be 2023-01-10 thomas if (freemask & GOT_PATHLIST_FREE_DATA) {
287 21c2d8be 2023-01-10 thomas free(pe->data);
288 21c2d8be 2023-01-10 thomas pe->data = NULL;
289 21c2d8be 2023-01-10 thomas }
290 e08cc72d 2019-02-05 stsp TAILQ_REMOVE(pathlist, pe, entry);
291 e08cc72d 2019-02-05 stsp free(pe);
292 0cd1c46a 2019-03-11 stsp }
293 0cd1c46a 2019-03-11 stsp }
294 0cd1c46a 2019-03-11 stsp
295 0cd1c46a 2019-03-11 stsp static const struct got_error *
296 0cd1c46a 2019-03-11 stsp make_parent_dirs(const char *abspath)
297 0cd1c46a 2019-03-11 stsp {
298 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
299 d1667f0d 2019-03-11 stsp char *parent;
300 0cd1c46a 2019-03-11 stsp
301 d1667f0d 2019-03-11 stsp err = got_path_dirname(&parent, abspath);
302 d1667f0d 2019-03-11 stsp if (err)
303 d1667f0d 2019-03-11 stsp return err;
304 0cd1c46a 2019-03-11 stsp
305 0cd1c46a 2019-03-11 stsp if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
306 0cd1c46a 2019-03-11 stsp if (errno == ENOENT) {
307 0cd1c46a 2019-03-11 stsp err = make_parent_dirs(parent);
308 0cd1c46a 2019-03-11 stsp if (err)
309 5e1c9f23 2019-03-11 stsp goto done;
310 5e1c9f23 2019-03-11 stsp if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
311 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", parent);
312 5e1c9f23 2019-03-11 stsp goto done;
313 5e1c9f23 2019-03-11 stsp }
314 0cd1c46a 2019-03-11 stsp } else
315 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", parent);
316 e08cc72d 2019-02-05 stsp }
317 5e1c9f23 2019-03-11 stsp done:
318 5e1c9f23 2019-03-11 stsp free(parent);
319 0cd1c46a 2019-03-11 stsp return err;
320 e08cc72d 2019-02-05 stsp }
321 0cd1c46a 2019-03-11 stsp
322 0cd1c46a 2019-03-11 stsp const struct got_error *
323 0cd1c46a 2019-03-11 stsp got_path_mkdir(const char *abspath)
324 0cd1c46a 2019-03-11 stsp {
325 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
326 0cd1c46a 2019-03-11 stsp
327 0cd1c46a 2019-03-11 stsp if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
328 ddcd8544 2019-03-11 stsp if (errno == ENOENT) {
329 0cd1c46a 2019-03-11 stsp err = make_parent_dirs(abspath);
330 0cd1c46a 2019-03-11 stsp if (err)
331 0cd1c46a 2019-03-11 stsp goto done;
332 0cd1c46a 2019-03-11 stsp if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1)
333 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", abspath);
334 0cd1c46a 2019-03-11 stsp } else
335 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", abspath);
336 0cd1c46a 2019-03-11 stsp }
337 0cd1c46a 2019-03-11 stsp
338 0cd1c46a 2019-03-11 stsp done:
339 0cd1c46a 2019-03-11 stsp return err;
340 3c45a30a 2019-05-12 jcs }
341 3c45a30a 2019-05-12 jcs
342 3c45a30a 2019-05-12 jcs int
343 280f921b 2019-05-12 stsp got_path_dir_is_empty(const char *dir)
344 3c45a30a 2019-05-12 jcs {
345 3c45a30a 2019-05-12 jcs DIR *d;
346 3c45a30a 2019-05-12 jcs struct dirent *dent;
347 3c45a30a 2019-05-12 jcs int empty = 1;
348 3c45a30a 2019-05-12 jcs
349 3c45a30a 2019-05-12 jcs d = opendir(dir);
350 3c45a30a 2019-05-12 jcs if (d == NULL)
351 3c45a30a 2019-05-12 jcs return 1;
352 3c45a30a 2019-05-12 jcs
353 3c45a30a 2019-05-12 jcs while ((dent = readdir(d)) != NULL) {
354 3c45a30a 2019-05-12 jcs if (strcmp(dent->d_name, ".") == 0 ||
355 3c45a30a 2019-05-12 jcs strcmp(dent->d_name, "..") == 0)
356 3c45a30a 2019-05-12 jcs continue;
357 3c45a30a 2019-05-12 jcs
358 3c45a30a 2019-05-12 jcs empty = 0;
359 3c45a30a 2019-05-12 jcs break;
360 3c45a30a 2019-05-12 jcs }
361 3c45a30a 2019-05-12 jcs
362 7f2a8dc2 2019-05-12 stsp closedir(d);
363 3c45a30a 2019-05-12 jcs return empty;
364 d1667f0d 2019-03-11 stsp }
365 d1667f0d 2019-03-11 stsp
366 d1667f0d 2019-03-11 stsp const struct got_error *
367 d1667f0d 2019-03-11 stsp got_path_dirname(char **parent, const char *path)
368 d1667f0d 2019-03-11 stsp {
369 0c4004e3 2020-10-20 stsp char buf[PATH_MAX];
370 d1667f0d 2019-03-11 stsp char *p;
371 d1667f0d 2019-03-11 stsp
372 0c4004e3 2020-10-20 stsp if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
373 0c4004e3 2020-10-20 stsp return got_error(GOT_ERR_NO_SPACE);
374 0c4004e3 2020-10-20 stsp
375 0c4004e3 2020-10-20 stsp p = dirname(buf);
376 d1667f0d 2019-03-11 stsp if (p == NULL)
377 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
378 d1667f0d 2019-03-11 stsp
379 d1667f0d 2019-03-11 stsp if (p[0] == '.' && p[1] == '\0')
380 63f810e6 2020-02-29 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
381 d1667f0d 2019-03-11 stsp
382 d1667f0d 2019-03-11 stsp *parent = strdup(p);
383 d1667f0d 2019-03-11 stsp if (*parent == NULL)
384 f2ea84fa 2019-07-27 stsp return got_error_from_errno("strdup");
385 f2ea84fa 2019-07-27 stsp
386 f2ea84fa 2019-07-27 stsp return NULL;
387 f2ea84fa 2019-07-27 stsp }
388 f2ea84fa 2019-07-27 stsp
389 f2ea84fa 2019-07-27 stsp const struct got_error *
390 20ccae39 2020-07-21 stsp got_path_dirent_type(int *type, const char *path_parent, struct dirent *dent)
391 20ccae39 2020-07-21 stsp {
392 20ccae39 2020-07-21 stsp const struct got_error *err = NULL;
393 20ccae39 2020-07-21 stsp char *path_child;
394 20ccae39 2020-07-21 stsp struct stat sb;
395 20ccae39 2020-07-21 stsp
396 20ccae39 2020-07-21 stsp if (dent->d_type != DT_UNKNOWN) {
397 20ccae39 2020-07-21 stsp *type = dent->d_type;
398 20ccae39 2020-07-21 stsp return NULL;
399 20ccae39 2020-07-21 stsp }
400 20ccae39 2020-07-21 stsp
401 20ccae39 2020-07-21 stsp *type = DT_UNKNOWN;
402 20ccae39 2020-07-21 stsp
403 20ccae39 2020-07-21 stsp /*
404 20ccae39 2020-07-21 stsp * This is a fallback to accommodate filesystems which do not
405 20ccae39 2020-07-21 stsp * provide directory entry type information. DT_UNKNOWN directory
406 20ccae39 2020-07-21 stsp * entries occur on NFS mounts without "readdir plus" RPC.
407 20ccae39 2020-07-21 stsp */
408 20ccae39 2020-07-21 stsp
409 20ccae39 2020-07-21 stsp if (asprintf(&path_child, "%s/%s", path_parent, dent->d_name) == -1)
410 20ccae39 2020-07-21 stsp return got_error_from_errno("asprintf");
411 20ccae39 2020-07-21 stsp
412 20ccae39 2020-07-21 stsp if (lstat(path_child, &sb) == -1) {
413 20ccae39 2020-07-21 stsp err = got_error_from_errno2("lstat", path_child);
414 20ccae39 2020-07-21 stsp goto done;
415 20ccae39 2020-07-21 stsp }
416 20ccae39 2020-07-21 stsp
417 20ccae39 2020-07-21 stsp if (S_ISFIFO(sb.st_mode))
418 20ccae39 2020-07-21 stsp *type = DT_FIFO;
419 20ccae39 2020-07-21 stsp else if (S_ISCHR(sb.st_mode))
420 20ccae39 2020-07-21 stsp *type = DT_CHR;
421 20ccae39 2020-07-21 stsp else if (S_ISDIR(sb.st_mode))
422 20ccae39 2020-07-21 stsp *type = DT_DIR;
423 20ccae39 2020-07-21 stsp else if (S_ISBLK(sb.st_mode))
424 20ccae39 2020-07-21 stsp *type = DT_BLK;
425 20ccae39 2020-07-21 stsp else if (S_ISLNK(sb.st_mode))
426 20ccae39 2020-07-21 stsp *type = DT_LNK;
427 20ccae39 2020-07-21 stsp else if (S_ISREG(sb.st_mode))
428 20ccae39 2020-07-21 stsp *type = DT_REG;
429 20ccae39 2020-07-21 stsp else if (S_ISSOCK(sb.st_mode))
430 20ccae39 2020-07-21 stsp *type = DT_SOCK;
431 20ccae39 2020-07-21 stsp done:
432 20ccae39 2020-07-21 stsp free(path_child);
433 20ccae39 2020-07-21 stsp return err;
434 20ccae39 2020-07-21 stsp }
435 20ccae39 2020-07-21 stsp
436 20ccae39 2020-07-21 stsp const struct got_error *
437 f2ea84fa 2019-07-27 stsp got_path_basename(char **s, const char *path)
438 f2ea84fa 2019-07-27 stsp {
439 0a9483d0 2020-10-19 stsp char buf[PATH_MAX];
440 f2ea84fa 2019-07-27 stsp char *base;
441 f2ea84fa 2019-07-27 stsp
442 0a9483d0 2020-10-19 stsp if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
443 0a9483d0 2020-10-19 stsp return got_error(GOT_ERR_NO_SPACE);
444 0a9483d0 2020-10-19 stsp
445 0a9483d0 2020-10-19 stsp base = basename(buf);
446 f2ea84fa 2019-07-27 stsp if (base == NULL)
447 f2ea84fa 2019-07-27 stsp return got_error_from_errno2("basename", path);
448 f2ea84fa 2019-07-27 stsp
449 f2ea84fa 2019-07-27 stsp *s = strdup(base);
450 f2ea84fa 2019-07-27 stsp if (*s == NULL)
451 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
452 d1667f0d 2019-03-11 stsp
453 d1667f0d 2019-03-11 stsp return NULL;
454 0cd1c46a 2019-03-11 stsp }
455 72151b04 2019-05-11 stsp
456 72151b04 2019-05-11 stsp void
457 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(char *path)
458 72151b04 2019-05-11 stsp {
459 455de7fa 2020-01-12 tb size_t x;
460 72151b04 2019-05-11 stsp
461 455de7fa 2020-01-12 tb x = strlen(path);
462 455de7fa 2020-01-12 tb while (x-- > 0 && path[x] == '/')
463 72151b04 2019-05-11 stsp path[x] = '\0';
464 0ee7065d 2019-05-13 stsp }
465 0ee7065d 2019-05-13 stsp
466 7ffb205f 2022-01-06 thomas /* based on findprog() from usr.bin/which/which.c */
467 0ee7065d 2019-05-13 stsp const struct got_error *
468 0ee7065d 2019-05-13 stsp got_path_find_prog(char **filename, const char *prog)
469 0ee7065d 2019-05-13 stsp {
470 202329ae 2019-08-11 stsp const struct got_error *err = NULL;
471 0c6f49ba 2022-07-01 thomas const char *path;
472 0ee7065d 2019-05-13 stsp char *p;
473 0ee7065d 2019-05-13 stsp int len;
474 0ee7065d 2019-05-13 stsp struct stat sbuf;
475 0c6f49ba 2022-07-01 thomas char *pathcpy, *dup = NULL;
476 0ee7065d 2019-05-13 stsp
477 0ee7065d 2019-05-13 stsp *filename = NULL;
478 0ee7065d 2019-05-13 stsp
479 0ee7065d 2019-05-13 stsp path = getenv("PATH");
480 0ee7065d 2019-05-13 stsp if (path == NULL)
481 0ee7065d 2019-05-13 stsp path = _PATH_DEFPATH;
482 0ee7065d 2019-05-13 stsp
483 0ee7065d 2019-05-13 stsp /* Special case if prog contains '/' */
484 0ee7065d 2019-05-13 stsp if (strchr(prog, '/')) {
485 0ee7065d 2019-05-13 stsp if ((stat(prog, &sbuf) == 0) && S_ISREG(sbuf.st_mode) &&
486 0ee7065d 2019-05-13 stsp access(prog, X_OK) == 0) {
487 0ee7065d 2019-05-13 stsp *filename = strdup(prog);
488 0ee7065d 2019-05-13 stsp if (*filename == NULL)
489 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
490 0ee7065d 2019-05-13 stsp }
491 0ee7065d 2019-05-13 stsp return NULL;
492 0ee7065d 2019-05-13 stsp }
493 0ee7065d 2019-05-13 stsp
494 0c6f49ba 2022-07-01 thomas if ((dup = strdup(path)) == NULL)
495 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
496 0c6f49ba 2022-07-01 thomas pathcpy = dup;
497 0ee7065d 2019-05-13 stsp
498 0ee7065d 2019-05-13 stsp while ((p = strsep(&pathcpy, ":")) != NULL) {
499 0c6f49ba 2022-07-01 thomas const char *d;
500 0ee7065d 2019-05-13 stsp
501 0ee7065d 2019-05-13 stsp len = strlen(p);
502 0ee7065d 2019-05-13 stsp while (len > 0 && p[len-1] == '/')
503 0ee7065d 2019-05-13 stsp p[--len] = '\0'; /* strip trailing '/' */
504 0ee7065d 2019-05-13 stsp
505 0c6f49ba 2022-07-01 thomas d = p;
506 0c6f49ba 2022-07-01 thomas if (*d == '\0')
507 0c6f49ba 2022-07-01 thomas d = ".";
508 0c6f49ba 2022-07-01 thomas
509 0c6f49ba 2022-07-01 thomas if (asprintf(filename, "%s/%s", d, prog) == -1) {
510 202329ae 2019-08-11 stsp err = got_error_from_errno("asprintf");
511 718ef3e9 2019-08-11 stsp break;
512 0ee7065d 2019-05-13 stsp }
513 0ee7065d 2019-05-13 stsp if ((stat(*filename, &sbuf) == 0) && S_ISREG(sbuf.st_mode) &&
514 718ef3e9 2019-08-11 stsp access(*filename, X_OK) == 0)
515 718ef3e9 2019-08-11 stsp break;
516 0ee7065d 2019-05-13 stsp free(*filename);
517 0ee7065d 2019-05-13 stsp *filename = NULL;
518 0ee7065d 2019-05-13 stsp }
519 0c6f49ba 2022-07-01 thomas free(dup);
520 5cade901 2019-09-22 stsp return err;
521 2c7829a4 2019-06-17 stsp }
522 2c7829a4 2019-06-17 stsp
523 2c7829a4 2019-06-17 stsp const struct got_error *
524 2c7829a4 2019-06-17 stsp got_path_create_file(const char *path, const char *content)
525 2c7829a4 2019-06-17 stsp {
526 2c7829a4 2019-06-17 stsp const struct got_error *err = NULL;
527 2c7829a4 2019-06-17 stsp int fd = -1;
528 2c7829a4 2019-06-17 stsp
529 06340621 2021-12-31 thomas fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC,
530 2c7829a4 2019-06-17 stsp GOT_DEFAULT_FILE_MODE);
531 2c7829a4 2019-06-17 stsp if (fd == -1) {
532 2c7829a4 2019-06-17 stsp err = got_error_from_errno2("open", path);
533 2c7829a4 2019-06-17 stsp goto done;
534 2c7829a4 2019-06-17 stsp }
535 2c7829a4 2019-06-17 stsp
536 2c7829a4 2019-06-17 stsp if (content) {
537 2c7829a4 2019-06-17 stsp int len = dprintf(fd, "%s\n", content);
538 2c7829a4 2019-06-17 stsp if (len != strlen(content) + 1) {
539 2c7829a4 2019-06-17 stsp err = got_error_from_errno("dprintf");
540 2c7829a4 2019-06-17 stsp goto done;
541 2c7829a4 2019-06-17 stsp }
542 2c7829a4 2019-06-17 stsp }
543 2c7829a4 2019-06-17 stsp
544 2c7829a4 2019-06-17 stsp done:
545 2c7829a4 2019-06-17 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
546 2c7829a4 2019-06-17 stsp err = got_error_from_errno("close");
547 2c7829a4 2019-06-17 stsp return err;
548 72151b04 2019-05-11 stsp }
549 301ae139 2022-10-16 thomas
550 301ae139 2022-10-16 thomas const struct got_error *
551 301ae139 2022-10-16 thomas got_path_move_file(const char *oldpath, const char *newpath)
552 301ae139 2022-10-16 thomas {
553 301ae139 2022-10-16 thomas const struct got_error *err;
554 301ae139 2022-10-16 thomas
555 301ae139 2022-10-16 thomas if (rename(oldpath, newpath) != -1)
556 301ae139 2022-10-16 thomas return NULL;
557 301ae139 2022-10-16 thomas
558 301ae139 2022-10-16 thomas if (errno != ENOENT)
559 301ae139 2022-10-16 thomas return got_error_from_errno3("rename", oldpath, newpath);
560 301ae139 2022-10-16 thomas
561 301ae139 2022-10-16 thomas err = make_parent_dirs(newpath);
562 301ae139 2022-10-16 thomas if (err)
563 301ae139 2022-10-16 thomas return err;
564 301ae139 2022-10-16 thomas
565 301ae139 2022-10-16 thomas if (rename(oldpath, newpath) == -1)
566 301ae139 2022-10-16 thomas return got_error_from_errno3("rename", oldpath, newpath);
567 301ae139 2022-10-16 thomas
568 301ae139 2022-10-16 thomas return NULL;
569 301ae139 2022-10-16 thomas }