entry.c 14 KB

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