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 e08cc72d 2019-02-05 stsp
20 4027f31a 2017-11-04 stsp #include <limits.h>
21 4027f31a 2017-11-04 stsp #include <stdlib.h>
22 4027f31a 2017-11-04 stsp #include <unistd.h>
23 4027f31a 2017-11-04 stsp #include <stdio.h>
24 4027f31a 2017-11-04 stsp #include <string.h>
25 4027f31a 2017-11-04 stsp
26 9d31a1d8 2018-03-11 stsp #include "got_error.h"
27 9d31a1d8 2018-03-11 stsp
28 718b3ab0 2018-03-17 stsp #include "got_lib_path.h"
29 1411938b 2018-02-12 stsp
30 e0159033 2019-01-08 stsp #ifndef MIN
31 e0159033 2019-01-08 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
32 e0159033 2019-01-08 stsp #endif
33 e0159033 2019-01-08 stsp
34 4027f31a 2017-11-04 stsp int
35 4027f31a 2017-11-04 stsp got_path_is_absolute(const char *path)
36 4027f31a 2017-11-04 stsp {
37 4027f31a 2017-11-04 stsp return path[0] == '/';
38 4027f31a 2017-11-04 stsp }
39 4027f31a 2017-11-04 stsp
40 4027f31a 2017-11-04 stsp char *
41 4027f31a 2017-11-04 stsp got_path_get_absolute(const char *relpath)
42 4027f31a 2017-11-04 stsp {
43 4027f31a 2017-11-04 stsp char cwd[PATH_MAX];
44 4027f31a 2017-11-04 stsp char *abspath;
45 4027f31a 2017-11-04 stsp
46 4027f31a 2017-11-04 stsp if (getcwd(cwd, sizeof(cwd)) == NULL)
47 4027f31a 2017-11-04 stsp return NULL;
48 4027f31a 2017-11-04 stsp
49 4027f31a 2017-11-04 stsp if (asprintf(&abspath, "%s/%s/", cwd, relpath) == -1)
50 4027f31a 2017-11-04 stsp return NULL;
51 4027f31a 2017-11-04 stsp
52 4027f31a 2017-11-04 stsp return abspath;
53 4027f31a 2017-11-04 stsp }
54 4027f31a 2017-11-04 stsp
55 4027f31a 2017-11-04 stsp char *
56 4027f31a 2017-11-04 stsp got_path_normalize(const char *path)
57 4027f31a 2017-11-04 stsp {
58 4027f31a 2017-11-04 stsp char *resolved;
59 4027f31a 2017-11-04 stsp
60 4027f31a 2017-11-04 stsp resolved = realpath(path, NULL);
61 4027f31a 2017-11-04 stsp if (resolved == NULL)
62 4027f31a 2017-11-04 stsp return NULL;
63 4027f31a 2017-11-04 stsp
64 4027f31a 2017-11-04 stsp if (!got_path_is_absolute(resolved)) {
65 4027f31a 2017-11-04 stsp char *abspath = got_path_get_absolute(resolved);
66 4027f31a 2017-11-04 stsp free(resolved);
67 4027f31a 2017-11-04 stsp resolved = abspath;
68 4027f31a 2017-11-04 stsp }
69 4027f31a 2017-11-04 stsp
70 4027f31a 2017-11-04 stsp return resolved;
71 4027f31a 2017-11-04 stsp }
72 e6eac3b8 2018-06-17 stsp
73 f7d20e89 2018-06-17 stsp /* based on canonpath() from kern_pledge.c */
74 f7d20e89 2018-06-17 stsp const struct got_error *
75 e6eac3b8 2018-06-17 stsp got_canonpath(const char *input, char *buf, size_t bufsize)
76 e6eac3b8 2018-06-17 stsp {
77 e6eac3b8 2018-06-17 stsp const char *p;
78 e6eac3b8 2018-06-17 stsp char *q;
79 e6eac3b8 2018-06-17 stsp
80 e6eac3b8 2018-06-17 stsp /* can't canon relative paths, don't bother */
81 e6eac3b8 2018-06-17 stsp if (!got_path_is_absolute(input)) {
82 e6eac3b8 2018-06-17 stsp if (strlcpy(buf, input, bufsize) >= bufsize)
83 f7d20e89 2018-06-17 stsp return got_error(GOT_ERR_NO_SPACE);
84 f7d20e89 2018-06-17 stsp return NULL;
85 e6eac3b8 2018-06-17 stsp }
86 e6eac3b8 2018-06-17 stsp
87 e6eac3b8 2018-06-17 stsp p = input;
88 e6eac3b8 2018-06-17 stsp q = buf;
89 e6eac3b8 2018-06-17 stsp while (*p && (q - buf < bufsize)) {
90 e6eac3b8 2018-06-17 stsp if (p[0] == '/' && (p[1] == '/' || p[1] == '\0')) {
91 e6eac3b8 2018-06-17 stsp p += 1;
92 e6eac3b8 2018-06-17 stsp
93 e6eac3b8 2018-06-17 stsp } else if (p[0] == '/' && p[1] == '.' &&
94 e6eac3b8 2018-06-17 stsp (p[2] == '/' || p[2] == '\0')) {
95 e6eac3b8 2018-06-17 stsp p += 2;
96 e6eac3b8 2018-06-17 stsp
97 e6eac3b8 2018-06-17 stsp } else if (p[0] == '/' && p[1] == '.' && p[2] == '.' &&
98 e6eac3b8 2018-06-17 stsp (p[3] == '/' || p[3] == '\0')) {
99 e6eac3b8 2018-06-17 stsp p += 3;
100 e6eac3b8 2018-06-17 stsp if (q != buf) /* "/../" at start of buf */
101 e6eac3b8 2018-06-17 stsp while (*--q != '/')
102 e6eac3b8 2018-06-17 stsp continue;
103 e6eac3b8 2018-06-17 stsp
104 e6eac3b8 2018-06-17 stsp } else {
105 e6eac3b8 2018-06-17 stsp *q++ = *p++;
106 e6eac3b8 2018-06-17 stsp }
107 e6eac3b8 2018-06-17 stsp }
108 e6eac3b8 2018-06-17 stsp if ((*p == '\0') && (q - buf < bufsize)) {
109 e6eac3b8 2018-06-17 stsp *q = 0;
110 f7d20e89 2018-06-17 stsp return NULL;
111 e6eac3b8 2018-06-17 stsp } else
112 f7d20e89 2018-06-17 stsp return got_error(GOT_ERR_NO_SPACE);
113 e6eac3b8 2018-06-17 stsp }
114 04ca23f4 2018-07-16 stsp
115 04ca23f4 2018-07-16 stsp const struct got_error *
116 04ca23f4 2018-07-16 stsp got_path_skip_common_ancestor(char **child, const char *parent_abspath,
117 04ca23f4 2018-07-16 stsp const char *abspath)
118 04ca23f4 2018-07-16 stsp {
119 04ca23f4 2018-07-16 stsp const struct got_error *err = NULL;
120 04ca23f4 2018-07-16 stsp size_t len_parent, len, bufsize;
121 04ca23f4 2018-07-16 stsp
122 04ca23f4 2018-07-16 stsp len_parent = strlen(parent_abspath);
123 04ca23f4 2018-07-16 stsp len = strlen(abspath);
124 04ca23f4 2018-07-16 stsp if (len_parent >= len)
125 04ca23f4 2018-07-16 stsp return got_error(GOT_ERR_BAD_PATH);
126 04ca23f4 2018-07-16 stsp if (strncmp(parent_abspath, abspath, len_parent) != 0)
127 04ca23f4 2018-07-16 stsp return got_error(GOT_ERR_BAD_PATH);
128 04ca23f4 2018-07-16 stsp if (abspath[len_parent] != '/')
129 04ca23f4 2018-07-16 stsp return got_error(GOT_ERR_BAD_PATH);
130 04ca23f4 2018-07-16 stsp bufsize = len - len_parent + 1;
131 04ca23f4 2018-07-16 stsp *child = malloc(bufsize);
132 04ca23f4 2018-07-16 stsp if (*child == NULL)
133 04ca23f4 2018-07-16 stsp return got_error_from_errno();
134 04ca23f4 2018-07-16 stsp if (strlcpy(*child, abspath + len_parent, bufsize) >= bufsize) {
135 04ca23f4 2018-07-16 stsp err = got_error_from_errno();
136 04ca23f4 2018-07-16 stsp free(*child);
137 04ca23f4 2018-07-16 stsp *child = NULL;
138 04ca23f4 2018-07-16 stsp return err;
139 04ca23f4 2018-07-16 stsp }
140 04ca23f4 2018-07-16 stsp return NULL;
141 04ca23f4 2018-07-16 stsp }
142 31cedeaf 2018-09-15 stsp
143 31cedeaf 2018-09-15 stsp int
144 31cedeaf 2018-09-15 stsp got_path_is_root_dir(const char *path)
145 31cedeaf 2018-09-15 stsp {
146 31cedeaf 2018-09-15 stsp return (path[0] == '/' && path[1] == '\0');
147 31cedeaf 2018-09-15 stsp }
148 e0159033 2019-01-08 stsp
149 e0159033 2019-01-08 stsp int
150 8da9e5f4 2019-01-12 stsp got_path_is_child(const char *child, const char *parent, size_t parent_len)
151 8da9e5f4 2019-01-12 stsp {
152 8da9e5f4 2019-01-12 stsp if (parent_len == 0)
153 8da9e5f4 2019-01-12 stsp return 1;
154 8da9e5f4 2019-01-12 stsp
155 8da9e5f4 2019-01-12 stsp if (strncmp(parent, child, parent_len) != 0)
156 8da9e5f4 2019-01-12 stsp return 0;
157 8da9e5f4 2019-01-12 stsp if (child[parent_len] != '/')
158 8da9e5f4 2019-01-12 stsp return 0;
159 8da9e5f4 2019-01-12 stsp
160 8da9e5f4 2019-01-12 stsp return 1;
161 8da9e5f4 2019-01-12 stsp }
162 8da9e5f4 2019-01-12 stsp
163 8da9e5f4 2019-01-12 stsp int
164 1beed999 2019-01-12 stsp got_path_cmp(const char *path1, const char *path2)
165 e0159033 2019-01-08 stsp {
166 e0159033 2019-01-08 stsp size_t len1 = strlen(path1);
167 e0159033 2019-01-08 stsp size_t len2 = strlen(path2);
168 e0159033 2019-01-08 stsp size_t min_len = MIN(len1, len2);
169 e0159033 2019-01-08 stsp size_t i = 0;
170 e0159033 2019-01-08 stsp
171 e08cc72d 2019-02-05 stsp /* Leading directory separators are insignificant. */
172 e08cc72d 2019-02-05 stsp while (path1[0] == '/')
173 e08cc72d 2019-02-05 stsp path1++;
174 e08cc72d 2019-02-05 stsp while (path2[0] == '/')
175 e08cc72d 2019-02-05 stsp path2++;
176 e08cc72d 2019-02-05 stsp
177 e08cc72d 2019-02-05 stsp len1 = strlen(path1);
178 e08cc72d 2019-02-05 stsp len2 = strlen(path2);
179 e08cc72d 2019-02-05 stsp min_len = MIN(len1, len2);
180 e08cc72d 2019-02-05 stsp
181 e0159033 2019-01-08 stsp /* Skip over common prefix. */
182 e0159033 2019-01-08 stsp while (i < min_len && path1[i] == path2[i])
183 e0159033 2019-01-08 stsp i++;
184 e0159033 2019-01-08 stsp
185 e08cc72d 2019-02-05 stsp /* Are the paths exactly equal (besides path separators)? */
186 e0159033 2019-01-08 stsp if (len1 == len2 && i >= min_len)
187 e0159033 2019-01-08 stsp return 0;
188 e0159033 2019-01-08 stsp
189 e08cc72d 2019-02-05 stsp /* Skip over redundant trailing path seperators. */
190 e08cc72d 2019-02-05 stsp while (path1[i] == '/' && path1[i + 1] == '/')
191 e08cc72d 2019-02-05 stsp path1++;
192 e08cc72d 2019-02-05 stsp while (path2[i] == '/' && path2[i + 1] == '/')
193 e08cc72d 2019-02-05 stsp path2++;
194 e08cc72d 2019-02-05 stsp
195 e08cc72d 2019-02-05 stsp /* Trailing path separators are insignificant. */
196 e08cc72d 2019-02-05 stsp if (path1[i] == '/' && path1[i + 1] == '\0' && path2[i] == '\0')
197 e08cc72d 2019-02-05 stsp return 0;
198 e08cc72d 2019-02-05 stsp if (path2[i] == '/' && path2[i + 1] == '\0' && path1[i] == '\0')
199 e08cc72d 2019-02-05 stsp return 0;
200 e08cc72d 2019-02-05 stsp
201 e0159033 2019-01-08 stsp /* Order children in subdirectories directly after their parents. */
202 e0159033 2019-01-08 stsp if (path1[i] == '/' && path2[i] == '\0')
203 e0159033 2019-01-08 stsp return 1;
204 e0159033 2019-01-08 stsp if (path2[i] == '/' && path1[i] == '\0')
205 e0159033 2019-01-08 stsp return -1;
206 e08cc72d 2019-02-05 stsp if (path1[i] == '/' && path2[i] != '\0')
207 e0159033 2019-01-08 stsp return -1;
208 e08cc72d 2019-02-05 stsp if (path2[i] == '/' && path1[i] != '\0')
209 e0159033 2019-01-08 stsp return 1;
210 e0159033 2019-01-08 stsp
211 e0159033 2019-01-08 stsp /* Next character following the common prefix determines order. */
212 e0159033 2019-01-08 stsp return (unsigned char)path1[i] < (unsigned char)path2[i] ? -1 : 1;
213 e0159033 2019-01-08 stsp }
214 e08cc72d 2019-02-05 stsp
215 e08cc72d 2019-02-05 stsp const struct got_error *
216 7e5c804b 2019-02-05 stsp got_pathlist_insert(struct got_pathlist_entry **inserted,
217 3d8df59c 2019-02-05 stsp struct got_pathlist_head *pathlist, const char *path, void *data)
218 e08cc72d 2019-02-05 stsp {
219 e08cc72d 2019-02-05 stsp struct got_pathlist_entry *new, *pe;
220 7e5c804b 2019-02-05 stsp
221 7e5c804b 2019-02-05 stsp if (inserted)
222 7e5c804b 2019-02-05 stsp *inserted = NULL;
223 e08cc72d 2019-02-05 stsp
224 e08cc72d 2019-02-05 stsp new = malloc(sizeof(*new));
225 e08cc72d 2019-02-05 stsp if (new == NULL)
226 e08cc72d 2019-02-05 stsp return got_error_from_errno();
227 e08cc72d 2019-02-05 stsp new->path = path;
228 3d8df59c 2019-02-05 stsp new->data = data;
229 e08cc72d 2019-02-05 stsp
230 e08cc72d 2019-02-05 stsp /*
231 e08cc72d 2019-02-05 stsp * Many callers will provide paths in a somewhat sorted order while
232 e08cc72d 2019-02-05 stsp * constructing a path list from inputs such as tree objects or
233 e08cc72d 2019-02-05 stsp * dirents. Iterating backwards from the tail of the list should
234 e08cc72d 2019-02-05 stsp * be more efficient than traversing through the entire list each
235 e08cc72d 2019-02-05 stsp * time an element is inserted.
236 e08cc72d 2019-02-05 stsp */
237 e08cc72d 2019-02-05 stsp pe = TAILQ_LAST(pathlist, got_pathlist_head);
238 e08cc72d 2019-02-05 stsp while (pe) {
239 e08cc72d 2019-02-05 stsp int cmp = got_path_cmp(pe->path, path);
240 e08cc72d 2019-02-05 stsp if (cmp == 0) {
241 e08cc72d 2019-02-05 stsp free(new); /* duplicate */
242 e08cc72d 2019-02-05 stsp return NULL;
243 e08cc72d 2019-02-05 stsp } else if (cmp < 0) {
244 e08cc72d 2019-02-05 stsp TAILQ_INSERT_AFTER(pathlist, pe, new, entry);
245 7e5c804b 2019-02-05 stsp if (inserted)
246 7e5c804b 2019-02-05 stsp *inserted = new;
247 e08cc72d 2019-02-05 stsp return NULL;
248 e08cc72d 2019-02-05 stsp }
249 e08cc72d 2019-02-05 stsp pe = TAILQ_PREV(pe, got_pathlist_head, entry);
250 e08cc72d 2019-02-05 stsp }
251 e08cc72d 2019-02-05 stsp
252 e08cc72d 2019-02-05 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 e08cc72d 2019-02-05 stsp
258 e08cc72d 2019-02-05 stsp void
259 e08cc72d 2019-02-05 stsp got_pathlist_free(struct got_pathlist_head *pathlist)
260 e08cc72d 2019-02-05 stsp {
261 e08cc72d 2019-02-05 stsp struct got_pathlist_entry *pe;
262 e08cc72d 2019-02-05 stsp
263 e08cc72d 2019-02-05 stsp while ((pe = TAILQ_FIRST(pathlist)) != NULL) {
264 e08cc72d 2019-02-05 stsp TAILQ_REMOVE(pathlist, pe, entry);
265 e08cc72d 2019-02-05 stsp free(pe);
266 e08cc72d 2019-02-05 stsp }
267 e08cc72d 2019-02-05 stsp }