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.
 
 
 
 
 
 

293 lines
6.5 KiB

  1. #include "cache.h"
  2. #include "tmp-objdir.h"
  3. #include "dir.h"
  4. #include "sigchain.h"
  5. #include "string-list.h"
  6. #include "strbuf.h"
  7. #include "argv-array.h"
  8. #include "quote.h"
  9. #include "object-store.h"
  10. struct tmp_objdir {
  11. struct strbuf path;
  12. struct argv_array env;
  13. };
  14. /*
  15. * Allow only one tmp_objdir at a time in a running process, which simplifies
  16. * our signal/atexit cleanup routines. It's doubtful callers will ever need
  17. * more than one, and we can expand later if so. You can have many such
  18. * tmp_objdirs simultaneously in many processes, of course.
  19. */
  20. static struct tmp_objdir *the_tmp_objdir;
  21. static void tmp_objdir_free(struct tmp_objdir *t)
  22. {
  23. strbuf_release(&t->path);
  24. argv_array_clear(&t->env);
  25. free(t);
  26. }
  27. static int tmp_objdir_destroy_1(struct tmp_objdir *t, int on_signal)
  28. {
  29. int err;
  30. if (!t)
  31. return 0;
  32. if (t == the_tmp_objdir)
  33. the_tmp_objdir = NULL;
  34. /*
  35. * This may use malloc via strbuf_grow(), but we should
  36. * have pre-grown t->path sufficiently so that this
  37. * doesn't happen in practice.
  38. */
  39. err = remove_dir_recursively(&t->path, 0);
  40. /*
  41. * When we are cleaning up due to a signal, we won't bother
  42. * freeing memory; it may cause a deadlock if the signal
  43. * arrived while libc's allocator lock is held.
  44. */
  45. if (!on_signal)
  46. tmp_objdir_free(t);
  47. return err;
  48. }
  49. int tmp_objdir_destroy(struct tmp_objdir *t)
  50. {
  51. return tmp_objdir_destroy_1(t, 0);
  52. }
  53. static void remove_tmp_objdir(void)
  54. {
  55. tmp_objdir_destroy(the_tmp_objdir);
  56. }
  57. static void remove_tmp_objdir_on_signal(int signo)
  58. {
  59. tmp_objdir_destroy_1(the_tmp_objdir, 1);
  60. sigchain_pop(signo);
  61. raise(signo);
  62. }
  63. /*
  64. * These env_* functions are for setting up the child environment; the
  65. * "replace" variant overrides the value of any existing variable with that
  66. * "key". The "append" variant puts our new value at the end of a list,
  67. * separated by PATH_SEP (which is what separate values in
  68. * GIT_ALTERNATE_OBJECT_DIRECTORIES).
  69. */
  70. static void env_append(struct argv_array *env, const char *key, const char *val)
  71. {
  72. struct strbuf quoted = STRBUF_INIT;
  73. const char *old;
  74. /*
  75. * Avoid quoting if it's not necessary, for maximum compatibility
  76. * with older parsers which don't understand the quoting.
  77. */
  78. if (*val == '"' || strchr(val, PATH_SEP)) {
  79. strbuf_addch(&quoted, '"');
  80. quote_c_style(val, &quoted, NULL, 1);
  81. strbuf_addch(&quoted, '"');
  82. val = quoted.buf;
  83. }
  84. old = getenv(key);
  85. if (!old)
  86. argv_array_pushf(env, "%s=%s", key, val);
  87. else
  88. argv_array_pushf(env, "%s=%s%c%s", key, old, PATH_SEP, val);
  89. strbuf_release(&quoted);
  90. }
  91. static void env_replace(struct argv_array *env, const char *key, const char *val)
  92. {
  93. argv_array_pushf(env, "%s=%s", key, val);
  94. }
  95. static int setup_tmp_objdir(const char *root)
  96. {
  97. char *path;
  98. int ret = 0;
  99. path = xstrfmt("%s/pack", root);
  100. ret = mkdir(path, 0777);
  101. free(path);
  102. return ret;
  103. }
  104. struct tmp_objdir *tmp_objdir_create(void)
  105. {
  106. static int installed_handlers;
  107. struct tmp_objdir *t;
  108. if (the_tmp_objdir)
  109. BUG("only one tmp_objdir can be used at a time");
  110. t = xmalloc(sizeof(*t));
  111. strbuf_init(&t->path, 0);
  112. argv_array_init(&t->env);
  113. strbuf_addf(&t->path, "%s/incoming-XXXXXX", get_object_directory());
  114. /*
  115. * Grow the strbuf beyond any filename we expect to be placed in it.
  116. * If tmp_objdir_destroy() is called by a signal handler, then
  117. * we should be able to use the strbuf to remove files without
  118. * having to call malloc.
  119. */
  120. strbuf_grow(&t->path, 1024);
  121. if (!mkdtemp(t->path.buf)) {
  122. /* free, not destroy, as we never touched the filesystem */
  123. tmp_objdir_free(t);
  124. return NULL;
  125. }
  126. the_tmp_objdir = t;
  127. if (!installed_handlers) {
  128. atexit(remove_tmp_objdir);
  129. sigchain_push_common(remove_tmp_objdir_on_signal);
  130. installed_handlers++;
  131. }
  132. if (setup_tmp_objdir(t->path.buf)) {
  133. tmp_objdir_destroy(t);
  134. return NULL;
  135. }
  136. env_append(&t->env, ALTERNATE_DB_ENVIRONMENT,
  137. absolute_path(get_object_directory()));
  138. env_replace(&t->env, DB_ENVIRONMENT, absolute_path(t->path.buf));
  139. env_replace(&t->env, GIT_QUARANTINE_ENVIRONMENT,
  140. absolute_path(t->path.buf));
  141. return t;
  142. }
  143. /*
  144. * Make sure we copy packfiles and their associated metafiles in the correct
  145. * order. All of these ends_with checks are slightly expensive to do in
  146. * the midst of a sorting routine, but in practice it shouldn't matter.
  147. * We will have a relatively small number of packfiles to order, and loose
  148. * objects exit early in the first line.
  149. */
  150. static int pack_copy_priority(const char *name)
  151. {
  152. if (!starts_with(name, "pack"))
  153. return 0;
  154. if (ends_with(name, ".keep"))
  155. return 1;
  156. if (ends_with(name, ".pack"))
  157. return 2;
  158. if (ends_with(name, ".idx"))
  159. return 3;
  160. return 4;
  161. }
  162. static int pack_copy_cmp(const char *a, const char *b)
  163. {
  164. return pack_copy_priority(a) - pack_copy_priority(b);
  165. }
  166. static int read_dir_paths(struct string_list *out, const char *path)
  167. {
  168. DIR *dh;
  169. struct dirent *de;
  170. dh = opendir(path);
  171. if (!dh)
  172. return -1;
  173. while ((de = readdir(dh)))
  174. if (de->d_name[0] != '.')
  175. string_list_append(out, de->d_name);
  176. closedir(dh);
  177. return 0;
  178. }
  179. static int migrate_paths(struct strbuf *src, struct strbuf *dst);
  180. static int migrate_one(struct strbuf *src, struct strbuf *dst)
  181. {
  182. struct stat st;
  183. if (stat(src->buf, &st) < 0)
  184. return -1;
  185. if (S_ISDIR(st.st_mode)) {
  186. if (!mkdir(dst->buf, 0777)) {
  187. if (adjust_shared_perm(dst->buf))
  188. return -1;
  189. } else if (errno != EEXIST)
  190. return -1;
  191. return migrate_paths(src, dst);
  192. }
  193. return finalize_object_file(src->buf, dst->buf);
  194. }
  195. static int migrate_paths(struct strbuf *src, struct strbuf *dst)
  196. {
  197. size_t src_len = src->len, dst_len = dst->len;
  198. struct string_list paths = STRING_LIST_INIT_DUP;
  199. int i;
  200. int ret = 0;
  201. if (read_dir_paths(&paths, src->buf) < 0)
  202. return -1;
  203. paths.cmp = pack_copy_cmp;
  204. string_list_sort(&paths);
  205. for (i = 0; i < paths.nr; i++) {
  206. const char *name = paths.items[i].string;
  207. strbuf_addf(src, "/%s", name);
  208. strbuf_addf(dst, "/%s", name);
  209. ret |= migrate_one(src, dst);
  210. strbuf_setlen(src, src_len);
  211. strbuf_setlen(dst, dst_len);
  212. }
  213. string_list_clear(&paths, 0);
  214. return ret;
  215. }
  216. int tmp_objdir_migrate(struct tmp_objdir *t)
  217. {
  218. struct strbuf src = STRBUF_INIT, dst = STRBUF_INIT;
  219. int ret;
  220. if (!t)
  221. return 0;
  222. strbuf_addbuf(&src, &t->path);
  223. strbuf_addstr(&dst, get_object_directory());
  224. ret = migrate_paths(&src, &dst);
  225. strbuf_release(&src);
  226. strbuf_release(&dst);
  227. tmp_objdir_destroy(t);
  228. return ret;
  229. }
  230. const char **tmp_objdir_env(const struct tmp_objdir *t)
  231. {
  232. if (!t)
  233. return NULL;
  234. return t->env.argv;
  235. }
  236. void tmp_objdir_add_as_alternate(const struct tmp_objdir *t)
  237. {
  238. add_to_alternates_memory(t->path.buf);
  239. }