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