Sync with 'maint'
[git/gitster.git] / tmp-objdir.c
blob9da0071cba837586241994367d0d3392dbb1bfab
1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "tmp-objdir.h"
5 #include "abspath.h"
6 #include "chdir-notify.h"
7 #include "dir.h"
8 #include "environment.h"
9 #include "object-file.h"
10 #include "path.h"
11 #include "string-list.h"
12 #include "strbuf.h"
13 #include "strvec.h"
14 #include "quote.h"
15 #include "object-store-ll.h"
16 #include "repository.h"
18 struct tmp_objdir {
19 struct strbuf path;
20 struct strvec env;
21 struct object_directory *prev_odb;
22 int will_destroy;
26 * Allow only one tmp_objdir at a time in a running process, which simplifies
27 * our atexit cleanup routines. It's doubtful callers will ever need
28 * more than one, and we can expand later if so. You can have many such
29 * tmp_objdirs simultaneously in many processes, of course.
31 static struct tmp_objdir *the_tmp_objdir;
33 static void tmp_objdir_free(struct tmp_objdir *t)
35 strbuf_release(&t->path);
36 strvec_clear(&t->env);
37 free(t);
40 int tmp_objdir_destroy(struct tmp_objdir *t)
42 int err;
44 if (!t)
45 return 0;
47 if (t == the_tmp_objdir)
48 the_tmp_objdir = NULL;
50 if (t->prev_odb)
51 restore_primary_odb(t->prev_odb, t->path.buf);
53 err = remove_dir_recursively(&t->path, 0);
55 tmp_objdir_free(t);
57 return err;
60 static void remove_tmp_objdir(void)
62 tmp_objdir_destroy(the_tmp_objdir);
65 void tmp_objdir_discard_objects(struct tmp_objdir *t)
67 remove_dir_recursively(&t->path, REMOVE_DIR_KEEP_TOPLEVEL);
71 * These env_* functions are for setting up the child environment; the
72 * "replace" variant overrides the value of any existing variable with that
73 * "key". The "append" variant puts our new value at the end of a list,
74 * separated by PATH_SEP (which is what separate values in
75 * GIT_ALTERNATE_OBJECT_DIRECTORIES).
77 static void env_append(struct strvec *env, const char *key, const char *val)
79 struct strbuf quoted = STRBUF_INIT;
80 const char *old;
83 * Avoid quoting if it's not necessary, for maximum compatibility
84 * with older parsers which don't understand the quoting.
86 if (*val == '"' || strchr(val, PATH_SEP)) {
87 strbuf_addch(&quoted, '"');
88 quote_c_style(val, &quoted, NULL, 1);
89 strbuf_addch(&quoted, '"');
90 val = quoted.buf;
93 old = getenv(key);
94 if (!old)
95 strvec_pushf(env, "%s=%s", key, val);
96 else
97 strvec_pushf(env, "%s=%s%c%s", key, old, PATH_SEP, val);
99 strbuf_release(&quoted);
102 static void env_replace(struct strvec *env, const char *key, const char *val)
104 strvec_pushf(env, "%s=%s", key, val);
107 static int setup_tmp_objdir(const char *root)
109 char *path;
110 int ret = 0;
112 path = xstrfmt("%s/pack", root);
113 ret = mkdir(path, 0777);
114 free(path);
116 return ret;
119 struct tmp_objdir *tmp_objdir_create(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 strbuf_init(&t->path, 0);
129 strvec_init(&t->env);
132 * Use a string starting with tmp_ so that the builtin/prune.c code
133 * can recognize any stale objdirs left behind by a crash and delete
134 * them.
136 strbuf_addf(&t->path, "%s/tmp_objdir-%s-XXXXXX",
137 repo_get_object_directory(the_repository), prefix);
139 if (!mkdtemp(t->path.buf)) {
140 /* free, not destroy, as we never touched the filesystem */
141 tmp_objdir_free(t);
142 return NULL;
145 the_tmp_objdir = t;
146 if (!installed_handlers) {
147 atexit(remove_tmp_objdir);
148 installed_handlers++;
151 if (setup_tmp_objdir(t->path.buf)) {
152 tmp_objdir_destroy(t);
153 return NULL;
156 env_append(&t->env, ALTERNATE_DB_ENVIRONMENT,
157 absolute_path(repo_get_object_directory(the_repository)));
158 env_replace(&t->env, DB_ENVIRONMENT, absolute_path(t->path.buf));
159 env_replace(&t->env, GIT_QUARANTINE_ENVIRONMENT,
160 absolute_path(t->path.buf));
162 return t;
166 * Make sure we copy packfiles and their associated metafiles in the correct
167 * order. All of these ends_with checks are slightly expensive to do in
168 * the midst of a sorting routine, but in practice it shouldn't matter.
169 * We will have a relatively small number of packfiles to order, and loose
170 * objects exit early in the first line.
172 static int pack_copy_priority(const char *name)
174 if (!starts_with(name, "pack"))
175 return 0;
176 if (ends_with(name, ".keep"))
177 return 1;
178 if (ends_with(name, ".pack"))
179 return 2;
180 if (ends_with(name, ".rev"))
181 return 3;
182 if (ends_with(name, ".idx"))
183 return 4;
184 return 5;
187 static int pack_copy_cmp(const char *a, const char *b)
189 return pack_copy_priority(a) - pack_copy_priority(b);
192 static int read_dir_paths(struct string_list *out, const char *path)
194 DIR *dh;
195 struct dirent *de;
197 dh = opendir(path);
198 if (!dh)
199 return -1;
201 while ((de = readdir(dh)))
202 if (de->d_name[0] != '.')
203 string_list_append(out, de->d_name);
205 closedir(dh);
206 return 0;
209 static int migrate_paths(struct strbuf *src, struct strbuf *dst,
210 enum finalize_object_file_flags flags);
212 static int migrate_one(struct strbuf *src, struct strbuf *dst,
213 enum finalize_object_file_flags flags)
215 struct stat st;
217 if (stat(src->buf, &st) < 0)
218 return -1;
219 if (S_ISDIR(st.st_mode)) {
220 if (!mkdir(dst->buf, 0777)) {
221 if (adjust_shared_perm(dst->buf))
222 return -1;
223 } else if (errno != EEXIST)
224 return -1;
225 return migrate_paths(src, dst, flags);
227 return finalize_object_file_flags(src->buf, dst->buf, flags);
230 static int is_loose_object_shard(const char *name)
232 return strlen(name) == 2 && isxdigit(name[0]) && isxdigit(name[1]);
235 static int migrate_paths(struct strbuf *src, struct strbuf *dst,
236 enum finalize_object_file_flags flags)
238 size_t src_len = src->len, dst_len = dst->len;
239 struct string_list paths = STRING_LIST_INIT_DUP;
240 int i;
241 int ret = 0;
243 if (read_dir_paths(&paths, src->buf) < 0)
244 return -1;
245 paths.cmp = pack_copy_cmp;
246 string_list_sort(&paths);
248 for (i = 0; i < paths.nr; i++) {
249 const char *name = paths.items[i].string;
250 enum finalize_object_file_flags flags_copy = flags;
252 strbuf_addf(src, "/%s", name);
253 strbuf_addf(dst, "/%s", name);
255 if (is_loose_object_shard(name))
256 flags_copy |= FOF_SKIP_COLLISION_CHECK;
258 ret |= migrate_one(src, dst, flags_copy);
260 strbuf_setlen(src, src_len);
261 strbuf_setlen(dst, dst_len);
264 string_list_clear(&paths, 0);
265 return ret;
268 int tmp_objdir_migrate(struct tmp_objdir *t)
270 struct strbuf src = STRBUF_INIT, dst = STRBUF_INIT;
271 int ret;
273 if (!t)
274 return 0;
276 if (t->prev_odb) {
277 if (the_repository->objects->odb->will_destroy)
278 BUG("migrating an ODB that was marked for destruction");
279 restore_primary_odb(t->prev_odb, t->path.buf);
280 t->prev_odb = NULL;
283 strbuf_addbuf(&src, &t->path);
284 strbuf_addstr(&dst, repo_get_object_directory(the_repository));
286 ret = migrate_paths(&src, &dst, 0);
288 strbuf_release(&src);
289 strbuf_release(&dst);
291 tmp_objdir_destroy(t);
292 return ret;
295 const char **tmp_objdir_env(const struct tmp_objdir *t)
297 if (!t)
298 return NULL;
299 return t->env.v;
302 void tmp_objdir_add_as_alternate(const struct tmp_objdir *t)
304 add_to_alternates_memory(t->path.buf);
307 void tmp_objdir_replace_primary_odb(struct tmp_objdir *t, int will_destroy)
309 if (t->prev_odb)
310 BUG("the primary object database is already replaced");
311 t->prev_odb = set_temporary_primary_odb(t->path.buf, will_destroy);
312 t->will_destroy = will_destroy;
315 struct tmp_objdir *tmp_objdir_unapply_primary_odb(void)
317 if (!the_tmp_objdir || !the_tmp_objdir->prev_odb)
318 return NULL;
320 restore_primary_odb(the_tmp_objdir->prev_odb, the_tmp_objdir->path.buf);
321 the_tmp_objdir->prev_odb = NULL;
322 return the_tmp_objdir;
325 void tmp_objdir_reapply_primary_odb(struct tmp_objdir *t, const char *old_cwd,
326 const char *new_cwd)
328 char *path;
330 path = reparent_relative_path(old_cwd, new_cwd, t->path.buf);
331 strbuf_reset(&t->path);
332 strbuf_addstr(&t->path, path);
333 free(path);
334 tmp_objdir_replace_primary_odb(t, t->will_destroy);