📄 label_tape.c
字号:
changername = streamers[idx].changer->changername; pos_in_changer = streamers[idx].pos_in_changer + 1; num_changer_slots = streamers[idx].changer->num_slots; num_changer_loadports = streamers[idx].changer->num_loadports; } else{ changername = NULL; pos_in_changer = num_changer_slots = num_changer_loadports = 0; }}Int32find_tape_blocksize(UChar * devicen, UChar ** read_block){ static Int32 usual_blocksizes[] = { USUAL_BLOCKSIZES }; int fd; UChar *buf = NULL; Int32 *tryable_blocksizes = NULL; Int32 found_blocksize, i, j, n, n_usual; n_usual = sizeof(usual_blocksizes) / sizeof(usual_blocksizes[0]); found_blocksize = -1; fd = -1; tryable_blocksizes = get_list_from_int_list_file(used_blocksizes_file, &n); if(tryable_blocksizes){ tryable_blocksizes = RENEWP(tryable_blocksizes, Int32, 1 + n + n_usual); memmove(tryable_blocksizes + 1, tryable_blocksizes, sizeof(Int32) * n); } else{ tryable_blocksizes = NEWP(Int32, 1 + n_usual); n = 0; } if(!tryable_blocksizes) CLEANUP; tryable_blocksizes[0] = tapeblocksize; memcpy(tryable_blocksizes + n + 1, usual_blocksizes, sizeof(Int32) * n_usual); n += n_usual + 1; for(i = 0; i < n; i++){ for(j = i + 1; j < n; j++){ /* remove duplicates */ if(tryable_blocksizes[i] == tryable_blocksizes[j]){ memmove(tryable_blocksizes + j, tryable_blocksizes + j + 1, (n - j - 1) * sizeof(tryable_blocksizes[0])); n--; j--; } } } for(i = 0; i < n; i++){ buf = ZRENEWP(buf, UChar, tryable_blocksizes[i]); if(!buf) CLEANUP; if(!IS_REGFILE(devstatb) && !IS_DIRECTORY(devstatb)){ j = system(setfilecmd); if(j < 0 || WEXITSTATUS(j)) return(-ABS(j)); } fd = open(devicen, O_RDONLY); if(fd < 0) CLEANUP; j = read(fd, buf, tryable_blocksizes[i]); if(j > 0){ found_blocksize = j; if(read_block){ *read_block = buf; buf = NULL; } break; } close(fd); fd = -1; } cleanup: if(fd >= 0) close(fd); ZFREE(tryable_blocksizes); ZFREE(buf); return(found_blocksize);}Int32set_lock_msg(LockData * lockdata, Flag optional, Flag wait_for_lock){ Int32 res; while((res = set_lock(lockdata, optional)) != BU_GOT_LOCK){ if(!wait_for_lock){ if(res != BU_LOCKED){ report_problem(T_("Error: Cannot set lock on file `%s'.\n"), lockdata->lockfile); exit(5); } if(!optional){ report_problem("%s: %s.\n %s", T_("Error"), T_("An application using the device seems to be running"), T_("(The application's lock may be overridden using the flag -F)\n")); exit(5); } else{ if(interactive) report_problem("%s: %s.\n", T_("Warning"), T_("An application using the device seems to be running")); } break; } /* TODO: handle timeouts */ ms_sleep(3 * 1000); } if(res != BU_GOT_LOCK && !optional){ report_problem(T_("Error: Cannot set lock on lockfile. Device seems to be already in use by the afbackup service.\n")); } return(res);}static Int32reuse_tapes(UChar * str){ UChar **lines = NULL, *cptr, nbuf[32]; Uns32Range *tapes_to_reuse = NULL, *acttapes = NULL, *rgptr; Int32 r = 0, i, w, num_entries = 0, num_lines = 0; int lockfd, i1; struct stat statb; KeyValuePair *allentries = NULL; lockfd = -1; massage_string(str); tapes_to_reuse = sscan_Uns32Ranges__(str, 1, num_cartridges, NULL, NULL); if(!tapes_to_reuse) return(-1); /* remove from precious tapes file */ allentries = kfile_getall(precious_tapes_file, &num_entries, KFILE_LOCKED); if(allentries){ for(i = 0; i < num_entries; i++){ w = word_count(allentries[i].value); if(w < 1){ logmsg(LOG_WARNING, T_("Warning: Unrecognized line in file `%s': %s ...\n"), precious_tapes_file, allentries[i].key); continue; } ZFREE(acttapes); acttapes = sscan_Uns32Ranges__(allentries[i].value, 1, num_cartridges, NULL, NULL); if(!acttapes) CLEANUPR(-3); acttapes = del_range_from_Uns32Ranges(acttapes, tapes_to_reuse); if(!acttapes) CLEANUPR(-4); if(len_Uns32Ranges(acttapes) > 0){ cptr = str_Uns32Ranges(acttapes, 0); if(!cptr) CLEANUPR(-5); r = kfile_insert(precious_tapes_file, allentries[i].key, cptr, KFILE_SORT | KFILE_LOCKED); free(cptr); } else{ r = kfile_delete(precious_tapes_file, allentries[i].key, KFILE_LOCKED); } if(r){ logmsg(LOG_ERR, T_("Error: Cannot rewrite file `%s' to save the precious tapes"), precious_tapes_file); CLEANUP; } } kfile_freeall(allentries, num_entries); } /* remove from read only tapes file */ if((lockfd = set_wlock_forced(ro_tapes_file)) < 0){ r = -1; CLEANUP; } lines = read_asc_file(ro_tapes_file, & num_lines); if(!lines){ r = -2; CLEANUP; } for(i = 0; i < num_lines; i++){ ZFREE(acttapes); acttapes = sscan_Uns32Ranges__(lines[i], 1, num_cartridges, NULL, NULL); if(!acttapes){ r = -7; CLEANUP; } acttapes = del_range_from_Uns32Ranges(acttapes, tapes_to_reuse); if(!acttapes){ r = -8; CLEANUP; } free(lines[i]); if(len_Uns32Ranges(acttapes) > 0){ lines[i] = str_Uns32Ranges(acttapes, 0); } else{ memmove(lines + i, lines + i + 1, sizeof(UChar *) * (num_lines - i)); num_lines--; i--; } } i = stat(ro_tapes_file, &statb); if(write_asc_file_safe(ro_tapes_file, lines, 0)){ fprintf(stderr, T_("Error: Cannot rewrite file `%s' to save the read-only tapes"), ro_tapes_file); } if(i) chown(ro_tapes_file, default_stat.st_uid, default_stat.st_gid); free_asc_file(lines, 0); lines = NULL; close(lockfd); lockfd = -1; /* remove from cartridge order file */ allentries = kfile_getall(cartorder_file, &num_entries, KFILE_LOCKED); if(allentries){ for(i = 0; i < num_entries; i++){ cptr = strstr(allentries[i].value, "->"); if(cptr && cptr == allentries[i].value){ cptr = first_nospace(cptr + 2 /* strlen("->") */); w = sscanf(cptr, "%d", &i1); if(w > 0){ if(in_Uns32Ranges(tapes_to_reuse, (Int32) i1)){ *(cptr++) = '0'; while(isdigit(*cptr) && *cptr){ *(cptr++) = ' '; } r = kfile_insert(cartorder_file, allentries[i].key, allentries[i].value, KFILE_SORTN | KFILE_LOCKED); if(r){ logmsg(LOG_ERR, T_("Error: Cannot rewrite file `%s' to save the cartridge order"), cartorder_file); CLEANUP; } } } } } } for(rgptr = tapes_to_reuse; rgptr->first <= rgptr->last; rgptr++){ for(i = rgptr->first; i <= rgptr->last; i++){ sprintf(nbuf, "%d:", (int) i); kfile_delete(bytesontape_file, nbuf, KFILE_LOCKED); } } cleanup: if(lockfd >= 0) close(lockfd); ZFREE(tapes_to_reuse); ZFREE(acttapes); kfile_freeall(allentries, num_entries); free_asc_file(lines, 0); return(r);}static Int32 /* convert to new format */rewrite_tapeposfile(){ FILE *fp; int i1, i2; UChar numstr[64]; KeyValuePair *tapeposfile_entries; Int32 num_tapeposfile_entries, i, r; fp = fopen(tapeposfile, "r"); if(fp){ if(fscanf(fp, "%d%d", &i1, &i2) >= 2){ EM__(tapeposfile_entries = NEWP(KeyValuePair, 1)); EM__(tapeposfile_entries[0].key = strdup(devicename)); sprintf(numstr, "%d %d", i1, i2); EM__(tapeposfile_entries[0].value = strdup(numstr)); num_tapeposfile_entries = 1; while(fscanf(fp, "%d%d", &i1, &i2) >= 2){ EM__(tapeposfile_entries = RENEWP(tapeposfile_entries, KeyValuePair, num_tapeposfile_entries + 1)); sprintf(numstr, "%d:", (int) num_tapeposfile_entries - 1 + 1); EM__(tapeposfile_entries[num_tapeposfile_entries].key = strdup(numstr)); sprintf(numstr, "%d %d", i1, i2); EM__(tapeposfile_entries[num_tapeposfile_entries].value = strdup(numstr)); num_tapeposfile_entries++; } fclose(fp); truncate(tapeposfile, 0); for(i = 0; i < num_tapeposfile_entries; i++){ ER__(kfile_insert(tapeposfile, tapeposfile_entries[i].key, tapeposfile_entries[i].value, KFILE_SORTN | KFILE_LOCKED), r); } kfile_freeall(tapeposfile_entries, num_tapeposfile_entries); } else fclose(fp); } return(0);}static Int32rewrite_tapeposfile_msg(){ Int32 r; if( (r = rewrite_tapeposfile()) ) fprintf(stderr, T_("Error: Rewriting the tapepos-file failed.\n")); return(r);}static Int32reset_in_tapepos(Int32 cartno){ KeyValuePair *tapeposfile_entries; Int32 num_tapeposfile_entries, i, r; int i1, i2; ER__(rewrite_tapeposfile_msg(), r); tapeposfile_entries = kfile_getall(tapeposfile, &num_tapeposfile_entries, KFILE_LOCKED); for(i = 0; i < num_tapeposfile_entries; i++){ if(sscanf(tapeposfile_entries[i].value, "%d%d", &i1, &i2) >= 2){ if(i1 == cartno){ sprintf(tapeposfile_entries[i].value, "%d %d", i1, 1); ER__(kfile_insert(tapeposfile, tapeposfile_entries[i].key, tapeposfile_entries[i].value, KFILE_SORTN | KFILE_LOCKED), r); } } } kfile_freeall(tapeposfile_entries, num_tapeposfile_entries); return(0);}static Int32delete_tape(Int32 cartno){ char buf[32]; sprintf(buf, "%d", (int) cartno); return(reuse_tapes(buf));}Uns32Range *get_readonly_tapes(){ Uns32Range *alltapes, *newtapes = NULL; FILE *fp = NULL; UChar *line = NULL, *rangestr = NULL; int fd; alltapes = empty_Uns32Ranges(); if(!alltapes) return(NULL); fd = set_wlock_forced(ro_tapes_file); if(fd < 0) CLEANUP; fp = fopen(ro_tapes_file, "r"); if(fp){ while(!feof(fp)){ ZFREE(line); line = fget_alloc_str(fp); if(!line) continue; ZFREE(rangestr); rangestr = strdup(line); if(!rangestr) GETOUT; newtapes = sscan_Uns32Ranges__(rangestr, 1, num_cartridges, NULL, NULL); if(!newtapes) break; merge_Uns32Ranges(&alltapes, newtapes); ZFREE(newtapes); } } cleanup: if(fd >= 0) close(fd); if(fp) fclose(fp); ZFREE(newtapes); ZFREE(rangestr); ZFREE(line); return(alltapes); getout: ZFREE(alltapes); CLEANUP;}static UChar *select_lockfile(UChar * lfbasen){ UChar **cpptr, *lockf = NULL; struct stat statb; for(cpptr = lockdirs; *cpptr; cpptr++){ if(!stat(*cpptr, &statb)){ if(! access(*cpptr, W_OK)){ lockf = strchain(*cpptr, FN_DIRSEPSTR, lfbasen, NULL); break; } } } if(!lockf){ fprintf(stderr, T_("Error: Cannot find an appropriate directory for lockfile `%s'.\n"), lfbasen); } return(lockf);}static voidusage(char * pnam, Int32 mode){ pnam = FN_BASENAME((UChar *) pnam); switch(mode){ case MODE_LABEL_TAPE: fprintf(stderr, T_("usage: %s <label-number> [ -rfF ] \\\n"), pnam); fprintf(stderr, T_(" [ -c <configfile> ] \\\n")); fprintf(stderr, T_(" [ -S <sec-label-number> ] \\\n")); fprintf(stderr, T_(" [ -d <devicename> ] \\\n")); fprintf(stderr, T_(" [ -b <blocksize> ] \\\n")); fprintf(stderr, T_(" [ -s <set-file-cmd> ] \\\n")); fprintf(stderr, T_(" [ -C <num-cartridges> ] \\\n")); fprintf(stderr, T_(" [ -n <comment> ]\n")); fprintf(stderr, " %s -q \\\n", pnam); fprintf(stderr, T_(" [ -c <configfile> ] \\\n")); fprintf(stderr, T_(" [ -d <devicename> ] \\\n")); fprintf(stderr, T_(" [ -b <blocksize> ] \\\n")); fprintf(stderr, T_(" [ -s <set-file-cmd> ] \\\n")); break; case MODE_CART_CTL: fprintf(stderr, T_("usage: %s [ -ilmtefFr ] \\\n"), pnam); fprintf(stderr, T_(" [ -P [ <location> ] ] \\\n")); fprintf(stderr, T_(" [ -s <serverconfigfile> ] \\\n")); fprintf(stderr, T_(" [ -c <changerconfigfile> ] \\\n")); fprintf(stderr, T_(" [ -d <changerdevice> ] \\\n")); fprintf(stderr, T_(" [ -C <cartridges> ] \\\n")); fprintf(stderr, T_(" [ -S [ <slots> ] ] \\\n")); fprintf(stderr, T_(" [ -D [ <drives> ] ] \\\n")); fprintf(stderr, T_(" [ -L [ <loadports> ] ] \\\n")); fprintf(stderr, T_(" [ -b <blocksize> ] \\\n")); fprintf(stderr, T_(" [ -n <comment> ]\n")); break; case MODE_CART_IS: fprintf(stderr, T_("usage: %s { <cartridge-number> | \\\n"), pnam); fprintf(stderr, T_(" -i <cartridge-number> <tapefile-number> } \\\n")); fprintf(stderr, T_(" [ -S <cartridge-set> ]\n")); break; } exit(1);}Int32get_prog_options(int argc, char ** argv, Int32 mode){ Int32 i; switch(mode){ case MODE_LABEL_TAPE: i = goptions(-argc, (UChar **) argv, "s:c;i-1:;s:d;i:b;s:s;i:C;b:q;s:n;i:S;b:r;b:f;b:F", &configfile, &label_given, &label, &devicename, &tapeblocksize, &setfilecmd, &num_cartridges, &query, &comment, &scnd_label, &delfromhd, &dont_ask, &forced); if(i || (!label_given && !query) || (label_given && query))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -