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