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