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 04ca23f4 2018-07-16 stsp }
117 04ca23f4 2018-07-16 stsp return NULL;
118 04ca23f4 2018-07-16 stsp }
119 31cedeaf 2018-09-15 stsp
120 31cedeaf 2018-09-15 stsp int
121 31cedeaf 2018-09-15 stsp got_path_is_root_dir(const char *path)
122 31cedeaf 2018-09-15 stsp {
123 b8b3f209 2020-02-29 stsp while (*path == '/')
124 b8b3f209 2020-02-29 stsp path++;
125 b8b3f209 2020-02-29 stsp return (*path == '\0');
126 a129376b 2019-03-28 stsp }
127 a129376b 2019-03-28 stsp
128 a129376b 2019-03-28 stsp int
129 a129376b 2019-03-28 stsp got_path_is_current_dir(const char *path)
130 a129376b 2019-03-28 stsp {
131 a129376b 2019-03-28 stsp return (path[0] == '.' && path[1] == '\0');
132 31cedeaf 2018-09-15 stsp }
133 e0159033 2019-01-08 stsp
134 e0159033 2019-01-08 stsp int
135 8da9e5f4 2019-01-12 stsp got_path_is_child(const char *child, const char *parent, size_t parent_len)
136 8da9e5f4 2019-01-12 stsp {
137 eb4304b9 2019-05-09 stsp if (parent_len == 0 || got_path_is_root_dir(parent))
138 8da9e5f4 2019-01-12 stsp return 1;
139 8da9e5f4 2019-01-12 stsp
140 8da9e5f4 2019-01-12 stsp if (strncmp(parent, child, parent_len) != 0)
141 8da9e5f4 2019-01-12 stsp return 0;
142 8da9e5f4 2019-01-12 stsp if (child[parent_len] != '/')
143 8da9e5f4 2019-01-12 stsp return 0;
144 8da9e5f4 2019-01-12 stsp
145 8da9e5f4 2019-01-12 stsp return 1;
146 8da9e5f4 2019-01-12 stsp }
147 8da9e5f4 2019-01-12 stsp
148 8da9e5f4 2019-01-12 stsp int
149 d572f586 2019-08-02 stsp got_path_cmp(const char *path1, const char *path2, size_t len1, size_t len2)
150 e0159033 2019-01-08 stsp {
151 466d3b32 2019-08-02 stsp size_t min_len;
152 e0159033 2019-01-08 stsp size_t i = 0;
153 e0159033 2019-01-08 stsp
154 e08cc72d 2019-02-05 stsp /* Leading directory separators are insignificant. */
155 466d3b32 2019-08-02 stsp while (path1[0] == '/') {
156 e08cc72d 2019-02-05 stsp path1++;
157 466d3b32 2019-08-02 stsp len1--;
158 466d3b32 2019-08-02 stsp }
159 466d3b32 2019-08-02 stsp while (path2[0] == '/') {
160 e08cc72d 2019-02-05 stsp path2++;
161 466d3b32 2019-08-02 stsp len2--;
162 466d3b32 2019-08-02 stsp }
163 e08cc72d 2019-02-05 stsp
164 e08cc72d 2019-02-05 stsp min_len = MIN(len1, len2);
165 e08cc72d 2019-02-05 stsp
166 e0159033 2019-01-08 stsp /* Skip over common prefix. */
167 e0159033 2019-01-08 stsp while (i < min_len && path1[i] == path2[i])
168 e0159033 2019-01-08 stsp i++;
169 e0159033 2019-01-08 stsp
170 e08cc72d 2019-02-05 stsp /* Are the paths exactly equal (besides path separators)? */
171 e0159033 2019-01-08 stsp if (len1 == len2 && i >= min_len)
172 e0159033 2019-01-08 stsp return 0;
173 e0159033 2019-01-08 stsp
174 e08cc72d 2019-02-05 stsp /* Skip over redundant trailing path seperators. */
175 e08cc72d 2019-02-05 stsp while (path1[i] == '/' && path1[i + 1] == '/')
176 e08cc72d 2019-02-05 stsp path1++;
177 e08cc72d 2019-02-05 stsp while (path2[i] == '/' && path2[i + 1] == '/')
178 e08cc72d 2019-02-05 stsp path2++;
179 e08cc72d 2019-02-05 stsp
180 e08cc72d 2019-02-05 stsp /* Trailing path separators are insignificant. */
181 e08cc72d 2019-02-05 stsp if (path1[i] == '/' && path1[i + 1] == '\0' && path2[i] == '\0')
182 e08cc72d 2019-02-05 stsp return 0;
183 e08cc72d 2019-02-05 stsp if (path2[i] == '/' && path2[i + 1] == '\0' && path1[i] == '\0')
184 e08cc72d 2019-02-05 stsp return 0;
185 e08cc72d 2019-02-05 stsp
186 e0159033 2019-01-08 stsp /* Order children in subdirectories directly after their parents. */
187 e0159033 2019-01-08 stsp if (path1[i] == '/' && path2[i] == '\0')
188 e0159033 2019-01-08 stsp return 1;
189 e0159033 2019-01-08 stsp if (path2[i] == '/' && path1[i] == '\0')
190 e0159033 2019-01-08 stsp return -1;
191 e08cc72d 2019-02-05 stsp if (path1[i] == '/' && path2[i] != '\0')
192 e0159033 2019-01-08 stsp return -1;
193 e08cc72d 2019-02-05 stsp if (path2[i] == '/' && path1[i] != '\0')
194 e0159033 2019-01-08 stsp return 1;
195 e0159033 2019-01-08 stsp
196 e0159033 2019-01-08 stsp /* Next character following the common prefix determines order. */
197 e0159033 2019-01-08 stsp return (unsigned char)path1[i] < (unsigned char)path2[i] ? -1 : 1;
198 e0159033 2019-01-08 stsp }
199 e08cc72d 2019-02-05 stsp
200 e08cc72d 2019-02-05 stsp const struct got_error *
201 7e5c804b 2019-02-05 stsp got_pathlist_insert(struct got_pathlist_entry **inserted,
202 3d8df59c 2019-02-05 stsp struct got_pathlist_head *pathlist, const char *path, void *data)
203 e08cc72d 2019-02-05 stsp {
204 e08cc72d 2019-02-05 stsp struct got_pathlist_entry *new, *pe;
205 7e5c804b 2019-02-05 stsp
206 7e5c804b 2019-02-05 stsp if (inserted)
207 7e5c804b 2019-02-05 stsp *inserted = NULL;
208 e08cc72d 2019-02-05 stsp
209 e08cc72d 2019-02-05 stsp new = malloc(sizeof(*new));
210 e08cc72d 2019-02-05 stsp if (new == NULL)
211 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
212 e08cc72d 2019-02-05 stsp new->path = path;
213 f2b16ada 2019-08-02 stsp new->path_len = strlen(path);
214 3d8df59c 2019-02-05 stsp new->data = data;
215 e08cc72d 2019-02-05 stsp
216 e08cc72d 2019-02-05 stsp /*
217 e08cc72d 2019-02-05 stsp * Many callers will provide paths in a somewhat sorted order while
218 e08cc72d 2019-02-05 stsp * constructing a path list from inputs such as tree objects or
219 e08cc72d 2019-02-05 stsp * dirents. Iterating backwards from the tail of the list should
220 e08cc72d 2019-02-05 stsp * be more efficient than traversing through the entire list each
221 e08cc72d 2019-02-05 stsp * time an element is inserted.
222 e08cc72d 2019-02-05 stsp */
223 e08cc72d 2019-02-05 stsp pe = TAILQ_LAST(pathlist, got_pathlist_head);
224 e08cc72d 2019-02-05 stsp while (pe) {
225 f2b16ada 2019-08-02 stsp int cmp = got_path_cmp(pe->path, new->path,
226 f2b16ada 2019-08-02 stsp pe->path_len, new->path_len);
227 e08cc72d 2019-02-05 stsp if (cmp == 0) {
228 e08cc72d 2019-02-05 stsp free(new); /* duplicate */
229 e08cc72d 2019-02-05 stsp return NULL;
230 e08cc72d 2019-02-05 stsp } else if (cmp < 0) {
231 e08cc72d 2019-02-05 stsp TAILQ_INSERT_AFTER(pathlist, pe, new, entry);
232 7e5c804b 2019-02-05 stsp if (inserted)
233 7e5c804b 2019-02-05 stsp *inserted = new;
234 e08cc72d 2019-02-05 stsp return NULL;
235 e08cc72d 2019-02-05 stsp }
236 e08cc72d 2019-02-05 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
237 e08cc72d 2019-02-05 stsp }
238 e08cc72d 2019-02-05 stsp
239 e08cc72d 2019-02-05 stsp TAILQ_INSERT_HEAD(pathlist, new, entry);
240 7e5c804b 2019-02-05 stsp if (inserted)
241 7e5c804b 2019-02-05 stsp *inserted = new;
242 e08cc72d 2019-02-05 stsp return NULL;
243 e08cc72d 2019-02-05 stsp }
244 72ea6654 2019-07-27 stsp
245 72ea6654 2019-07-27 stsp const struct got_error *
246 adc19d55 2019-07-28 stsp got_pathlist_append(struct got_pathlist_head *pathlist,
247 adc19d55 2019-07-28 stsp const char *path, void *data)
248 72ea6654 2019-07-27 stsp {
249 72ea6654 2019-07-27 stsp struct got_pathlist_entry *new;
250 e08cc72d 2019-02-05 stsp
251 72ea6654 2019-07-27 stsp new = malloc(sizeof(*new));
252 72ea6654 2019-07-27 stsp if (new == NULL)
253 72ea6654 2019-07-27 stsp return got_error_from_errno("malloc");
254 72ea6654 2019-07-27 stsp new->path = path;
255 f2b16ada 2019-08-02 stsp new->path_len = strlen(path);
256 72ea6654 2019-07-27 stsp new->data = data;
257 72ea6654 2019-07-27 stsp TAILQ_INSERT_TAIL(pathlist, new, entry);
258 72ea6654 2019-07-27 stsp return NULL;
259 72ea6654 2019-07-27 stsp }
260 72ea6654 2019-07-27 stsp
261 e08cc72d 2019-02-05 stsp void
262 e08cc72d 2019-02-05 stsp got_pathlist_free(struct got_pathlist_head *pathlist)
263 e08cc72d 2019-02-05 stsp {
264 e08cc72d 2019-02-05 stsp struct got_pathlist_entry *pe;
265 e08cc72d 2019-02-05 stsp
266 e08cc72d 2019-02-05 stsp while ((pe = TAILQ_FIRST(pathlist)) != NULL) {
267 e08cc72d 2019-02-05 stsp TAILQ_REMOVE(pathlist, pe, entry);
268 e08cc72d 2019-02-05 stsp free(pe);
269 0cd1c46a 2019-03-11 stsp }
270 0cd1c46a 2019-03-11 stsp }
271 0cd1c46a 2019-03-11 stsp
272 0cd1c46a 2019-03-11 stsp static const struct got_error *
273 0cd1c46a 2019-03-11 stsp make_parent_dirs(const char *abspath)
274 0cd1c46a 2019-03-11 stsp {
275 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
276 d1667f0d 2019-03-11 stsp char *parent;
277 0cd1c46a 2019-03-11 stsp
278 d1667f0d 2019-03-11 stsp err = got_path_dirname(&parent, abspath);
279 d1667f0d 2019-03-11 stsp if (err)
280 d1667f0d 2019-03-11 stsp return err;
281 0cd1c46a 2019-03-11 stsp
282 0cd1c46a 2019-03-11 stsp if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
283 0cd1c46a 2019-03-11 stsp if (errno == ENOENT) {
284 0cd1c46a 2019-03-11 stsp err = make_parent_dirs(parent);
285 0cd1c46a 2019-03-11 stsp if (err)
286 5e1c9f23 2019-03-11 stsp goto done;
287 5e1c9f23 2019-03-11 stsp if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
288 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", parent);
289 5e1c9f23 2019-03-11 stsp goto done;
290 5e1c9f23 2019-03-11 stsp }
291 0cd1c46a 2019-03-11 stsp } else
292 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", parent);
293 e08cc72d 2019-02-05 stsp }
294 5e1c9f23 2019-03-11 stsp done:
295 5e1c9f23 2019-03-11 stsp free(parent);
296 0cd1c46a 2019-03-11 stsp return err;
297 e08cc72d 2019-02-05 stsp }
298 0cd1c46a 2019-03-11 stsp
299 0cd1c46a 2019-03-11 stsp const struct got_error *
300 0cd1c46a 2019-03-11 stsp got_path_mkdir(const char *abspath)
301 0cd1c46a 2019-03-11 stsp {
302 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
303 0cd1c46a 2019-03-11 stsp
304 0cd1c46a 2019-03-11 stsp if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
305 ddcd8544 2019-03-11 stsp if (errno == ENOENT) {
306 0cd1c46a 2019-03-11 stsp err = make_parent_dirs(abspath);
307 0cd1c46a 2019-03-11 stsp if (err)
308 0cd1c46a 2019-03-11 stsp goto done;
309 0cd1c46a 2019-03-11 stsp if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1)
310 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", abspath);
311 0cd1c46a 2019-03-11 stsp } else
312 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", abspath);
313 0cd1c46a 2019-03-11 stsp }
314 0cd1c46a 2019-03-11 stsp
315 0cd1c46a 2019-03-11 stsp done:
316 0cd1c46a 2019-03-11 stsp return err;
317 3c45a30a 2019-05-12 jcs }
318 3c45a30a 2019-05-12 jcs
319 3c45a30a 2019-05-12 jcs int
320 280f921b 2019-05-12 stsp got_path_dir_is_empty(const char *dir)
321 3c45a30a 2019-05-12 jcs {
322 3c45a30a 2019-05-12 jcs DIR *d;
323 3c45a30a 2019-05-12 jcs struct dirent *dent;
324 3c45a30a 2019-05-12 jcs int empty = 1;
325 3c45a30a 2019-05-12 jcs
326 3c45a30a 2019-05-12 jcs d = opendir(dir);
327 3c45a30a 2019-05-12 jcs if (d == NULL)
328 3c45a30a 2019-05-12 jcs return 1;
329 3c45a30a 2019-05-12 jcs
330 3c45a30a 2019-05-12 jcs while ((dent = readdir(d)) != NULL) {
331 3c45a30a 2019-05-12 jcs if (strcmp(dent->d_name, ".") == 0 ||
332 3c45a30a 2019-05-12 jcs strcmp(dent->d_name, "..") == 0)
333 3c45a30a 2019-05-12 jcs continue;
334 3c45a30a 2019-05-12 jcs
335 3c45a30a 2019-05-12 jcs empty = 0;
336 3c45a30a 2019-05-12 jcs break;
337 3c45a30a 2019-05-12 jcs }
338 3c45a30a 2019-05-12 jcs
339 7f2a8dc2 2019-05-12 stsp closedir(d);
340 3c45a30a 2019-05-12 jcs return empty;
341 d1667f0d 2019-03-11 stsp }
342 d1667f0d 2019-03-11 stsp
343 d1667f0d 2019-03-11 stsp const struct got_error *
344 d1667f0d 2019-03-11 stsp got_path_dirname(char **parent, const char *path)
345 d1667f0d 2019-03-11 stsp {
346 0c4004e3 2020-10-20 stsp char buf[PATH_MAX];
347 d1667f0d 2019-03-11 stsp char *p;
348 d1667f0d 2019-03-11 stsp
349 0c4004e3 2020-10-20 stsp if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
350 0c4004e3 2020-10-20 stsp return got_error(GOT_ERR_NO_SPACE);
351 0c4004e3 2020-10-20 stsp
352 0c4004e3 2020-10-20 stsp p = dirname(buf);
353 d1667f0d 2019-03-11 stsp if (p == NULL)
354 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
355 d1667f0d 2019-03-11 stsp
356 d1667f0d 2019-03-11 stsp if (p[0] == '.' && p[1] == '\0')
357 63f810e6 2020-02-29 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
358 d1667f0d 2019-03-11 stsp
359 d1667f0d 2019-03-11 stsp *parent = strdup(p);
360 d1667f0d 2019-03-11 stsp if (*parent == NULL)
361 f2ea84fa 2019-07-27 stsp return got_error_from_errno("strdup");
362 f2ea84fa 2019-07-27 stsp
363 f2ea84fa 2019-07-27 stsp return NULL;
364 f2ea84fa 2019-07-27 stsp }
365 f2ea84fa 2019-07-27 stsp
366 f2ea84fa 2019-07-27 stsp const struct got_error *
367 20ccae39 2020-07-21 stsp got_path_dirent_type(int *type, const char *path_parent, struct dirent *dent)
368 20ccae39 2020-07-21 stsp {
369 20ccae39 2020-07-21 stsp const struct got_error *err = NULL;
370 20ccae39 2020-07-21 stsp char *path_child;
371 20ccae39 2020-07-21 stsp struct stat sb;
372 20ccae39 2020-07-21 stsp
373 20ccae39 2020-07-21 stsp if (dent->d_type != DT_UNKNOWN) {
374 20ccae39 2020-07-21 stsp *type = dent->d_type;
375 20ccae39 2020-07-21 stsp return NULL;
376 20ccae39 2020-07-21 stsp }
377 20ccae39 2020-07-21 stsp
378 20ccae39 2020-07-21 stsp *type = DT_UNKNOWN;
379 20ccae39 2020-07-21 stsp
380 20ccae39 2020-07-21 stsp /*
381 20ccae39 2020-07-21 stsp * This is a fallback to accommodate filesystems which do not
382 20ccae39 2020-07-21 stsp * provide directory entry type information. DT_UNKNOWN directory
383 20ccae39 2020-07-21 stsp * entries occur on NFS mounts without "readdir plus" RPC.
384 20ccae39 2020-07-21 stsp */
385 20ccae39 2020-07-21 stsp
386 20ccae39 2020-07-21 stsp if (asprintf(&path_child, "%s/%s", path_parent, dent->d_name) == -1)
387 20ccae39 2020-07-21 stsp return got_error_from_errno("asprintf");
388 20ccae39 2020-07-21 stsp
389 20ccae39 2020-07-21 stsp if (lstat(path_child, &sb) == -1) {
390 20ccae39 2020-07-21 stsp err = got_error_from_errno2("lstat", path_child);
391 20ccae39 2020-07-21 stsp goto done;
392 20ccae39 2020-07-21 stsp }
393 20ccae39 2020-07-21 stsp
394 20ccae39 2020-07-21 stsp if (S_ISFIFO(sb.st_mode))
395 20ccae39 2020-07-21 stsp *type = DT_FIFO;
396 20ccae39 2020-07-21 stsp else if (S_ISCHR(sb.st_mode))
397 20ccae39 2020-07-21 stsp *type = DT_CHR;
398 20ccae39 2020-07-21 stsp else if (S_ISDIR(sb.st_mode))
399 20ccae39 2020-07-21 stsp *type = DT_DIR;
400 20ccae39 2020-07-21 stsp else if (S_ISBLK(sb.st_mode))
401 20ccae39 2020-07-21 stsp *type = DT_BLK;
402 20ccae39 2020-07-21 stsp else if (S_ISLNK(sb.st_mode))
403 20ccae39 2020-07-21 stsp *type = DT_LNK;
404 20ccae39 2020-07-21 stsp else if (S_ISREG(sb.st_mode))
405 20ccae39 2020-07-21 stsp *type = DT_REG;
406 20ccae39 2020-07-21 stsp else if (S_ISSOCK(sb.st_mode))
407 20ccae39 2020-07-21 stsp *type = DT_SOCK;
408 20ccae39 2020-07-21 stsp done:
409 20ccae39 2020-07-21 stsp free(path_child);
410 20ccae39 2020-07-21 stsp return err;
411 20ccae39 2020-07-21 stsp }
412 20ccae39 2020-07-21 stsp
413 20ccae39 2020-07-21 stsp const struct got_error *
414 f2ea84fa 2019-07-27 stsp got_path_basename(char **s, const char *path)
415 f2ea84fa 2019-07-27 stsp {
416 0a9483d0 2020-10-19 stsp char buf[PATH_MAX];
417 f2ea84fa 2019-07-27 stsp char *base;
418 f2ea84fa 2019-07-27 stsp
419 0a9483d0 2020-10-19 stsp if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
420 0a9483d0 2020-10-19 stsp return got_error(GOT_ERR_NO_SPACE);
421 0a9483d0 2020-10-19 stsp
422 0a9483d0 2020-10-19 stsp base = basename(buf);
423 f2ea84fa 2019-07-27 stsp if (base == NULL)
424 f2ea84fa 2019-07-27 stsp return got_error_from_errno2("basename", path);
425 f2ea84fa 2019-07-27 stsp
426 f2ea84fa 2019-07-27 stsp *s = strdup(base);
427 f2ea84fa 2019-07-27 stsp if (*s == NULL)
428 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
429 d1667f0d 2019-03-11 stsp
430 d1667f0d 2019-03-11 stsp return NULL;
431 0cd1c46a 2019-03-11 stsp }
432 72151b04 2019-05-11 stsp
433 72151b04 2019-05-11 stsp void
434 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(char *path)
435 72151b04 2019-05-11 stsp {
436 455de7fa 2020-01-12 tb size_t x;
437 72151b04 2019-05-11 stsp
438 455de7fa 2020-01-12 tb x = strlen(path);
439 455de7fa 2020-01-12 tb while (x-- > 0 && path[x] == '/')
440 72151b04 2019-05-11 stsp path[x] = '\0';
441 0ee7065d 2019-05-13 stsp }
442 0ee7065d 2019-05-13 stsp
443 08680430 2019-05-13 stsp /* based on findprog() from usr.sbin/which/which.c */
444 0ee7065d 2019-05-13 stsp const struct got_error *
445 0ee7065d 2019-05-13 stsp got_path_find_prog(char **filename, const char *prog)
446 0ee7065d 2019-05-13 stsp {
447 202329ae 2019-08-11 stsp const struct got_error *err = NULL;
448 0ee7065d 2019-05-13 stsp char *p;
449 0ee7065d 2019-05-13 stsp int len;
450 0ee7065d 2019-05-13 stsp struct stat sbuf;
451 0ee7065d 2019-05-13 stsp char *path, *pathcpy;
452 0ee7065d 2019-05-13 stsp
453 0ee7065d 2019-05-13 stsp *filename = NULL;
454 0ee7065d 2019-05-13 stsp
455 0ee7065d 2019-05-13 stsp path = getenv("PATH");
456 0ee7065d 2019-05-13 stsp if (path == NULL)
457 0ee7065d 2019-05-13 stsp path = _PATH_DEFPATH;
458 0ee7065d 2019-05-13 stsp
459 0ee7065d 2019-05-13 stsp /* Special case if prog contains '/' */
460 0ee7065d 2019-05-13 stsp if (strchr(prog, '/')) {
461 0ee7065d 2019-05-13 stsp if ((stat(prog, &sbuf) == 0) && S_ISREG(sbuf.st_mode) &&
462 0ee7065d 2019-05-13 stsp access(prog, X_OK) == 0) {
463 0ee7065d 2019-05-13 stsp *filename = strdup(prog);
464 0ee7065d 2019-05-13 stsp if (*filename == NULL)
465 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
466 0ee7065d 2019-05-13 stsp }
467 0ee7065d 2019-05-13 stsp return NULL;
468 0ee7065d 2019-05-13 stsp }
469 0ee7065d 2019-05-13 stsp
470 0ee7065d 2019-05-13 stsp if ((path = strdup(path)) == NULL)
471 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
472 0ee7065d 2019-05-13 stsp pathcpy = path;
473 0ee7065d 2019-05-13 stsp
474 0ee7065d 2019-05-13 stsp while ((p = strsep(&pathcpy, ":")) != NULL) {
475 0ee7065d 2019-05-13 stsp if (*p == '\0')
476 0ee7065d 2019-05-13 stsp p = ".";
477 0ee7065d 2019-05-13 stsp
478 0ee7065d 2019-05-13 stsp len = strlen(p);
479 0ee7065d 2019-05-13 stsp while (len > 0 && p[len-1] == '/')
480 0ee7065d 2019-05-13 stsp p[--len] = '\0'; /* strip trailing '/' */
481 0ee7065d 2019-05-13 stsp
482 0ee7065d 2019-05-13 stsp if (asprintf(filename, "%s/%s", p, prog) == -1) {
483 202329ae 2019-08-11 stsp err = got_error_from_errno("asprintf");
484 718ef3e9 2019-08-11 stsp break;
485 0ee7065d 2019-05-13 stsp }
486 0ee7065d 2019-05-13 stsp if ((stat(*filename, &sbuf) == 0) && S_ISREG(sbuf.st_mode) &&
487 718ef3e9 2019-08-11 stsp access(*filename, X_OK) == 0)
488 718ef3e9 2019-08-11 stsp break;
489 0ee7065d 2019-05-13 stsp free(*filename);
490 0ee7065d 2019-05-13 stsp *filename = NULL;
491 0ee7065d 2019-05-13 stsp continue;
492 0ee7065d 2019-05-13 stsp }
493 0ee7065d 2019-05-13 stsp free(path);
494 5cade901 2019-09-22 stsp return err;
495 2c7829a4 2019-06-17 stsp }
496 2c7829a4 2019-06-17 stsp
497 2c7829a4 2019-06-17 stsp const struct got_error *
498 2c7829a4 2019-06-17 stsp got_path_create_file(const char *path, const char *content)
499 2c7829a4 2019-06-17 stsp {
500 2c7829a4 2019-06-17 stsp const struct got_error *err = NULL;
501 2c7829a4 2019-06-17 stsp int fd = -1;
502 2c7829a4 2019-06-17 stsp
503 2c7829a4 2019-06-17 stsp fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
504 2c7829a4 2019-06-17 stsp GOT_DEFAULT_FILE_MODE);
505 2c7829a4 2019-06-17 stsp if (fd == -1) {
506 2c7829a4 2019-06-17 stsp err = got_error_from_errno2("open", path);
507 2c7829a4 2019-06-17 stsp goto done;
508 2c7829a4 2019-06-17 stsp }
509 2c7829a4 2019-06-17 stsp
510 2c7829a4 2019-06-17 stsp if (content) {
511 2c7829a4 2019-06-17 stsp int len = dprintf(fd, "%s\n", content);
512 2c7829a4 2019-06-17 stsp if (len != strlen(content) + 1) {
513 2c7829a4 2019-06-17 stsp err = got_error_from_errno("dprintf");
514 2c7829a4 2019-06-17 stsp goto done;
515 2c7829a4 2019-06-17 stsp }
516 2c7829a4 2019-06-17 stsp }
517 2c7829a4 2019-06-17 stsp
518 2c7829a4 2019-06-17 stsp done:
519 2c7829a4 2019-06-17 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
520 2c7829a4 2019-06-17 stsp err = got_error_from_errno("close");
521 2c7829a4 2019-06-17 stsp return err;
522 72151b04 2019-05-11 stsp }