1/* 2 * Check-out files from the "current cache directory" 3 * 4 * Copyright (C) 2005 Linus Torvalds 5 * 6 * Careful: order of argument flags does matter. For example, 7 * 8 * checkout-cache -a -f file.c 9 * 10 * Will first check out all files listed in the cache (but not 11 * overwrite any old ones), and then force-checkout "file.c" a 12 * second time (ie that one _will_ overwrite any old contents 13 * with the same filename). 14 * 15 * Also, just doing "checkout-cache" does nothing. You probably 16 * meant "checkout-cache -a". And if you want to force it, you 17 * want "checkout-cache -f -a". 18 * 19 * Intuitiveness is not the goal here. Repeatability is. The 20 * reason for the "no arguments means no work" thing is that 21 * from scripts you are supposed to be able to do things like 22 * 23 * find . -name '*.h' -print0 | xargs -0 checkout-cache -f -- 24 * 25 * which will force all existing *.h files to be replaced with 26 * their cached copies. If an empty command line implied "all", 27 * then this would force-refresh everything in the cache, which 28 * was not the point. 29 * 30 * Oh, and the "--" is just a good idea when you know the rest 31 * will be filenames. Just so that you wouldn't have a filename 32 * of "-a" causing problems (not possible in the above example, 33 * but get used to it in scripting!). 34 */ 35#include"cache.h" 36 37static int force =0, quiet =0, not_new =0; 38 39static voidcreate_directories(const char*path) 40{ 41int len =strlen(path); 42char*buf =xmalloc(len +1); 43const char*slash = path; 44 45while((slash =strchr(slash+1,'/')) != NULL) { 46 len = slash - path; 47memcpy(buf, path, len); 48 buf[len] =0; 49mkdir(buf,0755); 50} 51free(buf); 52} 53 54static intcreate_file(const char*path,unsigned int mode) 55{ 56int fd; 57 58 mode = (mode &0100) ?0777:0666; 59 fd =open(path, O_WRONLY | O_TRUNC | O_CREAT, mode); 60if(fd <0) { 61if(errno == ENOENT) { 62create_directories(path); 63 fd =open(path, O_WRONLY | O_TRUNC | O_CREAT, mode); 64} 65} 66return fd; 67} 68 69static intwrite_entry(struct cache_entry *ce,const char*path) 70{ 71int fd; 72void*new; 73unsigned long size; 74long wrote; 75char type[20]; 76char target[1024]; 77 78new=read_sha1_file(ce->sha1, type, &size); 79if(!new||strcmp(type,"blob")) { 80returnerror("checkout-cache: unable to read sha1 file of%s(%s)", 81 path,sha1_to_hex(ce->sha1)); 82} 83switch(ntohl(ce->ce_mode) & S_IFMT) { 84case S_IFREG: 85 fd =create_file(path,ntohl(ce->ce_mode)); 86if(fd <0) { 87free(new); 88returnerror("checkout-cache: unable to create file%s(%s)", 89 path,strerror(errno)); 90} 91 wrote =write(fd,new, size); 92close(fd); 93free(new); 94if(wrote != size) 95returnerror("checkout-cache: unable to write file%s", path); 96break; 97case S_IFLNK: 98memcpy(target,new, size); 99 target[size] ='\0'; 100create_directories(path); 101if(symlink(target, path)) { 102free(new); 103returnerror("checkout-cache: unable to create symlink%s(%s)", 104 path,strerror(errno)); 105} 106free(new); 107break; 108default: 109free(new); 110returnerror("checkout-cache: unknown file mode for%s", path); 111} 112return0; 113} 114 115static intcheckout_entry(struct cache_entry *ce,const char*base_dir) 116{ 117struct stat st; 118static char path[MAXPATHLEN+1]; 119int len =strlen(base_dir); 120 121memcpy(path, base_dir, len); 122strcpy(path + len, ce->name); 123 124if(!lstat(path, &st)) { 125unsigned changed =cache_match_stat(ce, &st); 126if(!changed) 127return0; 128if(!force) { 129if(!quiet) 130fprintf(stderr,"checkout-cache:%salready exists\n", path); 131return0; 132} 133 134/* 135 * We unlink the old file, to get the new one with the 136 * right permissions (including umask, which is nasty 137 * to emulate by hand - much easier to let the system 138 * just do the right thing) 139 */ 140unlink(path); 141}else if(not_new) 142return0; 143returnwrite_entry(ce, path); 144} 145 146static intcheckout_file(const char*name,const char*base_dir) 147{ 148int pos =cache_name_pos(name,strlen(name)); 149if(pos <0) { 150if(!quiet) { 151 pos = -pos -1; 152fprintf(stderr, 153"checkout-cache:%sis%s.\n", 154 name, 155(pos < active_nr && 156!strcmp(active_cache[pos]->name, name)) ? 157"unmerged":"not in the cache"); 158} 159return-1; 160} 161returncheckout_entry(active_cache[pos], base_dir); 162} 163 164static intcheckout_all(const char*base_dir) 165{ 166int i; 167 168for(i =0; i < active_nr ; i++) { 169struct cache_entry *ce = active_cache[i]; 170if(ce_stage(ce)) 171continue; 172if(checkout_entry(ce, base_dir) <0) 173return-1; 174} 175return0; 176} 177 178intmain(int argc,char**argv) 179{ 180int i, force_filename =0; 181const char*base_dir =""; 182 183if(read_cache() <0) { 184die("invalid cache"); 185} 186 187for(i =1; i < argc; i++) { 188const char*arg = argv[i]; 189if(!force_filename) { 190if(!strcmp(arg,"-a")) { 191checkout_all(base_dir); 192continue; 193} 194if(!strcmp(arg,"--")) { 195 force_filename =1; 196continue; 197} 198if(!strcmp(arg,"-f")) { 199 force =1; 200continue; 201} 202if(!strcmp(arg,"-q")) { 203 quiet =1; 204continue; 205} 206if(!strcmp(arg,"-n")) { 207 not_new =1; 208continue; 209} 210if(!memcmp(arg,"--prefix=",9)) { 211 base_dir = arg+9; 212continue; 213} 214} 215checkout_file(arg, base_dir); 216} 217return0; 218}