📄 label_tape.c
字号:
} } } } omode = O_WRONLY | O_BINARY; if(IS_DIRECTORY(statb)) omode |= O_CREAT; fd = open(devicename, omode, 0600); if(fd < 0){ fprintf(stderr, T_("Error: Cannot open tape device `%s'.\n"), devicename); return(4); } sprintf(infobuffer, "\n%s\n\n%s%d\n\n%s%d\n\n%s%s\n\n%s%d\n\n%s\n\n%s%s\n", INFOHEADER, CARTNOTEXT, (int) label, CART2NOTEXT, (int) scnd_label, DATETEXT, actimestr(), BLOCKSIZETEXT, (int) tapeblocksize, VERSIONTEXT, COMMENTTEXT, comment); if(write(fd, infobuffer, infobuffersize) < infobuffersize){ fprintf(stderr, T_("Error: The write to tape failed.\n")); return(6); } reset_in_tapepos(label); i = stat(bytesontape_file, &statb); if(save_bytes_per_tape(bytesontape_file, label, infobuffersize, 0, NO, time(NULL))){ fprintf(stderr, T_("Error: Cannot write file %s to save the number of bytes on tape.\n"), bytesontape_file); return(10); } if(i) chown(bytesontape_file, default_stat.st_uid, default_stat.st_gid); close(fd); i = stat(used_blocksizes_file, &statb); add_to_int_list_file(used_blocksizes_file, tapeblocksize); if(i) chown(used_blocksizes_file, default_stat.st_uid, default_stat.st_gid); } i = system(setfilecmd); if(i < 0 || WEXITSTATUS(i)){ fprintf(stderr, T_("Warning: Cannot rewind tape.\n")); } release_lock(&devicelock); if(waitlockfd >= 0) close(waitlockfd); return(0);}Int32poll_changer_cmd(UChar * cmd, Flag wait_success, Flag quiet){ int res, i; time_t t, t_mail, t_giveup; FILE *fp; res = system(cmd); if((res < 0 || WEXITSTATUS(res)) && wait_success){ t = time(NULL); t_mail = t + devunavail_send_mail * 60; t_giveup = t + devunavail_give_up * 60; do{ if(t_mail > 0 && t > t_mail && devunavail_send_mail > 0){ fp = tmp_fp(NULL); if(fp){ fprintf(fp, T_("The device %s on host %s is not ready for use.\n"), changername, unam.nodename); fprintf(fp, T_("You are requested to check the changer for possible\n")); fprintf(fp, T_("errors and to correct them.\n\n")); fprintf(fp, T_("Best regards from your backup service.\n")); i = message_to_operator(fp, YES); } if(i || !fp){ fprintf(stderr, T_("Error: Unable to ask user %s to check device availability.\n"), user_to_inform); } t_mail = 0; } if(t_giveup > 0 && t > t_giveup && devunavail_give_up > 0){ fprintf(stderr, T_("Warning: Changer command timed out.\n")); return((Int32) res); } ms_sleep(1000 * 30); t += 30; res = system(cmd); } while(res < 0 || WEXITSTATUS(res)); } if(res && !quiet){ if(interactive){ fp = stderr; } else{ fp = tmp_fp(NULL); if(!fp){ fprintf(stderr, T_("Error: Cannot send mail to report operate cartridge error.\n")); } else{ fprintf(fp, "%s\n\n", T_("Hello,")); } } fprintf(fp, T_("The command to operate the cartridge in changer %s\n" "failed with status %d.\n"), changername, (int) res); if(!interactive){ fprintf(fp, T_("You are requested to check the changer for possible\n")); fprintf(fp, T_("errors and to correct them.\n\n")); fprintf(fp, T_("Best regards from your backup service.\n")); if(message_to_operator(fp, YES)) fprintf(stderr, T_("Error: Cannot send mail to report operate cartridge error.\n")); } } return(res);}static voidreport_problem(UChar * fmt, ...){ va_list args; FILE *fp; va_start(args, fmt); if(interactive){ fp = stderr; } else{ fp = tmp_fp(NULL); if(!fp){ fprintf(stderr, T_("Error: Cannot send mail to report problem.\n")); fp = stderr; } else{ fprintf(fp, "Hello,\n\n"); } } vfprintf(fp, fmt, args); if(!interactive){ fprintf(fp, "\nRegards from your backup system\n"); } va_end(args); if(message_to_operator(fp, YES)) fprintf(stderr, T_("Error: Cannot send mail to report problem.\n"));}static Uns32Range *read_ranges_from_cmd(UChar * cmd){ Uns32Range *ranges = NULL, *more_ranges = NULL; UChar *line = NULL; FILE *fp; if(!cmd) return(NULL); fp = popen(cmd, "r"); if(!fp) GETOUT; while(!feof(fp)){ ZFREE(line); line = fget_alloc_str(fp); if(!line) continue; ZFREE(more_ranges); more_ranges = sscan_Uns32Ranges__(line, 1, MAXINT, NULL, NULL); merge_Uns32Ranges(&ranges, more_ranges); } cleanup: if(fp){ pclose(fp); } ZFREE(line); ZFREE(more_ranges); return(ranges); getout: ZFREE(ranges); CLEANUP;}static Uns32Range *get_free_slots(UChar * changer){ return(read_ranges_from_cmd(freeslots_cmd));}static Uns32Range *get_free_loadports(UChar * changer){ return(read_ranges_from_cmd(freeloadports_cmd));}static Int32get_first_from_range(Uns32Range * range){ Int32 f; if(!range) return(-1); if(num_Uns32Ranges(range) < 1) f = 0; else f = range[0].first; free(range); return(f);}#define get_first_free_slot(changer) \ get_first_from_range(get_free_slots(changer))#define get_first_free_loadport(changer) \ get_first_from_range(get_free_loadports(changer))static Int32set_location( Int32 cartnum, Int32 type, UChar *changer_or_text, Int32 num){ UChar *lbuf = NULL, nbuf[30]; Int32 r; sprintf(nbuf, "%d:", (int) cartnum); if(type == LOC_UNKNOWN) return(kfile_delete(cartloc_file, nbuf, KFILE_SORTN | KFILE_LOCKED)); if(type != LOC_EXTERNAL){ EM__(lbuf = NEWP(UChar, strlen(changer_or_text) + 60)); sprintf(lbuf, "%s %s %d", changer_or_text, loc_strings[type], (int) num); changer_or_text = lbuf; } r = kfile_insert(cartloc_file, nbuf, changer_or_text, KFILE_SORTN | KFILE_LOCKED); ZFREE(lbuf); return(r);}static Int32set_location_msg( Int32 cartnum, Int32 type, UChar *changer_or_text, Int32 num){ Int32 i; i = set_location(cartnum, type, changer_or_text, num); if(i){ report_problem(T_("Error: Writing the new location to file `%s' failed.\n"), cartloc_file); } return(i);}static Int32get_location( Int32 cartnum, Int32 *rtype, UChar **changer_or_text, Int32 *rnum){ Int32 type; /* uninitialized OK */ Int32 num, i; UChar *entry, nbuf[30], *cptr, *r_chgr_txt = NULL; int i1, n1; struct stat statb; sprintf(nbuf, "%d:", (int) cartnum); entry = kfile_get(cartloc_file, nbuf, KFILE_LOCKED); if(!entry){ if(rtype) *rtype = LOC_UNKNOWN; if(changer_or_text) EM__(*changer_or_text = strdup(T_("unknown"))); return(0); } num = -1; if(word_count(entry) == 3){ r_chgr_txt = strword(entry, 0); if(r_chgr_txt){ i = stat(r_chgr_txt, &statb); if(!i){ cptr = strword(entry, 1); if(cptr){ for(i = 0; i < NUM_LOC_CMD; i++){ if(!strcmp(cptr, loc_strings[i])){ type = i; break; } } if(i < NUM_LOC_CMD){ free(cptr); cptr = strword(entry, 2); if(cptr){ i = sscanf(cptr, "%d%n", &i1, &n1); if(i > 0 && n1 == strlen(cptr)) num = i1; /* poooh */ } } ZFREE(cptr); } } } } if(num > 0){ if(rtype) *rtype = type; if(rnum) *rnum = num; if(changer_or_text) *changer_or_text = r_chgr_txt; } else{ ZFREE(r_chgr_txt); if(changer_or_text) *changer_or_text = entry; if(rtype) *rtype = LOC_EXTERNAL; } return(0);}static Int32genmove( Int32 cartnum, /* this is just to inform a maintainer */ Int32 sourcetype, Int32 sourcenum, UChar *src_changer_or_text, Int32 targettype, Int32 targetnum, UChar *tgt_changer_or_text){ UChar *cmd, *cptr, c, nbuf[30]; Int32 i, j, free_loadport = 0, free_slot = 0; FILE *fp; int fd; if(sourcetype == targettype && (sourcenum == targetnum || sourcetype == LOC_EXTERNAL || sourcetype == LOC_UNKNOWN) && !strcmp(src_changer_or_text, tgt_changer_or_text)) return(0); if(sourcetype == LOC_EXTERNAL || sourcetype == LOC_UNKNOWN || targettype == LOC_EXTERNAL || targettype == LOC_UNKNOWN){ if(num_changer_loadports) free_loadport = get_first_free_loadport(changername); if(num_changer_slots) free_slot = get_first_free_slot(changername); } if(sourcetype == LOC_EXTERNAL || sourcetype == LOC_UNKNOWN || targettype == LOC_EXTERNAL){ if(targettype == LOC_EXTERNAL){ /* if target is external and there is a loadport, */ /* first move the cartridge to the load port */ if(sourcetype != LOC_EXTERNAL && sourcetype != LOC_LOADPORT && sourcetype != LOC_UNKNOWN){ if(cartmove_cmds[sourcetype][LOC_LOADPORT] && free_loadport > 0){ ER__(genmove(cartnum, sourcetype, sourcenum, src_changer_or_text, LOC_LOADPORT, free_loadport, src_changer_or_text), i); sourcetype = LOC_LOADPORT; sourcenum = free_loadport; } else if(sourcetype == LOC_DRIVE && cartmove_cmds[sourcetype][LOC_SLOT] && free_slot > 0){ ER__(genmove(cartnum, sourcetype, sourcenum, src_changer_or_text, LOC_SLOT, free_slot, src_changer_or_text), i); sourcetype = LOC_SLOT; sourcenum = free_slot; } } } if(interactive){ fp = stdout; } else{ fp = tmp_fp(NULL); if(!fp){ fprintf(stderr, T_("Error: Cannot send mail to request manual cartridge operation.\n")); return(24); } else{ fprintf(fp, "%s\n\n", T_("Hello,")); } } fprintf(fp, T_("The cartridge %d must be moved manually from location\n"), (int) cartnum); switch(sourcetype){ case LOC_UNKNOWN: case LOC_EXTERNAL: fprintf(fp, " %s\n", src_changer_or_text); break; case LOC_DRIVE: case LOC_SLOT: case LOC_LOADPORT: fprintf(fp, T_(" %s %d in changer `%s'\n"), T_(loc_strings[sourcetype]), (int) sourcenum, changername); break; } fprintf(fp, T_("to the location\n")); switch(targettype){ case LOC_EXTERNAL: fprintf(fp, " %s\n", tgt_changer_or_text); break; case LOC_DRIVE: if(cartmove_cmds[LOC_LOADPORT][targettype] && free_loadport > 0){ fprintf(fp, T_(" %s %d\n"), T_(loc_strings[LOC_LOADPORT]), (int) free_loadport); break; } if(cartmove_cmds[LOC_SLOT][targettype] && free_slot > 0){ fprintf(fp, T_(" %s %d\n"), T_(loc_strings[LOC_SLOT]), (int) free_slot); break; } fprintf(fp, T_(" %s %d\n"), T_(loc_strings[targettype]), (int) targetnum); fprintf(fp, T_("If this is not possible, please remove a cartridge from a slot,\n" "if there is no free one, and put cartridge %d into a free slot.\n" "Then tell the backup system, in what slot the cartridge is, using\n" "cart_ctl with options -P, -C and -S.\n"), (int) cartnum); break; case LOC_SLOT: if(cartmove_cmds[LOC_LOADPORT][targettype] && free_loadport > 0){ fprintf(fp, T_(" %s %d\n"), T_(loc_strings[LOC_LOADPORT]), (int) free_loadport); break; } case LOC_LOADPORT: fprintf(fp, T_(" %s %d\n"), T_(loc_strings[targettype]), (int) targetnum); break; } fprintf(fp, T_("Please perform the requested operation and when done:\n")); if(interactive){ fprintf(fp, T_("Press the Return key.\n")); forever{ i = getinchr(&c, 0); if(i > 0 && c == '\n') break; if(i <= 0) return(23); } } else{ fd = open(CHANGERREADY_FILE, O_CREAT | O_WRONLY, 0600); if(fd < 0){ fprintf(stderr, T_("Error: Cannot create flag file `%s' to wait for cartridge ready"), CHANGERREADY_FILE); return(23); } close(fd); fprintf(fp, T_("Run the command:\n\n changerready\n\n on host %s\n"), unam.nodename); if(message_to_operator(fp, YES)) fprintf(stderr, T_("Error: Cannot send mail to ask operator to move a cartridge manually.\n")); while(!access(CHANGERREADY_FILE, F_OK)) ms_sleep(1000); } i = get_location(cartnum, &j, &src_changer_or_text, &sourcenum); if(i){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -