THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

539 lines
14KB

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