📄 archive.c
字号:
,currtime->tm_year+1900 ,currtime->tm_hour ,currtime->tm_min); debug("generated archiveid is \"%s\"\n",conf__archive_oldid); } /*try to write the archive id to file*/ if(__write_archiveid()) return -1; /*no problems*/ return 0;}/******************************************************************** int OldArchiveId() Try to obtain the archive id if the device or archive is already present. arguments: none return: -1 on error 0 on success*****************************************************************/int OldArchiveId(){ FILE *file; int pid=-1; char strn[1024]; int status; unsigned type=0; struct stat fi; int initial_err_size=0; char command[256]; int rc; int first_try=0; struct stat statbuf; char archiveid_file[4096]; /* Types of archive that is handled in other ways */ if(conf__type==FILECOPY) { /* Does the archive exists ?? */ debug("Looking for an archive id in a filecopy archive\n"); if(stat(conf__archive,&statbuf)) { debug("Archive does not exist, no archive id\n"); return 0; } /* We have the archive id file, get the id */ snprintf(archiveid_file,4096,"%s/sitback.archiveid",conf__archive); if((file=fopen(archiveid_file,"r"))==NULL) { debug("No archive id file \n"); return 0; } fgets(strn,256,file); fclose(file); if(strn[strlen(strn)-1]=='\n') strn[strlen(strn)-1]='\0'; debug("Read the archiveid '%s'\n",strn); strcpy(conf__archive_id,strn); return 0; } /* Try to get the old archiveid assuming different types of tar archive */ debug("Trying to find an old archive id\n"); for(;;) { /*wait for the device to become available*/ if(ArchiveReady(3600)) { errcode=50; log("Archive or device no longer available"); return -1; } /*remove the sitback.archiveid file so that we may detect any new file extracted from the archive*/ if(unlink("sitback.tmpout") && errno!=ENOENT) { errcode=24; UI__Warning("Unable to delete 'sitback.tmperr'. %d '%s'",errno,strerror(errno)); return -1; } /* remove any previous sitback.tmperr, so that we can detect any errors */ if(unlink("sitback.tmperr") && errno!=ENOENT) { errcode=24; UI__Warning("Unable to delete 'sitback.tmperr'. %d '%s'",errno,strerror(errno)); return -1; } /* try extracting the id from non-compressed or compressed archives */ debug("Trying archive type %u\n",type); switch(type) { case 0: { if(conf__compressapp!=NULL && strstr(conf__compressapp,"bzip2")!=NULL) { /* search for an archiveid assuming the archive is compressed with bzip2 */ if(conf__bzip2[0]!='\0') { debug("Assuming bzip2 compressed archive\n"); if(conf__rhost[0]=='\0') sprintf(command,"tar -x --use-compress-program %s -P -O -f %s sitback.archiveid ",conf__bzip2,conf__archive); else sprintf(command,"tar -x --rsh-command=%s --use-compress-program %s -P -O -f %s sitback.archiveid ",conf__rsh,conf__bzip2,conf__archive); } first_try=2; } else if(conf__compressapp!=NULL && strstr(conf__compressapp,"gzip")!=NULL) { /* search for an archiveid assuming the archive is compressed with gzip */ if(conf__gzip[0]!='\0') { debug("Assuming gzip compressed archive\n"); if(conf__rhost[0]=='\0') sprintf(command,"tar -x --use-compress-program %s -P -O -f %s sitback.archiveid ",conf__gzip,conf__archive); else sprintf(command,"tar -x --rsh-command=%s --use-compress-program %s -P -O -f %s sitback.archiveid ",conf__rsh,conf__gzip,conf__archive); } first_try=3; } else if(conf__compressapp!=NULL && strstr(conf__compressapp,"compress")!=NULL) { /* search for an archiveid assuming the archive is compressed with gzip */ if(conf__gzip[0]!='\0') { debug("Assuming compress compressed archive\n"); if(conf__rhost[0]=='\0') sprintf(command,"tar -x --use-compress-program %s -P -O -f %s sitback.archiveid ",conf__gzip,conf__archive); else sprintf(command,"tar -x --rsh-command=%s --use-compress-program %s -P -O -f %s sitback.archiveid ",conf__rsh,conf__gzip,conf__archive); } first_try=4; } else { /*Search for an archiveid assuming the archive is uncompressed*/ debug("Assuming non-compressed archive\n"); if(conf__rhost[0]=='\0') sprintf(command,"tar -x -P -O -f %s sitback.archiveid",conf__archive); else sprintf(command,"tar -x --rsh-command=%s -P -O -f %s sitback.archiveid",conf__rsh,conf__archive); first_try=1; } if((pid=Execute(command))==-1) return -1; debug("Now searching\n"); type++; break; } case 1: { /*Search for an archiveid assuming the archive is uncompressed*/ if(first_try!=1) { debug("Assuming non-compressed archive\n"); if(conf__rhost[0]=='\0') sprintf(command,"tar -x -P -O -f %s sitback.archiveid",conf__archive); else sprintf(command,"tar -x --rsh-command=%s -P -O -f %s sitback.archiveid",conf__rsh,conf__archive); if((pid=Execute(command))==-1) return -1; debug("Now searching\n"); } type++; break; } case 2: { /* search for an archiveid assuming the archive is compressed with bzip2 */ if(conf__bzip2[0]!='\0' && first_try!=2) { debug("Assuming bzip2 compressed archive\n"); if(conf__rhost[0]=='\0') sprintf(command,"tar -x --use-compress-program %s -P -O -f %s sitback.archiveid ",conf__bzip2,conf__archive); else sprintf(command,"tar -x --rsh-command=%s --use-compress-program %s -P -O -f %s sitback.archiveid ",conf__rsh,conf__bzip2,conf__archive); if((pid=Execute(command))==-1) return -1; debug("Now searching\n"); } type++; break; } case 3: { /* search for an archiveid assuming the archive is compressed with gzip */ if(conf__gzip[0]!='\0' && first_try!=3) { debug("Assuming gzip compressed archive\n"); if(conf__rhost[0]=='\0') sprintf(command,"tar -x --use-compress-program %s -P -O -f %s sitback.archiveid ",conf__gzip,conf__archive); else sprintf(command,"tar -x --rsh-command=%s --use-compress-program %s -P -O -f %s sitback.archiveid ",conf__rsh,conf__gzip,conf__archive); if((pid=Execute(command))==-1) return -1; debug("Now searching\n"); } type++; break; } case 4: { /* search for an archiveid assuming the archive is compressed with compress */ if(conf__Zcompress[0]!='\0' && first_try!=4) { debug("Assuming compress compressed archive\n"); if(conf__rhost[0]=='\0') sprintf(command,"tar -x --use-compress-program %s -P -O -f %s sitback.archiveid ",conf__Zcompress,conf__archive); else sprintf(command,"tar -x --rsh-command=%s --use-compress-program %s -P -O -f %s sitback.archiveid ",conf__rsh,conf__Zcompress,conf__archive); if((pid=Execute(command))==-1) return -1; debug("Now searching\n"); } type++; break; } default: { /*no problems, but no id*/ debug("No more types of archives.. stopping\n"); log("No archive id"); return 0; } } /* Wait until we either got an archiveid or tar has stopped running */ while(1) { /*dont overpower the cpu*/ sleep(1); /* any errors ?? */ if(!stat("sitback.tmperr",&fi)) { if(initial_err_size==0) initial_err_size=fi.st_size; else if(fi.st_size!=initial_err_size) { /* errors.. kill tar */ debug("Errors while searching for the archive id\n"); Kill(pid); break; } } /*check if we got the archiveid*/ if((file=fopen("sitback.tmpout","r"))!=NULL) { if(fgets(strn,300,file)!=NULL) { debug("got archiveid, aborting the search\n"); fclose(file); Kill(pid); break; } fclose(file); } /*make sure tar is still alive*/ if(waitpid(pid,&status,WNOHANG | WUNTRACED)!=0) { debug("process no longer running, continuing checking for archiveid\n"); break; } } /*make sure we dont have a zombie tar*/ while((rc=waitpid(-1,&status,WNOHANG))>0); /*did we get a valid archive-id*/ if((file=fopen("sitback.tmpout","r"))!=NULL) { /*read no more than 256 characters*/ fgets(conf__archive_oldid,256,file); fclose(file); if(conf__archive_oldid[strlen(conf__archive_oldid)-1]=='\n') conf__archive_oldid[strlen(conf__archive_oldid)-1]='\0'; /*is it a valid archiveid*/ if(conf__archive_oldid[0]!='\n' && conf__archive_oldid[0]!='\0') { debug("got valid archiveid \"%s\"\n",conf__archive_oldid); /*do not continue*/ sprintf(strn,"Id: '%s'\n",conf__archive_oldid); UI__Message(strn); log("Found archive id '%s'",conf__archive_oldid); return 0; } } /*wait until the device is ready*/ if(ArchiveReady(3600)) { errcode=50; log("Archive or device no longer available"); return -1; } /* rewind */ if(RewindDevice()) return -1; } /*no problems, but no id*/ log("No archive id"); return 0;}/******************************************************************** int __write_archiveid() Write the sitback.archiveid file arguments: none return: -1 on error 0 on success********************************************************************/int __write_archiveid(){ FILE *file; /*try to open the file*/ if((file=fopen("sitback.archiveid","w"))==NULL) { UI__Warning("Unable to write the archiveid to file\n"); errcode=30; log("Unable to write the archiveid to file"); return -1; } /*write the archiveid to the file*/ if(conf__archive_id[0]!='\0') { debug("force archiveid \"%s\"\n",conf__archive_id); fprintf(file,"%s",conf__archive_id); /*rename the entry*/ if(conf__archive_oldid[0]!='\0') { /*try to rename the entry, then set the status.. Rename may fail if the database directory does not exist, perhaps due to a new-installation after a crach, so ignore the error*/ if(DataBaseRename(conf__archive_oldid,conf__archive_id)!=-1) DataBaseSetStatus(conf__archive_id,"UNKNOWN"); } } else { strcpy(conf__archive_id,conf__archive_oldid); fprintf(file,"%s\n",conf__archive_id); } fclose(file); /*no problems*/ return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -