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 { 10struct strbuf path; 11struct 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 voidtmp_objdir_free(struct tmp_objdir *t) 23{ 24strbuf_release(&t->path); 25argv_array_clear(&t->env); 26free(t); 27} 28 29static inttmp_objdir_destroy_1(struct tmp_objdir *t,int on_signal) 30{ 31int err; 32 33if(!t) 34return0; 35 36if(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 */ 51if(!on_signal) 52tmp_objdir_free(t); 53return err; 54} 55 56inttmp_objdir_destroy(struct tmp_objdir *t) 57{ 58returntmp_objdir_destroy_1(t,0); 59} 60 61static voidremove_tmp_objdir(void) 62{ 63tmp_objdir_destroy(the_tmp_objdir); 64} 65 66static voidremove_tmp_objdir_on_signal(int signo) 67{ 68tmp_objdir_destroy_1(the_tmp_objdir,1); 69sigchain_pop(signo); 70raise(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 voidenv_append(struct argv_array *env,const char*key,const char*val) 81{ 82const char*old =getenv(key); 83 84if(!old) 85argv_array_pushf(env,"%s=%s", key, val); 86else 87argv_array_pushf(env,"%s=%s%c%s", key, old, PATH_SEP, val); 88} 89 90static voidenv_replace(struct argv_array *env,const char*key,const char*val) 91{ 92argv_array_pushf(env,"%s=%s", key, val); 93} 94 95static intsetup_tmp_objdir(const char*root) 96{ 97char*path; 98int ret =0; 99 100 path =xstrfmt("%s/pack", root); 101 ret =mkdir(path,0777); 102free(path); 103 104return ret; 105} 106 107struct tmp_objdir *tmp_objdir_create(void) 108{ 109static int installed_handlers; 110struct tmp_objdir *t; 111 112if(the_tmp_objdir) 113die("BUG: only one tmp_objdir can be used at a time"); 114 115 t =xmalloc(sizeof(*t)); 116strbuf_init(&t->path,0); 117argv_array_init(&t->env); 118 119strbuf_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 */ 127strbuf_grow(&t->path,1024); 128 129if(!mkdtemp(t->path.buf)) { 130/* free, not destroy, as we never touched the filesystem */ 131tmp_objdir_free(t); 132return NULL; 133} 134 135 the_tmp_objdir = t; 136if(!installed_handlers) { 137atexit(remove_tmp_objdir); 138sigchain_push_common(remove_tmp_objdir_on_signal); 139 installed_handlers++; 140} 141 142if(setup_tmp_objdir(t->path.buf)) { 143tmp_objdir_destroy(t); 144return NULL; 145} 146 147env_append(&t->env, ALTERNATE_DB_ENVIRONMENT, 148absolute_path(get_object_directory())); 149env_replace(&t->env, DB_ENVIRONMENT,absolute_path(t->path.buf)); 150 151return t; 152} 153 154/* 155 * Make sure we copy packfiles and their associated metafiles in the correct 156 * order. All of these ends_with checks are slightly expensive to do in 157 * the midst of a sorting routine, but in practice it shouldn't matter. 158 * We will have a relatively small number of packfiles to order, and loose 159 * objects exit early in the first line. 160 */ 161static intpack_copy_priority(const char*name) 162{ 163if(!starts_with(name,"pack")) 164return0; 165if(ends_with(name,".keep")) 166return1; 167if(ends_with(name,".pack")) 168return2; 169if(ends_with(name,".idx")) 170return3; 171return4; 172} 173 174static intpack_copy_cmp(const char*a,const char*b) 175{ 176returnpack_copy_priority(a) -pack_copy_priority(b); 177} 178 179static intread_dir_paths(struct string_list *out,const char*path) 180{ 181DIR*dh; 182struct dirent *de; 183 184 dh =opendir(path); 185if(!dh) 186return-1; 187 188while((de =readdir(dh))) 189if(!is_dot_or_dotdot(de->d_name)) 190string_list_append(out, de->d_name); 191 192closedir(dh); 193return0; 194} 195 196static intmigrate_paths(struct strbuf *src,struct strbuf *dst); 197 198static intmigrate_one(struct strbuf *src,struct strbuf *dst) 199{ 200struct stat st; 201 202if(stat(src->buf, &st) <0) 203return-1; 204if(S_ISDIR(st.st_mode)) { 205if(!mkdir(dst->buf,0777)) { 206if(adjust_shared_perm(dst->buf)) 207return-1; 208}else if(errno != EEXIST) 209return-1; 210returnmigrate_paths(src, dst); 211} 212returnfinalize_object_file(src->buf, dst->buf); 213} 214 215static intmigrate_paths(struct strbuf *src,struct strbuf *dst) 216{ 217size_t src_len = src->len, dst_len = dst->len; 218struct string_list paths = STRING_LIST_INIT_DUP; 219int i; 220int ret =0; 221 222if(read_dir_paths(&paths, src->buf) <0) 223return-1; 224 paths.cmp = pack_copy_cmp; 225string_list_sort(&paths); 226 227for(i =0; i < paths.nr; i++) { 228const char*name = paths.items[i].string; 229 230strbuf_addf(src,"/%s", name); 231strbuf_addf(dst,"/%s", name); 232 233 ret |=migrate_one(src, dst); 234 235strbuf_setlen(src, src_len); 236strbuf_setlen(dst, dst_len); 237} 238 239string_list_clear(&paths,0); 240return ret; 241} 242 243inttmp_objdir_migrate(struct tmp_objdir *t) 244{ 245struct strbuf src = STRBUF_INIT, dst = STRBUF_INIT; 246int ret; 247 248if(!t) 249return0; 250 251strbuf_addbuf(&src, &t->path); 252strbuf_addstr(&dst,get_object_directory()); 253 254 ret =migrate_paths(&src, &dst); 255 256strbuf_release(&src); 257strbuf_release(&dst); 258 259tmp_objdir_destroy(t); 260return ret; 261} 262 263const char**tmp_objdir_env(const struct tmp_objdir *t) 264{ 265if(!t) 266return NULL; 267return t->env.argv; 268} 269 270voidtmp_objdir_add_as_alternate(const struct tmp_objdir *t) 271{ 272add_to_alternates_memory(t->path.buf); 273}