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