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