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