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