interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / tmp-objdir.c

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