📄 chg-scsi.c
字号:
chg->conf[drivenum].timefile = stralloc(value); } else { g_fprintf(stderr,_("Error: drive is not less than number_drives" " usagecount ignored\n")); } break; default: g_fprintf(stderr,_("Error: Unknown token\n")); break; } } amfree(linebuffer); } amfree(linebuffer); fclose(file); return 0;}/*----------------------------------------------------------------------------*//* The tape drive does not have an idea of current slot so * we use a file to store the current slot. It is not ideal * but it gets the job done */intget_current_slot( char *count_file){ FILE *inf; int retval = -1; int ret; /* return value for the fscanf function */ if ((inf=fopen(count_file,"r")) == NULL) { g_fprintf(stderr, _("%s: unable to open (%s)\n"), get_pname(), count_file); exit(2); } ret = fscanf(inf,"%d",&retval); fclose(inf); /* * check if we got an result * if no set retval to -1 */ if (ret == 0 || ret == EOF) { retval = -1; } if (retval < 0 || retval > 10000) { retval = -1; } return retval;}voidput_current_slot( char *count_file, int slot){ FILE *inf; if (!count_file) return; if ((inf=fopen(count_file,"w")) == NULL) { g_fprintf(stderr, _("%s: unable to open current slot file (%s)\n"), get_pname(), count_file); exit(2); } g_fprintf(inf, "%d\n", slot); fclose(inf);}/* * Here we handle the inventory DB * With this it should be possible to do an mapping * Barcode -> Volume label * Volume Label -> Barcode * Volume label -> Slot number * Return Values: * 1 -> Action was ok * 0 -> Action failed * * The passed struct MBC_T will hold the found entry in the DB */intMapBarCode( char *labelfile, MBC_T *result){ FILE *fp; int version; LabelV2_T *plabelv2; long unusedpos= 0; int unusedrec = 0; int record = 0; int loop = 1; size_t rsize; long pos; int rc; DebugPrint(DEBUG_INFO,SECTION_MAP_BARCODE,_("MapBarCode : Parameter\n")); DebugPrint(DEBUG_INFO,SECTION_MAP_BARCODE,_("labelfile -> %s, vol -> %s, barcode -> %s, action -> %c, slot -> %d, from -> %d\n"), labelfile, result->data.voltag, result->data.barcode, result->action, result->data.slot, result->data.from); if (labelfile == NULL) { DebugPrint(DEBUG_ERROR,SECTION_MAP_BARCODE,_("Got empty labelfile (NULL)\n")); ChgExit("MapBarCode", _("MapBarCode name of labelfile is not set\n"),FATAL); /*NOTREACHED*/ } if (access(labelfile, F_OK) == -1) { DebugPrint(DEBUG_INFO,SECTION_MAP_BARCODE, _("MapBarCode : creating %s"), labelfile); if ((fp = fopen(labelfile, "w+")) == NULL) { DebugPrint(DEBUG_ERROR,SECTION_MAP_BARCODE,_(" failed\n")); ChgExit("MapBarCode", _("MapBarCode, creating labelfile failed\n"), FATAL); /*NOTREACHED*/ } g_fprintf(fp,":%d:", LABEL_DB_VERSION); fclose(fp); } if ((fp = fopen(labelfile, "r+")) == NULL) { DebugPrint(DEBUG_ERROR,SECTION_MAP_BARCODE,_("MapBarCode : failed to open %s\n"), labelfile); ChgExit("MapBarCode", _("MapBarCode, opening labelfile for read/write failed\n"), FATAL); /*NOTREACHED*/ } if (fscanf(fp,":%d:", &version) != 1) { ChgExit("MapBarCode", _("MapBarCode, DB Version unreadable.\n"), FATAL); /*NOTREACHED*/ } DebugPrint(DEBUG_INFO,SECTION_MAP_BARCODE,_("MapBarCode : DB version %d\n"), version); pos = ftell(fp); if (version != LABEL_DB_VERSION) { ChgExit("MapBarCode", _("MapBarCode, DB Version does not match\n"), FATAL); /*NOTREACHED*/ } if (( plabelv2 = (LabelV2_T *)alloc(SIZEOF(LabelV2_T))) == NULL) { DebugPrint(DEBUG_ERROR,SECTION_MAP_BARCODE,_("MapBarCode : alloc failed\n")); ChgExit("MapBarCode", _("MapBarCode alloc failed\n"), FATAL); /*NOTREACHED*/ } memset(plabelv2, 0, SIZEOF(LabelV2_T)); while(feof(fp) == 0 && loop == 1) { rsize = fread(plabelv2, 1, SIZEOF(LabelV2_T), fp); if (rsize == SIZEOF(LabelV2_T)) { record++; DebugPrint(DEBUG_INFO,SECTION_MAP_BARCODE,_("MapBarCode : (%d) VolTag \"%s\", BarCode %s, inuse %d, slot %d, from %d, loadcount %d\n"),record, plabelv2->voltag, plabelv2->barcode, plabelv2->valid, plabelv2->slot, plabelv2->from, plabelv2->LoadCount); switch (result->action) { /* * Only dump the info */ case BARCODE_DUMP: g_printf(_("Slot -> %d, from -> %d, valid -> %d, Tag -> %s, Barcode -> %s, Loadcount %u\n"), plabelv2->slot, plabelv2->from, plabelv2->valid, plabelv2->voltag, plabelv2->barcode, plabelv2->LoadCount ); break; /* * Set all the record to invalid, used by the Inventory function */ case RESET_VALID: plabelv2->valid = 0; if (fseek(fp, pos, SEEK_SET) == -1) { fclose(fp); amfree(plabelv2); return 0; /* Fail */ } if (fwrite(plabelv2, 1, SIZEOF(LabelV2_T), fp) < SIZEOF(LabelV2_T)) { fclose(fp); amfree(plabelv2); return 0; /* Fail */ } break; /* * Add an entry */ case BARCODE_PUT: /* * If it is an invalid record we can use it, * so save the record number. * This value is used at the end if no other * record/action matches. */ if (plabelv2->valid == 0) { unusedpos = pos; unusedrec = record; } /* * OK this record matches the barcode label * so use/update it */ if (strcmp(plabelv2->barcode, result->data.barcode) == 0) { DebugPrint(DEBUG_INFO,SECTION_MAP_BARCODE,_("MapBarCode : update entry\n")); if (fseek(fp, pos, SEEK_SET) == -1) { fclose(fp); amfree(plabelv2); return 0; /* Fail */ } plabelv2->valid = 1; plabelv2->from = result->data.from; plabelv2->slot = result->data.slot; plabelv2->LoadCount = plabelv2->LoadCount + result->data.LoadCount; strncpy(plabelv2->voltag, result->data.voltag, SIZEOF(plabelv2->voltag)); strncpy(plabelv2->barcode, result->data.barcode, SIZEOF(plabelv2->barcode)); rc = (fwrite(plabelv2, 1, SIZEOF(LabelV2_T), fp) < SIZEOF(LabelV2_T)); fclose(fp); amfree(plabelv2); return(rc); } break; /* * Look for an entry an return the entry * if the voltag (the tape name) matches */ case FIND_SLOT: if (strcmp(plabelv2->voltag, result->data.voltag) == 0) { DebugPrint(DEBUG_INFO,SECTION_MAP_BARCODE,_("MapBarCode FIND_SLOT : \n")); memcpy(&(result->data), plabelv2, SIZEOF(LabelV2_T)); amfree(plabelv2); return(1); } break; /* * Update the entry, * reason can be an load, incr the LoadCount * or an new tape */ case UPDATE_SLOT: if (strcmp(plabelv2->voltag, result->data.voltag) == 0) { DebugPrint(DEBUG_INFO,SECTION_MAP_BARCODE,_("MapBarCode UPDATE_SLOT : update entry\n")); if (fseek(fp, pos, SEEK_SET) == -1) { fclose(fp); amfree(plabelv2); return 0; /* Fail */ } strncpy(plabelv2->voltag, result->data.voltag, SIZEOF(plabelv2->voltag)); strncpy(plabelv2->barcode, result->data.barcode, SIZEOF(plabelv2->barcode)); plabelv2->valid = 1; plabelv2->slot = result->data.slot; plabelv2->from = result->data.from; plabelv2->LoadCount = plabelv2->LoadCount + result->data.LoadCount; rc = (fwrite(plabelv2, 1, SIZEOF(LabelV2_T), fp) < SIZEOF(LabelV2_T)); fclose(fp); amfree(plabelv2); return(rc); } break; /* * Look for the barcode label of an given volume label * return the slot number and the barcode label. * If the entry is not valid return -1 as slot number */ case BARCODE_VOL: /* * DebugPrint(DEBUG_INFO,SECTION_MAP_BARCODE,_("MapBarCode: (%d) inside BARCODE_VOL\n"), record); DebugPrint(DEBUG_INFO,SECTION_MAP_BARCODE,_("file value: %s, searched for value: %s\n"), plabelv2->voltag, result->data.voltag); */ if (strcmp(plabelv2->voltag, result->data.voltag) == 0) { DebugPrint(DEBUG_INFO,SECTION_MAP_BARCODE,_("MapBarCode : VOL %s match\n"), result->data.voltag); fclose(fp); memcpy(&(result->data), plabelv2, SIZEOF(LabelV2_T)); amfree(plabelv2); return(1); } break; /* * Look for an entry which matches the passed * barcode label */ case BARCODE_BARCODE: if (strcmp(plabelv2->barcode, result->data.barcode) == 0) { DebugPrint(DEBUG_INFO,SECTION_MAP_BARCODE,_("MapBarCode : BARCODE %s match\n"), result->data.barcode); fclose(fp); memcpy(&(result->data), plabelv2, SIZEOF(LabelV2_T)); amfree(plabelv2); return(1); } break; default: DebugPrint(DEBUG_INFO,SECTION_MAP_BARCODE,_("MapBarCode : unknown action\n")); break; } pos = ftell(fp); } else { DebugPrint(DEBUG_INFO,SECTION_MAP_BARCODE,_("MapBarCode : feof (%d)\n"), feof(fp)); DebugPrint(DEBUG_INFO,SECTION_MAP_BARCODE,_("MapBarCode : error in read record expect %d, got %d\n"),SIZEOF(LabelV2_T), rsize); loop=0; } } /* * OK, if we come here and the action is either * PUT or update it seems that we have to create a new * record, becuae none of the exsisting records matches */ if (result->action == BARCODE_PUT || result->action == UPDATE_SLOT ) { /* * If we have an entry where the valid flag was set to 0 * we can use this record, so seek to this position * If we have no record for reuse the new record will be written to the end. */ if (unusedpos != 0) { DebugPrint(DEBUG_INFO,SECTION_MAP_BARCODE,_("MapBarCode : reuse record %d\n"), unusedrec); if (fseek(fp, unusedpos, SEEK_SET) == -1) { fclose(fp); amfree(plabelv2); return 0; /* Fail */ } } /* * Set all values to zero */ memset(plabelv2, 0, SIZEOF(LabelV2_T)); strncpy(plabelv2->voltag, result->data.voltag, SIZEOF(plabelv2->voltag)); strncpy(plabelv2->barcode, result->data.barcode, SIZEOF(plabelv2->barcode)); plabelv2->valid = 1; plabelv2->from = result->data.from; plabelv2->slot = result->data.slot; rc = (fwrite(plabelv2, 1, SIZEOF(LabelV2_T), fp) < SIZEOF(LabelV2_T)); fclose(fp); amfree(plabelv2); return(rc); } /* * If we hit this point nothing was * found, so return an 0 */ fclose(fp); amfree(plabelv2); return(0);}/* ---------------------------------------------------------------------- This stuff deals with parsing the command line */typedef struct com_arg{ char *str; int command_code; int takesparam;} argument;typedef struct com_stru{ int command_code; char *parameter;} command;void parse_args(int argc, char *argv[],command *rval);/* major command line args */#define COMCOUNT 13#define COM_SLOT 0#define COM_INFO 1#define COM_RESET 2#define COM_EJECT 3#define COM_CLEAN 4#define COM_LABEL 5#define COM_SEARCH 6#define COM_STATUS 7#define COM_TRACE 8#define COM_INVENTORY 9#define COM_DUMPDB 10#define COM_SCAN 11#define COM_GEN_CONF 12argument argdefs[]={{"-slot",COM_SLOT,1}, {"-info",COM_INFO,0}, {"-reset",COM_RESET,0}, {"-eject",COM_EJECT,0}, {"-clean",COM_CLEAN,0}, {"-label",COM_LABEL,1}, {"-search",COM_SEARCH,1}, {"-status",COM_STATUS,1}, {"-trace",COM_TRACE,1}, {"-inventory", COM_INVENTORY,0}, {"-dumpdb", COM_DUMPDB,0}, {"-scan", COM_SCAN,0}, {"-genconf", COM_GEN_CONF,0} };/* minor command line args */#define SLOT_CUR 0#define SLOT_NEXT 1#define SLOT_PREV 2#define SLOT_FIRST 3#define SLOT_LAST 4#define SLOT_ADVANCE 5argument slotdefs[]={{"current",SLOT_CUR,0}, {"next",SLOT_NEXT,0}, {"prev",SLOT_PREV,0}, {"first",SLOT_FIRST,0}, {"last",SLOT_LAST,0}, {"advance",SLOT_ADVANCE,0}, };#define SLOTCOUNT (int)(sizeof(slotdefs) / sizeof(slotdefs[0]))/* is the string a valid positive int? */intis_positive_number( char *tmp){ int i=0; if ((tmp==NULL)||(tmp[0]==0)) return 0; while ((tmp[i]>='0')&&(tmp[i]<='9')&&(tmp[i]!=0)) i++; if (tmp[i]==0) return 1; else return 0;}voidusage( char *argv[]){ int cnt; g_printf(_("%s: Usage error.\n"), argv[0]); for (cnt=0; cnt < COMCOUNT; cnt++){ g_printf(" %s %s",argv[0],argdefs[cnt].str); if (argdefs[cnt].takesparam) g_printf(" <param>\n"); else g_printf("\n"); } exit(2);}voidparse_args( int argc,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -