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