Merge branch 'rb/hpe'
[git.git] / tmp-objdir.c
blob91c00567f4d63367d6766808b0ff90f6faa08765
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"
8 #include "quote.h"
9 #include "object-store.h"
11 struct tmp_objdir {
12 struct strbuf path;
13 struct argv_array env;
17 * Allow only one tmp_objdir at a time in a running process, which simplifies
18 * our signal/atexit cleanup routines. It's doubtful callers will ever need
19 * more than one, and we can expand later if so. You can have many such
20 * tmp_objdirs simultaneously in many processes, of course.
22 static struct tmp_objdir *the_tmp_objdir;
24 static void tmp_objdir_free(struct tmp_objdir *t)
26 strbuf_release(&t->path);
27 argv_array_clear(&t->env);
28 free(t);
31 static int tmp_objdir_destroy_1(struct tmp_objdir *t, int on_signal)
33 int err;
35 if (!t)
36 return 0;
38 if (t == the_tmp_objdir)
39 the_tmp_objdir = NULL;
42 * This may use malloc via strbuf_grow(), but we should
43 * have pre-grown t->path sufficiently so that this
44 * doesn't happen in practice.
46 err = remove_dir_recursively(&t->path, 0);
49 * When we are cleaning up due to a signal, we won't bother
50 * freeing memory; it may cause a deadlock if the signal
51 * arrived while libc's allocator lock is held.
53 if (!on_signal)
54 tmp_objdir_free(t);
55 return err;
58 int tmp_objdir_destroy(struct tmp_objdir *t)
60 return tmp_objdir_destroy_1(t, 0);
63 static void remove_tmp_objdir(void)
65 tmp_objdir_destroy(the_tmp_objdir);
68 static void remove_tmp_objdir_on_signal(int signo)
70 tmp_objdir_destroy_1(the_tmp_objdir, 1);
71 sigchain_pop(signo);
72 raise(signo);
76 * These env_* functions are for setting up the child environment; the
77 * "replace" variant overrides the value of any existing variable with that
78 * "key". The "append" variant puts our new value at the end of a list,
79 * separated by PATH_SEP (which is what separate values in
80 * GIT_ALTERNATE_OBJECT_DIRECTORIES).
82 static void env_append(struct argv_array *env, const char *key, const char *val)
84 struct strbuf quoted = STRBUF_INIT;
85 const char *old;
88 * Avoid quoting if it's not necessary, for maximum compatibility
89 * with older parsers which don't understand the quoting.
91 if (*val == '"' || strchr(val, PATH_SEP)) {
92 strbuf_addch(&quoted, '"');
93 quote_c_style(val, &quoted, NULL, 1);
94 strbuf_addch(&quoted, '"');
95 val = quoted.buf;
98 old = getenv(key);
99 if (!old)
100 argv_array_pushf(env, "%s=%s", key, val);
101 else
102 argv_array_pushf(env, "%s=%s%c%s", key, old, PATH_SEP, val);
104 strbuf_release(&quoted);
107 static void env_replace(struct argv_array *env, const char *key, const char *val)
109 argv_array_pushf(env, "%s=%s", key, val);
112 static int setup_tmp_objdir(const char *root)
114 char *path;
115 int ret = 0;
117 path = xstrfmt("%s/pack", root);
118 ret = mkdir(path, 0777);
119 free(path);
121 return ret;
124 struct tmp_objdir *tmp_objdir_create(void)
126 static int installed_handlers;
127 struct tmp_objdir *t;
129 if (the_tmp_objdir)
130 BUG("only one tmp_objdir can be used at a time");
132 t = xmalloc(sizeof(*t));
133 strbuf_init(&t->path, 0);
134 argv_array_init(&t->env);
136 strbuf_addf(&t->path, "%s/incoming-XXXXXX", get_object_directory());
139 * Grow the strbuf beyond any filename we expect to be placed in it.
140 * If tmp_objdir_destroy() is called by a signal handler, then
141 * we should be able to use the strbuf to remove files without
142 * having to call malloc.
144 strbuf_grow(&t->path, 1024);
146 if (!mkdtemp(t->path.buf)) {
147 /* free, not destroy, as we never touched the filesystem */
148 tmp_objdir_free(t);
149 return NULL;
152 the_tmp_objdir = t;
153 if (!installed_handlers) {
154 atexit(remove_tmp_objdir);
155 sigchain_push_common(remove_tmp_objdir_on_signal);
156 installed_handlers++;
159 if (setup_tmp_objdir(t->path.buf)) {
160 tmp_objdir_destroy(t);
161 return NULL;
164 env_append(&t->env, ALTERNATE_DB_ENVIRONMENT,
165 absolute_path(get_object_directory()));
166 env_replace(&t->env, DB_ENVIRONMENT, absolute_path(t->path.buf));
167 env_replace(&t->env, GIT_QUARANTINE_ENVIRONMENT,
168 absolute_path(t->path.buf));
170 return t;
174 * Make sure we copy packfiles and their associated metafiles in the correct
175 * order. All of these ends_with checks are slightly expensive to do in
176 * the midst of a sorting routine, but in practice it shouldn't matter.
177 * We will have a relatively small number of packfiles to order, and loose
178 * objects exit early in the first line.
180 static int pack_copy_priority(const char *name)
182 if (!starts_with(name, "pack"))
183 return 0;
184 if (ends_with(name, ".keep"))
185 return 1;
186 if (ends_with(name, ".pack"))
187 return 2;
188 if (ends_with(name, ".idx"))
189 return 3;
190 return 4;
193 static int pack_copy_cmp(const char *a, const char *b)
195 return pack_copy_priority(a) - pack_copy_priority(b);
198 static int read_dir_paths(struct string_list *out, const char *path)
200 DIR *dh;
201 struct dirent *de;
203 dh = opendir(path);
204 if (!dh)
205 return -1;
207 while ((de = readdir(dh)))
208 if (de->d_name[0] != '.')
209 string_list_append(out, de->d_name);
211 closedir(dh);
212 return 0;
215 static int migrate_paths(struct strbuf *src, struct strbuf *dst);
217 static int migrate_one(struct strbuf *src, struct strbuf *dst)
219 struct stat st;
221 if (stat(src->buf, &st) < 0)
222 return -1;
223 if (S_ISDIR(st.st_mode)) {
224 if (!mkdir(dst->buf, 0777)) {
225 if (adjust_shared_perm(dst->buf))
226 return -1;
227 } else if (errno != EEXIST)
228 return -1;
229 return migrate_paths(src, dst);
231 return finalize_object_file(src->buf, dst->buf);
234 static int migrate_paths(struct strbuf *src, struct strbuf *dst)
236 size_t src_len = src->len, dst_len = dst->len;
237 struct string_list paths = STRING_LIST_INIT_DUP;
238 int i;
239 int ret = 0;
241 if (read_dir_paths(&paths, src->buf) < 0)
242 return -1;
243 paths.cmp = pack_copy_cmp;
244 string_list_sort(&paths);
246 for (i = 0; i < paths.nr; i++) {
247 const char *name = paths.items[i].string;
249 strbuf_addf(src, "/%s", name);
250 strbuf_addf(dst, "/%s", name);
252 ret |= migrate_one(src, dst);
254 strbuf_setlen(src, src_len);
255 strbuf_setlen(dst, dst_len);
258 string_list_clear(&paths, 0);
259 return ret;
262 int tmp_objdir_migrate(struct tmp_objdir *t)
264 struct strbuf src = STRBUF_INIT, dst = STRBUF_INIT;
265 int ret;
267 if (!t)
268 return 0;
270 strbuf_addbuf(&src, &t->path);
271 strbuf_addstr(&dst, get_object_directory());
273 ret = migrate_paths(&src, &dst);
275 strbuf_release(&src);
276 strbuf_release(&dst);
278 tmp_objdir_destroy(t);
279 return ret;
282 const char **tmp_objdir_env(const struct tmp_objdir *t)
284 if (!t)
285 return NULL;
286 return t->env.argv;
289 void tmp_objdir_add_as_alternate(const struct tmp_objdir *t)
291 add_to_alternates_memory(t->path.buf);