📄 util.c
字号:
arguments: none return: 0 on success -1 on error********************************************************************/int RewindDevice(){ int exec_pid; /* No can do if we are doing filecopy or creating a cd backup */ if(conf__type==FILECOPY || conf__type==CDR || conf__type==CDRW) return 0; /* Ignore evt. request for rewind from a stupid user */ /* do we need to rewind the tape ?? */ if(conf__non_rewind) { /* yep.. rewind the tape */ log("Rewinding tape"); UI__Message("Rewinding the tape\n"); debug("Device is local\n"); if((exec_pid=Execute("%s -f %s rewind",conf__mt,conf__archive))<0) { log("In backup. Unable to rewind the tape."); errcode=13; UI__Warning("Unable to rewind the tape\n"); return -1; } waitpid(exec_pid,NULL,0); /*wait for the archive to become available*/ if(ArchiveReady(3600)) { errcode=50; log("Archive or device no longer available"); return -1; } } /* ready */ return 0;}/******************************************************************** int Damonize() Become a daemon, but only if conf__mode=DAEMON arguments: none return: 0 on success (now a daemon, or mode not daemon) -1 on error********************************************************************/int Daemonize(){ int rc; /*single process or daemon*/ if(conf__mode==DAEMON) { /*start a new process*/ rc=fork(); /*return from parent, let child continue*/ if(rc==-1) { log("Unable to start new process. Backup can not run as daemon"); return -1; } /*child process running*/ else if(rc==0) { debug("child process ready to become a daemon\n"); /*now setsid to become a process group and session group leader. The manual promises that this only fail if we are already a process group leader, so thats not so bad, in the end we got what we wanted.*/ setsid(); /*now fork and exit the parent, so the child (and grandchilds) may never regain a controlling terminal*/ rc=fork(); if(rc==-1) { debug("Ouups.. first child failed to fork. Unable to become a daemon\n"); log("Unable to become a daemon"); errcode=48; return -1; } /*parent process returns (1.st child)*/ if(rc) { exit(0); } /*please let us control permissions*/ umask(0); /*pause a litle bit */ sleep(10); /*we like to keep stdout, stdin and stderr*/ log("Daemon process started"); } /*parent process returns*/ else { debug("Parent process ready to return\n"); exit(0); } } /* If we get here, either we should not become a daemon, or we successfully became a daemon. */ return 0;}/******************************************************************** void Sync() Sync the filesystem, local or remote arguments: none return: none********************************************************************/void Sync(){ struct stat statbuf; int pid; /* Sync. the filesystem related to the device or file we are writing */ if(conf__rhost[0]!='\0') { pid=Execute("%s -l %s %s sync",conf__rsh,conf__ruser,conf__rhost); while(kill(pid,0)!=-1) waitpid(-1,NULL,WNOHANG | WUNTRACED); } else { if(!stat("/bin/sync",&statbuf)) system("/bin/sync"); else if(!stat("/sbin/sync",&statbuf)) system("/sbin/sync"); else if(!stat("/usr/bin/sync",&statbuf)) system("/usr/bin/sync"); else if(!stat("/usr/sbin/sync",&statbuf)) system("/usr/sbin/sync"); else if(!stat("/usr/local/bin/sync",&statbuf)) system("/usr/local/bin/sync"); else if(!stat("/usr/local/sbin/sync",&statbuf)) system("/usr/local/sbin/sync"); }}/******************************************************************** init Initialize_Report() Initialize the backup report file arguments: none return: 0 on success -1 on error********************************************************************/int Initialize_Report(){ FILE *file; time_t timep; struct tm *currtime; unsigned int count=0; /* Try to open/overwrite the report file */ if((file=fopen("sitback.report","w"))==NULL) { UI__Warning("Unable to open report file\n"); errcode=98; return -1; } fclose(file); /* Write the header */ timep=time(NULL); currtime=localtime(&timep); conf__time_started=timep; Report("-------------------------------------------------------------------"); for(count=0;count<strlen(VERSION);count++) /*just to make things look nice*/ Report("-"); /* ... */ Report("\n sitback version %s Henrik Witt-Hansen, bean@mrbean.dk 1999-2004\n\n",VERSION); Report(" Backup report %02d/%02d-%04d %02d:%02d\n", currtime->tm_mon+1,currtime->tm_mday,currtime->tm_year+1900,currtime->tm_hour,currtime->tm_min); Report("-------------------------------------------------------------------"); for(count=0;count<strlen(VERSION);count++) /*just to make things look nice*/ Report("-"); /* ... */ Report("\n\n\n\n"); /* Ok, ready */ return 0;}/******************************************************************** Report() Add a message to the report Arguments: formatstring; printf-like string to print ...; arguments for the formatstring Returns; none********************************************************************/void Report(char *formatstring, ... ){ va_list ap; FILE *file; if((file=fopen("sitback.report","a"))!=NULL) { /* write the message */ va_start(ap,formatstring); vfprintf(file,formatstring,ap); fclose(file); va_end(ap); }}/******************************************************************** Finalize_Report() Add statistic info to the end of the report, and copy/print/mail it, if requested. Arguments: none Returns; none********************************************************************/void Finalize_Report(){ time_t timep; unsigned count; char *p; char timecomponent[5]; time_t currtime=time(NULL); struct tm *timeptr=localtime(&currtime); char reportname[263]; /* Add statistic output to the report */ Report("\n\n\n-------------------------------------------------------------------"); for(count=0;count<strlen(VERSION);count++) Report("-"); Report("\n\n\n\n"); Report(" script : %s\n",conf__script_name); Report(" Volume name : %s\n",conf__archive_name); Report(" Archive ID : %s\n",conf__archive_id); if(conf__archive_size>1024*1024) /*that's MegaBytes, ya know !!*/ Report(" Size : aprox. %lu Mbytes\n",conf__archive_size/(1024*1024)); else if(conf__archive_size>1024) /*oh, this must be KiloBytes*/ Report(" Size : aprox. %lu Kbytes\n",conf__archive_size/1024); else /*aaah. pick a guess*/ Report(" Size : aprox. %lu bytes\n",conf__archive_size); timep=time(NULL); timep-=conf__time_started; Report(" Time : %02d hours and %02d minutes\n",(int) timep/3600,(int) (timep%3600)/60); if(conf__filelist!=NULL) { while(conf__filelist->prev!=NULL) conf__filelist=conf__filelist->prev; while(conf__filelist!=NULL) { if(conf__filelist->prev==NULL && conf__filelist->next==NULL) Report(" Source : %s\n",conf__filelist->target); else if(conf__filelist->prev==NULL && conf__filelist->next!=NULL) Report(" Source(s) : %s\n",conf__filelist->target); else Report(" %s\n",conf__filelist->target); if(conf__filelist->next!=NULL) conf__filelist=conf__filelist->next; else break; } } else Report(" Source : (empty)\n"); /*check if the report should be printed.*/ if(conf__report_printer[0]!='\0') { /*try to print the report-file*/ debug("Request for printed report\n"); if(PrintReport("sitback.report")==-1) { log("Unable to print the report. errcode is %d",errcode); } } /*check if a special report-file should be written*/ if(conf__report_name[0]!='\0') { /* Get a local copy of the report name */ strcpy(reportname,conf__report_name); /* Any substitutions in the archive name ?? */ if((p=strstr(reportname,"%Y"))!=NULL && strlen(reportname)<252) { /* Year */ memmove(p+4,p+2,strlen(p+2)+1); sprintf(timecomponent,"%04d",timeptr->tm_year+1900); memcpy(p,timecomponent,4); } if((p=strstr(reportname,"%M"))!=NULL) { /* Month */ sprintf(timecomponent,"%02d",timeptr->tm_mon+1); memcpy(p,timecomponent,2); } if((p=strstr(reportname,"%D"))!=NULL) { /* Day of month */ sprintf(timecomponent,"%02d",timeptr->tm_mday); memcpy(p,timecomponent,2); } if((p=strstr(reportname,"%h"))!=NULL) { /* Hour */ sprintf(timecomponent,"%02d",timeptr->tm_hour); memcpy(p,timecomponent,2); } if((p=strstr(reportname,"%m"))!=NULL) { /* Minute */ sprintf(timecomponent,"%02d",timeptr->tm_min); memcpy(p,timecomponent,2); } if((p=strstr(reportname,"%s"))!=NULL) { /* Sec. */ sprintf(timecomponent,"%02d",timeptr->tm_sec); memcpy(p,timecomponent,2); } if((p=strstr(reportname,"%w"))!=NULL) { /* Sec. */ memmove(p,p+1,strlen(p+1)+1); sprintf(timecomponent,"%01d",timeptr->tm_wday); memcpy(p,timecomponent,1); } /*try to print the report-file*/ debug("Request for reportfile\n"); UI__Message("Writing report %s\n",reportname); if(WriteReport("sitback.report",reportname)==-1) { UI__Warning("Failed to write report\n"); log("Unable to write the report. errcode is %d",errcode); } } /* check if we should send a mail-copy of the report */ if(conf__report_mail[0]!='\0') { /* try to send the report-file */ debug("Request for reportmail\n"); UI__Message("Sending report to %s\n",conf__report_mail); if(SendMail("sitback.report","Backup report")==-1) { UI__Warning("Failed to send mail\n"); log("Unable to send the report via mail. errcode is %d",errcode); } }}/******************************************************************** base64_encode() Encode the string in base64. Arguments: string: The string to base64 encode Returns; A buffer with the encoded string. Users must free this buffer. NULL on error.********************************************************************/char *base64_encode(char *string){ char *buffer; int size; unsigned int input; int input_char = 0; int output_char = 0; unsigned int output; char base64_table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; /* Calculate final size */ size = (strlen(string) * 4) / 3; size += 10; /* Safety */ debug("Base64 encoded string will be less than %d bytes\n",size); /* Allocate the result buffer */ if( (buffer = (char*) malloc(size))==NULL ) { debug("Unable to allocate a buffer\n"); return NULL; } memset(buffer,0,size); /* Now do the encoding.. */ do { /* Add byte 1 */ input = 0; input += string[input_char]; input = input << 8; input_char++; /* Add byte 2 */ if( input_char < strlen(string) ) input += string[input_char]; input = input << 8; input_char++; /* Add byte 3 */ if( input_char < strlen(string) ) input += string[input_char]; input_char++; /* Get output */ output = (input & 0xFC0000) >> 18; buffer[output_char] = base64_table[output & 0xFF]; output_char++; output = (input & 0x03F000) >> 12; buffer[output_char] = base64_table[output & 0xFF]; output_char++; output = (input & 0x000FC0) >> 6; buffer[output_char] = base64_table[output & 0xFF]; output_char++; output = (input & 0x00003F); buffer[output_char] = base64_table[output & 0xFF]; output_char++; } while( input_char < strlen(string) ); /* Padding ?? */ if( input_char - strlen(string) == 1 ) { buffer[output_char-1] = '='; } else if( input_char - strlen(string) == 2 ) { buffer[output_char-1] = '='; buffer[output_char-2] = '='; } /* Done */ return buffer;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -