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