The sixteenth batch
[git/gitster.git] / tmp-objdir.c
blobae01eae9c415d3967ee19ff2d55367cb07b950d0
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 "object-file.h"
8 #include "path.h"
9 #include "string-list.h"
10 #include "strbuf.h"
11 #include "strvec.h"
12 #include "quote.h"
13 #include "odb.h"
14 #include "repository.h"
16 struct tmp_objdir {
17 struct repository *repo;
18 struct strbuf path;
19 struct strvec env;
20 struct odb_source *prev_source;
21 int will_destroy;
25 * Allow only one tmp_objdir at a time in a running process, which simplifies
26 * our atexit cleanup routines. It's doubtful callers will ever need
27 * more than one, and we can expand later if so. You can have many such
28 * tmp_objdirs simultaneously in many processes, of course.
30 static struct tmp_objdir *the_tmp_objdir;
32 static void tmp_objdir_free(struct tmp_objdir *t)
34 strbuf_release(&t->path);
35 strvec_clear(&t->env);
36 free(t);
39 int tmp_objdir_destroy(struct tmp_objdir *t)
41 int err;
43 if (!t)
44 return 0;
46 if (t == the_tmp_objdir)
47 the_tmp_objdir = NULL;
49 if (t->prev_source)
50 odb_restore_primary_source(t->repo->objects, t->prev_source, t->path.buf);
52 err = remove_dir_recursively(&t->path, 0);
54 tmp_objdir_free(t);
56 return err;
59 static void remove_tmp_objdir(void)
61 tmp_objdir_destroy(the_tmp_objdir);
64 void tmp_objdir_discard_objects(struct tmp_objdir *t)
66 remove_dir_recursively(&t->path, REMOVE_DIR_KEEP_TOPLEVEL);
70 * These env_* functions are for setting up the child environment; the
71 * "replace" variant overrides the value of any existing variable with that
72 * "key". The "append" variant puts our new value at the end of a list,
73 * separated by PATH_SEP (which is what separate values in
74 * GIT_ALTERNATE_OBJECT_DIRECTORIES).
76 static void env_append(struct strvec *env, const char *key, const char *val)
78 struct strbuf quoted = STRBUF_INIT;
79 const char *old;
82 * Avoid quoting if it's not necessary, for maximum compatibility
83 * with older parsers which don't understand the quoting.
85 if (*val == '"' || strchr(val, PATH_SEP)) {
86 strbuf_addch(&quoted, '"');
87 quote_c_style(val, &quoted, NULL, 1);
88 strbuf_addch(&quoted, '"');
89 val = quoted.buf;
92 old = getenv(key);
93 if (!old)
94 strvec_pushf(env, "%s=%s", key, val);
95 else
96 strvec_pushf(env, "%s=%s%c%s", key, old, PATH_SEP, val);
98 strbuf_release(&quoted);
101 static void env_replace(struct strvec *env, const char *key, const char *val)
103 strvec_pushf(env, "%s=%s", key, val);
106 static int setup_tmp_objdir(const char *root)
108 char *path;
109 int ret = 0;
111 path = xstrfmt("%s/pack", root);
112 ret = mkdir(path, 0777);
113 free(path);
115 return ret;
118 struct tmp_objdir *tmp_objdir_create(struct repository *r,
119 const char *prefix)
121 static int installed_handlers;
122 struct tmp_objdir *t;
124 if (the_tmp_objdir)
125 BUG("only one tmp_objdir can be used at a time");
127 t = xcalloc(1, sizeof(*t));
128 t->repo = r;
129 strbuf_init(&t->path, 0);
130 strvec_init(&t->env);
133 * Use a string starting with tmp_ so that the builtin/prune.c code
134 * can recognize any stale objdirs left behind by a crash and delete
135 * them.
137 strbuf_addf(&t->path, "%s/tmp_objdir-%s-XXXXXX",
138 repo_get_object_directory(r), prefix);
140 if (!mkdtemp(t->path.buf)) {
141 /* free, not destroy, as we never touched the filesystem */
142 tmp_objdir_free(t);
143 return NULL;
146 the_tmp_objdir = t;
147 if (!installed_handlers) {
148 atexit(remove_tmp_objdir);
149 installed_handlers++;
152 if (setup_tmp_objdir(t->path.buf)) {
153 tmp_objdir_destroy(t);
154 return NULL;
157 env_append(&t->env, ALTERNATE_DB_ENVIRONMENT,
158 absolute_path(repo_get_object_directory(r)));
159 env_replace(&t->env, DB_ENVIRONMENT, absolute_path(t->path.buf));
160 env_replace(&t->env, GIT_QUARANTINE_ENVIRONMENT,
161 absolute_path(t->path.buf));
163 return t;
167 * Make sure we copy packfiles and their associated metafiles in the correct
168 * order. All of these ends_with checks are slightly expensive to do in
169 * the midst of a sorting routine, but in practice it shouldn't matter.
170 * We will have a relatively small number of packfiles to order, and loose
171 * objects exit early in the first line.
173 static int pack_copy_priority(const char *name)
175 if (!starts_with(name, "pack"))
176 return 0;
177 if (ends_with(name, ".keep"))
178 return 1;
179 if (ends_with(name, ".pack"))
180 return 2;
181 if (ends_with(name, ".rev"))
182 return 3;
183 if (ends_with(name, ".idx"))
184 return 4;
185 return 5;
188 static int pack_copy_cmp(const char *a, const char *b)
190 return pack_copy_priority(a) - pack_copy_priority(b);
193 static int read_dir_paths(struct string_list *out, const char *path)
195 DIR *dh;
196 struct dirent *de;
198 dh = opendir(path);
199 if (!dh)
200 return -1;
202 while ((de = readdir(dh)))
203 if (de->d_name[0] != '.')
204 string_list_append(out, de->d_name);
206 closedir(dh);
207 return 0;
210 static int migrate_paths(struct tmp_objdir *t,
211 struct strbuf *src, struct strbuf *dst,
212 enum finalize_object_file_flags flags);
214 static int migrate_one(struct tmp_objdir *t,
215 struct strbuf *src, struct strbuf *dst,
216 enum finalize_object_file_flags flags)
218 struct stat st;
220 if (stat(src->buf, &st) < 0)
221 return -1;
222 if (S_ISDIR(st.st_mode)) {
223 if (!mkdir(dst->buf, 0777)) {
224 if (adjust_shared_perm(t->repo, dst->buf))
225 return -1;
226 } else if (errno != EEXIST)
227 return -1;
228 return migrate_paths(t, src, dst, flags);
230 return finalize_object_file_flags(src->buf, dst->buf, flags);
233 static int is_loose_object_shard(const char *name)
235 return strlen(name) == 2 && isxdigit(name[0]) && isxdigit(name[1]);
238 static int migrate_paths(struct tmp_objdir *t,
239 struct strbuf *src, struct strbuf *dst,
240 enum finalize_object_file_flags flags)
242 size_t src_len = src->len, dst_len = dst->len;
243 struct string_list paths = STRING_LIST_INIT_DUP;
244 int ret = 0;
246 if (read_dir_paths(&paths, src->buf) < 0)
247 return -1;
248 paths.cmp = pack_copy_cmp;
249 string_list_sort(&paths);
251 for (size_t i = 0; i < paths.nr; i++) {
252 const char *name = paths.items[i].string;
253 enum finalize_object_file_flags flags_copy = flags;
255 strbuf_addf(src, "/%s", name);
256 strbuf_addf(dst, "/%s", name);
258 if (is_loose_object_shard(name))
259 flags_copy |= FOF_SKIP_COLLISION_CHECK;
261 ret |= migrate_one(t, src, dst, flags_copy);
263 strbuf_setlen(src, src_len);
264 strbuf_setlen(dst, dst_len);
267 string_list_clear(&paths, 0);
268 return ret;
271 int tmp_objdir_migrate(struct tmp_objdir *t)
273 struct strbuf src = STRBUF_INIT, dst = STRBUF_INIT;
274 int ret;
276 if (!t)
277 return 0;
279 if (t->prev_source) {
280 if (t->repo->objects->sources->will_destroy)
281 BUG("migrating an ODB that was marked for destruction");
282 odb_restore_primary_source(t->repo->objects, t->prev_source, t->path.buf);
283 t->prev_source = NULL;
286 strbuf_addbuf(&src, &t->path);
287 strbuf_addstr(&dst, repo_get_object_directory(t->repo));
289 ret = migrate_paths(t, &src, &dst, 0);
291 strbuf_release(&src);
292 strbuf_release(&dst);
294 tmp_objdir_destroy(t);
295 return ret;
298 const char **tmp_objdir_env(const struct tmp_objdir *t)
300 if (!t)
301 return NULL;
302 return t->env.v;
305 void tmp_objdir_add_as_alternate(const struct tmp_objdir *t)
307 odb_add_to_alternates_memory(t->repo->objects, t->path.buf);
310 void tmp_objdir_replace_primary_odb(struct tmp_objdir *t, int will_destroy)
312 if (t->prev_source)
313 BUG("the primary object database is already replaced");
314 t->prev_source = odb_set_temporary_primary_source(t->repo->objects,
315 t->path.buf, will_destroy);
316 t->will_destroy = will_destroy;
319 struct tmp_objdir *tmp_objdir_unapply_primary_odb(void)
321 if (!the_tmp_objdir || !the_tmp_objdir->prev_source)
322 return NULL;
324 odb_restore_primary_source(the_tmp_objdir->repo->objects,
325 the_tmp_objdir->prev_source, the_tmp_objdir->path.buf);
326 the_tmp_objdir->prev_source = NULL;
327 return the_tmp_objdir;
330 void tmp_objdir_reapply_primary_odb(struct tmp_objdir *t, const char *old_cwd,
331 const char *new_cwd)
333 char *path;
335 path = reparent_relative_path(old_cwd, new_cwd, t->path.buf);
336 strbuf_reset(&t->path);
337 strbuf_addstr(&t->path, path);
338 free(path);
339 tmp_objdir_replace_primary_odb(t, t->will_destroy);