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 4027f31a 2017-11-04 stsp #include <limits.h>
24 0cd1c46a 2019-03-11 stsp #include <libgen.h>
25 4027f31a 2017-11-04 stsp #include <stdlib.h>
26 4027f31a 2017-11-04 stsp #include <unistd.h>
27 4027f31a 2017-11-04 stsp #include <stdio.h>
28 4027f31a 2017-11-04 stsp #include <string.h>
29 3c45a30a 2019-05-12 jcs #include <dirent.h>
30 0ee7065d 2019-05-13 stsp #include <paths.h>
31 4027f31a 2017-11-04 stsp
32 9d31a1d8 2018-03-11 stsp #include "got_error.h"
33 324d37e7 2019-05-11 stsp #include "got_path.h"
34 1411938b 2018-02-12 stsp
35 e0159033 2019-01-08 stsp #ifndef MIN
36 e0159033 2019-01-08 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
37 e0159033 2019-01-08 stsp #endif
38 e0159033 2019-01-08 stsp
39 4027f31a 2017-11-04 stsp int
40 4027f31a 2017-11-04 stsp got_path_is_absolute(const char *path)
41 4027f31a 2017-11-04 stsp {
42 4027f31a 2017-11-04 stsp return path[0] == '/';
43 4027f31a 2017-11-04 stsp }
44 4027f31a 2017-11-04 stsp
45 4027f31a 2017-11-04 stsp char *
46 4027f31a 2017-11-04 stsp got_path_get_absolute(const char *relpath)
47 4027f31a 2017-11-04 stsp {
48 4027f31a 2017-11-04 stsp char cwd[PATH_MAX];
49 4027f31a 2017-11-04 stsp char *abspath;
50 4027f31a 2017-11-04 stsp
51 4027f31a 2017-11-04 stsp if (getcwd(cwd, sizeof(cwd)) == NULL)
52 4027f31a 2017-11-04 stsp return NULL;
53 4027f31a 2017-11-04 stsp
54 4027f31a 2017-11-04 stsp if (asprintf(&abspath, "%s/%s/", cwd, relpath) == -1)
55 4027f31a 2017-11-04 stsp return NULL;
56 4027f31a 2017-11-04 stsp
57 4027f31a 2017-11-04 stsp return abspath;
58 4027f31a 2017-11-04 stsp }
59 4027f31a 2017-11-04 stsp
60 4027f31a 2017-11-04 stsp char *
61 4027f31a 2017-11-04 stsp got_path_normalize(const char *path)
62 4027f31a 2017-11-04 stsp {
63 4027f31a 2017-11-04 stsp char *resolved;
64 4027f31a 2017-11-04 stsp
65 4027f31a 2017-11-04 stsp resolved = realpath(path, NULL);
66 4027f31a 2017-11-04 stsp if (resolved == NULL)
67 4027f31a 2017-11-04 stsp return NULL;
68 4027f31a 2017-11-04 stsp
69 4027f31a 2017-11-04 stsp if (!got_path_is_absolute(resolved)) {
70 4027f31a 2017-11-04 stsp char *abspath = got_path_get_absolute(resolved);
71 4027f31a 2017-11-04 stsp free(resolved);
72 4027f31a 2017-11-04 stsp resolved = abspath;
73 4027f31a 2017-11-04 stsp }
74 4027f31a 2017-11-04 stsp
75 4027f31a 2017-11-04 stsp return resolved;
76 4027f31a 2017-11-04 stsp }
77 e6eac3b8 2018-06-17 stsp
78 f7d20e89 2018-06-17 stsp /* based on canonpath() from kern_pledge.c */
79 f7d20e89 2018-06-17 stsp const struct got_error *
80 e6eac3b8 2018-06-17 stsp got_canonpath(const char *input, char *buf, size_t bufsize)
81 e6eac3b8 2018-06-17 stsp {
82 e6eac3b8 2018-06-17 stsp const char *p;
83 e6eac3b8 2018-06-17 stsp char *q;
84 e6eac3b8 2018-06-17 stsp
85 e6eac3b8 2018-06-17 stsp /* can't canon relative paths, don't bother */
86 e6eac3b8 2018-06-17 stsp if (!got_path_is_absolute(input)) {
87 e6eac3b8 2018-06-17 stsp if (strlcpy(buf, input, bufsize) >= bufsize)
88 f7d20e89 2018-06-17 stsp return got_error(GOT_ERR_NO_SPACE);
89 f7d20e89 2018-06-17 stsp return NULL;
90 e6eac3b8 2018-06-17 stsp }
91 e6eac3b8 2018-06-17 stsp
92 e6eac3b8 2018-06-17 stsp p = input;
93 e6eac3b8 2018-06-17 stsp q = buf;
94 e6eac3b8 2018-06-17 stsp while (*p && (q - buf < bufsize)) {
95 e6eac3b8 2018-06-17 stsp if (p[0] == '/' && (p[1] == '/' || p[1] == '\0')) {
96 e6eac3b8 2018-06-17 stsp p += 1;
97 e6eac3b8 2018-06-17 stsp
98 e6eac3b8 2018-06-17 stsp } else if (p[0] == '/' && p[1] == '.' &&
99 e6eac3b8 2018-06-17 stsp (p[2] == '/' || p[2] == '\0')) {
100 e6eac3b8 2018-06-17 stsp p += 2;
101 e6eac3b8 2018-06-17 stsp
102 e6eac3b8 2018-06-17 stsp } else if (p[0] == '/' && p[1] == '.' && p[2] == '.' &&
103 e6eac3b8 2018-06-17 stsp (p[3] == '/' || p[3] == '\0')) {
104 e6eac3b8 2018-06-17 stsp p += 3;
105 e6eac3b8 2018-06-17 stsp if (q != buf) /* "/../" at start of buf */
106 e6eac3b8 2018-06-17 stsp while (*--q != '/')
107 e6eac3b8 2018-06-17 stsp continue;
108 e6eac3b8 2018-06-17 stsp
109 e6eac3b8 2018-06-17 stsp } else {
110 e6eac3b8 2018-06-17 stsp *q++ = *p++;
111 e6eac3b8 2018-06-17 stsp }
112 e6eac3b8 2018-06-17 stsp }
113 e6eac3b8 2018-06-17 stsp if ((*p == '\0') && (q - buf < bufsize)) {
114 e6eac3b8 2018-06-17 stsp *q = 0;
115 f7d20e89 2018-06-17 stsp return NULL;
116 e6eac3b8 2018-06-17 stsp } else
117 f7d20e89 2018-06-17 stsp return got_error(GOT_ERR_NO_SPACE);
118 e6eac3b8 2018-06-17 stsp }
119 04ca23f4 2018-07-16 stsp
120 04ca23f4 2018-07-16 stsp const struct got_error *
121 04ca23f4 2018-07-16 stsp got_path_skip_common_ancestor(char **child, const char *parent_abspath,
122 04ca23f4 2018-07-16 stsp const char *abspath)
123 04ca23f4 2018-07-16 stsp {
124 04ca23f4 2018-07-16 stsp const struct got_error *err = NULL;
125 04ca23f4 2018-07-16 stsp size_t len_parent, len, bufsize;
126 db5ba8ed 2019-03-26 stsp
127 db5ba8ed 2019-03-26 stsp *child = NULL;
128 04ca23f4 2018-07-16 stsp
129 04ca23f4 2018-07-16 stsp len_parent = strlen(parent_abspath);
130 04ca23f4 2018-07-16 stsp len = strlen(abspath);
131 04ca23f4 2018-07-16 stsp if (len_parent >= len)
132 04ca23f4 2018-07-16 stsp return got_error(GOT_ERR_BAD_PATH);
133 04ca23f4 2018-07-16 stsp if (strncmp(parent_abspath, abspath, len_parent) != 0)
134 04ca23f4 2018-07-16 stsp return got_error(GOT_ERR_BAD_PATH);
135 eb4304b9 2019-05-09 stsp if (!got_path_is_root_dir(parent_abspath) && abspath[len_parent] != '/')
136 04ca23f4 2018-07-16 stsp return got_error(GOT_ERR_BAD_PATH);
137 5e3ce57a 2019-03-26 stsp while (abspath[len_parent] == '/')
138 5e3ce57a 2019-03-26 stsp abspath++;
139 04ca23f4 2018-07-16 stsp bufsize = len - len_parent + 1;
140 04ca23f4 2018-07-16 stsp *child = malloc(bufsize);
141 04ca23f4 2018-07-16 stsp if (*child == NULL)
142 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
143 04ca23f4 2018-07-16 stsp if (strlcpy(*child, abspath + len_parent, bufsize) >= bufsize) {
144 638f9024 2019-05-13 stsp err = got_error_from_errno("strlcpy");
145 04ca23f4 2018-07-16 stsp free(*child);
146 04ca23f4 2018-07-16 stsp *child = NULL;
147 04ca23f4 2018-07-16 stsp return err;
148 04ca23f4 2018-07-16 stsp }
149 04ca23f4 2018-07-16 stsp return NULL;
150 04ca23f4 2018-07-16 stsp }
151 31cedeaf 2018-09-15 stsp
152 31cedeaf 2018-09-15 stsp int
153 31cedeaf 2018-09-15 stsp got_path_is_root_dir(const char *path)
154 31cedeaf 2018-09-15 stsp {
155 31cedeaf 2018-09-15 stsp return (path[0] == '/' && path[1] == '\0');
156 a129376b 2019-03-28 stsp }
157 a129376b 2019-03-28 stsp
158 a129376b 2019-03-28 stsp int
159 a129376b 2019-03-28 stsp got_path_is_current_dir(const char *path)
160 a129376b 2019-03-28 stsp {
161 a129376b 2019-03-28 stsp return (path[0] == '.' && path[1] == '\0');
162 31cedeaf 2018-09-15 stsp }
163 e0159033 2019-01-08 stsp
164 e0159033 2019-01-08 stsp int
165 8da9e5f4 2019-01-12 stsp got_path_is_child(const char *child, const char *parent, size_t parent_len)
166 8da9e5f4 2019-01-12 stsp {
167 eb4304b9 2019-05-09 stsp if (parent_len == 0 || got_path_is_root_dir(parent))
168 8da9e5f4 2019-01-12 stsp return 1;
169 8da9e5f4 2019-01-12 stsp
170 8da9e5f4 2019-01-12 stsp if (strncmp(parent, child, parent_len) != 0)
171 8da9e5f4 2019-01-12 stsp return 0;
172 8da9e5f4 2019-01-12 stsp if (child[parent_len] != '/')
173 8da9e5f4 2019-01-12 stsp return 0;
174 8da9e5f4 2019-01-12 stsp
175 8da9e5f4 2019-01-12 stsp return 1;
176 8da9e5f4 2019-01-12 stsp }
177 8da9e5f4 2019-01-12 stsp
178 8da9e5f4 2019-01-12 stsp int
179 1beed999 2019-01-12 stsp got_path_cmp(const char *path1, const char *path2)
180 e0159033 2019-01-08 stsp {
181 e0159033 2019-01-08 stsp size_t len1 = strlen(path1);
182 e0159033 2019-01-08 stsp size_t len2 = strlen(path2);
183 e0159033 2019-01-08 stsp size_t min_len = MIN(len1, len2);
184 e0159033 2019-01-08 stsp size_t i = 0;
185 e0159033 2019-01-08 stsp
186 e08cc72d 2019-02-05 stsp /* Leading directory separators are insignificant. */
187 e08cc72d 2019-02-05 stsp while (path1[0] == '/')
188 e08cc72d 2019-02-05 stsp path1++;
189 e08cc72d 2019-02-05 stsp while (path2[0] == '/')
190 e08cc72d 2019-02-05 stsp path2++;
191 e08cc72d 2019-02-05 stsp
192 e08cc72d 2019-02-05 stsp len1 = strlen(path1);
193 e08cc72d 2019-02-05 stsp len2 = strlen(path2);
194 e08cc72d 2019-02-05 stsp min_len = MIN(len1, len2);
195 e08cc72d 2019-02-05 stsp
196 e0159033 2019-01-08 stsp /* Skip over common prefix. */
197 e0159033 2019-01-08 stsp while (i < min_len && path1[i] == path2[i])
198 e0159033 2019-01-08 stsp i++;
199 e0159033 2019-01-08 stsp
200 e08cc72d 2019-02-05 stsp /* Are the paths exactly equal (besides path separators)? */
201 e0159033 2019-01-08 stsp if (len1 == len2 && i >= min_len)
202 e0159033 2019-01-08 stsp return 0;
203 e0159033 2019-01-08 stsp
204 e08cc72d 2019-02-05 stsp /* Skip over redundant trailing path seperators. */
205 e08cc72d 2019-02-05 stsp while (path1[i] == '/' && path1[i + 1] == '/')
206 e08cc72d 2019-02-05 stsp path1++;
207 e08cc72d 2019-02-05 stsp while (path2[i] == '/' && path2[i + 1] == '/')
208 e08cc72d 2019-02-05 stsp path2++;
209 e08cc72d 2019-02-05 stsp
210 e08cc72d 2019-02-05 stsp /* Trailing path separators are insignificant. */
211 e08cc72d 2019-02-05 stsp if (path1[i] == '/' && path1[i + 1] == '\0' && path2[i] == '\0')
212 e08cc72d 2019-02-05 stsp return 0;
213 e08cc72d 2019-02-05 stsp if (path2[i] == '/' && path2[i + 1] == '\0' && path1[i] == '\0')
214 e08cc72d 2019-02-05 stsp return 0;
215 e08cc72d 2019-02-05 stsp
216 e0159033 2019-01-08 stsp /* Order children in subdirectories directly after their parents. */
217 e0159033 2019-01-08 stsp if (path1[i] == '/' && path2[i] == '\0')
218 e0159033 2019-01-08 stsp return 1;
219 e0159033 2019-01-08 stsp if (path2[i] == '/' && path1[i] == '\0')
220 e0159033 2019-01-08 stsp return -1;
221 e08cc72d 2019-02-05 stsp if (path1[i] == '/' && path2[i] != '\0')
222 e0159033 2019-01-08 stsp return -1;
223 e08cc72d 2019-02-05 stsp if (path2[i] == '/' && path1[i] != '\0')
224 e0159033 2019-01-08 stsp return 1;
225 e0159033 2019-01-08 stsp
226 e0159033 2019-01-08 stsp /* Next character following the common prefix determines order. */
227 e0159033 2019-01-08 stsp return (unsigned char)path1[i] < (unsigned char)path2[i] ? -1 : 1;
228 e0159033 2019-01-08 stsp }
229 e08cc72d 2019-02-05 stsp
230 e08cc72d 2019-02-05 stsp const struct got_error *
231 7e5c804b 2019-02-05 stsp got_pathlist_insert(struct got_pathlist_entry **inserted,
232 3d8df59c 2019-02-05 stsp struct got_pathlist_head *pathlist, const char *path, void *data)
233 e08cc72d 2019-02-05 stsp {
234 e08cc72d 2019-02-05 stsp struct got_pathlist_entry *new, *pe;
235 7e5c804b 2019-02-05 stsp
236 7e5c804b 2019-02-05 stsp if (inserted)
237 7e5c804b 2019-02-05 stsp *inserted = NULL;
238 e08cc72d 2019-02-05 stsp
239 e08cc72d 2019-02-05 stsp new = malloc(sizeof(*new));
240 e08cc72d 2019-02-05 stsp if (new == NULL)
241 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
242 e08cc72d 2019-02-05 stsp new->path = path;
243 3d8df59c 2019-02-05 stsp new->data = data;
244 e08cc72d 2019-02-05 stsp
245 e08cc72d 2019-02-05 stsp /*
246 e08cc72d 2019-02-05 stsp * Many callers will provide paths in a somewhat sorted order while
247 e08cc72d 2019-02-05 stsp * constructing a path list from inputs such as tree objects or
248 e08cc72d 2019-02-05 stsp * dirents. Iterating backwards from the tail of the list should
249 e08cc72d 2019-02-05 stsp * be more efficient than traversing through the entire list each
250 e08cc72d 2019-02-05 stsp * time an element is inserted.
251 e08cc72d 2019-02-05 stsp */
252 e08cc72d 2019-02-05 stsp pe = TAILQ_LAST(pathlist, got_pathlist_head);
253 e08cc72d 2019-02-05 stsp while (pe) {
254 e08cc72d 2019-02-05 stsp int cmp = got_path_cmp(pe->path, path);
255 e08cc72d 2019-02-05 stsp if (cmp == 0) {
256 e08cc72d 2019-02-05 stsp free(new); /* duplicate */
257 e08cc72d 2019-02-05 stsp return NULL;
258 e08cc72d 2019-02-05 stsp } else if (cmp < 0) {
259 e08cc72d 2019-02-05 stsp TAILQ_INSERT_AFTER(pathlist, pe, new, entry);
260 7e5c804b 2019-02-05 stsp if (inserted)
261 7e5c804b 2019-02-05 stsp *inserted = new;
262 e08cc72d 2019-02-05 stsp return NULL;
263 e08cc72d 2019-02-05 stsp }
264 e08cc72d 2019-02-05 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
265 e08cc72d 2019-02-05 stsp }
266 e08cc72d 2019-02-05 stsp
267 e08cc72d 2019-02-05 stsp TAILQ_INSERT_HEAD(pathlist, new, entry);
268 7e5c804b 2019-02-05 stsp if (inserted)
269 7e5c804b 2019-02-05 stsp *inserted = new;
270 e08cc72d 2019-02-05 stsp return NULL;
271 e08cc72d 2019-02-05 stsp }
272 e08cc72d 2019-02-05 stsp
273 e08cc72d 2019-02-05 stsp void
274 e08cc72d 2019-02-05 stsp got_pathlist_free(struct got_pathlist_head *pathlist)
275 e08cc72d 2019-02-05 stsp {
276 e08cc72d 2019-02-05 stsp struct got_pathlist_entry *pe;
277 e08cc72d 2019-02-05 stsp
278 e08cc72d 2019-02-05 stsp while ((pe = TAILQ_FIRST(pathlist)) != NULL) {
279 e08cc72d 2019-02-05 stsp TAILQ_REMOVE(pathlist, pe, entry);
280 e08cc72d 2019-02-05 stsp free(pe);
281 0cd1c46a 2019-03-11 stsp }
282 0cd1c46a 2019-03-11 stsp }
283 0cd1c46a 2019-03-11 stsp
284 0cd1c46a 2019-03-11 stsp static const struct got_error *
285 0cd1c46a 2019-03-11 stsp make_parent_dirs(const char *abspath)
286 0cd1c46a 2019-03-11 stsp {
287 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
288 d1667f0d 2019-03-11 stsp char *parent;
289 0cd1c46a 2019-03-11 stsp
290 d1667f0d 2019-03-11 stsp err = got_path_dirname(&parent, abspath);
291 d1667f0d 2019-03-11 stsp if (err)
292 d1667f0d 2019-03-11 stsp return err;
293 0cd1c46a 2019-03-11 stsp
294 0cd1c46a 2019-03-11 stsp if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
295 0cd1c46a 2019-03-11 stsp if (errno == ENOENT) {
296 0cd1c46a 2019-03-11 stsp err = make_parent_dirs(parent);
297 0cd1c46a 2019-03-11 stsp if (err)
298 5e1c9f23 2019-03-11 stsp goto done;
299 5e1c9f23 2019-03-11 stsp if (mkdir(parent, GOT_DEFAULT_DIR_MODE) == -1) {
300 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", parent);
301 5e1c9f23 2019-03-11 stsp goto done;
302 5e1c9f23 2019-03-11 stsp }
303 0cd1c46a 2019-03-11 stsp } else
304 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", parent);
305 e08cc72d 2019-02-05 stsp }
306 5e1c9f23 2019-03-11 stsp done:
307 5e1c9f23 2019-03-11 stsp free(parent);
308 0cd1c46a 2019-03-11 stsp return err;
309 e08cc72d 2019-02-05 stsp }
310 0cd1c46a 2019-03-11 stsp
311 0cd1c46a 2019-03-11 stsp const struct got_error *
312 0cd1c46a 2019-03-11 stsp got_path_mkdir(const char *abspath)
313 0cd1c46a 2019-03-11 stsp {
314 0cd1c46a 2019-03-11 stsp const struct got_error *err = NULL;
315 0cd1c46a 2019-03-11 stsp
316 0cd1c46a 2019-03-11 stsp if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
317 ddcd8544 2019-03-11 stsp if (errno == ENOENT) {
318 0cd1c46a 2019-03-11 stsp err = make_parent_dirs(abspath);
319 0cd1c46a 2019-03-11 stsp if (err)
320 0cd1c46a 2019-03-11 stsp goto done;
321 0cd1c46a 2019-03-11 stsp if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1)
322 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", abspath);
323 0cd1c46a 2019-03-11 stsp } else
324 638f9024 2019-05-13 stsp err = got_error_from_errno2("mkdir", abspath);
325 0cd1c46a 2019-03-11 stsp }
326 0cd1c46a 2019-03-11 stsp
327 0cd1c46a 2019-03-11 stsp done:
328 0cd1c46a 2019-03-11 stsp return err;
329 3c45a30a 2019-05-12 jcs }
330 3c45a30a 2019-05-12 jcs
331 3c45a30a 2019-05-12 jcs int
332 280f921b 2019-05-12 stsp got_path_dir_is_empty(const char *dir)
333 3c45a30a 2019-05-12 jcs {
334 3c45a30a 2019-05-12 jcs DIR *d;
335 3c45a30a 2019-05-12 jcs struct dirent *dent;
336 3c45a30a 2019-05-12 jcs int empty = 1;
337 3c45a30a 2019-05-12 jcs
338 3c45a30a 2019-05-12 jcs d = opendir(dir);
339 3c45a30a 2019-05-12 jcs if (d == NULL)
340 3c45a30a 2019-05-12 jcs return 1;
341 3c45a30a 2019-05-12 jcs
342 3c45a30a 2019-05-12 jcs while ((dent = readdir(d)) != NULL) {
343 3c45a30a 2019-05-12 jcs if (strcmp(dent->d_name, ".") == 0 ||
344 3c45a30a 2019-05-12 jcs strcmp(dent->d_name, "..") == 0)
345 3c45a30a 2019-05-12 jcs continue;
346 3c45a30a 2019-05-12 jcs
347 3c45a30a 2019-05-12 jcs empty = 0;
348 3c45a30a 2019-05-12 jcs break;
349 3c45a30a 2019-05-12 jcs }
350 3c45a30a 2019-05-12 jcs
351 7f2a8dc2 2019-05-12 stsp closedir(d);
352 3c45a30a 2019-05-12 jcs return empty;
353 d1667f0d 2019-03-11 stsp }
354 d1667f0d 2019-03-11 stsp
355 d1667f0d 2019-03-11 stsp const struct got_error *
356 d1667f0d 2019-03-11 stsp got_path_dirname(char **parent, const char *path)
357 d1667f0d 2019-03-11 stsp {
358 d1667f0d 2019-03-11 stsp char *p;
359 d1667f0d 2019-03-11 stsp
360 d1667f0d 2019-03-11 stsp p = dirname(path);
361 d1667f0d 2019-03-11 stsp if (p == NULL)
362 638f9024 2019-05-13 stsp return got_error_from_errno2("dirname", path);
363 d1667f0d 2019-03-11 stsp
364 d1667f0d 2019-03-11 stsp if (p[0] == '.' && p[1] == '\0')
365 d1667f0d 2019-03-11 stsp return got_error(GOT_ERR_BAD_PATH);
366 d1667f0d 2019-03-11 stsp
367 d1667f0d 2019-03-11 stsp *parent = strdup(p);
368 d1667f0d 2019-03-11 stsp if (*parent == NULL)
369 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
370 d1667f0d 2019-03-11 stsp
371 d1667f0d 2019-03-11 stsp return NULL;
372 0cd1c46a 2019-03-11 stsp }
373 72151b04 2019-05-11 stsp
374 72151b04 2019-05-11 stsp void
375 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(char *path)
376 72151b04 2019-05-11 stsp {
377 72151b04 2019-05-11 stsp int x;
378 72151b04 2019-05-11 stsp
379 72151b04 2019-05-11 stsp while (path[x = strlen(path) - 1] == '/')
380 72151b04 2019-05-11 stsp path[x] = '\0';
381 0ee7065d 2019-05-13 stsp }
382 0ee7065d 2019-05-13 stsp
383 0ee7065d 2019-05-13 stsp const struct got_error *
384 0ee7065d 2019-05-13 stsp got_path_find_prog(char **filename, const char *prog)
385 0ee7065d 2019-05-13 stsp {
386 0ee7065d 2019-05-13 stsp char *p;
387 0ee7065d 2019-05-13 stsp int len;
388 0ee7065d 2019-05-13 stsp struct stat sbuf;
389 0ee7065d 2019-05-13 stsp char *path, *pathcpy;
390 0ee7065d 2019-05-13 stsp
391 0ee7065d 2019-05-13 stsp *filename = NULL;
392 0ee7065d 2019-05-13 stsp
393 0ee7065d 2019-05-13 stsp path = getenv("PATH");
394 0ee7065d 2019-05-13 stsp if (path == NULL)
395 0ee7065d 2019-05-13 stsp path = _PATH_DEFPATH;
396 0ee7065d 2019-05-13 stsp
397 0ee7065d 2019-05-13 stsp /* Special case if prog contains '/' */
398 0ee7065d 2019-05-13 stsp if (strchr(prog, '/')) {
399 0ee7065d 2019-05-13 stsp if ((stat(prog, &sbuf) == 0) && S_ISREG(sbuf.st_mode) &&
400 0ee7065d 2019-05-13 stsp access(prog, X_OK) == 0) {
401 0ee7065d 2019-05-13 stsp *filename = strdup(prog);
402 0ee7065d 2019-05-13 stsp if (*filename == NULL)
403 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
404 0ee7065d 2019-05-13 stsp }
405 0ee7065d 2019-05-13 stsp return NULL;
406 0ee7065d 2019-05-13 stsp }
407 0ee7065d 2019-05-13 stsp
408 0ee7065d 2019-05-13 stsp if ((path = strdup(path)) == NULL)
409 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
410 0ee7065d 2019-05-13 stsp pathcpy = path;
411 0ee7065d 2019-05-13 stsp
412 0ee7065d 2019-05-13 stsp while ((p = strsep(&pathcpy, ":")) != NULL) {
413 0ee7065d 2019-05-13 stsp if (*p == '\0')
414 0ee7065d 2019-05-13 stsp p = ".";
415 0ee7065d 2019-05-13 stsp
416 0ee7065d 2019-05-13 stsp len = strlen(p);
417 0ee7065d 2019-05-13 stsp while (len > 0 && p[len-1] == '/')
418 0ee7065d 2019-05-13 stsp p[--len] = '\0'; /* strip trailing '/' */
419 0ee7065d 2019-05-13 stsp
420 0ee7065d 2019-05-13 stsp if (asprintf(filename, "%s/%s", p, prog) == -1) {
421 0ee7065d 2019-05-13 stsp free(path);
422 0ee7065d 2019-05-13 stsp return got_error_from_errno("asprintf");
423 0ee7065d 2019-05-13 stsp }
424 0ee7065d 2019-05-13 stsp if ((stat(*filename, &sbuf) == 0) && S_ISREG(sbuf.st_mode) &&
425 0ee7065d 2019-05-13 stsp access(*filename, X_OK) == 0) {
426 0ee7065d 2019-05-13 stsp free(path);
427 0ee7065d 2019-05-13 stsp return NULL;
428 0ee7065d 2019-05-13 stsp }
429 0ee7065d 2019-05-13 stsp free(*filename);
430 0ee7065d 2019-05-13 stsp *filename = NULL;
431 0ee7065d 2019-05-13 stsp continue;
432 0ee7065d 2019-05-13 stsp }
433 0ee7065d 2019-05-13 stsp free(path);
434 0ee7065d 2019-05-13 stsp return NULL;
435 72151b04 2019-05-11 stsp }