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