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 7b19e0f1 2017-11-05 stsp *
5 7b19e0f1 2017-11-05 stsp * Permission to use, copy, modify, and distribute this software for any
6 7b19e0f1 2017-11-05 stsp * purpose with or without fee is hereby granted, provided that the above
7 7b19e0f1 2017-11-05 stsp * copyright notice and this permission notice appear in all copies.
8 7b19e0f1 2017-11-05 stsp *
9 7b19e0f1 2017-11-05 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 7b19e0f1 2017-11-05 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 7b19e0f1 2017-11-05 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 7b19e0f1 2017-11-05 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 7b19e0f1 2017-11-05 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 7b19e0f1 2017-11-05 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 7b19e0f1 2017-11-05 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 7b19e0f1 2017-11-05 stsp */
17 7b19e0f1 2017-11-05 stsp
18 e08cc72d 2019-02-05 stsp #include <sys/queue.h>
19 0cd1c46a 2019-03-11 stsp #include <sys/stat.h>
20 e08cc72d 2019-02-05 stsp
21 0cd1c46a 2019-03-11 stsp #include <errno.h>
22 4027f31a 2017-11-04 stsp #include <limits.h>
23 0cd1c46a 2019-03-11 stsp #include <libgen.h>
24 4027f31a 2017-11-04 stsp #include <stdlib.h>
25 4027f31a 2017-11-04 stsp #include <unistd.h>
26 4027f31a 2017-11-04 stsp #include <stdio.h>
27 4027f31a 2017-11-04 stsp #include <string.h>
28 4027f31a 2017-11-04 stsp
29 9d31a1d8 2018-03-11 stsp #include "got_error.h"
30 9d31a1d8 2018-03-11 stsp
31 718b3ab0 2018-03-17 stsp #include "got_lib_path.h"
32 1411938b 2018-02-12 stsp
33 e0159033 2019-01-08 stsp #ifndef MIN
34 e0159033 2019-01-08 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
35 e0159033 2019-01-08 stsp #endif
36 e0159033 2019-01-08 stsp
37 4027f31a 2017-11-04 stsp int
38 4027f31a 2017-11-04 stsp got_path_is_absolute(const char *path)
39 4027f31a 2017-11-04 stsp {
40 4027f31a 2017-11-04 stsp return path[0] == '/';
41 4027f31a 2017-11-04 stsp }
42 4027f31a 2017-11-04 stsp
43 4027f31a 2017-11-04 stsp char *
44 4027f31a 2017-11-04 stsp got_path_get_absolute(const char *relpath)
45 4027f31a 2017-11-04 stsp {
46 4027f31a 2017-11-04 stsp char cwd[PATH_MAX];
47 4027f31a 2017-11-04 stsp char *abspath;
48 4027f31a 2017-11-04 stsp
49 4027f31a 2017-11-04 stsp if (getcwd(cwd, sizeof(cwd)) == NULL)
50 4027f31a 2017-11-04 stsp return NULL;
51 4027f31a 2017-11-04 stsp
52 4027f31a 2017-11-04 stsp if (asprintf(&abspath, "%s/%s/", cwd, relpath) == -1)
53 4027f31a 2017-11-04 stsp return NULL;
54 4027f31a 2017-11-04 stsp
55 4027f31a 2017-11-04 stsp return abspath;
56 4027f31a 2017-11-04 stsp }
57 4027f31a 2017-11-04 stsp
58 4027f31a 2017-11-04 stsp char *
59 4027f31a 2017-11-04 stsp got_path_normalize(const char *path)
60 4027f31a 2017-11-04 stsp {
61 4027f31a 2017-11-04 stsp char *resolved;
62 4027f31a 2017-11-04 stsp
63 4027f31a 2017-11-04 stsp resolved = realpath(path, NULL);
64 4027f31a 2017-11-04 stsp if (resolved == NULL)
65 4027f31a 2017-11-04 stsp return NULL;
66 4027f31a 2017-11-04 stsp
67 4027f31a 2017-11-04 stsp if (!got_path_is_absolute(resolved)) {
68 4027f31a 2017-11-04 stsp char *abspath = got_path_get_absolute(resolved);
69 4027f31a 2017-11-04 stsp free(resolved);
70 4027f31a 2017-11-04 stsp resolved = abspath;
71 4027f31a 2017-11-04 stsp }
72 4027f31a 2017-11-04 stsp
73 4027f31a 2017-11-04 stsp return resolved;
74 4027f31a 2017-11-04 stsp }
75 e6eac3b8 2018-06-17 stsp
76 f7d20e89 2018-06-17 stsp /* based on canonpath() from kern_pledge.c */
77 f7d20e89 2018-06-17 stsp const struct got_error *
78 e6eac3b8 2018-06-17 stsp got_canonpath(const char *input, char *buf, size_t bufsize)
79 e6eac3b8 2018-06-17 stsp {
80 e6eac3b8 2018-06-17 stsp const char *p;
81 e6eac3b8 2018-06-17 stsp char *q;
82 e6eac3b8 2018-06-17 stsp
83 e6eac3b8 2018-06-17 stsp /* can't canon relative paths, don't bother */
84 e6eac3b8 2018-06-17 stsp if (!got_path_is_absolute(input)) {
85 e6eac3b8 2018-06-17 stsp if (strlcpy(buf, input, bufsize) >= bufsize)
86 f7d20e89 2018-06-17 stsp return got_error(GOT_ERR_NO_SPACE);
87 f7d20e89 2018-06-17 stsp return NULL;
88 e6eac3b8 2018-06-17 stsp }
89 e6eac3b8 2018-06-17 stsp
90 e6eac3b8 2018-06-17 stsp p = input;
91 e6eac3b8 2018-06-17 stsp q = buf;
92 e6eac3b8 2018-06-17 stsp while (*p && (q - buf < bufsize)) {
93 e6eac3b8 2018-06-17 stsp if (p[0] == '/' && (p[1] == '/' || p[1] == '\0')) {
94 e6eac3b8 2018-06-17 stsp p += 1;
95 e6eac3b8 2018-06-17 stsp
96 e6eac3b8 2018-06-17 stsp } else if (p[0] == '/' && p[1] == '.' &&
97 e6eac3b8 2018-06-17 stsp (p[2] == '/' || p[2] == '\0')) {
98 e6eac3b8 2018-06-17 stsp p += 2;
99 e6eac3b8 2018-06-17 stsp
100 e6eac3b8 2018-06-17 stsp } else if (p[0] == '/' && p[1] == '.' && p[2] == '.' &&
101 e6eac3b8 2018-06-17 stsp (p[3] == '/' || p[3] == '\0')) {
102 e6eac3b8 2018-06-17 stsp p += 3;
103 e6eac3b8 2018-06-17 stsp if (q != buf) /* "/../" at start of buf */
104 e6eac3b8 2018-06-17 stsp while (*--q != '/')
105 e6eac3b8 2018-06-17 stsp continue;
106 e6eac3b8 2018-06-17 stsp
107 e6eac3b8 2018-06-17 stsp } else {
108 e6eac3b8 2018-06-17 stsp *q++ = *p++;
109 e6eac3b8 2018-06-17 stsp }
110 e6eac3b8 2018-06-17 stsp }
111 e6eac3b8 2018-06-17 stsp if ((*p == '\0') && (q - buf < bufsize)) {
112 e6eac3b8 2018-06-17 stsp *q = 0;
113 f7d20e89 2018-06-17 stsp return NULL;
114 e6eac3b8 2018-06-17 stsp } else
115 f7d20e89 2018-06-17 stsp return got_error(GOT_ERR_NO_SPACE);
116 e6eac3b8 2018-06-17 stsp }
117 04ca23f4 2018-07-16 stsp
118 04ca23f4 2018-07-16 stsp const struct got_error *
119 04ca23f4 2018-07-16 stsp got_path_skip_common_ancestor(char **child, const char *parent_abspath,
120 04ca23f4 2018-07-16 stsp const char *abspath)
121 04ca23f4 2018-07-16 stsp {
122 04ca23f4 2018-07-16 stsp const struct got_error *err = NULL;
123 04ca23f4 2018-07-16 stsp size_t len_parent, len, bufsize;
124 04ca23f4 2018-07-16 stsp
125 04ca23f4 2018-07-16 stsp len_parent = strlen(parent_abspath);
126 04ca23f4 2018-07-16 stsp len = strlen(abspath);
127 04ca23f4 2018-07-16 stsp if (len_parent >= len)
128 04ca23f4 2018-07-16 stsp return got_error(GOT_ERR_BAD_PATH);
129 04ca23f4 2018-07-16 stsp if (strncmp(parent_abspath, abspath, len_parent) != 0)
130 04ca23f4 2018-07-16 stsp return got_error(GOT_ERR_BAD_PATH);
131 04ca23f4 2018-07-16 stsp if (abspath[len_parent] != '/')
132 04ca23f4 2018-07-16 stsp return got_error(GOT_ERR_BAD_PATH);
133 04ca23f4 2018-07-16 stsp bufsize = len - len_parent + 1;
134 04ca23f4 2018-07-16 stsp *child = malloc(bufsize);
135 04ca23f4 2018-07-16 stsp if (*child == NULL)
136 04ca23f4 2018-07-16 stsp return got_error_from_errno();
137 04ca23f4 2018-07-16 stsp if (strlcpy(*child, abspath + len_parent, bufsize) >= bufsize) {
138 04ca23f4 2018-07-16 stsp err = got_error_from_errno();
139 04ca23f4 2018-07-16 stsp free(*child);
140 04ca23f4 2018-07-16 stsp *child = NULL;
141 04ca23f4 2018-07-16 stsp return err;
142 04ca23f4 2018-07-16 stsp }
143 04ca23f4 2018-07-16 stsp return NULL;
144 04ca23f4 2018-07-16 stsp }
145 31cedeaf 2018-09-15 stsp
146 31cedeaf 2018-09-15 stsp int
147 31cedeaf 2018-09-15 stsp got_path_is_root_dir(const char *path)
148 31cedeaf 2018-09-15 stsp {
149 31cedeaf 2018-09-15 stsp return (path[0] == '/' && path[1] == '\0');
150 31cedeaf 2018-09-15 stsp }
151 e0159033 2019-01-08 stsp
152 e0159033 2019-01-08 stsp int
153 8da9e5f4 2019-01-12 stsp got_path_is_child(const char *child, const char *parent, size_t parent_len)
154 8da9e5f4 2019-01-12 stsp {
155 8da9e5f4 2019-01-12 stsp if (parent_len == 0)
156 8da9e5f4 2019-01-12 stsp return 1;
157 8da9e5f4 2019-01-12 stsp
158 8da9e5f4 2019-01-12 stsp if (strncmp(parent, child, parent_len) != 0)
159 8da9e5f4 2019-01-12 stsp return 0;
160 8da9e5f4 2019-01-12 stsp if (child[parent_len] != '/')
161 8da9e5f4 2019-01-12 stsp return 0;
162 8da9e5f4 2019-01-12 stsp
163 8da9e5f4 2019-01-12 stsp return 1;
164 8da9e5f4 2019-01-12 stsp }
165 8da9e5f4 2019-01-12 stsp
166 8da9e5f4 2019-01-12 stsp int
167 1beed999 2019-01-12 stsp got_path_cmp(const char *path1, const char *path2)
168 e0159033 2019-01-08 stsp {
169 e0159033 2019-01-08 stsp size_t len1 = strlen(path1);
170 e0159033 2019-01-08 stsp size_t len2 = strlen(path2);
171 e0159033 2019-01-08 stsp size_t min_len = MIN(len1, len2);
172 e0159033 2019-01-08 stsp size_t i = 0;
173 e0159033 2019-01-08 stsp
174 e08cc72d 2019-02-05 stsp /* Leading directory separators are insignificant. */
175 e08cc72d 2019-02-05 stsp while (path1[0] == '/')
176 e08cc72d 2019-02-05 stsp path1++;
177 e08cc72d 2019-02-05 stsp while (path2[0] == '/')
178 e08cc72d 2019-02-05 stsp path2++;
179 e08cc72d 2019-02-05 stsp
180 e08cc72d 2019-02-05 stsp len1 = strlen(path1);
181 e08cc72d 2019-02-05 stsp len2 = strlen(path2);
182 e08cc72d 2019-02-05 stsp min_len = MIN(len1, len2);
183 e08cc72d 2019-02-05 stsp
184 e0159033 2019-01-08 stsp /* Skip over common prefix. */
185 e0159033 2019-01-08 stsp while (i < min_len && path1[i] == path2[i])
186 e0159033 2019-01-08 stsp i++;
187 e0159033 2019-01-08 stsp
188 e08cc72d 2019-02-05 stsp /* Are the paths exactly equal (besides path separators)? */
189 e0159033 2019-01-08 stsp if (len1 == len2 && i >= min_len)
190 e0159033 2019-01-08 stsp return 0;
191 e0159033 2019-01-08 stsp
192 e08cc72d 2019-02-05 stsp /* Skip over redundant trailing path seperators. */
193 e08cc72d 2019-02-05 stsp while (path1[i] == '/' && path1[i + 1] == '/')
194 e08cc72d 2019-02-05 stsp path1++;
195 e08cc72d 2019-02-05 stsp while (path2[i] == '/' && path2[i + 1] == '/')
196 e08cc72d 2019-02-05 stsp path2++;
197 e08cc72d 2019-02-05 stsp
198 e08cc72d 2019-02-05 stsp /* Trailing path separators are insignificant. */
199 e08cc72d 2019-02-05 stsp if (path1[i] == '/' && path1[i + 1] == '\0' && path2[i] == '\0')
200 e08cc72d 2019-02-05 stsp return 0;
201 e08cc72d 2019-02-05 stsp if (path2[i] == '/' && path2[i + 1] == '\0' && path1[i] == '\0')
202 e08cc72d 2019-02-05 stsp return 0;
203 e08cc72d 2019-02-05 stsp
204 e0159033 2019-01-08 stsp /* Order children in subdirectories directly after their parents. */
205 e0159033 2019-01-08 stsp if (path1[i] == '/' && path2[i] == '\0')
206 e0159033 2019-01-08 stsp return 1;
207 e0159033 2019-01-08 stsp if (path2[i] == '/' && path1[i] == '\0')
208 e0159033 2019-01-08 stsp return -1;
209 e08cc72d 2019-02-05 stsp if (path1[i] == '/' && path2[i] != '\0')
210 e0159033 2019-01-08 stsp return -1;
211 e08cc72d 2019-02-05 stsp if (path2[i] == '/' && path1[i] != '\0')
212 e0159033 2019-01-08 stsp return 1;
213 e0159033 2019-01-08 stsp
214 e0159033 2019-01-08 stsp /* Next character following the common prefix determines order. */
215 e0159033 2019-01-08 stsp return (unsigned char)path1[i] < (unsigned char)path2[i] ? -1 : 1;
216 e0159033 2019-01-08 stsp }
217 e08cc72d 2019-02-05 stsp
218 e08cc72d 2019-02-05 stsp const struct got_error *
219 7e5c804b 2019-02-05 stsp got_pathlist_insert(struct got_pathlist_entry **inserted,
220 3d8df59c 2019-02-05 stsp struct got_pathlist_head *pathlist, const char *path, void *data)
221 e08cc72d 2019-02-05 stsp {
222 e08cc72d 2019-02-05 stsp struct got_pathlist_entry *new, *pe;
223 7e5c804b 2019-02-05 stsp
224 7e5c804b 2019-02-05 stsp if (inserted)
225 7e5c804b 2019-02-05 stsp *inserted = NULL;
226 e08cc72d 2019-02-05 stsp
227 e08cc72d 2019-02-05 stsp new = malloc(sizeof(*new));
228 e08cc72d 2019-02-05 stsp if (new == NULL)
229 e08cc72d 2019-02-05 stsp return got_error_from_errno();
230 e08cc72d 2019-02-05 stsp new->path = path;
231 3d8df59c 2019-02-05 stsp new->data = data;
232 e08cc72d 2019-02-05 stsp
233 e08cc72d 2019-02-05 stsp /*
234 e08cc72d 2019-02-05 stsp * Many callers will provide paths in a somewhat sorted order while
235 e08cc72d 2019-02-05 stsp * constructing a path list from inputs such as tree objects or
236 e08cc72d 2019-02-05 stsp * dirents. Iterating backwards from the tail of the list should
237 e08cc72d 2019-02-05 stsp * be more efficient than traversing through the entire list each
238 e08cc72d 2019-02-05 stsp * time an element is inserted.
239 e08cc72d 2019-02-05 stsp */
240 e08cc72d 2019-02-05 stsp pe = TAILQ_LAST(pathlist, got_pathlist_head);
241 e08cc72d 2019-02-05 stsp while (pe) {
242 e08cc72d 2019-02-05 stsp int cmp = got_path_cmp(pe->path, path);
243 e08cc72d 2019-02-05 stsp if (cmp == 0) {
244 e08cc72d 2019-02-05 stsp free(new); /* duplicate */
245 e08cc72d 2019-02-05 stsp return NULL;
246 e08cc72d 2019-02-05 stsp } else if (cmp < 0) {
247 e08cc72d 2019-02-05 stsp TAILQ_INSERT_AFTER(pathlist, pe, new, entry);
248 7e5c804b 2019-02-05 stsp if (inserted)
249 7e5c804b 2019-02-05 stsp *inserted = new;
250 e08cc72d 2019-02-05 stsp return NULL;
251 e08cc72d 2019-02-05 stsp }
252 e08cc72d 2019-02-05 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
253 e08cc72d 2019-02-05 stsp }
254 e08cc72d 2019-02-05 stsp
255 e08cc72d 2019-02-05 stsp TAILQ_INSERT_HEAD(pathlist, new, entry);
256 7e5c804b 2019-02-05 stsp if (inserted)
257 7e5c804b 2019-02-05 stsp *inserted = new;
258 e08cc72d 2019-02-05 stsp return NULL;
259 e08cc72d 2019-02-05 stsp }
260 e08cc72d 2019-02-05 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 5e1c9f23 2019-03-11 stsp err = got_error_from_errno();
289 5e1c9f23 2019-03-11 stsp goto done;
290 5e1c9f23 2019-03-11 stsp }
291 0cd1c46a 2019-03-11 stsp } else
292 0cd1c46a 2019-03-11 stsp err = got_error_from_errno();
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 0cd1c46a 2019-03-11 stsp err = got_error_from_errno();
311 0cd1c46a 2019-03-11 stsp } else
312 0cd1c46a 2019-03-11 stsp err = got_error_from_errno();
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 d1667f0d 2019-03-11 stsp }
318 d1667f0d 2019-03-11 stsp
319 d1667f0d 2019-03-11 stsp const struct got_error *
320 d1667f0d 2019-03-11 stsp got_path_dirname(char **parent, const char *path)
321 d1667f0d 2019-03-11 stsp {
322 d1667f0d 2019-03-11 stsp char *p;
323 d1667f0d 2019-03-11 stsp
324 d1667f0d 2019-03-11 stsp p = dirname(path);
325 d1667f0d 2019-03-11 stsp if (p == NULL)
326 d1667f0d 2019-03-11 stsp return got_error_from_errno();
327 d1667f0d 2019-03-11 stsp
328 d1667f0d 2019-03-11 stsp if (p[0] == '.' && p[1] == '\0')
329 d1667f0d 2019-03-11 stsp return got_error(GOT_ERR_BAD_PATH);
330 d1667f0d 2019-03-11 stsp
331 d1667f0d 2019-03-11 stsp *parent = strdup(p);
332 d1667f0d 2019-03-11 stsp if (*parent == NULL)
333 d1667f0d 2019-03-11 stsp return got_error_from_errno();
334 d1667f0d 2019-03-11 stsp
335 d1667f0d 2019-03-11 stsp return NULL;
336 0cd1c46a 2019-03-11 stsp }