]> git.ipfire.org Git - thirdparty/git.git/blame - entry.c
sparse-index.h: move declarations for sparse-index.c from cache.h
[thirdparty/git.git] / entry.c
CommitLineData
12dccc16 1#include "cache.h"
8e440259 2#include "blob.h"
cbd53a21 3#include "object-store.h"
8ca12c0d 4#include "dir.h"
32a8f510 5#include "environment.h"
f394e093 6#include "gettext.h"
41771fa4 7#include "hex.h"
f5653856 8#include "name-hash.h"
baf889c2 9#include "sparse-index.h"
dd8e9121 10#include "streaming.h"
6d14eac3 11#include "submodule.h"
cb2a5135 12#include "symlinks.h"
52f1d62e 13#include "progress.h"
883e248b 14#include "fsmonitor.h"
d052cc03 15#include "entry.h"
04155bda 16#include "parallel-checkout.h"
d5ebb50d 17#include "wrapper.h"
12dccc16 18
81a9aa60
KB
19static void create_directories(const char *path, int path_len,
20 const struct checkout *state)
12dccc16 21{
3733e694 22 char *buf = xmallocz(path_len);
81a9aa60
KB
23 int len = 0;
24
25 while (len < path_len) {
26 do {
27 buf[len] = path[len];
28 len++;
29 } while (len < path_len && path[len] != '/');
30 if (len >= path_len)
31 break;
12dccc16 32 buf[len] = 0;
fa2e71c9 33
bad4a54f
KB
34 /*
35 * For 'checkout-index --prefix=<dir>', <dir> is
36 * allowed to be a symlink to an existing directory,
37 * and we set 'state->base_dir_len' below, such that
38 * we test the path components of the prefix with the
39 * stat() function instead of the lstat() function.
40 */
57199892 41 if (has_dirs_only_path(buf, len, state->base_dir_len))
fa2e71c9
JH
42 continue; /* ok, it is already a directory. */
43
44 /*
bad4a54f
KB
45 * If this mkdir() would fail, it could be that there
46 * is already a symlink or something else exists
47 * there, therefore we then try to unlink it and try
48 * one more time to create the directory.
fa2e71c9 49 */
f312de01 50 if (mkdir(buf, 0777)) {
fa2e71c9 51 if (errno == EEXIST && state->force &&
691f1a28 52 !unlink_or_warn(buf) && !mkdir(buf, 0777))
fa2e71c9 53 continue;
0721c314 54 die_errno("cannot create directory at '%s'", buf);
12dccc16
LT
55 }
56 }
57 free(buf);
58}
59
2f29e0c6 60static void remove_subtree(struct strbuf *path)
12dccc16 61{
2f29e0c6 62 DIR *dir = opendir(path->buf);
12dccc16 63 struct dirent *de;
2f29e0c6 64 int origlen = path->len;
a6080a0a 65
12dccc16 66 if (!dir)
2f29e0c6 67 die_errno("cannot opendir '%s'", path->buf);
b548f0f1 68 while ((de = readdir_skip_dot_and_dotdot(dir)) != NULL) {
12dccc16 69 struct stat st;
2f29e0c6 70
2f29e0c6
MH
71 strbuf_addch(path, '/');
72 strbuf_addstr(path, de->d_name);
73 if (lstat(path->buf, &st))
74 die_errno("cannot lstat '%s'", path->buf);
12dccc16 75 if (S_ISDIR(st.st_mode))
2f29e0c6
MH
76 remove_subtree(path);
77 else if (unlink(path->buf))
78 die_errno("cannot unlink '%s'", path->buf);
79 strbuf_setlen(path, origlen);
12dccc16
LT
80 }
81 closedir(dir);
2f29e0c6
MH
82 if (rmdir(path->buf))
83 die_errno("cannot rmdir '%s'", path->buf);
12dccc16
LT
84}
85
d48a72f3 86static int create_file(const char *path, unsigned int mode)
12dccc16 87{
12dccc16 88 mode = (mode & 0100) ? 0777 : 0666;
781411ed 89 return open(path, O_WRONLY | O_CREAT | O_EXCL, mode);
12dccc16
LT
90}
91
e9aa762c 92void *read_blob_entry(const struct cache_entry *ce, size_t *size)
f0807e62
LT
93{
94 enum object_type type;
e9aa762c 95 unsigned long ul;
bc726bd0
ÆAB
96 void *blob_data = repo_read_object_file(the_repository, &ce->oid,
97 &type, &ul);
f0807e62 98
e9aa762c 99 *size = ul;
d8f71807 100 if (blob_data) {
f0807e62 101 if (type == OBJ_BLOB)
d8f71807
BW
102 return blob_data;
103 free(blob_data);
f0807e62
LT
104 }
105 return NULL;
106}
107
9c5e6c80 108static int open_output_fd(char *path, const struct cache_entry *ce, int to_tempfile)
fd5db55d
JH
109{
110 int symlink = (ce->ce_mode & S_IFMT) != S_IFREG;
111 if (to_tempfile) {
330c8e26
JK
112 xsnprintf(path, TEMPORARY_FILENAME_LENGTH, "%s",
113 symlink ? ".merge_link_XXXXXX" : ".merge_file_XXXXXX");
fd5db55d
JH
114 return mkstemp(path);
115 } else {
116 return create_file(path, !symlink ? ce->ce_mode : 0666);
117 }
118}
119
49cfd903 120int fstat_checkout_output(int fd, const struct checkout *state, struct stat *st)
fd5db55d
JH
121{
122 /* use fstat() only when path == ce->name */
123 if (fstat_is_reliable() &&
124 state->refresh_cache && !state->base_dir_len) {
35e6e212 125 return !fstat(fd, st);
fd5db55d
JH
126 }
127 return 0;
128}
129
9c5e6c80 130static int streaming_write_entry(const struct cache_entry *ce, char *path,
b6691092 131 struct stream_filter *filter,
dd8e9121
JH
132 const struct checkout *state, int to_tempfile,
133 int *fstat_done, struct stat *statbuf)
134{
d9c31e14 135 int result = 0;
47a02ff2 136 int fd;
dd8e9121
JH
137
138 fd = open_output_fd(path, ce, to_tempfile);
d9c31e14
JK
139 if (fd < 0)
140 return -1;
141
7eda0e4f 142 result |= stream_blob_to_fd(fd, &ce->oid, filter, 1);
49cfd903 143 *fstat_done = fstat_checkout_output(fd, state, statbuf);
d9c31e14
JK
144 result |= close(fd);
145
146 if (result)
dd8e9121
JH
147 unlink(path);
148 return result;
149}
150
2841e8f8
LS
151void enable_delayed_checkout(struct checkout *state)
152{
153 if (!state->delayed_checkout) {
154 state->delayed_checkout = xmalloc(sizeof(*state->delayed_checkout));
155 state->delayed_checkout->state = CE_CAN_DELAY;
bc40dfb1
ÆAB
156 string_list_init_nodup(&state->delayed_checkout->filters);
157 string_list_init_nodup(&state->delayed_checkout->paths);
2841e8f8
LS
158 }
159}
160
161static int remove_available_paths(struct string_list_item *item, void *cb_data)
162{
163 struct string_list *available_paths = cb_data;
164 struct string_list_item *available;
165
166 available = string_list_lookup(available_paths, item->string);
167 if (available)
611c7785 168 available->util = item->util;
2841e8f8
LS
169 return !available;
170}
171
611c7785 172int finish_delayed_checkout(struct checkout *state, int show_progress)
2841e8f8
LS
173{
174 int errs = 0;
bf6d819b 175 unsigned processed_paths = 0;
52f1d62e 176 off_t filtered_bytes = 0;
2841e8f8 177 struct string_list_item *filter, *path;
bf6d819b 178 struct progress *progress = NULL;
2841e8f8
LS
179 struct delayed_checkout *dco = state->delayed_checkout;
180
181 if (!state->delayed_checkout)
182 return errs;
183
184 dco->state = CE_RETRY;
bf6d819b
SG
185 if (show_progress)
186 progress = start_delayed_progress(_("Filtering content"), dco->paths.nr);
2841e8f8
LS
187 while (dco->filters.nr > 0) {
188 for_each_string_list_item(filter, &dco->filters) {
189 struct string_list available_paths = STRING_LIST_INIT_NODUP;
190
191 if (!async_query_available_blobs(filter->string, &available_paths)) {
192 /* Filter reported an error */
193 errs = 1;
194 filter->string = "";
195 continue;
196 }
197 if (available_paths.nr <= 0) {
198 /*
199 * Filter responded with no entries. That means
200 * the filter is done and we can remove the
201 * filter from the list (see
202 * "string_list_remove_empty_items" call below).
203 */
204 filter->string = "";
205 continue;
206 }
207
208 /*
209 * In dco->paths we store a list of all delayed paths.
210 * The filter just send us a list of available paths.
211 * Remove them from the list.
212 */
213 filter_string_list(&dco->paths, 0,
214 &remove_available_paths, &available_paths);
215
216 for_each_string_list_item(path, &available_paths) {
217 struct cache_entry* ce;
218
219 if (!path->util) {
220 error("external filter '%s' signaled that '%s' "
221 "is now available although it has not been "
222 "delayed earlier",
223 filter->string, path->string);
224 errs |= 1;
225
226 /*
227 * Do not ask the filter for available blobs,
228 * again, as the filter is likely buggy.
229 */
230 filter->string = "";
231 continue;
232 }
233 ce = index_file_exists(state->istate, path->string,
234 strlen(path->string), 0);
52f1d62e 235 if (ce) {
bf6d819b 236 display_progress(progress, ++processed_paths);
611c7785 237 errs |= checkout_entry(ce, state, NULL, path->util);
52f1d62e
LS
238 filtered_bytes += ce->ce_stat_data.sd_size;
239 display_throughput(progress, filtered_bytes);
240 } else
241 errs = 1;
2841e8f8
LS
242 }
243 }
244 string_list_remove_empty_items(&dco->filters, 0);
245 }
52f1d62e 246 stop_progress(&progress);
2841e8f8
LS
247 string_list_clear(&dco->filters, 0);
248
249 /* At this point we should not have any delayed paths anymore. */
250 errs |= dco->paths.nr;
251 for_each_string_list_item(path, &dco->paths) {
252 error("'%s' was not filtered properly", path->string);
253 }
254 string_list_clear(&dco->paths, 0);
255
256 free(dco);
257 state->delayed_checkout = NULL;
258
259 return errs;
260}
261
584a0d13
MT
262void update_ce_after_write(const struct checkout *state, struct cache_entry *ce,
263 struct stat *st)
264{
265 if (state->refresh_cache) {
266 assert(state->istate);
267 fill_stat_cache_info(state->istate, ce, st);
268 ce->ce_flags |= CE_UPDATE_IN_BASE;
269 mark_fsmonitor_invalid(state->istate, ce);
270 state->istate->cache_changed |= CE_ENTRY_CHANGED;
271 }
272}
273
30419e7e
MT
274/* Note: ca is used (and required) iff the entry refers to a regular file. */
275static int write_entry(struct cache_entry *ce, char *path, struct conv_attrs *ca,
611c7785
MT
276 const struct checkout *state, int to_tempfile,
277 int *nr_checkouts)
12dccc16 278{
4857c761 279 unsigned int ce_mode_s_ifmt = ce->ce_mode & S_IFMT;
c602d3a9 280 struct delayed_checkout *dco = state->delayed_checkout;
e4c72923 281 int fd, ret, fstat_done = 0;
d8f71807 282 char *new_blob;
4857c761 283 struct strbuf buf = STRBUF_INIT;
e9aa762c 284 size_t size;
564bde9a
JK
285 ssize_t wrote;
286 size_t newsize = 0;
e4c72923 287 struct stat st;
6d14eac3 288 const struct submodule *sub;
c397aac0 289 struct checkout_metadata meta;
611c7785 290 static int scratch_nr_checkouts;
c397aac0 291
292 clone_checkout_metadata(&meta, &state->meta, &ce->oid);
4857c761 293
b6691092 294 if (ce_mode_s_ifmt == S_IFREG) {
30419e7e 295 struct stream_filter *filter = get_stream_filter_ca(ca, &ce->oid);
b6691092
JH
296 if (filter &&
297 !streaming_write_entry(ce, path, filter,
298 state, to_tempfile,
299 &fstat_done, &st))
300 goto finish;
301 }
dd8e9121 302
4857c761 303 switch (ce_mode_s_ifmt) {
4857c761 304 case S_IFLNK:
d8f71807
BW
305 new_blob = read_blob_entry(ce, &size);
306 if (!new_blob)
d43e9073 307 return error("unable to read sha1 file of %s (%s)",
9334ea8e 308 ce->name, oid_to_hex(&ce->oid));
1a9d7e9b 309
7cbbf9d6
JK
310 /*
311 * We can't make a real symlink; write out a regular file entry
312 * with the symlink destination as its contents.
313 */
314 if (!has_symlinks || to_tempfile)
315 goto write_file_entry;
316
d8f71807
BW
317 ret = symlink(new_blob, path);
318 free(new_blob);
7cbbf9d6
JK
319 if (ret)
320 return error_errno("unable to create symlink %s", path);
321 break;
322
323 case S_IFREG:
c602d3a9
JK
324 /*
325 * We do not send the blob in case of a retry, so do not
326 * bother reading it at all.
327 */
7cbbf9d6 328 if (dco && dco->state == CE_RETRY) {
d8f71807 329 new_blob = NULL;
c602d3a9
JK
330 size = 0;
331 } else {
d8f71807
BW
332 new_blob = read_blob_entry(ce, &size);
333 if (!new_blob)
c602d3a9 334 return error("unable to read sha1 file of %s (%s)",
9334ea8e 335 ce->name, oid_to_hex(&ce->oid));
4857c761
KB
336 }
337
1a9d7e9b
JH
338 /*
339 * Convert from git internal format to working tree format
340 */
7cbbf9d6 341 if (dco && dco->state != CE_NO_DELAY) {
30419e7e
MT
342 ret = async_convert_to_working_tree_ca(ca, ce->name,
343 new_blob, size,
344 &buf, &meta, dco);
611c7785
MT
345 if (ret) {
346 struct string_list_item *item =
347 string_list_lookup(&dco->paths, ce->name);
348 if (item) {
349 item->util = nr_checkouts ? nr_checkouts
350 : &scratch_nr_checkouts;
351 free(new_blob);
352 goto delayed;
353 }
2841e8f8 354 }
30419e7e
MT
355 } else {
356 ret = convert_to_working_tree_ca(ca, ce->name, new_blob,
357 size, &buf, &meta);
358 }
7cbbf9d6
JK
359
360 if (ret) {
d8f71807
BW
361 free(new_blob);
362 new_blob = strbuf_detach(&buf, &newsize);
7cbbf9d6 363 size = newsize;
1a9d7e9b 364 }
7cbbf9d6
JK
365 /*
366 * No "else" here as errors from convert are OK at this
367 * point. If the error would have been fatal (e.g.
368 * filter is required), then we would have died already.
369 */
1a9d7e9b 370
7cbbf9d6 371 write_file_entry:
fd5db55d 372 fd = open_output_fd(path, ce, to_tempfile);
12dccc16 373 if (fd < 0) {
d8f71807 374 free(new_blob);
e1ebb3c2 375 return error_errno("unable to create file %s", path);
12dccc16 376 }
6c510bee 377
d8f71807 378 wrote = write_in_full(fd, new_blob, size);
fd5db55d 379 if (!to_tempfile)
49cfd903 380 fstat_done = fstat_checkout_output(fd, state, &st);
12dccc16 381 close(fd);
d8f71807 382 free(new_blob);
564bde9a 383 if (wrote < 0)
d43e9073 384 return error("unable to write file %s", path);
12dccc16 385 break;
7cbbf9d6 386
302b9282 387 case S_IFGITLINK:
f0807e62 388 if (to_tempfile)
9334ea8e 389 return error("cannot create temporary submodule %s", ce->name);
f0807e62 390 if (mkdir(path, 0777) < 0)
42063f95 391 return error("cannot create submodule directory %s", path);
6d14eac3
SB
392 sub = submodule_from_ce(ce);
393 if (sub)
4002ec3d 394 return submodule_move_head(ce->name, state->super_prefix,
cd279e2e
SB
395 NULL, oid_to_hex(&ce->oid),
396 state->force ? SUBMODULE_MOVE_HEAD_FORCE : 0);
f0807e62 397 break;
7cbbf9d6 398
12dccc16 399 default:
9334ea8e 400 return error("unknown file mode for %s in index", ce->name);
12dccc16
LT
401 }
402
dd8e9121 403finish:
6ee67f26 404 if (state->refresh_cache) {
584a0d13
MT
405 if (!fstat_done && lstat(ce->name, &st) < 0)
406 return error_errno("unable to stat just-written file %s",
407 ce->name);
408 update_ce_after_write(state, ce , &st);
12dccc16 409 }
611c7785
MT
410 if (nr_checkouts)
411 (*nr_checkouts)++;
03b95333 412delayed:
12dccc16
LT
413 return 0;
414}
415
b6986d8a
LT
416/*
417 * This is like 'lstat()', except it refuses to follow symlinks
da02ca50 418 * in the path, after skipping "skiplen".
b6986d8a 419 */
61b97df7 420static int check_path(const char *path, int len, struct stat *st, int skiplen)
b6986d8a 421{
da02ca50
JH
422 const char *slash = path + len;
423
424 while (path < slash && *slash != '/')
425 slash--;
426 if (!has_dirs_only_path(path, slash - path, skiplen)) {
b6986d8a
LT
427 errno = ENOENT;
428 return -1;
429 }
430 return lstat(path, st);
431}
432
b878579a
NTND
433static void mark_colliding_entries(const struct checkout *state,
434 struct cache_entry *ce, struct stat *st)
435{
436 int i, trust_ino = check_stat;
437
e66ceca9 438#if defined(GIT_WINDOWS_NATIVE) || defined(__CYGWIN__)
b878579a
NTND
439 trust_ino = 0;
440#endif
441
442 ce->ce_flags |= CE_MATCHED;
443
3450a304
DS
444 /* TODO: audit for interaction with sparse-index. */
445 ensure_full_index(state->istate);
b878579a
NTND
446 for (i = 0; i < state->istate->cache_nr; i++) {
447 struct cache_entry *dup = state->istate->cache[i];
448
04155bda
MT
449 if (dup == ce) {
450 /*
451 * Parallel checkout doesn't create the files in index
452 * order. So the other side of the collision may appear
453 * after the given cache_entry in the array.
454 */
455 if (parallel_checkout_status() == PC_RUNNING)
456 continue;
457 else
458 break;
459 }
b878579a
NTND
460
461 if (dup->ce_flags & (CE_MATCHED | CE_VALID | CE_SKIP_WORKTREE))
462 continue;
463
e66ceca9 464 if ((trust_ino && !match_stat_data(&dup->ce_stat_data, st)) ||
b878579a
NTND
465 (!trust_ino && !fspathcmp(ce->name, dup->name))) {
466 dup->ce_flags |= CE_MATCHED;
467 break;
468 }
469 }
470}
471
ae22751f
MT
472int checkout_entry_ca(struct cache_entry *ce, struct conv_attrs *ca,
473 const struct checkout *state, char *topath,
474 int *nr_checkouts)
12dccc16 475{
f63272a3 476 static struct strbuf path = STRBUF_INIT;
de84f99c 477 struct stat st;
ae22751f 478 struct conv_attrs ca_buf;
12dccc16 479
536ec183
TG
480 if (ce->ce_flags & CE_WT_REMOVE) {
481 if (topath)
482 /*
483 * No content and thus no path to create, so we have
484 * no pathname to return.
485 */
486 BUG("Can't remove entry to a path");
4002ec3d 487 unlink_entry(ce, state->super_prefix);
536ec183
TG
488 return 0;
489 }
490
30419e7e 491 if (topath) {
ae22751f 492 if (S_ISREG(ce->ce_mode) && !ca) {
30419e7e
MT
493 convert_attrs(state->istate, &ca_buf, ce->name);
494 ca = &ca_buf;
495 }
611c7785 496 return write_entry(ce, topath, ca, state, 1, nr_checkouts);
30419e7e 497 }
de84f99c 498
f63272a3
MH
499 strbuf_reset(&path);
500 strbuf_add(&path, state->base_dir, state->base_dir_len);
501 strbuf_add(&path, ce->name, ce_namelen(ce));
12dccc16 502
f63272a3 503 if (!check_path(path.buf, path.len, &st, state->base_dir_len)) {
6d14eac3 504 const struct submodule *sub;
74cfc0ee
NTND
505 unsigned changed = ie_match_stat(state->istate, ce, &st,
506 CE_MATCH_IGNORE_VALID | CE_MATCH_IGNORE_SKIP_WORKTREE);
6d14eac3
SB
507 /*
508 * Needs to be checked before !changed returns early,
509 * as the possibly empty directory was not changed
510 */
511 sub = submodule_from_ce(ce);
512 if (sub) {
513 int err;
514 if (!is_submodule_populated_gently(ce->name, &err)) {
515 struct stat sb;
516 if (lstat(ce->name, &sb))
517 die(_("could not stat file '%s'"), ce->name);
518 if (!(st.st_mode & S_IFDIR))
519 unlink_or_warn(ce->name);
520
4002ec3d 521 return submodule_move_head(ce->name, state->super_prefix,
cd279e2e 522 NULL, oid_to_hex(&ce->oid), 0);
6d14eac3 523 } else
4002ec3d 524 return submodule_move_head(ce->name, state->super_prefix,
6d14eac3 525 "HEAD", oid_to_hex(&ce->oid),
cd279e2e 526 state->force ? SUBMODULE_MOVE_HEAD_FORCE : 0);
6d14eac3
SB
527 }
528
12dccc16
LT
529 if (!changed)
530 return 0;
531 if (!state->force) {
532 if (!state->quiet)
f63272a3
MH
533 fprintf(stderr,
534 "%s already exists, no checkout\n",
535 path.buf);
4b12dae6 536 return -1;
12dccc16
LT
537 }
538
b878579a
NTND
539 if (state->clone)
540 mark_colliding_entries(state, ce, &st);
541
12dccc16
LT
542 /*
543 * We unlink the old file, to get the new one with the
544 * right permissions (including umask, which is nasty
545 * to emulate by hand - much easier to let the system
546 * just do the right thing)
547 */
d48a72f3 548 if (S_ISDIR(st.st_mode)) {
f0807e62 549 /* If it is a gitlink, leave it alone! */
7a51ed66 550 if (S_ISGITLINK(ce->ce_mode))
f0807e62 551 return 0;
2f29e0c6 552 remove_subtree(&path);
f63272a3 553 } else if (unlink(path.buf))
e1ebb3c2 554 return error_errno("unable to unlink old '%s'", path.buf);
de84f99c 555 } else if (state->not_new)
12dccc16 556 return 0;
f63272a3
MH
557
558 create_directories(path.buf, path.len, state);
30419e7e 559
ae22751f 560 if (S_ISREG(ce->ce_mode) && !ca) {
30419e7e
MT
561 convert_attrs(state->istate, &ca_buf, ce->name);
562 ca = &ca_buf;
563 }
564
611c7785 565 if (!enqueue_checkout(ce, ca, nr_checkouts))
04155bda
MT
566 return 0;
567
611c7785 568 return write_entry(ce, path.buf, ca, state, 0, nr_checkouts);
12dccc16 569}
b702dd12 570
4002ec3d 571void unlink_entry(const struct cache_entry *ce, const char *super_prefix)
b702dd12
TG
572{
573 const struct submodule *sub = submodule_from_ce(ce);
574 if (sub) {
575 /* state.force is set at the caller. */
4002ec3d 576 submodule_move_head(ce->name, super_prefix, "HEAD", NULL,
b702dd12
TG
577 SUBMODULE_MOVE_HEAD_FORCE);
578 }
fab78a0c 579 if (check_leading_path(ce->name, ce_namelen(ce), 1) >= 0)
b702dd12
TG
580 return;
581 if (remove_or_warn(ce->ce_mode, ce->name))
582 return;
583 schedule_dir_for_removal(ce->name, ce_namelen(ce));
584}