fetch: drop unused DISPLAY_FORMAT_UNKNOWN enum value
[alt-git.git] / tmp-objdir.c
blobc33a554f921eca089d7535609fe7023614ad1985
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 "sigchain.h"
9 #include "string-list.h"
10 #include "strbuf.h"
11 #include "strvec.h"
12 #include "quote.h"
13 #include "object-store.h"
15 struct tmp_objdir {
16 struct strbuf path;
17 struct strvec env;
18 struct object_directory *prev_odb;
19 int will_destroy;
23 * Allow only one tmp_objdir at a time in a running process, which simplifies
24 * our atexit cleanup routines. It's doubtful callers will ever need
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.
28 static struct tmp_objdir *the_tmp_objdir;
30 static void tmp_objdir_free(struct tmp_objdir *t)
32 strbuf_release(&t->path);
33 strvec_clear(&t->env);
34 free(t);
37 int tmp_objdir_destroy(struct tmp_objdir *t)
39 int err;
41 if (!t)
42 return 0;
44 if (t == the_tmp_objdir)
45 the_tmp_objdir = NULL;
47 if (t->prev_odb)
48 restore_primary_odb(t->prev_odb, t->path.buf);
50 err = remove_dir_recursively(&t->path, 0);
52 tmp_objdir_free(t);
54 return err;
57 static void remove_tmp_objdir(void)
59 tmp_objdir_destroy(the_tmp_objdir);
62 void tmp_objdir_discard_objects(struct tmp_objdir *t)
64 remove_dir_recursively(&t->path, REMOVE_DIR_KEEP_TOPLEVEL);
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).
74 static void env_append(struct strvec *env, const char *key, const char *val)
76 struct strbuf quoted = STRBUF_INIT;
77 const char *old;
80 * Avoid quoting if it's not necessary, for maximum compatibility
81 * with older parsers which don't understand the quoting.
83 if (*val == '"' || strchr(val, PATH_SEP)) {
84 strbuf_addch(&quoted, '"');
85 quote_c_style(val, &quoted, NULL, 1);
86 strbuf_addch(&quoted, '"');
87 val = quoted.buf;
90 old = getenv(key);
91 if (!old)
92 strvec_pushf(env, "%s=%s", key, val);
93 else
94 strvec_pushf(env, "%s=%s%c%s", key, old, PATH_SEP, val);
96 strbuf_release(&quoted);
99 static void env_replace(struct strvec *env, const char *key, const char *val)
101 strvec_pushf(env, "%s=%s", key, val);
104 static int setup_tmp_objdir(const char *root)
106 char *path;
107 int ret = 0;
109 path = xstrfmt("%s/pack", root);
110 ret = mkdir(path, 0777);
111 free(path);
113 return ret;
116 struct tmp_objdir *tmp_objdir_create(const char *prefix)
118 static int installed_handlers;
119 struct tmp_objdir *t;
121 if (the_tmp_objdir)
122 BUG("only one tmp_objdir can be used at a time");
124 t = xcalloc(1, sizeof(*t));
125 strbuf_init(&t->path, 0);
126 strvec_init(&t->env);
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.
133 strbuf_addf(&t->path, "%s/tmp_objdir-%s-XXXXXX", get_object_directory(), prefix);
135 if (!mkdtemp(t->path.buf)) {
136 /* free, not destroy, as we never touched the filesystem */
137 tmp_objdir_free(t);
138 return NULL;
141 the_tmp_objdir = t;
142 if (!installed_handlers) {
143 atexit(remove_tmp_objdir);
144 installed_handlers++;
147 if (setup_tmp_objdir(t->path.buf)) {
148 tmp_objdir_destroy(t);
149 return NULL;
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));
155 env_replace(&t->env, GIT_QUARANTINE_ENVIRONMENT,
156 absolute_path(t->path.buf));
158 return t;
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.
168 static int pack_copy_priority(const char *name)
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;
176 if (ends_with(name, ".rev"))
177 return 3;
178 if (ends_with(name, ".idx"))
179 return 4;
180 return 5;
183 static int pack_copy_cmp(const char *a, const char *b)
185 return pack_copy_priority(a) - pack_copy_priority(b);
188 static int read_dir_paths(struct string_list *out, const char *path)
190 DIR *dh;
191 struct dirent *de;
193 dh = opendir(path);
194 if (!dh)
195 return -1;
197 while ((de = readdir(dh)))
198 if (de->d_name[0] != '.')
199 string_list_append(out, de->d_name);
201 closedir(dh);
202 return 0;
205 static int migrate_paths(struct strbuf *src, struct strbuf *dst);
207 static int migrate_one(struct strbuf *src, struct strbuf *dst)
209 struct stat st;
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);
221 return finalize_object_file(src->buf, dst->buf);
224 static int migrate_paths(struct strbuf *src, struct strbuf *dst)
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;
231 if (read_dir_paths(&paths, src->buf) < 0)
232 return -1;
233 paths.cmp = pack_copy_cmp;
234 string_list_sort(&paths);
236 for (i = 0; i < paths.nr; i++) {
237 const char *name = paths.items[i].string;
239 strbuf_addf(src, "/%s", name);
240 strbuf_addf(dst, "/%s", name);
242 ret |= migrate_one(src, dst);
244 strbuf_setlen(src, src_len);
245 strbuf_setlen(dst, dst_len);
248 string_list_clear(&paths, 0);
249 return ret;
252 int tmp_objdir_migrate(struct tmp_objdir *t)
254 struct strbuf src = STRBUF_INIT, dst = STRBUF_INIT;
255 int ret;
257 if (!t)
258 return 0;
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;
267 strbuf_addbuf(&src, &t->path);
268 strbuf_addstr(&dst, get_object_directory());
270 ret = migrate_paths(&src, &dst);
272 strbuf_release(&src);
273 strbuf_release(&dst);
275 tmp_objdir_destroy(t);
276 return ret;
279 const char **tmp_objdir_env(const struct tmp_objdir *t)
281 if (!t)
282 return NULL;
283 return t->env.v;
286 void tmp_objdir_add_as_alternate(const struct tmp_objdir *t)
288 add_to_alternates_memory(t->path.buf);
291 void tmp_objdir_replace_primary_odb(struct tmp_objdir *t, int will_destroy)
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;
299 struct tmp_objdir *tmp_objdir_unapply_primary_odb(void)
301 if (!the_tmp_objdir || !the_tmp_objdir->prev_odb)
302 return NULL;
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;
309 void tmp_objdir_reapply_primary_odb(struct tmp_objdir *t, const char *old_cwd,
310 const char *new_cwd)
312 char *path;
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);