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