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)); 150env_replace(&t->env, GIT_QUARANTINE_ENVIRONMENT, 151absolute_path(t->path.buf)); 152 153return 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 intpack_copy_priority(const char*name) 164{ 165if(!starts_with(name,"pack")) 166return0; 167if(ends_with(name,".keep")) 168return1; 169if(ends_with(name,".pack")) 170return2; 171if(ends_with(name,".idx")) 172return3; 173return4; 174} 175 176static intpack_copy_cmp(const char*a,const char*b) 177{ 178returnpack_copy_priority(a) -pack_copy_priority(b); 179} 180 181static intread_dir_paths(struct string_list *out,const char*path) 182{ 183DIR*dh; 184struct dirent *de; 185 186 dh =opendir(path); 187if(!dh) 188return-1; 189 190while((de =readdir(dh))) 191if(de->d_name[0] !='.') 192string_list_append(out, de->d_name); 193 194closedir(dh); 195return0; 196} 197 198static intmigrate_paths(struct strbuf *src,struct strbuf *dst); 199 200static intmigrate_one(struct strbuf *src,struct strbuf *dst) 201{ 202struct stat st; 203 204if(stat(src->buf, &st) <0) 205return-1; 206if(S_ISDIR(st.st_mode)) { 207if(!mkdir(dst->buf,0777)) { 208if(adjust_shared_perm(dst->buf)) 209return-1; 210}else if(errno != EEXIST) 211return-1; 212returnmigrate_paths(src, dst); 213} 214returnfinalize_object_file(src->buf, dst->buf); 215} 216 217static intmigrate_paths(struct strbuf *src,struct strbuf *dst) 218{ 219size_t src_len = src->len, dst_len = dst->len; 220struct string_list paths = STRING_LIST_INIT_DUP; 221int i; 222int ret =0; 223 224if(read_dir_paths(&paths, src->buf) <0) 225return-1; 226 paths.cmp = pack_copy_cmp; 227string_list_sort(&paths); 228 229for(i =0; i < paths.nr; i++) { 230const char*name = paths.items[i].string; 231 232strbuf_addf(src,"/%s", name); 233strbuf_addf(dst,"/%s", name); 234 235 ret |=migrate_one(src, dst); 236 237strbuf_setlen(src, src_len); 238strbuf_setlen(dst, dst_len); 239} 240 241string_list_clear(&paths,0); 242return ret; 243} 244 245inttmp_objdir_migrate(struct tmp_objdir *t) 246{ 247struct strbuf src = STRBUF_INIT, dst = STRBUF_INIT; 248int ret; 249 250if(!t) 251return0; 252 253strbuf_addbuf(&src, &t->path); 254strbuf_addstr(&dst,get_object_directory()); 255 256 ret =migrate_paths(&src, &dst); 257 258strbuf_release(&src); 259strbuf_release(&dst); 260 261tmp_objdir_destroy(t); 262return ret; 263} 264 265const char**tmp_objdir_env(const struct tmp_objdir *t) 266{ 267if(!t) 268return NULL; 269return t->env.argv; 270} 271 272voidtmp_objdir_add_as_alternate(const struct tmp_objdir *t) 273{ 274add_to_alternates_memory(t->path.buf); 275}