Merge branch 'jc/diff-unique-abbrev-comments'
[git.git] / tmp-objdir.c
blob64435f23a483dfc40e91437f0689bdfb9ac4b8f8
1 #include "cache.h"
2 #include "tmp-objdir.h"
3 #include "dir.h"
4 #include "sigchain.h"
5 #include "string-list.h"
6 #include "strbuf.h"
7 #include "argv-array.h"
9 struct tmp_objdir {
10 struct strbuf path;
11 struct argv_array env;
15 * Allow only one tmp_objdir at a time in a running process, which simplifies
16 * our signal/atexit cleanup routines. It's doubtful callers will ever need
17 * more than one, and we can expand later if so. You can have many such
18 * tmp_objdirs simultaneously in many processes, of course.
20 static struct tmp_objdir *the_tmp_objdir;
22 static void tmp_objdir_free(struct tmp_objdir *t)
24 strbuf_release(&t->path);
25 argv_array_clear(&t->env);
26 free(t);
29 static int tmp_objdir_destroy_1(struct tmp_objdir *t, int on_signal)
31 int err;
33 if (!t)
34 return 0;
36 if (t == the_tmp_objdir)
37 the_tmp_objdir = NULL;
40 * This may use malloc via strbuf_grow(), but we should
41 * have pre-grown t->path sufficiently so that this
42 * doesn't happen in practice.
44 err = remove_dir_recursively(&t->path, 0);
47 * When we are cleaning up due to a signal, we won't bother
48 * freeing memory; it may cause a deadlock if the signal
49 * arrived while libc's allocator lock is held.
51 if (!on_signal)
52 tmp_objdir_free(t);
53 return err;
56 int tmp_objdir_destroy(struct tmp_objdir *t)
58 return tmp_objdir_destroy_1(t, 0);
61 static void remove_tmp_objdir(void)
63 tmp_objdir_destroy(the_tmp_objdir);
66 static void remove_tmp_objdir_on_signal(int signo)
68 tmp_objdir_destroy_1(the_tmp_objdir, 1);
69 sigchain_pop(signo);
70 raise(signo);
74 * These env_* functions are for setting up the child environment; the
75 * "replace" variant overrides the value of any existing variable with that
76 * "key". The "append" variant puts our new value at the end of a list,
77 * separated by PATH_SEP (which is what separate values in
78 * GIT_ALTERNATE_OBJECT_DIRECTORIES).
80 static void env_append(struct argv_array *env, const char *key, const char *val)
82 const char *old = getenv(key);
84 if (!old)
85 argv_array_pushf(env, "%s=%s", key, val);
86 else
87 argv_array_pushf(env, "%s=%s%c%s", key, old, PATH_SEP, val);
90 static void env_replace(struct argv_array *env, const char *key, const char *val)
92 argv_array_pushf(env, "%s=%s", key, val);
95 static int setup_tmp_objdir(const char *root)
97 char *path;
98 int ret = 0;
100 path = xstrfmt("%s/pack", root);
101 ret = mkdir(path, 0777);
102 free(path);
104 return ret;
107 struct tmp_objdir *tmp_objdir_create(void)
109 static int installed_handlers;
110 struct tmp_objdir *t;
112 if (the_tmp_objdir)
113 die("BUG: only one tmp_objdir can be used at a time");
115 t = xmalloc(sizeof(*t));
116 strbuf_init(&t->path, 0);
117 argv_array_init(&t->env);
119 strbuf_addf(&t->path, "%s/incoming-XXXXXX", get_object_directory());
122 * Grow the strbuf beyond any filename we expect to be placed in it.
123 * If tmp_objdir_destroy() is called by a signal handler, then
124 * we should be able to use the strbuf to remove files without
125 * having to call malloc.
127 strbuf_grow(&t->path, 1024);
129 if (!mkdtemp(t->path.buf)) {
130 /* free, not destroy, as we never touched the filesystem */
131 tmp_objdir_free(t);
132 return NULL;
135 the_tmp_objdir = t;
136 if (!installed_handlers) {
137 atexit(remove_tmp_objdir);
138 sigchain_push_common(remove_tmp_objdir_on_signal);
139 installed_handlers++;
142 if (setup_tmp_objdir(t->path.buf)) {
143 tmp_objdir_destroy(t);
144 return NULL;
147 env_append(&t->env, ALTERNATE_DB_ENVIRONMENT,
148 absolute_path(get_object_directory()));
149 env_replace(&t->env, DB_ENVIRONMENT, absolute_path(t->path.buf));
150 env_replace(&t->env, GIT_QUARANTINE_ENVIRONMENT,
151 absolute_path(t->path.buf));
153 return t;
157 * Make sure we copy packfiles and their associated metafiles in the correct
158 * order. All of these ends_with checks are slightly expensive to do in
159 * the midst of a sorting routine, but in practice it shouldn't matter.
160 * We will have a relatively small number of packfiles to order, and loose
161 * objects exit early in the first line.
163 static int pack_copy_priority(const char *name)
165 if (!starts_with(name, "pack"))
166 return 0;
167 if (ends_with(name, ".keep"))
168 return 1;
169 if (ends_with(name, ".pack"))
170 return 2;
171 if (ends_with(name, ".idx"))
172 return 3;
173 return 4;
176 static int pack_copy_cmp(const char *a, const char *b)
178 return pack_copy_priority(a) - pack_copy_priority(b);
181 static int read_dir_paths(struct string_list *out, const char *path)
183 DIR *dh;
184 struct dirent *de;
186 dh = opendir(path);
187 if (!dh)
188 return -1;
190 while ((de = readdir(dh)))
191 if (de->d_name[0] != '.')
192 string_list_append(out, de->d_name);
194 closedir(dh);
195 return 0;
198 static int migrate_paths(struct strbuf *src, struct strbuf *dst);
200 static int migrate_one(struct strbuf *src, struct strbuf *dst)
202 struct stat st;
204 if (stat(src->buf, &st) < 0)
205 return -1;
206 if (S_ISDIR(st.st_mode)) {
207 if (!mkdir(dst->buf, 0777)) {
208 if (adjust_shared_perm(dst->buf))
209 return -1;
210 } else if (errno != EEXIST)
211 return -1;
212 return migrate_paths(src, dst);
214 return finalize_object_file(src->buf, dst->buf);
217 static int migrate_paths(struct strbuf *src, struct strbuf *dst)
219 size_t src_len = src->len, dst_len = dst->len;
220 struct string_list paths = STRING_LIST_INIT_DUP;
221 int i;
222 int ret = 0;
224 if (read_dir_paths(&paths, src->buf) < 0)
225 return -1;
226 paths.cmp = pack_copy_cmp;
227 string_list_sort(&paths);
229 for (i = 0; i < paths.nr; i++) {
230 const char *name = paths.items[i].string;
232 strbuf_addf(src, "/%s", name);
233 strbuf_addf(dst, "/%s", name);
235 ret |= migrate_one(src, dst);
237 strbuf_setlen(src, src_len);
238 strbuf_setlen(dst, dst_len);
241 string_list_clear(&paths, 0);
242 return ret;
245 int tmp_objdir_migrate(struct tmp_objdir *t)
247 struct strbuf src = STRBUF_INIT, dst = STRBUF_INIT;
248 int ret;
250 if (!t)
251 return 0;
253 strbuf_addbuf(&src, &t->path);
254 strbuf_addstr(&dst, get_object_directory());
256 ret = migrate_paths(&src, &dst);
258 strbuf_release(&src);
259 strbuf_release(&dst);
261 tmp_objdir_destroy(t);
262 return ret;
265 const char **tmp_objdir_env(const struct tmp_objdir *t)
267 if (!t)
268 return NULL;
269 return t->env.argv;
272 void tmp_objdir_add_as_alternate(const struct tmp_objdir *t)
274 add_to_alternates_memory(t->path.buf);