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