📄 aimage.cpp
字号:
if(opt_use_timers) im->write_timer.start(); break; case 4: /* End of writing */ if(opt_use_timers) im->write_timer.stop(); /* log if necessary */ if(logfile){ fprintf(logfile, " pagenum=%"I64d" bytes_to_write=%d bytes_written=%d lap_time=%f\n", acbi->pagenum, acbi->bytes_to_write, acbi->bytes_written, im->write_timer.lap_time()); } im->callback_bytes_to_write += acbi->bytes_to_write; im->callback_bytes_written += acbi->bytes_written; im->total_segments_written ++; if(opt_auto_compress){ /* Handle automatic compression. */ if(im->total_segments_written==1){ /* First segment was written. * Tabulate time and turn off compression. */ ac_compress_write_time += im->write_timer.lap_time(); af_enable_compression(acbi->af,AF_COMPRESSION_ALG_NONE,opt_compression_level); } if(im->total_segments_written==2){ /* Second segment was written. * Tabulate times and process. */ ac_nocompress_write_time += im->write_timer.lap_time(); /* Figure out which was faster */ if(ac_compress_write_time < ac_nocompress_write_time){ /* Turn on compression */ af_enable_compression(acbi->af, opt_compression_alg, opt_compression_level); } else { /* Turn off compression */ af_enable_compression(acbi->af, AF_COMPRESSION_ALG_NONE, opt_compression_level); } } } } /* Refresh if necessary */ if(opt_quiet==0 && opt_silent==0){ my_refresh(im,acbi); }}void gotorc(int row,int col){ char buf[256]; char *cc = buf; tputs(tgoto(tgetstr("cm",&cc),col,row),0,putchar);}/* sig_intr: * Close the AFF file if possible * If this is the second time we were called, just exit... */int depth = 0;void sig_intr(int arg){ int rows = tgetnum("li"); gui_shutdown(); depth++; if(depth>1){ printf("\r\n\nInterrupted interrupt. Quitting.\n\r"); fflush(stdout); exit(1); } gotorc(rows-1,0); printf("\n\n\n\rInterrupt!\n\r"); printf("\n\n\n"); fflush(stdout); if(opt_fast_quit){ printf("*** FAST QUIT ***\n\r"); exit(1); } imager *im = imagers[current_imager]; if(im && im->af){ /* We had the AF open when the ^c came; shut things down nicely */ AFFILE *af = im->af; printf("Closing output AFF file...\n\r"); fflush(stdout); af_set_callback(af,0); af_enable_compression(af, 0, 0); if(af_close(af)){ warnx("Can't close file '%s'\n",af_filename(af)); } } fflush(stdout); exit(1);}char lastchar(const char *str){ return str[strlen(str)-1];}/* Special logic for opening a FreeBSD device... */void Sleep(int msec){ usleep(msec*1000);}void make_config(char *fname){ if(access(fname,F_OK)==0){ return; // don't make the file if it exists exit(1); } FILE *f = fopen(fname,"w"); if(!f) err(1,"make_config(%s) ",fname); fputs("#\n",f); fputs("#\n",f); fputs("#\n",f); fputs("# Sample config file for aimage...\n",f); fputs("# Two commands:\n",f); fputs("# ask <segname> <question> --- asks the user a question\n",f); //fputs("# set <option> --- sets the option\n",f); fputs("#\n",f); fputs("# examples:\n",f); fputs("# ask " AF_ACQUISITION_TECHNICIAN " Your Name:\n",f); fputs("# ask " AF_CASE_NUM " Case Number:\n",f); fputs("# ask " AF_ACQUISITION_ISO_COUNTRY " Acquisition ISO Country code:\n",f); fputs("# ask " AF_ACQUISITION_NOTES " acquisition notes:\n",f); //fputs("# set --no_compress\n",f); //fputs("# set --raw\n",f); fputs("#\n",f); //fputs("# Remember, config file is processed *before* command-line options.\n",f); //fputs("# Feel free to add your own!\n",f); fclose(f); printf("%s created\n",fname); exit(0);}void process_config_questions(AFFILE *af,class imager *){ FILE *f = fopen(config_filename,"r"); // open the config file to find questions to ask if(!f) return; // get back to the user while(!feof(f)){ char buf[1024]; // a righteous buffer for questions and answers char *sep = " \t"; char *last; memset(buf,0,sizeof(buf)); fgets(buf,sizeof(buf)-1,f); char *cc = index(buf,'\n'); if(cc) *cc = '\000'; // remove the \n char *cmd = strtok_r(buf,sep,&last); if(!cmd || strcmp(cmd,"ask")!=0) continue; // not an ask command char *segname = strtok_r(NULL,sep,&last); if(!segname){ fprintf(stderr,"error in config file. No segnament name in: %s\n",buf); continue; }#ifdef HAVE_LIBREADLINE /* Ask the question and get the response */ char *val = readline(last);#else char buf2[1024]; memset(buf2,0,sizeof(buf2)); char *val = fgets(buf2,sizeof(buf2)-1,stdin);#endif /* And write the response into the segment */ af_update_seg(af,segname,0,val,strlen(val)); free(val); } fclose(f);}char *append(char *base,char *str){ base = (char *)realloc(base,strlen(base)+strlen(str)+1); strcat(base,str); return base;}/* Return 1 if the PID is running, 0 if it is not * in an operating-system independent kind of way. */int checkpid(int pid){ char buf[1024]; int found = 0; snprintf(buf,sizeof(buf),"ps %d",pid); FILE *f = popen(buf,"r"); while(!feof(f)){ char *cc = buf; fgets(buf,sizeof(buf),f); buf[sizeof(buf)-1] = 0; if(atoi(cc)==pid) found = 1; } pclose(f); return found;}/* getlock(char *infile): * See if another copy of aimage is imaging this infile... * A lock file is created in /tmp that has the PID of the aimage process. * Return 0 if okay, -1 if not okay */int getlock(class imager *im){ /* If the file exists and the PID in the file is running, * can't get the lock. */ char lockfile[MAXPATHLEN]; snprintf(lockfile,sizeof(lockfile),"/tmp/aimge.%s.lock",im->infile); if(access(lockfile,F_OK)==0){ /* Lockfile exists. Get it's pid */ char buf[1024]; FILE *f = fopen(lockfile,"r"); if(!f){ perror(lockfile); // can't read lockfile... return -1; } fgets(buf,sizeof(buf),f); buf[sizeof(buf)-1] = 0; int pid = atoi(buf); if(checkpid(pid)==0){ /* PID is not running; we can delete the lockfile */ if(unlink(lockfile)){ err(1,"could not delete lockfile %s: ",lockfile); } } /* PID is running; generate error */ errx(1,"%s is locked by process %d\n",im->infile,pid); } FILE *f = fopen(lockfile,"w"); if(!f){ err(1,"%s",lockfile); } fprintf(f,"%d\n",getpid()); // save our PID. fclose(f); return 0;}void open_logfile(const char *ifn){ /* Open the logfile, interperting ~ if necessary. */ if(ifn[0]=='~' && ifn[1]=='/'){ strlcpy(logfile_fname,getenv("HOME"),sizeof(logfile_fname)); strlcat(logfile_fname,ifn+1,sizeof(logfile_fname)); } else { strlcpy(logfile_fname,ifn,sizeof(logfile_fname)); } logfile = fopen(logfile_fname,"a");}int64 scaled_atoi(const char *arg){ int64 ret=0; int multiplier=1; char ch,junk; switch(sscanf(arg,"%"I64d"%c%c",&ret,&ch,&junk)){ case 1: return ret; // no multiplier case 2: switch(ch){ case 'g': case 'G': multiplier=1024*1024*1024;break; case 'm': case 'M': multiplier=1024*1024; break; case 'k': case 'K': multiplier=1024; break; case 'b': case 'B': multiplier=1;break; case '1':case '2':case '3': case '4':case '5':case '6': case '7':case '8':case '9':case '0': break; // no multiplier provided default: errx(1,"Specify multiplier units of g, m, k or b\n"); } break; default: errx(1,"Could not decode '%s'",arg); } return ret * multiplier;}/* * process each option character. * We do this so that options in the config file can be processed * before options passed in on the command line. */void process_option(class imager *im,char ch,char *optarg){ switch (ch) { case 'a': opt_append ++; break; case 'B': opt_beeps = 0; break; case 'd': opt_debug = atoi(optarg); if(opt_debug==0) debug_list(); break; case 'D': opt_no_dmesg=1;break; case 'e': opt_error_mode = atoi(optarg); break; case 'H': im->hash_invalid = 1; break; // don't calculate the hash case 'I': opt_no_ifconfig=1;break; case 'r': strcpy(im->fname_raw,optarg); break; case 'o': strcpy(im->fname_aff,optarg); break; case 'q': opt_quiet++; break; case 'Q': opt_silent++; break; case 'R': opt_readsectors = atoi(optarg); break; case 'z': opt_zap ++; break; case 'c': opt_recover_scan++; opt_append++; break; case 'k': opt_skip = atoi(optarg);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -