blob: 9da0071cba837586241994367d0d3392dbb1bfab [file] [log] [blame]
Patrick Steinhardte7da9382024-06-14 08:50:23 +02001#define USE_THE_REPOSITORY_VARIABLE
2
Elijah Newrenb6fdc442023-04-11 00:41:54 -07003#include "git-compat-util.h"
Jeff King2564d992016-10-03 16:49:11 -04004#include "tmp-objdir.h"
Elijah Newren0b027f62023-03-21 06:25:58 +00005#include "abspath.h"
Neeraj Singhb3cecf42021-12-06 22:05:04 +00006#include "chdir-notify.h"
Jeff King2564d992016-10-03 16:49:11 -04007#include "dir.h"
Elijah Newren32a8f512023-03-21 06:26:03 +00008#include "environment.h"
Elijah Newren87bed172023-04-11 00:41:53 -07009#include "object-file.h"
Elijah Newrenc3399322023-05-16 06:33:59 +000010#include "path.h"
Jeff King2564d992016-10-03 16:49:11 -040011#include "string-list.h"
12#include "strbuf.h"
Jeff Kingdbbcd442020-07-28 16:23:39 -040013#include "strvec.h"
Jeff Kingaae2ae42016-12-12 14:53:55 -050014#include "quote.h"
Elijah Newrena034e912023-05-16 06:34:06 +000015#include "object-store-ll.h"
Patrick Steinhardta3673f42024-09-12 13:29:30 +020016#include "repository.h"
Jeff King2564d992016-10-03 16:49:11 -040017
18struct tmp_objdir {
19 struct strbuf path;
Jeff Kingc972bf42020-07-28 16:25:12 -040020 struct strvec env;
Neeraj Singhb3cecf42021-12-06 22:05:04 +000021 struct object_directory *prev_odb;
22 int will_destroy;
Jeff King2564d992016-10-03 16:49:11 -040023};
24
25/*
26 * Allow only one tmp_objdir at a time in a running process, which simplifies
John Cai22613b22022-09-30 20:47:11 +000027 * our atexit cleanup routines. It's doubtful callers will ever need
Jeff King2564d992016-10-03 16:49:11 -040028 * 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 */
31static struct tmp_objdir *the_tmp_objdir;
32
33static void tmp_objdir_free(struct tmp_objdir *t)
34{
35 strbuf_release(&t->path);
Jeff Kingc972bf42020-07-28 16:25:12 -040036 strvec_clear(&t->env);
Jeff King2564d992016-10-03 16:49:11 -040037 free(t);
38}
39
John Cai22613b22022-09-30 20:47:11 +000040int tmp_objdir_destroy(struct tmp_objdir *t)
Jeff King2564d992016-10-03 16:49:11 -040041{
42 int err;
43
44 if (!t)
45 return 0;
46
47 if (t == the_tmp_objdir)
48 the_tmp_objdir = NULL;
49
John Cai22613b22022-09-30 20:47:11 +000050 if (t->prev_odb)
Neeraj Singhb3cecf42021-12-06 22:05:04 +000051 restore_primary_odb(t->prev_odb, t->path.buf);
52
Jeff King2564d992016-10-03 16:49:11 -040053 err = remove_dir_recursively(&t->path, 0);
54
John Cai22613b22022-09-30 20:47:11 +000055 tmp_objdir_free(t);
Neeraj Singhb3cecf42021-12-06 22:05:04 +000056
Jeff King2564d992016-10-03 16:49:11 -040057 return err;
58}
59
Jeff King2564d992016-10-03 16:49:11 -040060static void remove_tmp_objdir(void)
61{
62 tmp_objdir_destroy(the_tmp_objdir);
63}
64
Elijah Newren7b90ab42022-02-02 02:37:29 +000065void tmp_objdir_discard_objects(struct tmp_objdir *t)
66{
67 remove_dir_recursively(&t->path, REMOVE_DIR_KEEP_TOPLEVEL);
68}
69
Jeff King2564d992016-10-03 16:49:11 -040070/*
71 * These env_* functions are for setting up the child environment; the
72 * "replace" variant overrides the value of any existing variable with that
73 * "key". The "append" variant puts our new value at the end of a list,
74 * separated by PATH_SEP (which is what separate values in
75 * GIT_ALTERNATE_OBJECT_DIRECTORIES).
76 */
Jeff Kingc972bf42020-07-28 16:25:12 -040077static void env_append(struct strvec *env, const char *key, const char *val)
Jeff King2564d992016-10-03 16:49:11 -040078{
Jeff Kingaae2ae42016-12-12 14:53:55 -050079 struct strbuf quoted = STRBUF_INIT;
80 const char *old;
Jeff King2564d992016-10-03 16:49:11 -040081
Jeff Kingaae2ae42016-12-12 14:53:55 -050082 /*
83 * Avoid quoting if it's not necessary, for maximum compatibility
84 * with older parsers which don't understand the quoting.
85 */
86 if (*val == '"' || strchr(val, PATH_SEP)) {
87 strbuf_addch(&quoted, '"');
88 quote_c_style(val, &quoted, NULL, 1);
89 strbuf_addch(&quoted, '"');
90 val = quoted.buf;
91 }
92
93 old = getenv(key);
Jeff King2564d992016-10-03 16:49:11 -040094 if (!old)
Jeff Kingc972bf42020-07-28 16:25:12 -040095 strvec_pushf(env, "%s=%s", key, val);
Jeff King2564d992016-10-03 16:49:11 -040096 else
Jeff Kingc972bf42020-07-28 16:25:12 -040097 strvec_pushf(env, "%s=%s%c%s", key, old, PATH_SEP, val);
Jeff Kingaae2ae42016-12-12 14:53:55 -050098
99 strbuf_release(&quoted);
Jeff King2564d992016-10-03 16:49:11 -0400100}
101
Jeff Kingc972bf42020-07-28 16:25:12 -0400102static void env_replace(struct strvec *env, const char *key, const char *val)
Jeff King2564d992016-10-03 16:49:11 -0400103{
Jeff Kingc972bf42020-07-28 16:25:12 -0400104 strvec_pushf(env, "%s=%s", key, val);
Jeff King2564d992016-10-03 16:49:11 -0400105}
106
107static int setup_tmp_objdir(const char *root)
108{
109 char *path;
110 int ret = 0;
111
112 path = xstrfmt("%s/pack", root);
113 ret = mkdir(path, 0777);
114 free(path);
115
116 return ret;
117}
118
Neeraj Singhb3cecf42021-12-06 22:05:04 +0000119struct tmp_objdir *tmp_objdir_create(const char *prefix)
Jeff King2564d992016-10-03 16:49:11 -0400120{
121 static int installed_handlers;
122 struct tmp_objdir *t;
123
124 if (the_tmp_objdir)
Johannes Schindelin033abf92018-05-02 11:38:39 +0200125 BUG("only one tmp_objdir can be used at a time");
Jeff King2564d992016-10-03 16:49:11 -0400126
Neeraj Singhb3cecf42021-12-06 22:05:04 +0000127 t = xcalloc(1, sizeof(*t));
Jeff King2564d992016-10-03 16:49:11 -0400128 strbuf_init(&t->path, 0);
Jeff Kingc972bf42020-07-28 16:25:12 -0400129 strvec_init(&t->env);
Jeff King2564d992016-10-03 16:49:11 -0400130
Neeraj Singhb3cecf42021-12-06 22:05:04 +0000131 /*
132 * Use a string starting with tmp_ so that the builtin/prune.c code
133 * can recognize any stale objdirs left behind by a crash and delete
134 * them.
135 */
Patrick Steinhardta3673f42024-09-12 13:29:30 +0200136 strbuf_addf(&t->path, "%s/tmp_objdir-%s-XXXXXX",
137 repo_get_object_directory(the_repository), prefix);
Jeff King2564d992016-10-03 16:49:11 -0400138
Jeff King2564d992016-10-03 16:49:11 -0400139 if (!mkdtemp(t->path.buf)) {
140 /* free, not destroy, as we never touched the filesystem */
141 tmp_objdir_free(t);
142 return NULL;
143 }
144
145 the_tmp_objdir = t;
146 if (!installed_handlers) {
147 atexit(remove_tmp_objdir);
Jeff King2564d992016-10-03 16:49:11 -0400148 installed_handlers++;
149 }
150
151 if (setup_tmp_objdir(t->path.buf)) {
152 tmp_objdir_destroy(t);
153 return NULL;
154 }
155
156 env_append(&t->env, ALTERNATE_DB_ENVIRONMENT,
Patrick Steinhardta3673f42024-09-12 13:29:30 +0200157 absolute_path(repo_get_object_directory(the_repository)));
Jeff King2564d992016-10-03 16:49:11 -0400158 env_replace(&t->env, DB_ENVIRONMENT, absolute_path(t->path.buf));
Jeff Kinge34c2e02016-10-03 16:49:18 -0400159 env_replace(&t->env, GIT_QUARANTINE_ENVIRONMENT,
160 absolute_path(t->path.buf));
Jeff King2564d992016-10-03 16:49:11 -0400161
162 return t;
163}
164
165/*
166 * Make sure we copy packfiles and their associated metafiles in the correct
167 * order. All of these ends_with checks are slightly expensive to do in
168 * the midst of a sorting routine, but in practice it shouldn't matter.
169 * We will have a relatively small number of packfiles to order, and loose
170 * objects exit early in the first line.
171 */
172static int pack_copy_priority(const char *name)
173{
174 if (!starts_with(name, "pack"))
175 return 0;
176 if (ends_with(name, ".keep"))
177 return 1;
178 if (ends_with(name, ".pack"))
179 return 2;
Taylor Blau2f4ba2a2021-01-25 18:37:14 -0500180 if (ends_with(name, ".rev"))
Jeff King2564d992016-10-03 16:49:11 -0400181 return 3;
Taylor Blau2f4ba2a2021-01-25 18:37:14 -0500182 if (ends_with(name, ".idx"))
183 return 4;
184 return 5;
Jeff King2564d992016-10-03 16:49:11 -0400185}
186
187static int pack_copy_cmp(const char *a, const char *b)
188{
189 return pack_copy_priority(a) - pack_copy_priority(b);
190}
191
192static int read_dir_paths(struct string_list *out, const char *path)
193{
194 DIR *dh;
195 struct dirent *de;
196
197 dh = opendir(path);
198 if (!dh)
199 return -1;
200
201 while ((de = readdir(dh)))
Jeff King62fe0eb2016-10-03 16:49:22 -0400202 if (de->d_name[0] != '.')
Jeff King2564d992016-10-03 16:49:11 -0400203 string_list_append(out, de->d_name);
204
205 closedir(dh);
206 return 0;
207}
208
Taylor Blaub1b8dfd2024-09-26 11:22:38 -0400209static int migrate_paths(struct strbuf *src, struct strbuf *dst,
210 enum finalize_object_file_flags flags);
Jeff King2564d992016-10-03 16:49:11 -0400211
Taylor Blaub1b8dfd2024-09-26 11:22:38 -0400212static int migrate_one(struct strbuf *src, struct strbuf *dst,
213 enum finalize_object_file_flags flags)
Jeff King2564d992016-10-03 16:49:11 -0400214{
215 struct stat st;
216
217 if (stat(src->buf, &st) < 0)
218 return -1;
219 if (S_ISDIR(st.st_mode)) {
220 if (!mkdir(dst->buf, 0777)) {
221 if (adjust_shared_perm(dst->buf))
222 return -1;
223 } else if (errno != EEXIST)
224 return -1;
Taylor Blaub1b8dfd2024-09-26 11:22:38 -0400225 return migrate_paths(src, dst, flags);
Jeff King2564d992016-10-03 16:49:11 -0400226 }
Taylor Blaub1b8dfd2024-09-26 11:22:38 -0400227 return finalize_object_file_flags(src->buf, dst->buf, flags);
Jeff King2564d992016-10-03 16:49:11 -0400228}
229
Taylor Blaub1b8dfd2024-09-26 11:22:38 -0400230static int is_loose_object_shard(const char *name)
231{
232 return strlen(name) == 2 && isxdigit(name[0]) && isxdigit(name[1]);
233}
234
235static int migrate_paths(struct strbuf *src, struct strbuf *dst,
236 enum finalize_object_file_flags flags)
Jeff King2564d992016-10-03 16:49:11 -0400237{
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;
Taylor Blaub1b8dfd2024-09-26 11:22:38 -0400250 enum finalize_object_file_flags flags_copy = flags;
Jeff King2564d992016-10-03 16:49:11 -0400251
252 strbuf_addf(src, "/%s", name);
253 strbuf_addf(dst, "/%s", name);
254
Taylor Blaub1b8dfd2024-09-26 11:22:38 -0400255 if (is_loose_object_shard(name))
256 flags_copy |= FOF_SKIP_COLLISION_CHECK;
257
258 ret |= migrate_one(src, dst, flags_copy);
Jeff King2564d992016-10-03 16:49:11 -0400259
260 strbuf_setlen(src, src_len);
261 strbuf_setlen(dst, dst_len);
262 }
263
264 string_list_clear(&paths, 0);
265 return ret;
266}
267
268int tmp_objdir_migrate(struct tmp_objdir *t)
269{
270 struct strbuf src = STRBUF_INIT, dst = STRBUF_INIT;
271 int ret;
272
273 if (!t)
274 return 0;
275
Neeraj Singhb3cecf42021-12-06 22:05:04 +0000276 if (t->prev_odb) {
277 if (the_repository->objects->odb->will_destroy)
278 BUG("migrating an ODB that was marked for destruction");
279 restore_primary_odb(t->prev_odb, t->path.buf);
280 t->prev_odb = NULL;
281 }
282
Jeff King2564d992016-10-03 16:49:11 -0400283 strbuf_addbuf(&src, &t->path);
Patrick Steinhardta3673f42024-09-12 13:29:30 +0200284 strbuf_addstr(&dst, repo_get_object_directory(the_repository));
Jeff King2564d992016-10-03 16:49:11 -0400285
Taylor Blaub1b8dfd2024-09-26 11:22:38 -0400286 ret = migrate_paths(&src, &dst, 0);
Jeff King2564d992016-10-03 16:49:11 -0400287
288 strbuf_release(&src);
289 strbuf_release(&dst);
290
291 tmp_objdir_destroy(t);
292 return ret;
293}
294
295const char **tmp_objdir_env(const struct tmp_objdir *t)
296{
297 if (!t)
298 return NULL;
Jeff Kingd70a9eb2020-07-28 20:37:20 -0400299 return t->env.v;
Jeff King2564d992016-10-03 16:49:11 -0400300}
301
302void tmp_objdir_add_as_alternate(const struct tmp_objdir *t)
303{
304 add_to_alternates_memory(t->path.buf);
305}
Neeraj Singhb3cecf42021-12-06 22:05:04 +0000306
307void tmp_objdir_replace_primary_odb(struct tmp_objdir *t, int will_destroy)
308{
309 if (t->prev_odb)
310 BUG("the primary object database is already replaced");
311 t->prev_odb = set_temporary_primary_odb(t->path.buf, will_destroy);
312 t->will_destroy = will_destroy;
313}
314
315struct tmp_objdir *tmp_objdir_unapply_primary_odb(void)
316{
317 if (!the_tmp_objdir || !the_tmp_objdir->prev_odb)
318 return NULL;
319
320 restore_primary_odb(the_tmp_objdir->prev_odb, the_tmp_objdir->path.buf);
321 the_tmp_objdir->prev_odb = NULL;
322 return the_tmp_objdir;
323}
324
325void tmp_objdir_reapply_primary_odb(struct tmp_objdir *t, const char *old_cwd,
326 const char *new_cwd)
327{
328 char *path;
329
330 path = reparent_relative_path(old_cwd, new_cwd, t->path.buf);
331 strbuf_reset(&t->path);
332 strbuf_addstr(&t->path, path);
333 free(path);
334 tmp_objdir_replace_primary_odb(t, t->will_destroy);
335}