at commit 609ea236
| 1 | #include "git-compat-util.h" |
| 2 | #include "tmp-objdir.h" |
| 3 | #include "abspath.h" |
| 4 | #include "chdir-notify.h" |
| 5 | #include "dir.h" |
| 6 | #include "environment.h" |
| 7 | #include "object-file.h" |
| 8 | #include "path.h" |
| 9 | #include "string-list.h" |
| 10 | #include "strbuf.h" |
| 11 | #include "strvec.h" |
| 12 | #include "quote.h" |
| 13 | #include "odb.h" |
| 14 | #include "odb/source.h" |
| 15 | #include "repository.h" |
| 16 | |
| 17 | struct tmp_objdir { |
| 18 | struct repository *repo; |
| 19 | struct strbuf path; |
| 20 | struct strvec env; |
| 21 | struct odb_source *prev_source; |
| 22 | int will_destroy; |
| 23 | }; |
| 24 | |
| 25 | /* |
| 26 | * Allow only one tmp_objdir at a time in a running process, which simplifies |
| 27 | * our atexit cleanup routines. It's doubtful callers will ever need |
| 28 | * more than one, and we can expand later if so. You can have many such |
| 29 | * tmp_objdirs simultaneously in many processes, of course. |
| 30 | */ |
| 31 | static struct tmp_objdir *the_tmp_objdir; |
| 32 | |
| 33 | static void tmp_objdir_free(struct tmp_objdir *t) |
| 34 | { |
| 35 | strbuf_release(&t->path); |
| 36 | strvec_clear(&t->env); |
| 37 | free(t); |
| 38 | } |
| 39 | |
| 40 | static void tmp_objdir_reparent(const char *old_cwd, |
| 41 | const char *new_cwd, |
| 42 | void *cb_data) |
| 43 | { |
| 44 | struct tmp_objdir *t = cb_data; |
| 45 | char *path; |
| 46 | |
| 47 | path = reparent_relative_path(old_cwd, new_cwd, |
| 48 | t->path.buf); |
| 49 | strbuf_reset(&t->path); |
| 50 | strbuf_addstr(&t->path, path); |
| 51 | free(path); |
| 52 | } |
| 53 | |
| 54 | int tmp_objdir_destroy(struct tmp_objdir *t) |
| 55 | { |
| 56 | int err; |
| 57 | |
| 58 | if (!t) |
| 59 | return 0; |
| 60 | |
| 61 | if (t == the_tmp_objdir) |
| 62 | the_tmp_objdir = NULL; |
| 63 | |
| 64 | if (t->prev_source) |
| 65 | odb_restore_primary_source(t->repo->objects, t->prev_source, t->path.buf); |
| 66 | |
| 67 | err = remove_dir_recursively(&t->path, 0); |
| 68 | |
| 69 | chdir_notify_unregister(tmp_objdir_reparent, t); |
| 70 | tmp_objdir_free(t); |
| 71 | |
| 72 | return err; |
| 73 | } |
| 74 | |
| 75 | static void remove_tmp_objdir(void) |
| 76 | { |
| 77 | tmp_objdir_destroy(the_tmp_objdir); |
| 78 | } |
| 79 | |
| 80 | void tmp_objdir_discard_objects(struct tmp_objdir *t) |
| 81 | { |
| 82 | remove_dir_recursively(&t->path, REMOVE_DIR_KEEP_TOPLEVEL); |
| 83 | } |
| 84 | |
| 85 | /* |
| 86 | * These env_* functions are for setting up the child environment; the |
| 87 | * "replace" variant overrides the value of any existing variable with that |
| 88 | * "key". The "append" variant puts our new value at the end of a list, |
| 89 | * separated by PATH_SEP (which is what separate values in |
| 90 | * GIT_ALTERNATE_OBJECT_DIRECTORIES). |
| 91 | */ |
| 92 | static void env_append(struct strvec *env, const char *key, const char *val) |
| 93 | { |
| 94 | struct strbuf quoted = STRBUF_INIT; |
| 95 | const char *old; |
| 96 | |
| 97 | /* |
| 98 | * Avoid quoting if it's not necessary, for maximum compatibility |
| 99 | * with older parsers which don't understand the quoting. |
| 100 | */ |
| 101 | if (*val == '"' || strchr(val, PATH_SEP)) { |
| 102 | strbuf_addch("ed, '"'); |
| 103 | quote_c_style(val, "ed, NULL, 1); |
| 104 | strbuf_addch("ed, '"'); |
| 105 | val = quoted.buf; |
| 106 | } |
| 107 | |
| 108 | old = getenv(key); |
| 109 | if (!old) |
| 110 | strvec_pushf(env, "%s=%s", key, val); |
| 111 | else |
| 112 | strvec_pushf(env, "%s=%s%c%s", key, old, PATH_SEP, val); |
| 113 | |
| 114 | strbuf_release("ed); |
| 115 | } |
| 116 | |
| 117 | static void env_replace(struct strvec *env, const char *key, const char *val) |
| 118 | { |
| 119 | strvec_pushf(env, "%s=%s", key, val); |
| 120 | } |
| 121 | |
| 122 | static int setup_tmp_objdir(const char *root) |
| 123 | { |
| 124 | char *path; |
| 125 | int ret = 0; |
| 126 | |
| 127 | path = xstrfmt("%s/pack", root); |
| 128 | ret = mkdir(path, 0777); |
| 129 | free(path); |
| 130 | |
| 131 | return ret; |
| 132 | } |
| 133 | |
| 134 | struct tmp_objdir *tmp_objdir_create(struct repository *r, |
| 135 | const char *prefix) |
| 136 | { |
| 137 | static int installed_handlers; |
| 138 | struct tmp_objdir *t; |
| 139 | |
| 140 | if (the_tmp_objdir) |
| 141 | BUG("only one tmp_objdir can be used at a time"); |
| 142 | |
| 143 | t = xcalloc(1, sizeof(*t)); |
| 144 | t->repo = r; |
| 145 | strbuf_init(&t->path, 0); |
| 146 | strvec_init(&t->env); |
| 147 | |
| 148 | /* |
| 149 | * Use a string starting with tmp_ so that the builtin/prune.c code |
| 150 | * can recognize any stale objdirs left behind by a crash and delete |
| 151 | * them. |
| 152 | */ |
| 153 | strbuf_addf(&t->path, "%s/tmp_objdir-%s-XXXXXX", |
| 154 | repo_get_object_directory(r), prefix); |
| 155 | |
| 156 | if (!is_absolute_path(t->path.buf)) |
| 157 | chdir_notify_register(tmp_objdir_reparent, t); |
| 158 | |
| 159 | if (!mkdtemp(t->path.buf)) { |
| 160 | /* free, not destroy, as we never touched the filesystem */ |
| 161 | tmp_objdir_free(t); |
| 162 | return NULL; |
| 163 | } |
| 164 | |
| 165 | the_tmp_objdir = t; |
| 166 | if (!installed_handlers) { |
| 167 | atexit(remove_tmp_objdir); |
| 168 | installed_handlers++; |
| 169 | } |
| 170 | |
| 171 | if (setup_tmp_objdir(t->path.buf)) { |
| 172 | tmp_objdir_destroy(t); |
| 173 | return NULL; |
| 174 | } |
| 175 | |
| 176 | env_append(&t->env, ALTERNATE_DB_ENVIRONMENT, |
| 177 | absolute_path(repo_get_object_directory(r))); |
| 178 | env_replace(&t->env, DB_ENVIRONMENT, absolute_path(t->path.buf)); |
| 179 | env_replace(&t->env, GIT_QUARANTINE_ENVIRONMENT, |
| 180 | absolute_path(t->path.buf)); |
| 181 | |
| 182 | return t; |
| 183 | } |
| 184 | |
| 185 | /* |
| 186 | * Make sure we copy packfiles and their associated metafiles in the correct |
| 187 | * order. All of these ends_with checks are slightly expensive to do in |
| 188 | * the midst of a sorting routine, but in practice it shouldn't matter. |
| 189 | * We will have a relatively small number of packfiles to order, and loose |
| 190 | * objects exit early in the first line. |
| 191 | */ |
| 192 | static int pack_copy_priority(const char *name) |
| 193 | { |
| 194 | if (!starts_with(name, "pack")) |
| 195 | return 0; |
| 196 | if (ends_with(name, ".keep")) |
| 197 | return 1; |
| 198 | if (ends_with(name, ".pack")) |
| 199 | return 2; |
| 200 | if (ends_with(name, ".rev")) |
| 201 | return 3; |
| 202 | if (ends_with(name, ".idx")) |
| 203 | return 4; |
| 204 | return 5; |
| 205 | } |
| 206 | |
| 207 | static int pack_copy_cmp(const char *a, const char *b) |
| 208 | { |
| 209 | return pack_copy_priority(a) - pack_copy_priority(b); |
| 210 | } |
| 211 | |
| 212 | static int read_dir_paths(struct string_list *out, const char *path) |
| 213 | { |
| 214 | DIR *dh; |
| 215 | struct dirent *de; |
| 216 | |
| 217 | dh = opendir(path); |
| 218 | if (!dh) |
| 219 | return -1; |
| 220 | |
| 221 | while ((de = readdir(dh))) |
| 222 | if (de->d_name[0] != '.') |
| 223 | string_list_append(out, de->d_name); |
| 224 | |
| 225 | closedir(dh); |
| 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | static int migrate_paths(struct tmp_objdir *t, |
| 230 | struct strbuf *src, struct strbuf *dst, |
| 231 | enum finalize_object_file_flags flags); |
| 232 | |
| 233 | static int migrate_one(struct tmp_objdir *t, |
| 234 | struct strbuf *src, struct strbuf *dst, |
| 235 | enum finalize_object_file_flags flags) |
| 236 | { |
| 237 | struct stat st; |
| 238 | |
| 239 | if (stat(src->buf, &st) < 0) |
| 240 | return -1; |
| 241 | if (S_ISDIR(st.st_mode)) { |
| 242 | if (!mkdir(dst->buf, 0777)) { |
| 243 | if (adjust_shared_perm(t->repo, dst->buf)) |
| 244 | return -1; |
| 245 | } else if (errno != EEXIST) |
| 246 | return -1; |
| 247 | return migrate_paths(t, src, dst, flags); |
| 248 | } |
| 249 | return finalize_object_file_flags(t->repo, src->buf, dst->buf, flags); |
| 250 | } |
| 251 | |
| 252 | static int is_loose_object_shard(const char *name) |
| 253 | { |
| 254 | return strlen(name) == 2 && isxdigit(name[0]) && isxdigit(name[1]); |
| 255 | } |
| 256 | |
| 257 | static int migrate_paths(struct tmp_objdir *t, |
| 258 | struct strbuf *src, struct strbuf *dst, |
| 259 | enum finalize_object_file_flags flags) |
| 260 | { |
| 261 | size_t src_len = src->len, dst_len = dst->len; |
| 262 | struct string_list paths = STRING_LIST_INIT_DUP; |
| 263 | int ret = 0; |
| 264 | |
| 265 | if (read_dir_paths(&paths, src->buf) < 0) |
| 266 | return -1; |
| 267 | paths.cmp = pack_copy_cmp; |
| 268 | string_list_sort(&paths); |
| 269 | |
| 270 | for (size_t i = 0; i < paths.nr; i++) { |
| 271 | const char *name = paths.items[i].string; |
| 272 | enum finalize_object_file_flags flags_copy = flags; |
| 273 | |
| 274 | strbuf_addf(src, "/%s", name); |
| 275 | strbuf_addf(dst, "/%s", name); |
| 276 | |
| 277 | if (is_loose_object_shard(name)) |
| 278 | flags_copy |= FOF_SKIP_COLLISION_CHECK; |
| 279 | |
| 280 | ret |= migrate_one(t, src, dst, flags_copy); |
| 281 | |
| 282 | strbuf_setlen(src, src_len); |
| 283 | strbuf_setlen(dst, dst_len); |
| 284 | } |
| 285 | |
| 286 | string_list_clear(&paths, 0); |
| 287 | return ret; |
| 288 | } |
| 289 | |
| 290 | int tmp_objdir_migrate(struct tmp_objdir *t) |
| 291 | { |
| 292 | struct strbuf src = STRBUF_INIT, dst = STRBUF_INIT; |
| 293 | int ret; |
| 294 | |
| 295 | if (!t) |
| 296 | return 0; |
| 297 | |
| 298 | if (t->prev_source) { |
| 299 | if (t->repo->objects->sources->will_destroy) |
| 300 | BUG("migrating an ODB that was marked for destruction"); |
| 301 | odb_restore_primary_source(t->repo->objects, t->prev_source, t->path.buf); |
| 302 | t->prev_source = NULL; |
| 303 | } |
| 304 | |
| 305 | strbuf_addbuf(&src, &t->path); |
| 306 | strbuf_addstr(&dst, repo_get_object_directory(t->repo)); |
| 307 | |
| 308 | ret = migrate_paths(t, &src, &dst, 0); |
| 309 | |
| 310 | strbuf_release(&src); |
| 311 | strbuf_release(&dst); |
| 312 | |
| 313 | tmp_objdir_destroy(t); |
| 314 | return ret; |
| 315 | } |
| 316 | |
| 317 | const char **tmp_objdir_env(const struct tmp_objdir *t) |
| 318 | { |
| 319 | if (!t) |
| 320 | return NULL; |
| 321 | return t->env.v; |
| 322 | } |
| 323 | |
| 324 | void tmp_objdir_add_as_alternate(const struct tmp_objdir *t) |
| 325 | { |
| 326 | odb_add_to_alternates_memory(t->repo->objects, t->path.buf); |
| 327 | } |
| 328 | |
| 329 | struct odb_source *tmp_objdir_replace_primary_odb(struct tmp_objdir *t, |
| 330 | int will_destroy) |
| 331 | { |
| 332 | if (t->prev_source) |
| 333 | BUG("the primary object database is already replaced"); |
| 334 | t->will_destroy = will_destroy; |
| 335 | |
| 336 | return odb_set_temporary_primary_source(t->repo->objects, t->path.buf, |
| 337 | will_destroy, &t->prev_source); |
| 338 | } |