#include #include #include #include #include #include #include #include "vutil.h" #define ETCS_DIR "/vsynth/etcs/" static const int ETCS_DIR_LEN = strlen(ETCS_DIR); #define VI_CMD "vim" int main(int argc, char *argv[]) { glocal int ret = -1; glocal.ret = (argc,argv); setproginfo ("vvi",VERSION ,"Edit configuration files for synthetic vservers\n" "\n" "vvi filepath\n"); int ret = -1; if (argc != 1){ usage(); }else{ const char *fname = argv[0]; SSTRING absname; if (fname[0] == '/'){ absname = fname; }else{ char cwd[PATH_MAX]; if (getcwd(cwd,sizeof(cwd))!=NULL){ absname.setfromf ("%s/%s",cwd,fname); }else{ tlmp_error ("Can't get the current working directory\n"); exit (1); } } if (absname.ncmp(ETCS_DIR,ETCS_DIR_LEN)==0){ // Ok, within the synthetic vserver configuration directory const char *ptname = absname.c_str()+ETCS_DIR_LEN; const char *pt = strchr(ptname,'/'); if (pt == NULL){ tlmp_error ("Can't identify the vserver from %s\n" ,absname.c_str()); }else{ SSTRING vserver,relpath; vserver.setfrom (ptname,pt-ptname); relpath = pt + 1; debug_printf ("vserver=%s,relpath=%s\n",vserver.c_str(),relpath.c_str()); VSERVER_CONF conf; // Configuration of the vserver VSERVER_CONF refconf; // Configuration of the reference if (vutil_readconf(vserver.c_str(),conf)==-1){ tlmp_error ("Can't read configuration for vserver %s\n" ,vserver.c_str()); }else if (conf.buildfrom.size()==0){ tlmp_error ("Vserver %s is not synthetic\n",vserver.c_str()); }else if (vutil_readconf(conf.buildfrom,refconf)==-1){ tlmp_error ("Can't read configuration from buildfrom vserver %s\n" ,conf.buildfrom.c_str()); }else{ SSTRING editpath,refpath; editpath.setfromf ("%s/etc/%s",conf.vserverdir.c_str() ,relpath.c_str()); refpath.setfromf ("%s/etc/%s",refconf.vserverdir.c_str() ,relpath.c_str()); debug_printf ("editpath=%s,refpath=%s\n" ,editpath.c_str(),refpath.c_str()); if (file_type(conf.vserverdir.c_str())==1){ // Ok the vserver is currently assembled // We will edit the file there and import the changes // in /vsynth/etcs SSTRING cmd; cmd.setfromf ("%s %s",VI_CMD,editpath.c_str()); system (cmd.c_str()); // Make sure the target directory exist in ETCS_DIR pt = relpath.strrchr ('/'); if (pt != NULL){ // Ok the file is in a sub-directory SSTRING subdir; subdir.setfrom (relpath.c_str(),pt-relpath.c_str()); subdir.setfromf ("%s/%s/%s",ETCS_DIR ,vserver.c_str(),subdir.c_str()); file_mkdirp (subdir.c_str(),"root","root",0755); } // Produce a diff and keep it in the ETCS_DIR with // the extension .diff cmd.setfromf ("diff -c %s %s >%s.diff" ,refpath.c_str(),editpath.c_str() ,absname.c_str()); system (cmd.c_str()); printf ("File %s.diff was produced\n" ,absname.c_str()); }else{ // The vserver is not assembled, we will need // the path of the reference vserver file_copy (refpath.c_str(),absname.c_str()); SSTRING cmd; cmd.setfromf ("%s %s",VI_CMD,absname.c_str()); system (cmd.c_str()); // Produce a diff and keep it in the ETCS_DIR with // the extension .diff cmd.setfromf ("diff -c %s %s >%s.diff" ,refpath.c_str(),absname.c_str() ,absname.c_str()); system (cmd.c_str()); unlink (absname.c_str()); printf ("File %s.diff was produced\n" ,absname.c_str()); } } } }else{ // No, if the file exist, let vi have it if (file_exist(absname.c_str())){ execlp (VI_CMD,VI_CMD,absname.c_str(),NULL); tlmp_error ("Can't execute vi: %s",strerror(errno)); }else{ tlmp_error ("File %s does not exist\n" "Not part of any synthetic vserver configuration\n" "Nothing I can do\n",absname.c_str()); } } } return ret; return glocal.ret; }