Line | Count | Source |
1 | | /* |
2 | | * Utilities for paths and pathnames |
3 | | */ |
4 | | |
5 | | #include "git-compat-util.h" |
6 | | #include "abspath.h" |
7 | | #include "gettext.h" |
8 | | #include "repository.h" |
9 | | #include "strbuf.h" |
10 | | #include "string-list.h" |
11 | | #include "dir.h" |
12 | | #include "worktree.h" |
13 | | #include "setup.h" |
14 | | #include "submodule-config.h" |
15 | | #include "path.h" |
16 | | #include "packfile.h" |
17 | | #include "odb.h" |
18 | | #include "lockfile.h" |
19 | | #include "exec-cmd.h" |
20 | | |
21 | | static int get_st_mode_bits(const char *path, int *mode) |
22 | 0 | { |
23 | 0 | struct stat st; |
24 | 0 | if (lstat(path, &st) < 0) |
25 | 0 | return -1; |
26 | 0 | *mode = st.st_mode; |
27 | 0 | return 0; |
28 | 0 | } |
29 | | |
30 | | static struct strbuf *get_pathname(void) |
31 | 0 | { |
32 | 0 | static struct strbuf pathname_array[4] = { |
33 | 0 | STRBUF_INIT, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT |
34 | 0 | }; |
35 | 0 | static int index; |
36 | 0 | struct strbuf *sb = &pathname_array[index]; |
37 | 0 | index = (index + 1) % ARRAY_SIZE(pathname_array); |
38 | 0 | strbuf_reset(sb); |
39 | 0 | return sb; |
40 | 0 | } |
41 | | |
42 | | static const char *cleanup_path(const char *path) |
43 | 0 | { |
44 | | /* Clean it up */ |
45 | 0 | if (skip_prefix(path, "./", &path)) { |
46 | 0 | while (*path == '/') |
47 | 0 | path++; |
48 | 0 | } |
49 | 0 | return path; |
50 | 0 | } |
51 | | |
52 | | static void strbuf_cleanup_path(struct strbuf *sb) |
53 | 0 | { |
54 | 0 | const char *path = cleanup_path(sb->buf); |
55 | 0 | if (path > sb->buf) |
56 | 0 | strbuf_remove(sb, 0, path - sb->buf); |
57 | 0 | } |
58 | | |
59 | | int dir_prefix(const char *buf, const char *dir) |
60 | 0 | { |
61 | 0 | size_t len = strlen(dir); |
62 | 0 | return !strncmp(buf, dir, len) && |
63 | 0 | (is_dir_sep(buf[len]) || buf[len] == '\0'); |
64 | 0 | } |
65 | | |
66 | | /* $buf =~ m|$dir/+$file| but without regex */ |
67 | | static int is_dir_file(const char *buf, const char *dir, const char *file) |
68 | 0 | { |
69 | 0 | int len = strlen(dir); |
70 | 0 | if (strncmp(buf, dir, len) || !is_dir_sep(buf[len])) |
71 | 0 | return 0; |
72 | 0 | while (is_dir_sep(buf[len])) |
73 | 0 | len++; |
74 | 0 | return !strcmp(buf + len, file); |
75 | 0 | } |
76 | | |
77 | | static void replace_dir(struct strbuf *buf, int len, const char *newdir) |
78 | 0 | { |
79 | 0 | int newlen = strlen(newdir); |
80 | 0 | int need_sep = (buf->buf[len] && !is_dir_sep(buf->buf[len])) && |
81 | 0 | !is_dir_sep(newdir[newlen - 1]); |
82 | 0 | if (need_sep) |
83 | 0 | len--; /* keep one char, to be replaced with '/' */ |
84 | 0 | strbuf_splice(buf, 0, len, newdir, newlen); |
85 | 0 | if (need_sep) |
86 | 0 | buf->buf[newlen] = '/'; |
87 | 0 | } |
88 | | |
89 | | struct common_dir { |
90 | | /* Not considered garbage for report_linked_checkout_garbage */ |
91 | | unsigned ignore_garbage:1; |
92 | | unsigned is_dir:1; |
93 | | /* Belongs to the common dir, though it may contain paths that don't */ |
94 | | unsigned is_common:1; |
95 | | const char *path; |
96 | | }; |
97 | | |
98 | | static struct common_dir common_list[] = { |
99 | | { 0, 1, 1, "branches" }, |
100 | | { 0, 1, 1, "common" }, |
101 | | { 0, 1, 1, "hooks" }, |
102 | | { 0, 1, 1, "info" }, |
103 | | { 0, 0, 0, "info/sparse-checkout" }, |
104 | | { 1, 1, 1, "logs" }, |
105 | | { 1, 0, 0, "logs/HEAD" }, |
106 | | { 0, 1, 0, "logs/refs/bisect" }, |
107 | | { 0, 1, 0, "logs/refs/rewritten" }, |
108 | | { 0, 1, 0, "logs/refs/worktree" }, |
109 | | { 0, 1, 1, "lost-found" }, |
110 | | { 0, 1, 1, "objects" }, |
111 | | { 0, 1, 1, "refs" }, |
112 | | { 0, 1, 0, "refs/bisect" }, |
113 | | { 0, 1, 0, "refs/rewritten" }, |
114 | | { 0, 1, 0, "refs/worktree" }, |
115 | | { 0, 1, 1, "remotes" }, |
116 | | { 0, 1, 1, "worktrees" }, |
117 | | { 0, 1, 1, "rr-cache" }, |
118 | | { 0, 1, 1, "svn" }, |
119 | | { 0, 0, 1, "config" }, |
120 | | { 1, 0, 1, "gc.pid" }, |
121 | | { 0, 0, 1, "packed-refs" }, |
122 | | { 0, 0, 1, "shallow" }, |
123 | | { 0, 0, 0, NULL } |
124 | | }; |
125 | | |
126 | | /* |
127 | | * A compressed trie. A trie node consists of zero or more characters that |
128 | | * are common to all elements with this prefix, optionally followed by some |
129 | | * children. If value is not NULL, the trie node is a terminal node. |
130 | | * |
131 | | * For example, consider the following set of strings: |
132 | | * abc |
133 | | * def |
134 | | * definite |
135 | | * definition |
136 | | * |
137 | | * The trie would look like: |
138 | | * root: len = 0, children a and d non-NULL, value = NULL. |
139 | | * a: len = 2, contents = bc, value = (data for "abc") |
140 | | * d: len = 2, contents = ef, children i non-NULL, value = (data for "def") |
141 | | * i: len = 3, contents = nit, children e and i non-NULL, value = NULL |
142 | | * e: len = 0, children all NULL, value = (data for "definite") |
143 | | * i: len = 2, contents = on, children all NULL, |
144 | | * value = (data for "definition") |
145 | | */ |
146 | | struct trie { |
147 | | struct trie *children[256]; |
148 | | int len; |
149 | | char *contents; |
150 | | void *value; |
151 | | }; |
152 | | |
153 | | static struct trie *make_trie_node(const char *key, void *value) |
154 | 0 | { |
155 | 0 | struct trie *new_node = xcalloc(1, sizeof(*new_node)); |
156 | 0 | new_node->len = strlen(key); |
157 | 0 | if (new_node->len) { |
158 | 0 | new_node->contents = xmalloc(new_node->len); |
159 | 0 | memcpy(new_node->contents, key, new_node->len); |
160 | 0 | } |
161 | 0 | new_node->value = value; |
162 | 0 | return new_node; |
163 | 0 | } |
164 | | |
165 | | /* |
166 | | * Add a key/value pair to a trie. The key is assumed to be \0-terminated. |
167 | | * If there was an existing value for this key, return it. |
168 | | */ |
169 | | static void *add_to_trie(struct trie *root, const char *key, void *value) |
170 | 0 | { |
171 | 0 | struct trie *child; |
172 | 0 | void *old; |
173 | 0 | int i; |
174 | |
|
175 | 0 | if (!*key) { |
176 | | /* we have reached the end of the key */ |
177 | 0 | old = root->value; |
178 | 0 | root->value = value; |
179 | 0 | return old; |
180 | 0 | } |
181 | | |
182 | 0 | for (i = 0; i < root->len; i++) { |
183 | 0 | if (root->contents[i] == key[i]) |
184 | 0 | continue; |
185 | | |
186 | | /* |
187 | | * Split this node: child will contain this node's |
188 | | * existing children. |
189 | | */ |
190 | 0 | child = xmalloc(sizeof(*child)); |
191 | 0 | memcpy(child->children, root->children, sizeof(root->children)); |
192 | |
|
193 | 0 | child->len = root->len - i - 1; |
194 | 0 | if (child->len) { |
195 | 0 | child->contents = xstrndup(root->contents + i + 1, |
196 | 0 | child->len); |
197 | 0 | } |
198 | 0 | child->value = root->value; |
199 | 0 | root->value = NULL; |
200 | 0 | root->len = i; |
201 | |
|
202 | 0 | memset(root->children, 0, sizeof(root->children)); |
203 | 0 | root->children[(unsigned char)root->contents[i]] = child; |
204 | | |
205 | | /* This is the newly-added child. */ |
206 | 0 | root->children[(unsigned char)key[i]] = |
207 | 0 | make_trie_node(key + i + 1, value); |
208 | 0 | return NULL; |
209 | 0 | } |
210 | | |
211 | | /* We have matched the entire compressed section */ |
212 | 0 | if (key[i]) { |
213 | 0 | child = root->children[(unsigned char)key[root->len]]; |
214 | 0 | if (child) { |
215 | 0 | return add_to_trie(child, key + root->len + 1, value); |
216 | 0 | } else { |
217 | 0 | child = make_trie_node(key + root->len + 1, value); |
218 | 0 | root->children[(unsigned char)key[root->len]] = child; |
219 | 0 | return NULL; |
220 | 0 | } |
221 | 0 | } |
222 | | |
223 | 0 | old = root->value; |
224 | 0 | root->value = value; |
225 | 0 | return old; |
226 | 0 | } |
227 | | |
228 | | typedef int (*match_fn)(const char *unmatched, void *value, void *baton); |
229 | | |
230 | | /* |
231 | | * Search a trie for some key. Find the longest /-or-\0-terminated |
232 | | * prefix of the key for which the trie contains a value. If there is |
233 | | * no such prefix, return -1. Otherwise call fn with the unmatched |
234 | | * portion of the key and the found value. If fn returns 0 or |
235 | | * positive, then return its return value. If fn returns negative, |
236 | | * then call fn with the next-longest /-terminated prefix of the key |
237 | | * (i.e. a parent directory) for which the trie contains a value, and |
238 | | * handle its return value the same way. If there is no shorter |
239 | | * /-terminated prefix with a value left, then return the negative |
240 | | * return value of the most recent fn invocation. |
241 | | * |
242 | | * The key is partially normalized: consecutive slashes are skipped. |
243 | | * |
244 | | * For example, consider the trie containing only [logs, |
245 | | * logs/refs/bisect], both with values, but not logs/refs. |
246 | | * |
247 | | * | key | unmatched | prefix to node | return value | |
248 | | * |--------------------|----------------|------------------|--------------| |
249 | | * | a | not called | n/a | -1 | |
250 | | * | logstore | not called | n/a | -1 | |
251 | | * | logs | \0 | logs | as per fn | |
252 | | * | logs/ | / | logs | as per fn | |
253 | | * | logs/refs | /refs | logs | as per fn | |
254 | | * | logs/refs/ | /refs/ | logs | as per fn | |
255 | | * | logs/refs/b | /refs/b | logs | as per fn | |
256 | | * | logs/refs/bisected | /refs/bisected | logs | as per fn | |
257 | | * | logs/refs/bisect | \0 | logs/refs/bisect | as per fn | |
258 | | * | logs/refs/bisect/ | / | logs/refs/bisect | as per fn | |
259 | | * | logs/refs/bisect/a | /a | logs/refs/bisect | as per fn | |
260 | | * | (If fn in the previous line returns -1, then fn is called once more:) | |
261 | | * | logs/refs/bisect/a | /refs/bisect/a | logs | as per fn | |
262 | | * |--------------------|----------------|------------------|--------------| |
263 | | */ |
264 | | static int trie_find(struct trie *root, const char *key, match_fn fn, |
265 | | void *baton) |
266 | 0 | { |
267 | 0 | int i; |
268 | 0 | int result; |
269 | 0 | struct trie *child; |
270 | |
|
271 | 0 | if (!*key) { |
272 | | /* we have reached the end of the key */ |
273 | 0 | if (root->value && !root->len) |
274 | 0 | return fn(key, root->value, baton); |
275 | 0 | else |
276 | 0 | return -1; |
277 | 0 | } |
278 | | |
279 | 0 | for (i = 0; i < root->len; i++) { |
280 | | /* Partial path normalization: skip consecutive slashes. */ |
281 | 0 | if (key[i] == '/' && key[i+1] == '/') { |
282 | 0 | key++; |
283 | 0 | continue; |
284 | 0 | } |
285 | 0 | if (root->contents[i] != key[i]) |
286 | 0 | return -1; |
287 | 0 | } |
288 | | |
289 | | /* Matched the entire compressed section */ |
290 | 0 | key += i; |
291 | 0 | if (!*key) { |
292 | | /* End of key */ |
293 | 0 | if (root->value) |
294 | 0 | return fn(key, root->value, baton); |
295 | 0 | else |
296 | 0 | return -1; |
297 | 0 | } |
298 | | |
299 | | /* Partial path normalization: skip consecutive slashes */ |
300 | 0 | while (key[0] == '/' && key[1] == '/') |
301 | 0 | key++; |
302 | |
|
303 | 0 | child = root->children[(unsigned char)*key]; |
304 | 0 | if (child) |
305 | 0 | result = trie_find(child, key + 1, fn, baton); |
306 | 0 | else |
307 | 0 | result = -1; |
308 | |
|
309 | 0 | if (result >= 0 || (*key != '/' && *key != 0)) |
310 | 0 | return result; |
311 | 0 | if (root->value) |
312 | 0 | return fn(key, root->value, baton); |
313 | 0 | else |
314 | 0 | return -1; |
315 | 0 | } |
316 | | |
317 | | static struct trie common_trie; |
318 | | static int common_trie_done_setup; |
319 | | |
320 | | static void init_common_trie(void) |
321 | 0 | { |
322 | 0 | struct common_dir *p; |
323 | |
|
324 | 0 | if (common_trie_done_setup) |
325 | 0 | return; |
326 | | |
327 | 0 | for (p = common_list; p->path; p++) |
328 | 0 | add_to_trie(&common_trie, p->path, p); |
329 | |
|
330 | 0 | common_trie_done_setup = 1; |
331 | 0 | } |
332 | | |
333 | | /* |
334 | | * Helper function for update_common_dir: returns 1 if the dir |
335 | | * prefix is common. |
336 | | */ |
337 | | static int check_common(const char *unmatched, void *value, |
338 | | void *baton UNUSED) |
339 | 0 | { |
340 | 0 | struct common_dir *dir = value; |
341 | |
|
342 | 0 | if (dir->is_dir && (unmatched[0] == 0 || unmatched[0] == '/')) |
343 | 0 | return dir->is_common; |
344 | | |
345 | 0 | if (!dir->is_dir && unmatched[0] == 0) |
346 | 0 | return dir->is_common; |
347 | | |
348 | 0 | return 0; |
349 | 0 | } |
350 | | |
351 | | static void update_common_dir(struct strbuf *buf, int git_dir_len, |
352 | | const char *common_dir) |
353 | 0 | { |
354 | 0 | char *base = buf->buf + git_dir_len; |
355 | 0 | int has_lock_suffix = strbuf_strip_suffix(buf, LOCK_SUFFIX); |
356 | |
|
357 | 0 | init_common_trie(); |
358 | 0 | if (trie_find(&common_trie, base, check_common, NULL) > 0) |
359 | 0 | replace_dir(buf, git_dir_len, common_dir); |
360 | |
|
361 | 0 | if (has_lock_suffix) |
362 | 0 | strbuf_addstr(buf, LOCK_SUFFIX); |
363 | 0 | } |
364 | | |
365 | | void report_linked_checkout_garbage(struct repository *r) |
366 | 0 | { |
367 | 0 | struct strbuf sb = STRBUF_INIT; |
368 | 0 | const struct common_dir *p; |
369 | 0 | int len; |
370 | |
|
371 | 0 | if (!r->different_commondir) |
372 | 0 | return; |
373 | 0 | strbuf_addf(&sb, "%s/", r->gitdir); |
374 | 0 | len = sb.len; |
375 | 0 | for (p = common_list; p->path; p++) { |
376 | 0 | const char *path = p->path; |
377 | 0 | if (p->ignore_garbage) |
378 | 0 | continue; |
379 | 0 | strbuf_setlen(&sb, len); |
380 | 0 | strbuf_addstr(&sb, path); |
381 | 0 | if (file_exists(sb.buf)) |
382 | 0 | report_garbage(PACKDIR_FILE_GARBAGE, sb.buf); |
383 | 0 | } |
384 | 0 | strbuf_release(&sb); |
385 | 0 | } |
386 | | |
387 | | static void adjust_git_path(struct repository *repo, |
388 | | struct strbuf *buf, int git_dir_len) |
389 | 0 | { |
390 | 0 | const char *base = buf->buf + git_dir_len; |
391 | |
|
392 | 0 | if (is_dir_file(base, "info", "grafts")) |
393 | 0 | strbuf_splice(buf, 0, buf->len, |
394 | 0 | repo->graft_file, strlen(repo->graft_file)); |
395 | 0 | else if (!strcmp(base, "index")) |
396 | 0 | strbuf_splice(buf, 0, buf->len, |
397 | 0 | repo->index_file, strlen(repo->index_file)); |
398 | 0 | else if (dir_prefix(base, "objects")) |
399 | 0 | replace_dir(buf, git_dir_len + 7, repo->objects->source->path); |
400 | 0 | else if (repo_settings_get_hooks_path(repo) && dir_prefix(base, "hooks")) |
401 | 0 | replace_dir(buf, git_dir_len + 5, repo_settings_get_hooks_path(repo)); |
402 | 0 | else if (repo->different_commondir) |
403 | 0 | update_common_dir(buf, git_dir_len, repo->commondir); |
404 | 0 | } |
405 | | |
406 | | static void strbuf_worktree_gitdir(struct strbuf *buf, |
407 | | const struct repository *repo, |
408 | | const struct worktree *wt) |
409 | 0 | { |
410 | 0 | if (!wt) |
411 | 0 | strbuf_addstr(buf, repo->gitdir); |
412 | 0 | else if (!wt->id) |
413 | 0 | strbuf_addstr(buf, repo->commondir); |
414 | 0 | else |
415 | 0 | repo_common_path_append(repo, buf, "worktrees/%s", wt->id); |
416 | 0 | } |
417 | | |
418 | | static void repo_git_pathv(struct repository *repo, |
419 | | const struct worktree *wt, struct strbuf *buf, |
420 | | const char *fmt, va_list args) |
421 | 0 | { |
422 | 0 | int gitdir_len; |
423 | 0 | strbuf_worktree_gitdir(buf, repo, wt); |
424 | 0 | if (buf->len && !is_dir_sep(buf->buf[buf->len - 1])) |
425 | 0 | strbuf_addch(buf, '/'); |
426 | 0 | gitdir_len = buf->len; |
427 | 0 | strbuf_vaddf(buf, fmt, args); |
428 | 0 | if (!wt) |
429 | 0 | adjust_git_path(repo, buf, gitdir_len); |
430 | 0 | strbuf_cleanup_path(buf); |
431 | 0 | } |
432 | | |
433 | | char *repo_git_path(struct repository *repo, |
434 | | const char *fmt, ...) |
435 | 0 | { |
436 | 0 | struct strbuf path = STRBUF_INIT; |
437 | 0 | va_list args; |
438 | 0 | va_start(args, fmt); |
439 | 0 | repo_git_pathv(repo, NULL, &path, fmt, args); |
440 | 0 | va_end(args); |
441 | 0 | return strbuf_detach(&path, NULL); |
442 | 0 | } |
443 | | |
444 | | const char *repo_git_path_append(struct repository *repo, |
445 | | struct strbuf *sb, |
446 | | const char *fmt, ...) |
447 | 0 | { |
448 | 0 | va_list args; |
449 | 0 | va_start(args, fmt); |
450 | 0 | repo_git_pathv(repo, NULL, sb, fmt, args); |
451 | 0 | va_end(args); |
452 | 0 | return sb->buf; |
453 | 0 | } |
454 | | |
455 | | const char *repo_git_path_replace(struct repository *repo, |
456 | | struct strbuf *sb, |
457 | | const char *fmt, ...) |
458 | 0 | { |
459 | 0 | va_list args; |
460 | 0 | strbuf_reset(sb); |
461 | 0 | va_start(args, fmt); |
462 | 0 | repo_git_pathv(repo, NULL, sb, fmt, args); |
463 | 0 | va_end(args); |
464 | 0 | return sb->buf; |
465 | 0 | } |
466 | | |
467 | | char *mkpathdup(const char *fmt, ...) |
468 | 0 | { |
469 | 0 | struct strbuf sb = STRBUF_INIT; |
470 | 0 | va_list args; |
471 | 0 | va_start(args, fmt); |
472 | 0 | strbuf_vaddf(&sb, fmt, args); |
473 | 0 | va_end(args); |
474 | 0 | strbuf_cleanup_path(&sb); |
475 | 0 | return strbuf_detach(&sb, NULL); |
476 | 0 | } |
477 | | |
478 | | const char *mkpath(const char *fmt, ...) |
479 | 0 | { |
480 | 0 | va_list args; |
481 | 0 | struct strbuf *pathname = get_pathname(); |
482 | 0 | va_start(args, fmt); |
483 | 0 | strbuf_vaddf(pathname, fmt, args); |
484 | 0 | va_end(args); |
485 | 0 | return cleanup_path(pathname->buf); |
486 | 0 | } |
487 | | |
488 | | const char *worktree_git_path(const struct worktree *wt, const char *fmt, ...) |
489 | 0 | { |
490 | 0 | struct strbuf *pathname = get_pathname(); |
491 | 0 | va_list args; |
492 | |
|
493 | 0 | if (!wt) |
494 | 0 | BUG("%s() called with NULL worktree", __func__); |
495 | | |
496 | 0 | va_start(args, fmt); |
497 | 0 | repo_git_pathv(wt->repo, wt, pathname, fmt, args); |
498 | 0 | va_end(args); |
499 | 0 | return pathname->buf; |
500 | 0 | } |
501 | | |
502 | | static void do_worktree_path(const struct repository *repo, |
503 | | struct strbuf *buf, |
504 | | const char *fmt, va_list args) |
505 | 0 | { |
506 | 0 | strbuf_addstr(buf, repo->worktree); |
507 | 0 | if(buf->len && !is_dir_sep(buf->buf[buf->len - 1])) |
508 | 0 | strbuf_addch(buf, '/'); |
509 | |
|
510 | 0 | strbuf_vaddf(buf, fmt, args); |
511 | 0 | strbuf_cleanup_path(buf); |
512 | 0 | } |
513 | | |
514 | | char *repo_worktree_path(const struct repository *repo, const char *fmt, ...) |
515 | 0 | { |
516 | 0 | struct strbuf path = STRBUF_INIT; |
517 | 0 | va_list args; |
518 | |
|
519 | 0 | va_start(args, fmt); |
520 | 0 | do_worktree_path(repo, &path, fmt, args); |
521 | 0 | va_end(args); |
522 | |
|
523 | 0 | return strbuf_detach(&path, NULL); |
524 | 0 | } |
525 | | |
526 | | const char *repo_worktree_path_append(const struct repository *repo, |
527 | | struct strbuf *sb, |
528 | | const char *fmt, ...) |
529 | 0 | { |
530 | 0 | va_list args; |
531 | |
|
532 | 0 | if (!repo->worktree) |
533 | 0 | return NULL; |
534 | | |
535 | 0 | va_start(args, fmt); |
536 | 0 | do_worktree_path(repo, sb, fmt, args); |
537 | 0 | va_end(args); |
538 | |
|
539 | 0 | return sb->buf; |
540 | 0 | } |
541 | | |
542 | | const char *repo_worktree_path_replace(const struct repository *repo, |
543 | | struct strbuf *sb, |
544 | | const char *fmt, ...) |
545 | 0 | { |
546 | 0 | va_list args; |
547 | |
|
548 | 0 | strbuf_reset(sb); |
549 | 0 | if (!repo->worktree) |
550 | 0 | return NULL; |
551 | | |
552 | 0 | va_start(args, fmt); |
553 | 0 | do_worktree_path(repo, sb, fmt, args); |
554 | 0 | va_end(args); |
555 | |
|
556 | 0 | return sb->buf; |
557 | 0 | } |
558 | | |
559 | | /* Returns 0 on success, negative on failure. */ |
560 | | static int do_submodule_path(struct repository *repo, |
561 | | struct strbuf *buf, const char *path, |
562 | | const char *fmt, va_list args) |
563 | 0 | { |
564 | 0 | struct strbuf git_submodule_common_dir = STRBUF_INIT; |
565 | 0 | struct strbuf git_submodule_dir = STRBUF_INIT; |
566 | 0 | int ret; |
567 | |
|
568 | 0 | ret = submodule_to_gitdir(repo, &git_submodule_dir, path); |
569 | 0 | if (ret) |
570 | 0 | goto cleanup; |
571 | | |
572 | 0 | strbuf_complete(&git_submodule_dir, '/'); |
573 | 0 | strbuf_addbuf(buf, &git_submodule_dir); |
574 | 0 | strbuf_vaddf(buf, fmt, args); |
575 | |
|
576 | 0 | if (get_common_dir_noenv(&git_submodule_common_dir, git_submodule_dir.buf)) |
577 | 0 | update_common_dir(buf, git_submodule_dir.len, git_submodule_common_dir.buf); |
578 | |
|
579 | 0 | strbuf_cleanup_path(buf); |
580 | |
|
581 | 0 | cleanup: |
582 | 0 | strbuf_release(&git_submodule_dir); |
583 | 0 | strbuf_release(&git_submodule_common_dir); |
584 | 0 | return ret; |
585 | 0 | } |
586 | | |
587 | | char *repo_submodule_path(struct repository *repo, |
588 | | const char *path, const char *fmt, ...) |
589 | 0 | { |
590 | 0 | int err; |
591 | 0 | va_list args; |
592 | 0 | struct strbuf buf = STRBUF_INIT; |
593 | 0 | va_start(args, fmt); |
594 | 0 | err = do_submodule_path(repo, &buf, path, fmt, args); |
595 | 0 | va_end(args); |
596 | 0 | if (err) { |
597 | 0 | strbuf_release(&buf); |
598 | 0 | return NULL; |
599 | 0 | } |
600 | 0 | return strbuf_detach(&buf, NULL); |
601 | 0 | } |
602 | | |
603 | | const char *repo_submodule_path_append(struct repository *repo, |
604 | | struct strbuf *buf, |
605 | | const char *path, |
606 | | const char *fmt, ...) |
607 | 0 | { |
608 | 0 | int err; |
609 | 0 | va_list args; |
610 | 0 | va_start(args, fmt); |
611 | 0 | err = do_submodule_path(repo, buf, path, fmt, args); |
612 | 0 | va_end(args); |
613 | 0 | if (err) |
614 | 0 | return NULL; |
615 | 0 | return buf->buf; |
616 | 0 | } |
617 | | |
618 | | const char *repo_submodule_path_replace(struct repository *repo, |
619 | | struct strbuf *buf, |
620 | | const char *path, |
621 | | const char *fmt, ...) |
622 | 0 | { |
623 | 0 | int err; |
624 | 0 | va_list args; |
625 | 0 | strbuf_reset(buf); |
626 | 0 | va_start(args, fmt); |
627 | 0 | err = do_submodule_path(repo, buf, path, fmt, args); |
628 | 0 | va_end(args); |
629 | 0 | if (err) |
630 | 0 | return NULL; |
631 | 0 | return buf->buf; |
632 | 0 | } |
633 | | |
634 | | static void repo_common_pathv(const struct repository *repo, |
635 | | struct strbuf *sb, |
636 | | const char *fmt, |
637 | | va_list args) |
638 | 0 | { |
639 | 0 | strbuf_addstr(sb, repo->commondir); |
640 | 0 | if (sb->len && !is_dir_sep(sb->buf[sb->len - 1])) |
641 | 0 | strbuf_addch(sb, '/'); |
642 | 0 | strbuf_vaddf(sb, fmt, args); |
643 | 0 | strbuf_cleanup_path(sb); |
644 | 0 | } |
645 | | |
646 | | char *repo_common_path(const struct repository *repo, |
647 | | const char *fmt, ...) |
648 | 0 | { |
649 | 0 | struct strbuf sb = STRBUF_INIT; |
650 | 0 | va_list args; |
651 | 0 | va_start(args, fmt); |
652 | 0 | repo_common_pathv(repo, &sb, fmt, args); |
653 | 0 | va_end(args); |
654 | 0 | return strbuf_detach(&sb, NULL); |
655 | 0 | } |
656 | | |
657 | | const char *repo_common_path_append(const struct repository *repo, |
658 | | struct strbuf *sb, |
659 | | const char *fmt, ...) |
660 | 0 | { |
661 | 0 | va_list args; |
662 | 0 | va_start(args, fmt); |
663 | 0 | repo_common_pathv(repo, sb, fmt, args); |
664 | 0 | va_end(args); |
665 | 0 | return sb->buf; |
666 | 0 | } |
667 | | |
668 | | const char *repo_common_path_replace(const struct repository *repo, |
669 | | struct strbuf *sb, |
670 | | const char *fmt, ...) |
671 | 0 | { |
672 | 0 | va_list args; |
673 | 0 | strbuf_reset(sb); |
674 | 0 | va_start(args, fmt); |
675 | 0 | repo_common_pathv(repo, sb, fmt, args); |
676 | 0 | va_end(args); |
677 | 0 | return sb->buf; |
678 | 0 | } |
679 | | |
680 | | static struct passwd *getpw_str(const char *username, size_t len) |
681 | 0 | { |
682 | 0 | struct passwd *pw; |
683 | 0 | char *username_z = xmemdupz(username, len); |
684 | 0 | pw = getpwnam(username_z); |
685 | 0 | free(username_z); |
686 | 0 | return pw; |
687 | 0 | } |
688 | | |
689 | | /* |
690 | | * Return a string with ~ and ~user expanded via getpw*. Returns NULL on getpw |
691 | | * failure or if path is NULL. |
692 | | * |
693 | | * If real_home is true, strbuf_realpath($HOME) is used in the `~/` expansion. |
694 | | * |
695 | | * If the path starts with `%(prefix)/`, the remainder is interpreted as |
696 | | * relative to where Git is installed, and expanded to the absolute path. |
697 | | */ |
698 | | char *interpolate_path(const char *path, int real_home) |
699 | 0 | { |
700 | 0 | struct strbuf user_path = STRBUF_INIT; |
701 | 0 | const char *to_copy = path; |
702 | |
|
703 | 0 | if (!path) |
704 | 0 | goto return_null; |
705 | | |
706 | 0 | if (skip_prefix(path, "%(prefix)/", &path)) |
707 | 0 | return system_path(path); |
708 | | |
709 | 0 | if (path[0] == '~') { |
710 | 0 | const char *first_slash = strchrnul(path, '/'); |
711 | 0 | const char *username = path + 1; |
712 | 0 | size_t username_len = first_slash - username; |
713 | 0 | if (username_len == 0) { |
714 | 0 | const char *home = getenv("HOME"); |
715 | 0 | if (!home) |
716 | 0 | goto return_null; |
717 | 0 | if (real_home) |
718 | 0 | strbuf_add_real_path(&user_path, home); |
719 | 0 | else |
720 | 0 | strbuf_addstr(&user_path, home); |
721 | | #ifdef GIT_WINDOWS_NATIVE |
722 | | convert_slashes(user_path.buf); |
723 | | #endif |
724 | 0 | } else { |
725 | 0 | struct passwd *pw = getpw_str(username, username_len); |
726 | 0 | if (!pw) |
727 | 0 | goto return_null; |
728 | 0 | strbuf_addstr(&user_path, pw->pw_dir); |
729 | 0 | } |
730 | 0 | to_copy = first_slash; |
731 | 0 | } |
732 | 0 | strbuf_addstr(&user_path, to_copy); |
733 | 0 | return strbuf_detach(&user_path, NULL); |
734 | 0 | return_null: |
735 | 0 | strbuf_release(&user_path); |
736 | 0 | return NULL; |
737 | 0 | } |
738 | | |
739 | | int calc_shared_perm(struct repository *repo, |
740 | | int mode) |
741 | 0 | { |
742 | 0 | int tweak; |
743 | 0 | int shared_repo = repo_settings_get_shared_repository(repo); |
744 | 0 | if (shared_repo < 0) |
745 | 0 | tweak = -shared_repo; |
746 | 0 | else |
747 | 0 | tweak = shared_repo; |
748 | |
|
749 | 0 | if (!(mode & S_IWUSR)) |
750 | 0 | tweak &= ~0222; |
751 | 0 | if (mode & S_IXUSR) |
752 | | /* Copy read bits to execute bits */ |
753 | 0 | tweak |= (tweak & 0444) >> 2; |
754 | 0 | if (shared_repo < 0) |
755 | 0 | mode = (mode & ~0777) | tweak; |
756 | 0 | else |
757 | 0 | mode |= tweak; |
758 | |
|
759 | 0 | return mode; |
760 | 0 | } |
761 | | |
762 | | int adjust_shared_perm(struct repository *repo, |
763 | | const char *path) |
764 | 0 | { |
765 | 0 | int old_mode, new_mode; |
766 | |
|
767 | 0 | if (!repo_settings_get_shared_repository(repo)) |
768 | 0 | return 0; |
769 | 0 | if (get_st_mode_bits(path, &old_mode) < 0) |
770 | 0 | return -1; |
771 | | |
772 | 0 | new_mode = calc_shared_perm(repo, old_mode); |
773 | 0 | if (S_ISDIR(old_mode)) { |
774 | | /* Copy read bits to execute bits */ |
775 | 0 | new_mode |= (new_mode & 0444) >> 2; |
776 | | |
777 | | /* |
778 | | * g+s matters only if any extra access is granted |
779 | | * based on group membership. |
780 | | */ |
781 | 0 | if (FORCE_DIR_SET_GID && (new_mode & 060)) |
782 | 0 | new_mode |= FORCE_DIR_SET_GID; |
783 | 0 | } |
784 | |
|
785 | 0 | if (((old_mode ^ new_mode) & ~S_IFMT) && |
786 | 0 | chmod(path, (new_mode & ~S_IFMT)) < 0) |
787 | 0 | return -2; |
788 | 0 | return 0; |
789 | 0 | } |
790 | | |
791 | | void safe_create_dir(struct repository *repo, const char *dir, int share) |
792 | 0 | { |
793 | 0 | if (mkdir(dir, 0777) < 0) { |
794 | 0 | if (errno != EEXIST) { |
795 | 0 | perror(dir); |
796 | 0 | exit(1); |
797 | 0 | } |
798 | 0 | } |
799 | 0 | else if (share && adjust_shared_perm(repo, dir)) |
800 | 0 | die(_("Could not make %s writable by group"), dir); |
801 | 0 | } |
802 | | |
803 | | int safe_create_dir_in_gitdir(struct repository *repo, const char *path) |
804 | 0 | { |
805 | 0 | if (mkdir(path, 0777)) { |
806 | 0 | int saved_errno = errno; |
807 | 0 | struct stat st; |
808 | 0 | struct strbuf sb = STRBUF_INIT; |
809 | |
|
810 | 0 | if (errno != EEXIST) |
811 | 0 | return -1; |
812 | | /* |
813 | | * Are we looking at a path in a symlinked worktree |
814 | | * whose original repository does not yet have it? |
815 | | * e.g. .git/rr-cache pointing at its original |
816 | | * repository in which the user hasn't performed any |
817 | | * conflict resolution yet? |
818 | | */ |
819 | 0 | if (lstat(path, &st) || !S_ISLNK(st.st_mode) || |
820 | 0 | strbuf_readlink(&sb, path, st.st_size) || |
821 | 0 | !is_absolute_path(sb.buf) || |
822 | 0 | mkdir(sb.buf, 0777)) { |
823 | 0 | strbuf_release(&sb); |
824 | 0 | errno = saved_errno; |
825 | 0 | return -1; |
826 | 0 | } |
827 | 0 | strbuf_release(&sb); |
828 | 0 | } |
829 | 0 | return adjust_shared_perm(repo, path); |
830 | 0 | } |
831 | | |
832 | | enum scld_error safe_create_leading_directories(struct repository *repo, |
833 | | char *path) |
834 | 0 | { |
835 | 0 | char *next_component = path + offset_1st_component(path); |
836 | 0 | enum scld_error ret = SCLD_OK; |
837 | |
|
838 | 0 | while (ret == SCLD_OK && next_component) { |
839 | 0 | struct stat st; |
840 | 0 | char *slash = next_component, slash_character; |
841 | |
|
842 | 0 | while (*slash && !is_dir_sep(*slash)) |
843 | 0 | slash++; |
844 | |
|
845 | 0 | if (!*slash) |
846 | 0 | break; |
847 | | |
848 | 0 | next_component = slash + 1; |
849 | 0 | while (is_dir_sep(*next_component)) |
850 | 0 | next_component++; |
851 | 0 | if (!*next_component) |
852 | 0 | break; |
853 | | |
854 | 0 | slash_character = *slash; |
855 | 0 | *slash = '\0'; |
856 | 0 | if (!stat(path, &st)) { |
857 | | /* path exists */ |
858 | 0 | if (!S_ISDIR(st.st_mode)) { |
859 | 0 | errno = ENOTDIR; |
860 | 0 | ret = SCLD_EXISTS; |
861 | 0 | } |
862 | 0 | } else if (mkdir(path, 0777)) { |
863 | 0 | if (errno == EEXIST && |
864 | 0 | !stat(path, &st) && S_ISDIR(st.st_mode)) |
865 | 0 | ; /* somebody created it since we checked */ |
866 | 0 | else if (errno == ENOENT) |
867 | | /* |
868 | | * Either mkdir() failed because |
869 | | * somebody just pruned the containing |
870 | | * directory, or stat() failed because |
871 | | * the file that was in our way was |
872 | | * just removed. Either way, inform |
873 | | * the caller that it might be worth |
874 | | * trying again: |
875 | | */ |
876 | 0 | ret = SCLD_VANISHED; |
877 | 0 | else |
878 | 0 | ret = SCLD_FAILED; |
879 | 0 | } else if (repo && adjust_shared_perm(repo, path)) { |
880 | 0 | ret = SCLD_PERMS; |
881 | 0 | } |
882 | 0 | *slash = slash_character; |
883 | 0 | } |
884 | 0 | return ret; |
885 | 0 | } |
886 | | |
887 | | enum scld_error safe_create_leading_directories_no_share(char *path) |
888 | 0 | { |
889 | 0 | return safe_create_leading_directories(NULL, path); |
890 | 0 | } |
891 | | |
892 | | enum scld_error safe_create_leading_directories_no_share_const(const char *path) |
893 | 0 | { |
894 | 0 | return safe_create_leading_directories_const(NULL, path); |
895 | 0 | } |
896 | | |
897 | | enum scld_error safe_create_leading_directories_const(struct repository *repo, |
898 | | const char *path) |
899 | 0 | { |
900 | 0 | int save_errno; |
901 | | /* path points to cache entries, so xstrdup before messing with it */ |
902 | 0 | char *buf = xstrdup(path); |
903 | 0 | enum scld_error result = safe_create_leading_directories(repo, buf); |
904 | |
|
905 | 0 | save_errno = errno; |
906 | 0 | free(buf); |
907 | 0 | errno = save_errno; |
908 | 0 | return result; |
909 | 0 | } |
910 | | |
911 | | int safe_create_file_with_leading_directories(struct repository *repo, |
912 | | const char *path) |
913 | 0 | { |
914 | 0 | int fd; |
915 | |
|
916 | 0 | fd = open(path, O_RDWR|O_CREAT|O_EXCL, 0600); |
917 | 0 | if (0 <= fd) |
918 | 0 | return fd; |
919 | | |
920 | | /* slow path */ |
921 | 0 | safe_create_leading_directories_const(repo, path); |
922 | 0 | return open(path, O_RDWR|O_CREAT|O_EXCL, 0600); |
923 | 0 | } |
924 | | |
925 | | static int have_same_root(const char *path1, const char *path2) |
926 | 0 | { |
927 | 0 | int is_abs1, is_abs2; |
928 | |
|
929 | 0 | is_abs1 = is_absolute_path(path1); |
930 | 0 | is_abs2 = is_absolute_path(path2); |
931 | 0 | return (is_abs1 && is_abs2 && tolower(path1[0]) == tolower(path2[0])) || |
932 | 0 | (!is_abs1 && !is_abs2); |
933 | 0 | } |
934 | | |
935 | | /* |
936 | | * Give path as relative to prefix. |
937 | | * |
938 | | * The strbuf may or may not be used, so do not assume it contains the |
939 | | * returned path. |
940 | | */ |
941 | | const char *relative_path(const char *in, const char *prefix, |
942 | | struct strbuf *sb) |
943 | 0 | { |
944 | 0 | int in_len = in ? strlen(in) : 0; |
945 | 0 | int prefix_len = prefix ? strlen(prefix) : 0; |
946 | 0 | int in_off = 0; |
947 | 0 | int prefix_off = 0; |
948 | 0 | int i = 0, j = 0; |
949 | |
|
950 | 0 | if (!in_len) |
951 | 0 | return "./"; |
952 | 0 | else if (!prefix_len) |
953 | 0 | return in; |
954 | | |
955 | 0 | if (have_same_root(in, prefix)) |
956 | | /* bypass dos_drive, for "c:" is identical to "C:" */ |
957 | 0 | i = j = has_dos_drive_prefix(in); |
958 | 0 | else { |
959 | 0 | return in; |
960 | 0 | } |
961 | | |
962 | 0 | while (i < prefix_len && j < in_len && prefix[i] == in[j]) { |
963 | 0 | if (is_dir_sep(prefix[i])) { |
964 | 0 | while (is_dir_sep(prefix[i])) |
965 | 0 | i++; |
966 | 0 | while (is_dir_sep(in[j])) |
967 | 0 | j++; |
968 | 0 | prefix_off = i; |
969 | 0 | in_off = j; |
970 | 0 | } else { |
971 | 0 | i++; |
972 | 0 | j++; |
973 | 0 | } |
974 | 0 | } |
975 | |
|
976 | 0 | if ( |
977 | | /* "prefix" seems like prefix of "in" */ |
978 | 0 | i >= prefix_len && |
979 | | /* |
980 | | * but "/foo" is not a prefix of "/foobar" |
981 | | * (i.e. prefix not end with '/') |
982 | | */ |
983 | 0 | prefix_off < prefix_len) { |
984 | 0 | if (j >= in_len) { |
985 | | /* in="/a/b", prefix="/a/b" */ |
986 | 0 | in_off = in_len; |
987 | 0 | } else if (is_dir_sep(in[j])) { |
988 | | /* in="/a/b/c", prefix="/a/b" */ |
989 | 0 | while (is_dir_sep(in[j])) |
990 | 0 | j++; |
991 | 0 | in_off = j; |
992 | 0 | } else { |
993 | | /* in="/a/bbb/c", prefix="/a/b" */ |
994 | 0 | i = prefix_off; |
995 | 0 | } |
996 | 0 | } else if ( |
997 | | /* "in" is short than "prefix" */ |
998 | 0 | j >= in_len && |
999 | | /* "in" not end with '/' */ |
1000 | 0 | in_off < in_len) { |
1001 | 0 | if (is_dir_sep(prefix[i])) { |
1002 | | /* in="/a/b", prefix="/a/b/c/" */ |
1003 | 0 | while (is_dir_sep(prefix[i])) |
1004 | 0 | i++; |
1005 | 0 | in_off = in_len; |
1006 | 0 | } |
1007 | 0 | } |
1008 | 0 | in += in_off; |
1009 | 0 | in_len -= in_off; |
1010 | |
|
1011 | 0 | if (i >= prefix_len) { |
1012 | 0 | if (!in_len) |
1013 | 0 | return "./"; |
1014 | 0 | else |
1015 | 0 | return in; |
1016 | 0 | } |
1017 | | |
1018 | 0 | strbuf_reset(sb); |
1019 | 0 | strbuf_grow(sb, in_len); |
1020 | |
|
1021 | 0 | while (i < prefix_len) { |
1022 | 0 | if (is_dir_sep(prefix[i])) { |
1023 | 0 | strbuf_addstr(sb, "../"); |
1024 | 0 | while (is_dir_sep(prefix[i])) |
1025 | 0 | i++; |
1026 | 0 | continue; |
1027 | 0 | } |
1028 | 0 | i++; |
1029 | 0 | } |
1030 | 0 | if (!is_dir_sep(prefix[prefix_len - 1])) |
1031 | 0 | strbuf_addstr(sb, "../"); |
1032 | |
|
1033 | 0 | strbuf_addstr(sb, in); |
1034 | |
|
1035 | 0 | return sb->buf; |
1036 | 0 | } |
1037 | | |
1038 | | /* |
1039 | | * A simpler implementation of relative_path |
1040 | | * |
1041 | | * Get relative path by removing "prefix" from "in". This function |
1042 | | * first appears in v1.5.6-1-g044bbbc, and makes git_dir shorter |
1043 | | * to increase performance when traversing the path to work_tree. |
1044 | | */ |
1045 | | const char *remove_leading_path(const char *in, const char *prefix) |
1046 | 0 | { |
1047 | 0 | static struct strbuf buf = STRBUF_INIT; |
1048 | 0 | int i = 0, j = 0; |
1049 | |
|
1050 | 0 | if (!prefix || !prefix[0]) |
1051 | 0 | return in; |
1052 | 0 | while (prefix[i]) { |
1053 | 0 | if (is_dir_sep(prefix[i])) { |
1054 | 0 | if (!is_dir_sep(in[j])) |
1055 | 0 | return in; |
1056 | 0 | while (is_dir_sep(prefix[i])) |
1057 | 0 | i++; |
1058 | 0 | while (is_dir_sep(in[j])) |
1059 | 0 | j++; |
1060 | 0 | continue; |
1061 | 0 | } else if (in[j] != prefix[i]) { |
1062 | 0 | return in; |
1063 | 0 | } |
1064 | 0 | i++; |
1065 | 0 | j++; |
1066 | 0 | } |
1067 | 0 | if ( |
1068 | | /* "/foo" is a prefix of "/foo" */ |
1069 | 0 | in[j] && |
1070 | | /* "/foo" is not a prefix of "/foobar" */ |
1071 | 0 | !is_dir_sep(prefix[i-1]) && !is_dir_sep(in[j]) |
1072 | 0 | ) |
1073 | 0 | return in; |
1074 | 0 | while (is_dir_sep(in[j])) |
1075 | 0 | j++; |
1076 | |
|
1077 | 0 | strbuf_reset(&buf); |
1078 | 0 | if (!in[j]) |
1079 | 0 | strbuf_addstr(&buf, "."); |
1080 | 0 | else |
1081 | 0 | strbuf_addstr(&buf, in + j); |
1082 | 0 | return buf.buf; |
1083 | 0 | } |
1084 | | |
1085 | | /* |
1086 | | * It is okay if dst == src, but they should not overlap otherwise. |
1087 | | * The "dst" buffer must be at least as long as "src"; normalizing may shrink |
1088 | | * the size of the path, but will never grow it. |
1089 | | * |
1090 | | * Performs the following normalizations on src, storing the result in dst: |
1091 | | * - Ensures that components are separated by '/' (Windows only) |
1092 | | * - Squashes sequences of '/' except "//server/share" on Windows |
1093 | | * - Removes "." components. |
1094 | | * - Removes ".." components, and the components the precede them. |
1095 | | * Returns failure (non-zero) if a ".." component appears as first path |
1096 | | * component anytime during the normalization. Otherwise, returns success (0). |
1097 | | * |
1098 | | * Note that this function is purely textual. It does not follow symlinks, |
1099 | | * verify the existence of the path, or make any system calls. |
1100 | | * |
1101 | | * prefix_len != NULL is for a specific case of prefix_pathspec(): |
1102 | | * assume that src == dst and src[0..prefix_len-1] is already |
1103 | | * normalized, any time "../" eats up to the prefix_len part, |
1104 | | * prefix_len is reduced. In the end prefix_len is the remaining |
1105 | | * prefix that has not been overridden by user pathspec. |
1106 | | * |
1107 | | * NEEDSWORK: This function doesn't perform normalization w.r.t. trailing '/'. |
1108 | | * For everything but the root folder itself, the normalized path should not |
1109 | | * end with a '/', then the callers need to be fixed up accordingly. |
1110 | | * |
1111 | | */ |
1112 | | |
1113 | | static const char *skip_slashes(const char *p) |
1114 | 0 | { |
1115 | 0 | while (is_dir_sep(*p)) |
1116 | 0 | p++; |
1117 | 0 | return p; |
1118 | 0 | } |
1119 | | |
1120 | | int normalize_path_copy_len(char *dst, const char *src, int *prefix_len) |
1121 | 0 | { |
1122 | 0 | char *dst0; |
1123 | 0 | const char *end; |
1124 | | |
1125 | | /* |
1126 | | * Copy initial part of absolute path: "/", "C:/", "//server/share/". |
1127 | | */ |
1128 | 0 | end = src + offset_1st_component(src); |
1129 | 0 | while (src < end) { |
1130 | 0 | char c = *src++; |
1131 | 0 | if (is_dir_sep(c)) |
1132 | 0 | c = '/'; |
1133 | 0 | *dst++ = c; |
1134 | 0 | } |
1135 | 0 | dst0 = dst; |
1136 | |
|
1137 | 0 | src = skip_slashes(src); |
1138 | |
|
1139 | 0 | for (;;) { |
1140 | 0 | char c = *src; |
1141 | | |
1142 | | /* |
1143 | | * A path component that begins with . could be |
1144 | | * special: |
1145 | | * (1) "." and ends -- ignore and terminate. |
1146 | | * (2) "./" -- ignore them, eat slash and continue. |
1147 | | * (3) ".." and ends -- strip one and terminate. |
1148 | | * (4) "../" -- strip one, eat slash and continue. |
1149 | | */ |
1150 | 0 | if (c == '.') { |
1151 | 0 | if (!src[1]) { |
1152 | | /* (1) */ |
1153 | 0 | src++; |
1154 | 0 | } else if (is_dir_sep(src[1])) { |
1155 | | /* (2) */ |
1156 | 0 | src += 2; |
1157 | 0 | src = skip_slashes(src); |
1158 | 0 | continue; |
1159 | 0 | } else if (src[1] == '.') { |
1160 | 0 | if (!src[2]) { |
1161 | | /* (3) */ |
1162 | 0 | src += 2; |
1163 | 0 | goto up_one; |
1164 | 0 | } else if (is_dir_sep(src[2])) { |
1165 | | /* (4) */ |
1166 | 0 | src += 3; |
1167 | 0 | src = skip_slashes(src); |
1168 | 0 | goto up_one; |
1169 | 0 | } |
1170 | 0 | } |
1171 | 0 | } |
1172 | | |
1173 | | /* copy up to the next '/', and eat all '/' */ |
1174 | 0 | while ((c = *src++) != '\0' && !is_dir_sep(c)) |
1175 | 0 | *dst++ = c; |
1176 | 0 | if (is_dir_sep(c)) { |
1177 | 0 | *dst++ = '/'; |
1178 | 0 | while (is_dir_sep(c)) |
1179 | 0 | c = *src++; |
1180 | 0 | src--; |
1181 | 0 | } else if (!c) |
1182 | 0 | break; |
1183 | 0 | continue; |
1184 | | |
1185 | 0 | up_one: |
1186 | | /* |
1187 | | * strip the last component |
1188 | | * |
1189 | | * dst0..dst is prefix portion, and dst[-1] is '/'; |
1190 | | * go up one level. |
1191 | | */ |
1192 | 0 | dst--; /* go to trailing '/' */ |
1193 | 0 | if (dst <= dst0) |
1194 | 0 | return -1; |
1195 | | /* Windows: dst[-1] cannot be backslash anymore */ |
1196 | 0 | while (dst0 < dst && dst[-1] != '/') |
1197 | 0 | dst--; |
1198 | 0 | if (prefix_len && *prefix_len > dst - dst0) |
1199 | 0 | *prefix_len = dst - dst0; |
1200 | 0 | } |
1201 | 0 | *dst = '\0'; |
1202 | 0 | return 0; |
1203 | 0 | } |
1204 | | |
1205 | | int normalize_path_copy(char *dst, const char *src) |
1206 | 0 | { |
1207 | 0 | return normalize_path_copy_len(dst, src, NULL); |
1208 | 0 | } |
1209 | | |
1210 | | int strbuf_normalize_path(struct strbuf *src) |
1211 | 0 | { |
1212 | 0 | struct strbuf dst = STRBUF_INIT; |
1213 | |
|
1214 | 0 | strbuf_grow(&dst, src->len); |
1215 | 0 | if (normalize_path_copy(dst.buf, src->buf) < 0) { |
1216 | 0 | strbuf_release(&dst); |
1217 | 0 | return -1; |
1218 | 0 | } |
1219 | | |
1220 | | /* |
1221 | | * normalize_path does not tell us the new length, so we have to |
1222 | | * compute it by looking for the new NUL it placed |
1223 | | */ |
1224 | 0 | strbuf_setlen(&dst, strlen(dst.buf)); |
1225 | 0 | strbuf_swap(src, &dst); |
1226 | 0 | strbuf_release(&dst); |
1227 | 0 | return 0; |
1228 | 0 | } |
1229 | | |
1230 | | /* |
1231 | | * path = Canonical absolute path |
1232 | | * prefixes = string_list containing normalized, absolute paths without |
1233 | | * trailing slashes (except for the root directory, which is denoted by "/"). |
1234 | | * |
1235 | | * Determines, for each path in prefixes, whether the "prefix" |
1236 | | * is an ancestor directory of path. Returns the length of the longest |
1237 | | * ancestor directory, excluding any trailing slashes, or -1 if no prefix |
1238 | | * is an ancestor. (Note that this means 0 is returned if prefixes is |
1239 | | * ["/"].) "/foo" is not considered an ancestor of "/foobar". Directories |
1240 | | * are not considered to be their own ancestors. path must be in a |
1241 | | * canonical form: empty components, or "." or ".." components are not |
1242 | | * allowed. |
1243 | | */ |
1244 | | int longest_ancestor_length(const char *path, struct string_list *prefixes) |
1245 | 0 | { |
1246 | 0 | int max_len = -1; |
1247 | |
|
1248 | 0 | if (!strcmp(path, "/")) |
1249 | 0 | return -1; |
1250 | | |
1251 | 0 | for (size_t i = 0; i < prefixes->nr; i++) { |
1252 | 0 | const char *ceil = prefixes->items[i].string; |
1253 | 0 | int len = strlen(ceil); |
1254 | | |
1255 | | /* |
1256 | | * For root directories (`/`, `C:/`, `//server/share/`) |
1257 | | * adjust the length to exclude the trailing slash. |
1258 | | */ |
1259 | 0 | if (len > 0 && ceil[len - 1] == '/') |
1260 | 0 | len--; |
1261 | |
|
1262 | 0 | if (strncmp(path, ceil, len) || |
1263 | 0 | path[len] != '/' || !path[len + 1]) |
1264 | 0 | continue; /* no match */ |
1265 | | |
1266 | 0 | if (len > max_len) |
1267 | 0 | max_len = len; |
1268 | 0 | } |
1269 | |
|
1270 | 0 | return max_len; |
1271 | 0 | } |
1272 | | |
1273 | | /* strip arbitrary amount of directory separators at end of path */ |
1274 | | static inline int chomp_trailing_dir_sep(const char *path, int len) |
1275 | 0 | { |
1276 | 0 | while (len && is_dir_sep(path[len - 1])) |
1277 | 0 | len--; |
1278 | 0 | return len; |
1279 | 0 | } |
1280 | | |
1281 | | /* |
1282 | | * If path ends with suffix (complete path components), returns the offset of |
1283 | | * the last character in the path before the suffix (sans trailing directory |
1284 | | * separators), and -1 otherwise. |
1285 | | */ |
1286 | | static ssize_t stripped_path_suffix_offset(const char *path, const char *suffix) |
1287 | 0 | { |
1288 | 0 | int path_len = strlen(path), suffix_len = strlen(suffix); |
1289 | |
|
1290 | 0 | while (suffix_len) { |
1291 | 0 | if (!path_len) |
1292 | 0 | return -1; |
1293 | | |
1294 | 0 | if (is_dir_sep(path[path_len - 1])) { |
1295 | 0 | if (!is_dir_sep(suffix[suffix_len - 1])) |
1296 | 0 | return -1; |
1297 | 0 | path_len = chomp_trailing_dir_sep(path, path_len); |
1298 | 0 | suffix_len = chomp_trailing_dir_sep(suffix, suffix_len); |
1299 | 0 | } |
1300 | 0 | else if (path[--path_len] != suffix[--suffix_len]) |
1301 | 0 | return -1; |
1302 | 0 | } |
1303 | | |
1304 | 0 | if (path_len && !is_dir_sep(path[path_len - 1])) |
1305 | 0 | return -1; |
1306 | 0 | return chomp_trailing_dir_sep(path, path_len); |
1307 | 0 | } |
1308 | | |
1309 | | /* |
1310 | | * Returns true if the path ends with components, considering only complete path |
1311 | | * components, and false otherwise. |
1312 | | */ |
1313 | | int ends_with_path_components(const char *path, const char *components) |
1314 | 0 | { |
1315 | 0 | return stripped_path_suffix_offset(path, components) != -1; |
1316 | 0 | } |
1317 | | |
1318 | | /* |
1319 | | * If path ends with suffix (complete path components), returns the |
1320 | | * part before suffix (sans trailing directory separators). |
1321 | | * Otherwise returns NULL. |
1322 | | */ |
1323 | | char *strip_path_suffix(const char *path, const char *suffix) |
1324 | 0 | { |
1325 | 0 | ssize_t offset = stripped_path_suffix_offset(path, suffix); |
1326 | |
|
1327 | 0 | return offset == -1 ? NULL : xstrndup(path, offset); |
1328 | 0 | } |
1329 | | |
1330 | | int daemon_avoid_alias(const char *p) |
1331 | 0 | { |
1332 | 0 | int sl, ndot; |
1333 | | |
1334 | | /* |
1335 | | * This resurrects the belts and suspenders paranoia check by HPA |
1336 | | * done in <435560F7.4080006@zytor.com> thread, now enter_repo() |
1337 | | * does not do getcwd() based path canonicalization. |
1338 | | * |
1339 | | * sl becomes true immediately after seeing '/' and continues to |
1340 | | * be true as long as dots continue after that without intervening |
1341 | | * non-dot character. |
1342 | | */ |
1343 | 0 | if (!p || (*p != '/' && *p != '~')) |
1344 | 0 | return -1; |
1345 | 0 | sl = 1; ndot = 0; |
1346 | 0 | p++; |
1347 | |
|
1348 | 0 | while (1) { |
1349 | 0 | char ch = *p++; |
1350 | 0 | if (sl) { |
1351 | 0 | if (ch == '.') |
1352 | 0 | ndot++; |
1353 | 0 | else if (ch == '/') { |
1354 | 0 | if (ndot < 3) |
1355 | | /* reject //, /./ and /../ */ |
1356 | 0 | return -1; |
1357 | 0 | ndot = 0; |
1358 | 0 | } |
1359 | 0 | else if (ch == 0) { |
1360 | 0 | if (0 < ndot && ndot < 3) |
1361 | | /* reject /.$ and /..$ */ |
1362 | 0 | return -1; |
1363 | 0 | return 0; |
1364 | 0 | } |
1365 | 0 | else |
1366 | 0 | sl = ndot = 0; |
1367 | 0 | } |
1368 | 0 | else if (ch == 0) |
1369 | 0 | return 0; |
1370 | 0 | else if (ch == '/') { |
1371 | 0 | sl = 1; |
1372 | 0 | ndot = 0; |
1373 | 0 | } |
1374 | 0 | } |
1375 | 0 | } |
1376 | | |
1377 | | /* |
1378 | | * On NTFS, we need to be careful to disallow certain synonyms of the `.git/` |
1379 | | * directory: |
1380 | | * |
1381 | | * - For historical reasons, file names that end in spaces or periods are |
1382 | | * automatically trimmed. Therefore, `.git . . ./` is a valid way to refer |
1383 | | * to `.git/`. |
1384 | | * |
1385 | | * - For other historical reasons, file names that do not conform to the 8.3 |
1386 | | * format (up to eight characters for the basename, three for the file |
1387 | | * extension, certain characters not allowed such as `+`, etc) are associated |
1388 | | * with a so-called "short name", at least on the `C:` drive by default. |
1389 | | * Which means that `git~1/` is a valid way to refer to `.git/`. |
1390 | | * |
1391 | | * Note: Technically, `.git/` could receive the short name `git~2` if the |
1392 | | * short name `git~1` were already used. In Git, however, we guarantee that |
1393 | | * `.git` is the first item in a directory, therefore it will be associated |
1394 | | * with the short name `git~1` (unless short names are disabled). |
1395 | | * |
1396 | | * - For yet other historical reasons, NTFS supports so-called "Alternate Data |
1397 | | * Streams", i.e. metadata associated with a given file, referred to via |
1398 | | * `<filename>:<stream-name>:<stream-type>`. There exists a default stream |
1399 | | * type for directories, allowing `.git/` to be accessed via |
1400 | | * `.git::$INDEX_ALLOCATION/`. |
1401 | | * |
1402 | | * When this function returns 1, it indicates that the specified file/directory |
1403 | | * name refers to a `.git` file or directory, or to any of these synonyms, and |
1404 | | * Git should therefore not track it. |
1405 | | * |
1406 | | * For performance reasons, _all_ Alternate Data Streams of `.git/` are |
1407 | | * forbidden, not just `::$INDEX_ALLOCATION`. |
1408 | | * |
1409 | | * This function is intended to be used by `git fsck` even on platforms where |
1410 | | * the backslash is a regular filename character, therefore it needs to handle |
1411 | | * backlash characters in the provided `name` specially: they are interpreted |
1412 | | * as directory separators. |
1413 | | */ |
1414 | | int is_ntfs_dotgit(const char *name) |
1415 | 0 | { |
1416 | 0 | char c; |
1417 | | |
1418 | | /* |
1419 | | * Note that when we don't find `.git` or `git~1` we end up with `name` |
1420 | | * advanced partway through the string. That's okay, though, as we |
1421 | | * return immediately in those cases, without looking at `name` any |
1422 | | * further. |
1423 | | */ |
1424 | 0 | c = *(name++); |
1425 | 0 | if (c == '.') { |
1426 | | /* .git */ |
1427 | 0 | if (((c = *(name++)) != 'g' && c != 'G') || |
1428 | 0 | ((c = *(name++)) != 'i' && c != 'I') || |
1429 | 0 | ((c = *(name++)) != 't' && c != 'T')) |
1430 | 0 | return 0; |
1431 | 0 | } else if (c == 'g' || c == 'G') { |
1432 | | /* git ~1 */ |
1433 | 0 | if (((c = *(name++)) != 'i' && c != 'I') || |
1434 | 0 | ((c = *(name++)) != 't' && c != 'T') || |
1435 | 0 | *(name++) != '~' || |
1436 | 0 | *(name++) != '1') |
1437 | 0 | return 0; |
1438 | 0 | } else |
1439 | 0 | return 0; |
1440 | | |
1441 | 0 | for (;;) { |
1442 | 0 | c = *(name++); |
1443 | 0 | if (!c || is_xplatform_dir_sep(c) || c == ':') |
1444 | 0 | return 1; |
1445 | 0 | if (c != '.' && c != ' ') |
1446 | 0 | return 0; |
1447 | 0 | } |
1448 | 0 | } |
1449 | | |
1450 | | static int is_ntfs_dot_generic(const char *name, |
1451 | | const char *dotgit_name, |
1452 | | size_t len, |
1453 | | const char *dotgit_ntfs_shortname_prefix) |
1454 | 0 | { |
1455 | 0 | int saw_tilde; |
1456 | 0 | size_t i; |
1457 | |
|
1458 | 0 | if ((name[0] == '.' && !strncasecmp(name + 1, dotgit_name, len))) { |
1459 | 0 | i = len + 1; |
1460 | 0 | only_spaces_and_periods: |
1461 | 0 | for (;;) { |
1462 | 0 | char c = name[i++]; |
1463 | 0 | if (!c || c == ':') |
1464 | 0 | return 1; |
1465 | 0 | if (c != ' ' && c != '.') |
1466 | 0 | return 0; |
1467 | 0 | } |
1468 | 0 | } |
1469 | | |
1470 | | /* |
1471 | | * Is it a regular NTFS short name, i.e. shortened to 6 characters, |
1472 | | * followed by ~1, ... ~4? |
1473 | | */ |
1474 | 0 | if (!strncasecmp(name, dotgit_name, 6) && name[6] == '~' && |
1475 | 0 | name[7] >= '1' && name[7] <= '4') { |
1476 | 0 | i = 8; |
1477 | 0 | goto only_spaces_and_periods; |
1478 | 0 | } |
1479 | | |
1480 | | /* |
1481 | | * Is it a fall-back NTFS short name (for details, see |
1482 | | * https://en.wikipedia.org/wiki/8.3_filename? |
1483 | | */ |
1484 | 0 | for (i = 0, saw_tilde = 0; i < 8; i++) |
1485 | 0 | if (name[i] == '\0') |
1486 | 0 | return 0; |
1487 | 0 | else if (saw_tilde) { |
1488 | 0 | if (name[i] < '0' || name[i] > '9') |
1489 | 0 | return 0; |
1490 | 0 | } else if (name[i] == '~') { |
1491 | 0 | if (name[++i] < '1' || name[i] > '9') |
1492 | 0 | return 0; |
1493 | 0 | saw_tilde = 1; |
1494 | 0 | } else if (i >= 6) |
1495 | 0 | return 0; |
1496 | 0 | else if (name[i] & 0x80) { |
1497 | | /* |
1498 | | * We know our needles contain only ASCII, so we clamp |
1499 | | * here to make the results of tolower() sane. |
1500 | | */ |
1501 | 0 | return 0; |
1502 | 0 | } else if (tolower(name[i]) != dotgit_ntfs_shortname_prefix[i]) |
1503 | 0 | return 0; |
1504 | | |
1505 | 0 | goto only_spaces_and_periods; |
1506 | 0 | } |
1507 | | |
1508 | | /* |
1509 | | * Inline helper to make sure compiler resolves strlen() on literals at |
1510 | | * compile time. |
1511 | | */ |
1512 | | static inline int is_ntfs_dot_str(const char *name, const char *dotgit_name, |
1513 | | const char *dotgit_ntfs_shortname_prefix) |
1514 | 0 | { |
1515 | 0 | return is_ntfs_dot_generic(name, dotgit_name, strlen(dotgit_name), |
1516 | 0 | dotgit_ntfs_shortname_prefix); |
1517 | 0 | } |
1518 | | |
1519 | | int is_ntfs_dotgitmodules(const char *name) |
1520 | 0 | { |
1521 | 0 | return is_ntfs_dot_str(name, "gitmodules", "gi7eba"); |
1522 | 0 | } |
1523 | | |
1524 | | int is_ntfs_dotgitignore(const char *name) |
1525 | 0 | { |
1526 | 0 | return is_ntfs_dot_str(name, "gitignore", "gi250a"); |
1527 | 0 | } |
1528 | | |
1529 | | int is_ntfs_dotgitattributes(const char *name) |
1530 | 0 | { |
1531 | 0 | return is_ntfs_dot_str(name, "gitattributes", "gi7d29"); |
1532 | 0 | } |
1533 | | |
1534 | | int is_ntfs_dotmailmap(const char *name) |
1535 | 0 | { |
1536 | 0 | return is_ntfs_dot_str(name, "mailmap", "maba30"); |
1537 | 0 | } |
1538 | | |
1539 | | int looks_like_command_line_option(const char *str) |
1540 | 0 | { |
1541 | 0 | return str && str[0] == '-'; |
1542 | 0 | } |
1543 | | |
1544 | | char *xdg_config_home_for(const char *subdir, const char *filename) |
1545 | 0 | { |
1546 | 0 | const char *home, *config_home; |
1547 | |
|
1548 | 0 | assert(subdir); |
1549 | 0 | assert(filename); |
1550 | 0 | config_home = getenv("XDG_CONFIG_HOME"); |
1551 | 0 | if (config_home && *config_home) |
1552 | 0 | return mkpathdup("%s/%s/%s", config_home, subdir, filename); |
1553 | | |
1554 | 0 | home = getenv("HOME"); |
1555 | 0 | if (home) |
1556 | 0 | return mkpathdup("%s/.config/%s/%s", home, subdir, filename); |
1557 | | |
1558 | 0 | return NULL; |
1559 | 0 | } |
1560 | | |
1561 | | char *xdg_config_home(const char *filename) |
1562 | 0 | { |
1563 | 0 | return xdg_config_home_for("git", filename); |
1564 | 0 | } |
1565 | | |
1566 | | char *xdg_cache_home(const char *filename) |
1567 | 0 | { |
1568 | 0 | const char *home, *cache_home; |
1569 | |
|
1570 | 0 | assert(filename); |
1571 | 0 | cache_home = getenv("XDG_CACHE_HOME"); |
1572 | 0 | if (cache_home && *cache_home) |
1573 | 0 | return mkpathdup("%s/git/%s", cache_home, filename); |
1574 | | |
1575 | 0 | home = getenv("HOME"); |
1576 | 0 | if (home) |
1577 | 0 | return mkpathdup("%s/.cache/git/%s", home, filename); |
1578 | 0 | return NULL; |
1579 | 0 | } |
1580 | | |
1581 | | void format_path(struct strbuf *dest, const char *path, |
1582 | | const char *prefix, enum path_format format) |
1583 | 0 | { |
1584 | 0 | strbuf_reset(dest); |
1585 | |
|
1586 | 0 | switch (format) { |
1587 | 0 | case PATH_FORMAT_UNMODIFIED: |
1588 | 0 | strbuf_addstr(dest, path); |
1589 | 0 | break; |
1590 | | |
1591 | 0 | case PATH_FORMAT_RELATIVE: { |
1592 | 0 | struct strbuf relative_buf = STRBUF_INIT; |
1593 | 0 | struct strbuf real_path = STRBUF_INIT; |
1594 | 0 | struct strbuf real_prefix = STRBUF_INIT; |
1595 | 0 | char *cwd = NULL; |
1596 | | |
1597 | | /* |
1598 | | * We don't ever produce a relative path if prefix is NULL, |
1599 | | * so set the prefix to the current directory so that we can |
1600 | | * produce a relative path whenever possible. |
1601 | | */ |
1602 | 0 | if (!prefix) |
1603 | 0 | prefix = cwd = xgetcwd(); |
1604 | |
|
1605 | 0 | if (!is_absolute_path(path)) { |
1606 | 0 | strbuf_realpath_forgiving(&real_path, path, 1); |
1607 | 0 | path = real_path.buf; |
1608 | 0 | } |
1609 | 0 | if (!is_absolute_path(prefix)) { |
1610 | 0 | strbuf_realpath_forgiving(&real_prefix, prefix, 1); |
1611 | 0 | prefix = real_prefix.buf; |
1612 | 0 | } |
1613 | |
|
1614 | 0 | strbuf_addstr(dest, relative_path(path, prefix, &relative_buf)); |
1615 | |
|
1616 | 0 | strbuf_release(&relative_buf); |
1617 | 0 | strbuf_release(&real_path); |
1618 | 0 | strbuf_release(&real_prefix); |
1619 | 0 | free(cwd); |
1620 | 0 | break; |
1621 | 0 | } |
1622 | | |
1623 | 0 | case PATH_FORMAT_RELATIVE_IF_SHARED: { |
1624 | 0 | struct strbuf relative_buf = STRBUF_INIT; |
1625 | | |
1626 | | /* |
1627 | | * If we're using RELATIVE_IF_SHARED mode, then we want an |
1628 | | * absolute path unless the two share a common prefix, so don't |
1629 | | * default the prefix to the current working directory. Doing so |
1630 | | * would cause a relative path to always be produced if possible. |
1631 | | */ |
1632 | 0 | strbuf_addstr(dest, relative_path(path, prefix, &relative_buf)); |
1633 | 0 | strbuf_release(&relative_buf); |
1634 | 0 | break; |
1635 | 0 | } |
1636 | | |
1637 | 0 | case PATH_FORMAT_CANONICAL: |
1638 | | /* |
1639 | | * strbuf_realpath_forgiving inherently resets the destination |
1640 | | * buffer, safely aligning with our replace semantics. |
1641 | | */ |
1642 | 0 | strbuf_realpath_forgiving(dest, path, 1); |
1643 | 0 | break; |
1644 | | |
1645 | 0 | default: |
1646 | 0 | BUG("unknown path_format value %d", format); |
1647 | 0 | } |
1648 | 0 | } |
1649 | | |
1650 | | REPO_GIT_PATH_FUNC(squash_msg, "SQUASH_MSG") |
1651 | | REPO_GIT_PATH_FUNC(merge_msg, "MERGE_MSG") |
1652 | | REPO_GIT_PATH_FUNC(merge_rr, "MERGE_RR") |
1653 | | REPO_GIT_PATH_FUNC(merge_mode, "MERGE_MODE") |
1654 | | REPO_GIT_PATH_FUNC(merge_head, "MERGE_HEAD") |
1655 | | REPO_GIT_PATH_FUNC(fetch_head, "FETCH_HEAD") |
1656 | | REPO_GIT_PATH_FUNC(shallow, "shallow") |
1657 | | REPO_GIT_PATH_FUNC(merge_labels, "MERGE_LABELS") |