1
#include "cache.h"
2
#include "tmp-objdir.h"
3
+#include "chdir-notify.h"
4
#include "dir.h"
5
#include "sigchain.h"
6
#include "string-list.h"
12
struct tmp_objdir {
13
struct strbuf path;
14
struct strvec env;
15
+ struct object_directory *prev_odb;
16
+ int will_destroy;
17
};
18
19
/*
41
if (t == the_tmp_objdir)
42
the_tmp_objdir = NULL;
43
44
+ if (!on_signal && t->prev_odb)
45
+ restore_primary_odb(t->prev_odb, t->path.buf);
46
+
47
/*
48
* This may use malloc via strbuf_grow(), but we should
49
* have pre-grown t->path sufficiently so that this
58
*/
59
if (!on_signal)
60
tmp_objdir_free(t);
61
+
62
return err;
63
}
64
128
return ret;
129
}
130
124
-struct tmp_objdir *tmp_objdir_create(void)
131
+struct tmp_objdir *tmp_objdir_create(const char *prefix)
132
{
133
static int installed_handlers;
134
struct tmp_objdir *t;
136
if (the_tmp_objdir)
137
BUG("only one tmp_objdir can be used at a time");
138
132
- t = xmalloc(sizeof(*t));
139
+ t = xcalloc(1, sizeof(*t));
140
strbuf_init(&t->path, 0);
141
strvec_init(&t->env);
142
136
- strbuf_addf(&t->path, "%s/incoming-XXXXXX", get_object_directory());
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);
149
150
/*
151
* Grow the strbuf beyond any filename we expect to be placed in it.
281
if (!t)
282
return 0;
283
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
+
291
strbuf_addbuf(&src, &t->path);
292
strbuf_addstr(&dst, get_object_directory());
293
311
{
312
add_to_alternates_memory(t->path.buf);
313
}
314
+
315
+void 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
+
323
+struct 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
+
333
+void 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
+}