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