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