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 9struct tmp_objdir { 10 struct strbuf path; 11 struct argv_array env; 12}; 13 14/* 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. 19 */ 20static struct tmp_objdir *the_tmp_objdir; 21 22static void tmp_objdir_free(struct tmp_objdir *t) 23{ 24 strbuf_release(&t->path); 25 argv_array_clear(&t->env); 26 free(t); 27} 28 29static int tmp_objdir_destroy_1(struct tmp_objdir *t, int on_signal) 30{ 31 int err; 32 33 if (!t) 34 return 0; 35 36 if (t == the_tmp_objdir) 37 the_tmp_objdir = NULL; 38 39 /* 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. 43 */ 44 err = remove_dir_recursively(&t->path, 0); 45 46 /* 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. 50 */ 51 if (!on_signal) 52 tmp_objdir_free(t); 53 return err; 54} 55 56int tmp_objdir_destroy(struct tmp_objdir *t) 57{ 58 return tmp_objdir_destroy_1(t, 0); 59} 60 61static void remove_tmp_objdir(void) 62{ 63 tmp_objdir_destroy(the_tmp_objdir); 64} 65 66static void remove_tmp_objdir_on_signal(int signo) 67{ 68 tmp_objdir_destroy_1(the_tmp_objdir, 1); 69 sigchain_pop(signo); 70 raise(signo); 71} 72 73/* 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). 79 */ 80static void env_append(struct argv_array *env, const char *key, const char *val) 81{ 82 const char *old = getenv(key); 83 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); 88} 89 90static void env_replace(struct argv_array *env, const char *key, const char *val) 91{ 92 argv_array_pushf(env, "%s=%s", key, val); 93} 94 95static int setup_tmp_objdir(const char *root) 96{ 97 char *path; 98 int ret = 0; 99 100 path = xstrfmt("%s/pack", root); 101 ret = mkdir(path, 0777); 102 free(path); 103 104 return ret; 105} 106 107struct tmp_objdir *tmp_objdir_create(void) 108{ 109 static int installed_handlers; 110 struct tmp_objdir *t; 111 112 if (the_tmp_objdir) 113 die("BUG: only one tmp_objdir can be used at a time"); 114 115 t = xmalloc(sizeof(*t)); 116 strbuf_init(&t->path, 0); 117 argv_array_init(&t->env); 118 119 strbuf_addf(&t->path, "%s/incoming-XXXXXX", get_object_directory()); 120 121 /* 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. 126 */ 127 strbuf_grow(&t->path, 1024); 128 129 if (!mkdtemp(t->path.buf)) { 130 /* free, not destroy, as we never touched the filesystem */ 131 tmp_objdir_free(t); 132 return NULL; 133 } 134 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++; 140 } 141 142 if (setup_tmp_objdir(t->path.buf)) { 143 tmp_objdir_destroy(t); 144 return NULL; 145 } 146 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)); 152 153 return t; 154} 155 156/* 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. 162 */ 163static int pack_copy_priority(const char *name) 164{ 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; 174} 175 176static int pack_copy_cmp(const char *a, const char *b) 177{ 178 return pack_copy_priority(a) - pack_copy_priority(b); 179} 180 181static int read_dir_paths(struct string_list *out, const char *path) 182{ 183 DIR *dh; 184 struct dirent *de; 185 186 dh = opendir(path); 187 if (!dh) 188 return -1; 189 190 while ((de = readdir(dh))) 191 if (de->d_name[0] != '.') 192 string_list_append(out, de->d_name); 193 194 closedir(dh); 195 return 0; 196} 197 198static int migrate_paths(struct strbuf *src, struct strbuf *dst); 199 200static int migrate_one(struct strbuf *src, struct strbuf *dst) 201{ 202 struct stat st; 203 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); 213 } 214 return finalize_object_file(src->buf, dst->buf); 215} 216 217static int migrate_paths(struct strbuf *src, struct strbuf *dst) 218{ 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; 223 224 if (read_dir_paths(&paths, src->buf) < 0) 225 return -1; 226 paths.cmp = pack_copy_cmp; 227 string_list_sort(&paths); 228 229 for (i = 0; i < paths.nr; i++) { 230 const char *name = paths.items[i].string; 231 232 strbuf_addf(src, "/%s", name); 233 strbuf_addf(dst, "/%s", name); 234 235 ret |= migrate_one(src, dst); 236 237 strbuf_setlen(src, src_len); 238 strbuf_setlen(dst, dst_len); 239 } 240 241 string_list_clear(&paths, 0); 242 return ret; 243} 244 245int tmp_objdir_migrate(struct tmp_objdir *t) 246{ 247 struct strbuf src = STRBUF_INIT, dst = STRBUF_INIT; 248 int ret; 249 250 if (!t) 251 return 0; 252 253 strbuf_addbuf(&src, &t->path); 254 strbuf_addstr(&dst, get_object_directory()); 255 256 ret = migrate_paths(&src, &dst); 257 258 strbuf_release(&src); 259 strbuf_release(&dst); 260 261 tmp_objdir_destroy(t); 262 return ret; 263} 264 265const char **tmp_objdir_env(const struct tmp_objdir *t) 266{ 267 if (!t) 268 return NULL; 269 return t->env.argv; 270} 271 272void tmp_objdir_add_as_alternate(const struct tmp_objdir *t) 273{ 274 add_to_alternates_memory(t->path.buf); 275}