column: release strbuf and string_list after use
[alt-git.git] / entry.c
blob616e4f073c1d6bd4016252423d4ced0958612432
1 #include "cache.h"
2 #include "blob.h"
3 #include "object-store.h"
4 #include "dir.h"
5 #include "streaming.h"
6 #include "submodule.h"
7 #include "progress.h"
8 #include "fsmonitor.h"
9 #include "entry.h"
10 #include "parallel-checkout.h"
12 static void create_directories(const char *path, int path_len,
13 const struct checkout *state)
15 char *buf = xmallocz(path_len);
16 int len = 0;
18 while (len < path_len) {
19 do {
20 buf[len] = path[len];
21 len++;
22 } while (len < path_len && path[len] != '/');
23 if (len >= path_len)
24 break;
25 buf[len] = 0;
28 * For 'checkout-index --prefix=<dir>', <dir> is
29 * allowed to be a symlink to an existing directory,
30 * and we set 'state->base_dir_len' below, such that
31 * we test the path components of the prefix with the
32 * stat() function instead of the lstat() function.
34 if (has_dirs_only_path(buf, len, state->base_dir_len))
35 continue; /* ok, it is already a directory. */
38 * If this mkdir() would fail, it could be that there
39 * is already a symlink or something else exists
40 * there, therefore we then try to unlink it and try
41 * one more time to create the directory.
43 if (mkdir(buf, 0777)) {
44 if (errno == EEXIST && state->force &&
45 !unlink_or_warn(buf) && !mkdir(buf, 0777))
46 continue;
47 die_errno("cannot create directory at '%s'", buf);
50 free(buf);
53 static void remove_subtree(struct strbuf *path)
55 DIR *dir = opendir(path->buf);
56 struct dirent *de;
57 int origlen = path->len;
59 if (!dir)
60 die_errno("cannot opendir '%s'", path->buf);
61 while ((de = readdir_skip_dot_and_dotdot(dir)) != NULL) {
62 struct stat st;
64 strbuf_addch(path, '/');
65 strbuf_addstr(path, de->d_name);
66 if (lstat(path->buf, &st))
67 die_errno("cannot lstat '%s'", path->buf);
68 if (S_ISDIR(st.st_mode))
69 remove_subtree(path);
70 else if (unlink(path->buf))
71 die_errno("cannot unlink '%s'", path->buf);
72 strbuf_setlen(path, origlen);
74 closedir(dir);
75 if (rmdir(path->buf))
76 die_errno("cannot rmdir '%s'", path->buf);
79 static int create_file(const char *path, unsigned int mode)
81 mode = (mode & 0100) ? 0777 : 0666;
82 return open(path, O_WRONLY | O_CREAT | O_EXCL, mode);
85 void *read_blob_entry(const struct cache_entry *ce, size_t *size)
87 enum object_type type;
88 unsigned long ul;
89 void *blob_data = read_object_file(&ce->oid, &type, &ul);
91 *size = ul;
92 if (blob_data) {
93 if (type == OBJ_BLOB)
94 return blob_data;
95 free(blob_data);
97 return NULL;
100 static int open_output_fd(char *path, const struct cache_entry *ce, int to_tempfile)
102 int symlink = (ce->ce_mode & S_IFMT) != S_IFREG;
103 if (to_tempfile) {
104 xsnprintf(path, TEMPORARY_FILENAME_LENGTH, "%s",
105 symlink ? ".merge_link_XXXXXX" : ".merge_file_XXXXXX");
106 return mkstemp(path);
107 } else {
108 return create_file(path, !symlink ? ce->ce_mode : 0666);
112 int fstat_checkout_output(int fd, const struct checkout *state, struct stat *st)
114 /* use fstat() only when path == ce->name */
115 if (fstat_is_reliable() &&
116 state->refresh_cache && !state->base_dir_len) {
117 return !fstat(fd, st);
119 return 0;
122 static int streaming_write_entry(const struct cache_entry *ce, char *path,
123 struct stream_filter *filter,
124 const struct checkout *state, int to_tempfile,
125 int *fstat_done, struct stat *statbuf)
127 int result = 0;
128 int fd;
130 fd = open_output_fd(path, ce, to_tempfile);
131 if (fd < 0)
132 return -1;
134 result |= stream_blob_to_fd(fd, &ce->oid, filter, 1);
135 *fstat_done = fstat_checkout_output(fd, state, statbuf);
136 result |= close(fd);
138 if (result)
139 unlink(path);
140 return result;
143 void enable_delayed_checkout(struct checkout *state)
145 if (!state->delayed_checkout) {
146 state->delayed_checkout = xmalloc(sizeof(*state->delayed_checkout));
147 state->delayed_checkout->state = CE_CAN_DELAY;
148 string_list_init_nodup(&state->delayed_checkout->filters);
149 string_list_init_nodup(&state->delayed_checkout->paths);
153 static int remove_available_paths(struct string_list_item *item, void *cb_data)
155 struct string_list *available_paths = cb_data;
156 struct string_list_item *available;
158 available = string_list_lookup(available_paths, item->string);
159 if (available)
160 available->util = item->util;
161 return !available;
164 int finish_delayed_checkout(struct checkout *state, int show_progress)
166 int errs = 0;
167 unsigned processed_paths = 0;
168 off_t filtered_bytes = 0;
169 struct string_list_item *filter, *path;
170 struct progress *progress = NULL;
171 struct delayed_checkout *dco = state->delayed_checkout;
173 if (!state->delayed_checkout)
174 return errs;
176 dco->state = CE_RETRY;
177 if (show_progress)
178 progress = start_delayed_progress(_("Filtering content"), dco->paths.nr);
179 while (dco->filters.nr > 0) {
180 for_each_string_list_item(filter, &dco->filters) {
181 struct string_list available_paths = STRING_LIST_INIT_NODUP;
183 if (!async_query_available_blobs(filter->string, &available_paths)) {
184 /* Filter reported an error */
185 errs = 1;
186 filter->string = "";
187 continue;
189 if (available_paths.nr <= 0) {
191 * Filter responded with no entries. That means
192 * the filter is done and we can remove the
193 * filter from the list (see
194 * "string_list_remove_empty_items" call below).
196 filter->string = "";
197 continue;
201 * In dco->paths we store a list of all delayed paths.
202 * The filter just send us a list of available paths.
203 * Remove them from the list.
205 filter_string_list(&dco->paths, 0,
206 &remove_available_paths, &available_paths);
208 for_each_string_list_item(path, &available_paths) {
209 struct cache_entry* ce;
211 if (!path->util) {
212 error("external filter '%s' signaled that '%s' "
213 "is now available although it has not been "
214 "delayed earlier",
215 filter->string, path->string);
216 errs |= 1;
219 * Do not ask the filter for available blobs,
220 * again, as the filter is likely buggy.
222 filter->string = "";
223 continue;
225 ce = index_file_exists(state->istate, path->string,
226 strlen(path->string), 0);
227 if (ce) {
228 display_progress(progress, ++processed_paths);
229 errs |= checkout_entry(ce, state, NULL, path->util);
230 filtered_bytes += ce->ce_stat_data.sd_size;
231 display_throughput(progress, filtered_bytes);
232 } else
233 errs = 1;
236 string_list_remove_empty_items(&dco->filters, 0);
238 stop_progress(&progress);
239 string_list_clear(&dco->filters, 0);
241 /* At this point we should not have any delayed paths anymore. */
242 errs |= dco->paths.nr;
243 for_each_string_list_item(path, &dco->paths) {
244 error("'%s' was not filtered properly", path->string);
246 string_list_clear(&dco->paths, 0);
248 free(dco);
249 state->delayed_checkout = NULL;
251 return errs;
254 void update_ce_after_write(const struct checkout *state, struct cache_entry *ce,
255 struct stat *st)
257 if (state->refresh_cache) {
258 assert(state->istate);
259 fill_stat_cache_info(state->istate, ce, st);
260 ce->ce_flags |= CE_UPDATE_IN_BASE;
261 mark_fsmonitor_invalid(state->istate, ce);
262 state->istate->cache_changed |= CE_ENTRY_CHANGED;
266 /* Note: ca is used (and required) iff the entry refers to a regular file. */
267 static int write_entry(struct cache_entry *ce, char *path, struct conv_attrs *ca,
268 const struct checkout *state, int to_tempfile,
269 int *nr_checkouts)
271 unsigned int ce_mode_s_ifmt = ce->ce_mode & S_IFMT;
272 struct delayed_checkout *dco = state->delayed_checkout;
273 int fd, ret, fstat_done = 0;
274 char *new_blob;
275 struct strbuf buf = STRBUF_INIT;
276 size_t size;
277 ssize_t wrote;
278 size_t newsize = 0;
279 struct stat st;
280 const struct submodule *sub;
281 struct checkout_metadata meta;
282 static int scratch_nr_checkouts;
284 clone_checkout_metadata(&meta, &state->meta, &ce->oid);
286 if (ce_mode_s_ifmt == S_IFREG) {
287 struct stream_filter *filter = get_stream_filter_ca(ca, &ce->oid);
288 if (filter &&
289 !streaming_write_entry(ce, path, filter,
290 state, to_tempfile,
291 &fstat_done, &st))
292 goto finish;
295 switch (ce_mode_s_ifmt) {
296 case S_IFLNK:
297 new_blob = read_blob_entry(ce, &size);
298 if (!new_blob)
299 return error("unable to read sha1 file of %s (%s)",
300 ce->name, oid_to_hex(&ce->oid));
303 * We can't make a real symlink; write out a regular file entry
304 * with the symlink destination as its contents.
306 if (!has_symlinks || to_tempfile)
307 goto write_file_entry;
309 ret = symlink(new_blob, path);
310 free(new_blob);
311 if (ret)
312 return error_errno("unable to create symlink %s", path);
313 break;
315 case S_IFREG:
317 * We do not send the blob in case of a retry, so do not
318 * bother reading it at all.
320 if (dco && dco->state == CE_RETRY) {
321 new_blob = NULL;
322 size = 0;
323 } else {
324 new_blob = read_blob_entry(ce, &size);
325 if (!new_blob)
326 return error("unable to read sha1 file of %s (%s)",
327 ce->name, oid_to_hex(&ce->oid));
331 * Convert from git internal format to working tree format
333 if (dco && dco->state != CE_NO_DELAY) {
334 ret = async_convert_to_working_tree_ca(ca, ce->name,
335 new_blob, size,
336 &buf, &meta, dco);
337 if (ret) {
338 struct string_list_item *item =
339 string_list_lookup(&dco->paths, ce->name);
340 if (item) {
341 item->util = nr_checkouts ? nr_checkouts
342 : &scratch_nr_checkouts;
343 free(new_blob);
344 goto delayed;
347 } else {
348 ret = convert_to_working_tree_ca(ca, ce->name, new_blob,
349 size, &buf, &meta);
352 if (ret) {
353 free(new_blob);
354 new_blob = strbuf_detach(&buf, &newsize);
355 size = newsize;
358 * No "else" here as errors from convert are OK at this
359 * point. If the error would have been fatal (e.g.
360 * filter is required), then we would have died already.
363 write_file_entry:
364 fd = open_output_fd(path, ce, to_tempfile);
365 if (fd < 0) {
366 free(new_blob);
367 return error_errno("unable to create file %s", path);
370 wrote = write_in_full(fd, new_blob, size);
371 if (!to_tempfile)
372 fstat_done = fstat_checkout_output(fd, state, &st);
373 close(fd);
374 free(new_blob);
375 if (wrote < 0)
376 return error("unable to write file %s", path);
377 break;
379 case S_IFGITLINK:
380 if (to_tempfile)
381 return error("cannot create temporary submodule %s", ce->name);
382 if (mkdir(path, 0777) < 0)
383 return error("cannot create submodule directory %s", path);
384 sub = submodule_from_ce(ce);
385 if (sub)
386 return submodule_move_head(ce->name,
387 NULL, oid_to_hex(&ce->oid),
388 state->force ? SUBMODULE_MOVE_HEAD_FORCE : 0);
389 break;
391 default:
392 return error("unknown file mode for %s in index", ce->name);
395 finish:
396 if (state->refresh_cache) {
397 if (!fstat_done && lstat(ce->name, &st) < 0)
398 return error_errno("unable to stat just-written file %s",
399 ce->name);
400 update_ce_after_write(state, ce , &st);
402 if (nr_checkouts)
403 (*nr_checkouts)++;
404 delayed:
405 return 0;
409 * This is like 'lstat()', except it refuses to follow symlinks
410 * in the path, after skipping "skiplen".
412 static int check_path(const char *path, int len, struct stat *st, int skiplen)
414 const char *slash = path + len;
416 while (path < slash && *slash != '/')
417 slash--;
418 if (!has_dirs_only_path(path, slash - path, skiplen)) {
419 errno = ENOENT;
420 return -1;
422 return lstat(path, st);
425 static void mark_colliding_entries(const struct checkout *state,
426 struct cache_entry *ce, struct stat *st)
428 int i, trust_ino = check_stat;
430 #if defined(GIT_WINDOWS_NATIVE) || defined(__CYGWIN__)
431 trust_ino = 0;
432 #endif
434 ce->ce_flags |= CE_MATCHED;
436 /* TODO: audit for interaction with sparse-index. */
437 ensure_full_index(state->istate);
438 for (i = 0; i < state->istate->cache_nr; i++) {
439 struct cache_entry *dup = state->istate->cache[i];
441 if (dup == ce) {
443 * Parallel checkout doesn't create the files in index
444 * order. So the other side of the collision may appear
445 * after the given cache_entry in the array.
447 if (parallel_checkout_status() == PC_RUNNING)
448 continue;
449 else
450 break;
453 if (dup->ce_flags & (CE_MATCHED | CE_VALID | CE_SKIP_WORKTREE))
454 continue;
456 if ((trust_ino && !match_stat_data(&dup->ce_stat_data, st)) ||
457 (!trust_ino && !fspathcmp(ce->name, dup->name))) {
458 dup->ce_flags |= CE_MATCHED;
459 break;
464 int checkout_entry_ca(struct cache_entry *ce, struct conv_attrs *ca,
465 const struct checkout *state, char *topath,
466 int *nr_checkouts)
468 static struct strbuf path = STRBUF_INIT;
469 struct stat st;
470 struct conv_attrs ca_buf;
472 if (ce->ce_flags & CE_WT_REMOVE) {
473 if (topath)
475 * No content and thus no path to create, so we have
476 * no pathname to return.
478 BUG("Can't remove entry to a path");
479 unlink_entry(ce);
480 return 0;
483 if (topath) {
484 if (S_ISREG(ce->ce_mode) && !ca) {
485 convert_attrs(state->istate, &ca_buf, ce->name);
486 ca = &ca_buf;
488 return write_entry(ce, topath, ca, state, 1, nr_checkouts);
491 strbuf_reset(&path);
492 strbuf_add(&path, state->base_dir, state->base_dir_len);
493 strbuf_add(&path, ce->name, ce_namelen(ce));
495 if (!check_path(path.buf, path.len, &st, state->base_dir_len)) {
496 const struct submodule *sub;
497 unsigned changed = ie_match_stat(state->istate, ce, &st,
498 CE_MATCH_IGNORE_VALID | CE_MATCH_IGNORE_SKIP_WORKTREE);
500 * Needs to be checked before !changed returns early,
501 * as the possibly empty directory was not changed
503 sub = submodule_from_ce(ce);
504 if (sub) {
505 int err;
506 if (!is_submodule_populated_gently(ce->name, &err)) {
507 struct stat sb;
508 if (lstat(ce->name, &sb))
509 die(_("could not stat file '%s'"), ce->name);
510 if (!(st.st_mode & S_IFDIR))
511 unlink_or_warn(ce->name);
513 return submodule_move_head(ce->name,
514 NULL, oid_to_hex(&ce->oid), 0);
515 } else
516 return submodule_move_head(ce->name,
517 "HEAD", oid_to_hex(&ce->oid),
518 state->force ? SUBMODULE_MOVE_HEAD_FORCE : 0);
521 if (!changed)
522 return 0;
523 if (!state->force) {
524 if (!state->quiet)
525 fprintf(stderr,
526 "%s already exists, no checkout\n",
527 path.buf);
528 return -1;
531 if (state->clone)
532 mark_colliding_entries(state, ce, &st);
535 * We unlink the old file, to get the new one with the
536 * right permissions (including umask, which is nasty
537 * to emulate by hand - much easier to let the system
538 * just do the right thing)
540 if (S_ISDIR(st.st_mode)) {
541 /* If it is a gitlink, leave it alone! */
542 if (S_ISGITLINK(ce->ce_mode))
543 return 0;
544 remove_subtree(&path);
545 } else if (unlink(path.buf))
546 return error_errno("unable to unlink old '%s'", path.buf);
547 } else if (state->not_new)
548 return 0;
550 create_directories(path.buf, path.len, state);
552 if (S_ISREG(ce->ce_mode) && !ca) {
553 convert_attrs(state->istate, &ca_buf, ce->name);
554 ca = &ca_buf;
557 if (!enqueue_checkout(ce, ca, nr_checkouts))
558 return 0;
560 return write_entry(ce, path.buf, ca, state, 0, nr_checkouts);
563 void unlink_entry(const struct cache_entry *ce)
565 const struct submodule *sub = submodule_from_ce(ce);
566 if (sub) {
567 /* state.force is set at the caller. */
568 submodule_move_head(ce->name, "HEAD", NULL,
569 SUBMODULE_MOVE_HEAD_FORCE);
571 if (check_leading_path(ce->name, ce_namelen(ce), 1) >= 0)
572 return;
573 if (remove_or_warn(ce->ce_mode, ce->name))
574 return;
575 schedule_dir_for_removal(ce->name, ce_namelen(ce));