blob: c2fb9f91930a30bdbd813112517d7c9eafde1858 [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
209static int migrate_paths(struct strbuf *src, struct strbuf *dst);
210
211static int migrate_one(struct strbuf *src, struct strbuf *dst)
212{
213 struct stat st;
214
215 if (stat(src->buf, &st) < 0)
216 return -1;
217 if (S_ISDIR(st.st_mode)) {
218 if (!mkdir(dst->buf, 0777)) {
219 if (adjust_shared_perm(dst->buf))
220 return -1;
221 } else if (errno != EEXIST)
222 return -1;
223 return migrate_paths(src, dst);
224 }
225 return finalize_object_file(src->buf, dst->buf);
226}
227
228static int migrate_paths(struct strbuf *src, struct strbuf *dst)
229{
230 size_t src_len = src->len, dst_len = dst->len;
231 struct string_list paths = STRING_LIST_INIT_DUP;
232 int i;
233 int ret = 0;
234
235 if (read_dir_paths(&paths, src->buf) < 0)
236 return -1;
237 paths.cmp = pack_copy_cmp;
238 string_list_sort(&paths);
239
240 for (i = 0; i < paths.nr; i++) {
241 const char *name = paths.items[i].string;
242
243 strbuf_addf(src, "/%s", name);
244 strbuf_addf(dst, "/%s", name);
245
246 ret |= migrate_one(src, dst);
247
248 strbuf_setlen(src, src_len);
249 strbuf_setlen(dst, dst_len);
250 }
251
252 string_list_clear(&paths, 0);
253 return ret;
254}
255
256int tmp_objdir_migrate(struct tmp_objdir *t)
257{
258 struct strbuf src = STRBUF_INIT, dst = STRBUF_INIT;
259 int ret;
260
261 if (!t)
262 return 0;
263
Neeraj Singhb3cecf42021-12-06 22:05:04 +0000264 if (t->prev_odb) {
265 if (the_repository->objects->odb->will_destroy)
266 BUG("migrating an ODB that was marked for destruction");
267 restore_primary_odb(t->prev_odb, t->path.buf);
268 t->prev_odb = NULL;
269 }
270
Jeff King2564d992016-10-03 16:49:11 -0400271 strbuf_addbuf(&src, &t->path);
Patrick Steinhardta3673f42024-09-12 13:29:30 +0200272 strbuf_addstr(&dst, repo_get_object_directory(the_repository));
Jeff King2564d992016-10-03 16:49:11 -0400273
274 ret = migrate_paths(&src, &dst);
275
276 strbuf_release(&src);
277 strbuf_release(&dst);
278
279 tmp_objdir_destroy(t);
280 return ret;
281}
282
283const char **tmp_objdir_env(const struct tmp_objdir *t)
284{
285 if (!t)
286 return NULL;
Jeff Kingd70a9eb2020-07-28 20:37:20 -0400287 return t->env.v;
Jeff King2564d992016-10-03 16:49:11 -0400288}
289
290void tmp_objdir_add_as_alternate(const struct tmp_objdir *t)
291{
292 add_to_alternates_memory(t->path.buf);
293}
Neeraj Singhb3cecf42021-12-06 22:05:04 +0000294
295void tmp_objdir_replace_primary_odb(struct tmp_objdir *t, int will_destroy)
296{
297 if (t->prev_odb)
298 BUG("the primary object database is already replaced");
299 t->prev_odb = set_temporary_primary_odb(t->path.buf, will_destroy);
300 t->will_destroy = will_destroy;
301}
302
303struct tmp_objdir *tmp_objdir_unapply_primary_odb(void)
304{
305 if (!the_tmp_objdir || !the_tmp_objdir->prev_odb)
306 return NULL;
307
308 restore_primary_odb(the_tmp_objdir->prev_odb, the_tmp_objdir->path.buf);
309 the_tmp_objdir->prev_odb = NULL;
310 return the_tmp_objdir;
311}
312
313void tmp_objdir_reapply_primary_odb(struct tmp_objdir *t, const char *old_cwd,
314 const char *new_cwd)
315{
316 char *path;
317
318 path = reparent_relative_path(old_cwd, new_cwd, t->path.buf);
319 strbuf_reset(&t->path);
320 strbuf_addstr(&t->path, path);
321 free(path);
322 tmp_objdir_replace_primary_odb(t, t->will_destroy);
323}