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 4027f31a 2017-11-04 stsp #include <limits.h>
19 4027f31a 2017-11-04 stsp #include <stdlib.h>
20 4027f31a 2017-11-04 stsp #include <unistd.h>
21 4027f31a 2017-11-04 stsp #include <stdio.h>
22 4027f31a 2017-11-04 stsp #include <string.h>
23 4027f31a 2017-11-04 stsp
24 9d31a1d8 2018-03-11 stsp #include "got_error.h"
25 9d31a1d8 2018-03-11 stsp
26 718b3ab0 2018-03-17 stsp #include "got_lib_path.h"
27 1411938b 2018-02-12 stsp
28 e0159033 2019-01-08 stsp #ifndef MIN
29 e0159033 2019-01-08 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
30 e0159033 2019-01-08 stsp #endif
31 e0159033 2019-01-08 stsp
32 4027f31a 2017-11-04 stsp int
33 4027f31a 2017-11-04 stsp got_path_is_absolute(const char *path)
34 4027f31a 2017-11-04 stsp {
35 4027f31a 2017-11-04 stsp return path[0] == '/';
36 4027f31a 2017-11-04 stsp }
37 4027f31a 2017-11-04 stsp
38 4027f31a 2017-11-04 stsp char *
39 4027f31a 2017-11-04 stsp got_path_get_absolute(const char *relpath)
40 4027f31a 2017-11-04 stsp {
41 4027f31a 2017-11-04 stsp char cwd[PATH_MAX];
42 4027f31a 2017-11-04 stsp char *abspath;
43 4027f31a 2017-11-04 stsp
44 4027f31a 2017-11-04 stsp if (getcwd(cwd, sizeof(cwd)) == NULL)
45 4027f31a 2017-11-04 stsp return NULL;
46 4027f31a 2017-11-04 stsp
47 4027f31a 2017-11-04 stsp if (asprintf(&abspath, "%s/%s/", cwd, relpath) == -1)
48 4027f31a 2017-11-04 stsp return NULL;
49 4027f31a 2017-11-04 stsp
50 4027f31a 2017-11-04 stsp return abspath;
51 4027f31a 2017-11-04 stsp }
52 4027f31a 2017-11-04 stsp
53 4027f31a 2017-11-04 stsp char *
54 4027f31a 2017-11-04 stsp got_path_normalize(const char *path)
55 4027f31a 2017-11-04 stsp {
56 4027f31a 2017-11-04 stsp char *resolved;
57 4027f31a 2017-11-04 stsp
58 4027f31a 2017-11-04 stsp resolved = realpath(path, NULL);
59 4027f31a 2017-11-04 stsp if (resolved == NULL)
60 4027f31a 2017-11-04 stsp return NULL;
61 4027f31a 2017-11-04 stsp
62 4027f31a 2017-11-04 stsp if (!got_path_is_absolute(resolved)) {
63 4027f31a 2017-11-04 stsp char *abspath = got_path_get_absolute(resolved);
64 4027f31a 2017-11-04 stsp free(resolved);
65 4027f31a 2017-11-04 stsp resolved = abspath;
66 4027f31a 2017-11-04 stsp }
67 4027f31a 2017-11-04 stsp
68 4027f31a 2017-11-04 stsp return resolved;
69 4027f31a 2017-11-04 stsp }
70 e6eac3b8 2018-06-17 stsp
71 f7d20e89 2018-06-17 stsp /* based on canonpath() from kern_pledge.c */
72 f7d20e89 2018-06-17 stsp const struct got_error *
73 e6eac3b8 2018-06-17 stsp got_canonpath(const char *input, char *buf, size_t bufsize)
74 e6eac3b8 2018-06-17 stsp {
75 e6eac3b8 2018-06-17 stsp const char *p;
76 e6eac3b8 2018-06-17 stsp char *q;
77 e6eac3b8 2018-06-17 stsp
78 e6eac3b8 2018-06-17 stsp /* can't canon relative paths, don't bother */
79 e6eac3b8 2018-06-17 stsp if (!got_path_is_absolute(input)) {
80 e6eac3b8 2018-06-17 stsp if (strlcpy(buf, input, bufsize) >= bufsize)
81 f7d20e89 2018-06-17 stsp return got_error(GOT_ERR_NO_SPACE);
82 f7d20e89 2018-06-17 stsp return NULL;
83 e6eac3b8 2018-06-17 stsp }
84 e6eac3b8 2018-06-17 stsp
85 e6eac3b8 2018-06-17 stsp p = input;
86 e6eac3b8 2018-06-17 stsp q = buf;
87 e6eac3b8 2018-06-17 stsp while (*p && (q - buf < bufsize)) {
88 e6eac3b8 2018-06-17 stsp if (p[0] == '/' && (p[1] == '/' || p[1] == '\0')) {
89 e6eac3b8 2018-06-17 stsp p += 1;
90 e6eac3b8 2018-06-17 stsp
91 e6eac3b8 2018-06-17 stsp } else if (p[0] == '/' && p[1] == '.' &&
92 e6eac3b8 2018-06-17 stsp (p[2] == '/' || p[2] == '\0')) {
93 e6eac3b8 2018-06-17 stsp p += 2;
94 e6eac3b8 2018-06-17 stsp
95 e6eac3b8 2018-06-17 stsp } else if (p[0] == '/' && p[1] == '.' && p[2] == '.' &&
96 e6eac3b8 2018-06-17 stsp (p[3] == '/' || p[3] == '\0')) {
97 e6eac3b8 2018-06-17 stsp p += 3;
98 e6eac3b8 2018-06-17 stsp if (q != buf) /* "/../" at start of buf */
99 e6eac3b8 2018-06-17 stsp while (*--q != '/')
100 e6eac3b8 2018-06-17 stsp continue;
101 e6eac3b8 2018-06-17 stsp
102 e6eac3b8 2018-06-17 stsp } else {
103 e6eac3b8 2018-06-17 stsp *q++ = *p++;
104 e6eac3b8 2018-06-17 stsp }
105 e6eac3b8 2018-06-17 stsp }
106 e6eac3b8 2018-06-17 stsp if ((*p == '\0') && (q - buf < bufsize)) {
107 e6eac3b8 2018-06-17 stsp *q = 0;
108 f7d20e89 2018-06-17 stsp return NULL;
109 e6eac3b8 2018-06-17 stsp } else
110 f7d20e89 2018-06-17 stsp return got_error(GOT_ERR_NO_SPACE);
111 e6eac3b8 2018-06-17 stsp }
112 04ca23f4 2018-07-16 stsp
113 04ca23f4 2018-07-16 stsp const struct got_error *
114 04ca23f4 2018-07-16 stsp got_path_skip_common_ancestor(char **child, const char *parent_abspath,
115 04ca23f4 2018-07-16 stsp const char *abspath)
116 04ca23f4 2018-07-16 stsp {
117 04ca23f4 2018-07-16 stsp const struct got_error *err = NULL;
118 04ca23f4 2018-07-16 stsp size_t len_parent, len, bufsize;
119 04ca23f4 2018-07-16 stsp
120 04ca23f4 2018-07-16 stsp len_parent = strlen(parent_abspath);
121 04ca23f4 2018-07-16 stsp len = strlen(abspath);
122 04ca23f4 2018-07-16 stsp if (len_parent >= len)
123 04ca23f4 2018-07-16 stsp return got_error(GOT_ERR_BAD_PATH);
124 04ca23f4 2018-07-16 stsp if (strncmp(parent_abspath, abspath, len_parent) != 0)
125 04ca23f4 2018-07-16 stsp return got_error(GOT_ERR_BAD_PATH);
126 04ca23f4 2018-07-16 stsp if (abspath[len_parent] != '/')
127 04ca23f4 2018-07-16 stsp return got_error(GOT_ERR_BAD_PATH);
128 04ca23f4 2018-07-16 stsp bufsize = len - len_parent + 1;
129 04ca23f4 2018-07-16 stsp *child = malloc(bufsize);
130 04ca23f4 2018-07-16 stsp if (*child == NULL)
131 04ca23f4 2018-07-16 stsp return got_error_from_errno();
132 04ca23f4 2018-07-16 stsp if (strlcpy(*child, abspath + len_parent, bufsize) >= bufsize) {
133 04ca23f4 2018-07-16 stsp err = got_error_from_errno();
134 04ca23f4 2018-07-16 stsp free(*child);
135 04ca23f4 2018-07-16 stsp *child = NULL;
136 04ca23f4 2018-07-16 stsp return err;
137 04ca23f4 2018-07-16 stsp }
138 04ca23f4 2018-07-16 stsp return NULL;
139 04ca23f4 2018-07-16 stsp }
140 31cedeaf 2018-09-15 stsp
141 31cedeaf 2018-09-15 stsp int
142 31cedeaf 2018-09-15 stsp got_path_is_root_dir(const char *path)
143 31cedeaf 2018-09-15 stsp {
144 31cedeaf 2018-09-15 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 8da9e5f4 2019-01-12 stsp if (parent_len == 0)
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 e0159033 2019-01-08 stsp /* Skip over common prefix. */
170 e0159033 2019-01-08 stsp while (i < min_len && path1[i] == path2[i])
171 e0159033 2019-01-08 stsp i++;
172 e0159033 2019-01-08 stsp
173 e0159033 2019-01-08 stsp /* Are the paths exactly equal? */
174 e0159033 2019-01-08 stsp if (len1 == len2 && i >= min_len)
175 e0159033 2019-01-08 stsp return 0;
176 e0159033 2019-01-08 stsp
177 e0159033 2019-01-08 stsp /* Order children in subdirectories directly after their parents. */
178 e0159033 2019-01-08 stsp if (path1[i] == '/' && path2[i] == '\0')
179 e0159033 2019-01-08 stsp return 1;
180 e0159033 2019-01-08 stsp if (path2[i] == '/' && path1[i] == '\0')
181 e0159033 2019-01-08 stsp return -1;
182 e0159033 2019-01-08 stsp if (path1[i] == '/')
183 e0159033 2019-01-08 stsp return -1;
184 e0159033 2019-01-08 stsp if (path2[i] == '/')
185 e0159033 2019-01-08 stsp return 1;
186 e0159033 2019-01-08 stsp
187 e0159033 2019-01-08 stsp /* Next character following the common prefix determines order. */
188 e0159033 2019-01-08 stsp return (unsigned char)path1[i] < (unsigned char)path2[i] ? -1 : 1;
189 e0159033 2019-01-08 stsp }