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