2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 1991-1998 University of Maryland at College Park
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of U.M. not be used in advertising or
11 * publicity pertaining to distribution of the software without specific,
12 * written prior permission. U.M. makes no representations about the
13 * suitability of this software for any purpose. It is provided "as is"
14 * without express or implied warranty.
16 * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
18 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 * Authors: the Amanda Development Team. Its members are listed in a
24 * file named AUTHORS, in the root directory of this distribution.
27 * $Id: amtape.c,v 1.47 2006/07/25 18:27:57 martinea Exp $
29 * tape changer interface program
34 #include "taperscan.h"
39 #include "timestamp.h"
42 static void usage(void);
43 int main(int argc, char **argv);
44 static void reset_changer(int argc, char **argv);
45 static void eject_tape(int argc, char **argv);
46 static void clean_tape(int argc, char **argv);
47 static void load_slot(int argc, char **argv);
48 static void load_label(int argc, char **argv);
49 static void show_slots(int argc, char **argv);
50 static void show_current(int argc, char **argv);
51 static void amtape_taper_scan(int argc, char **argv);
52 static void show_device(int argc, char **argv);
53 static int loadlabel_slot(void *ud, int rc, char *slotstr, char *device);
54 int show_init(void *ud, int rc, int ns, int bk, int s);
55 static int show_slots_slot(void *ud, int rc, char *slotstr, char *device);
59 void (*fn)(int, char **);
62 { "reset", reset_changer,
63 T_("reset Reset changer to known state") },
64 { "eject", eject_tape,
65 T_("eject Eject current tape from drive") },
66 { "clean", clean_tape,
67 T_("clean Clean the drive") },
69 T_("show Show contents of all slots") },
70 { "current", show_current,
71 T_("current Show contents of current slot") },
73 T_("slot <slot #> load tape from slot <slot #>") },
75 T_("slot current load tape from current slot") },
77 T_("slot prev load tape from previous slot") },
79 T_("slot next load tape from next slot") },
81 T_("slot advance advance to next slot but do not load") },
83 T_("slot first load tape from first slot") },
85 T_("slot last load tape from last slot") },
86 { "label", load_label,
87 T_("label <label> find and load labeled tape") },
88 { "taper", amtape_taper_scan,
89 T_("taper perform taper's scan alg.") },
90 { "device", show_device,
91 T_("device show current tape device") },
92 { "update", show_slots,
93 T_("update update the label matchingdatabase")},
95 #define NCMDS (int)(sizeof(cmdtab) / sizeof(cmdtab[0]))
102 g_fprintf(stderr, _("Usage: amtape%s <conf> <command> {<args>} [-o configoption]*\n"), versionsuffix());
103 g_fprintf(stderr, _("\tValid commands are:\n"));
104 for (i = 0; i < NCMDS; i++)
105 g_fprintf(stderr, "\t\t%s\n", _(cmdtab[i].usage));
117 config_overwrites_t *cfg_ovr = NULL;
120 * Configure program for internationalization:
121 * 1) Only set the message locale for now.
122 * 2) Set textdomain for all amanda related programs to "amanda"
123 * We don't want to be forced to support dozens of message catalogs.
125 setlocale(LC_MESSAGES, "C");
126 textdomain("amanda");
133 /* Don't die when child closes pipe */
134 signal(SIGPIPE, SIG_IGN);
136 dbopen(DBG_SUBDIR_SERVER);
138 erroutput_type = ERR_INTERACTIVE;
140 cfg_ovr = extract_commandline_config_overwrites(&argc, &argv);
141 if(argc < 3) usage();
143 config_init(CONFIG_INIT_EXPLICIT_NAME | CONFIG_INIT_FATAL,
145 apply_config_overwrites(cfg_ovr);
147 check_running_as(RUNNING_AS_DUMPUSER);
149 dbrename(config_name, DBG_SUBDIR_SERVER);
151 conf_tapelist = config_dir_relative(getconf_str(CNF_TAPELIST));
152 if (read_tapelist(conf_tapelist)) {
153 error(_("could not load tapelist \"%s\""), conf_tapelist);
156 amfree(conf_tapelist);
158 if((have_changer = changer_init()) == 0) {
159 error(_("no tpchanger specified in \"%s\""), config_filename);
161 } else if (have_changer != 1) {
162 error(_("changer initialization failed: %s"), strerror(errno));
166 /* switch on command name */
168 argc -= 2; argv += 2;
169 for (i = 0; i < NCMDS; i++)
170 if (strcmp(argv[0], cmdtab[i].name) == 0) {
171 (*cmdtab[i].fn)(argc, argv);
175 g_fprintf(stderr, _("%s: unknown command \"%s\"\n"), get_pname(), argv[0]);
179 amfree(changer_resultstr);
185 /* ---------------------------- */
188 reset_changer(G_GNUC_UNUSED int argc,
189 G_GNUC_UNUSED char ** argv) {
190 char *slotstr = NULL;
192 switch(changer_reset(&slotstr)) {
194 g_fprintf(stderr, _("%s: changer is reset, slot %s is loaded.\n"),
195 get_pname(), slotstr);
198 g_fprintf(stderr, _("%s: changer is reset, but slot %s not loaded: %s\n"),
199 get_pname(), slotstr, changer_resultstr);
202 error(_("could not reset changer: %s"), changer_resultstr);
209 /* ---------------------------- */
211 clean_tape(G_GNUC_UNUSED int argc,
212 G_GNUC_UNUSED char ** argv) {
215 if(changer_clean(&devstr) == 0) {
216 g_fprintf(stderr, _("%s: device %s is clean.\n"), get_pname(), devstr);
218 g_fprintf(stderr, _("%s: device %s not clean: %s\n"),
219 get_pname(), devstr ? devstr : "??", changer_resultstr);
225 /* ---------------------------- */
227 eject_tape(G_GNUC_UNUSED int argc,
228 G_GNUC_UNUSED char ** argv) {
229 char *slotstr = NULL;
231 if(changer_eject(&slotstr) == 0) {
232 g_fprintf(stderr, _("%s: slot %3s is ejected.\n"), get_pname(), slotstr);
234 g_fprintf(stderr, _("%s: slot %3s not ejected: %s\n"),
235 get_pname(), slotstr ? slotstr : "??", changer_resultstr);
241 /* ---------------------------- */
248 char *slotstr = NULL, *devicename = NULL;
257 is_advance = (strcmp(argv[1], "advance") == 0);
258 if(changer_loadslot(argv[1], &slotstr, &devicename)) {
259 error(_("could not load slot %s: %s"), slotstr, changer_resultstr);
264 device = device_open(devicename);
265 if (device == NULL) {
267 _("%s: could not open device %s"), get_pname(),
270 g_object_unref(device);
274 g_fprintf(stderr, _("%s: changed to slot %s"), get_pname(), slotstr);
276 g_fprintf(stderr, _(" on %s"), devicename);
284 /* ---------------------------- */
286 /* This initalizes the ChangerStatus structure for all commands that
287 use changer_find; namely, show_slots, load_label, and
290 scan_init(G_GNUC_UNUSED void * data, int rc, G_GNUC_UNUSED int numslots,
291 G_GNUC_UNUSED int backwards, G_GNUC_UNUSED int searchable) {
293 error(_("could not get changer info: %s"), changer_resultstr);
300 static int scan_init_print(void * data, int rc, int numslots,
301 int backwards, int searchable) {
303 g_fprintf(stderr, _("%s: scanning all %d slots in tape-changer rack:\n"),
304 get_pname(), numslots);
306 return scan_init(data, rc, numslots, backwards, searchable);
312 } LabelChangerStatus;
314 /* This is the 'user_slot' callback for the 'load label' command. */
322 LabelChangerStatus * status = ud;
324 ReadLabelStatusFlags label_status;
327 error(_("could not load slot %s: %s"), slotstr, changer_resultstr);
328 g_assert_not_reached();
329 } else if (rc == 1) {
330 g_fprintf(stderr, _("%s: slot %3s: %s\n"),
331 get_pname(), slotstr, changer_resultstr);
335 device = device_open(device_name);
336 if (device == NULL) {
337 g_fprintf(stderr, _("%s: slot %3s: Could not open device.\n"),
338 get_pname(), slotstr);
342 device_set_startup_properties_from_config(device);
344 label_status = device_read_label(device);
345 if (label_status != READ_LABEL_STATUS_SUCCESS) {
347 g_english_strjoinv_and_free
348 (g_flags_nick_to_strv(label_status,
349 READ_LABEL_STATUS_FLAGS_TYPE), "or");
350 g_fprintf(stderr, _("%s: slot %3s: %s\n"),
351 get_pname(), slotstr, errstr);
352 g_object_unref(device);
356 g_fprintf(stderr, _("%s: slot %3s: time %-14s label %s"),
357 get_pname(), slotstr, device->volume_time, device->volume_label);
359 if(strcmp(device->volume_label, status->searchlabel) != 0) {
360 g_fprintf(stderr, _(" (wrong tape)\n"));
361 g_object_unref(device);
364 g_fprintf(stderr, _(" (exact label match)\n"));
365 g_object_unref(device);
370 g_assert_not_reached();
373 /* This does the 'load label' command. */
379 LabelChangerStatus status;
386 status.searchlabel = argv[1];
388 g_fprintf(stderr, _("%s: scanning for tape with label %s\n"),
389 get_pname(), status.searchlabel);
393 changer_find(&status, scan_init, loadlabel_slot, status.searchlabel);
396 g_fprintf(stderr, _("%s: label %s is now loaded.\n"),
397 get_pname(), status.searchlabel);
399 g_fprintf(stderr, _("%s: could not find label %s in tape rack.\n"),
400 get_pname(), status.searchlabel);
404 /* ---------------------------- */
406 /* This is the user_slot function for "amtape show". */
408 show_slots_slot(G_GNUC_UNUSED void * data, int rc, char * slotstr,
414 error(_("could not load slot %s: %s"), slotstr, changer_resultstr);
415 g_assert_not_reached();
419 g_fprintf(stderr, _("slot %s: %s\n"), slotstr, changer_resultstr);
423 device = device_open(device_name);
424 if (device == NULL) {
425 g_fprintf(stderr, _("%s: slot %3s: Could not open device.\n"),
426 get_pname(), slotstr);
428 ReadLabelStatusFlags label_status;
429 device_set_startup_properties_from_config(device);
430 label_status = device_read_label(device);
432 if (label_status != READ_LABEL_STATUS_SUCCESS) {
434 g_english_strjoinv_and_free
435 (g_flags_nick_to_strv(label_status,
436 READ_LABEL_STATUS_FLAGS_TYPE), "or");
437 g_fprintf(stderr, _("%s: slot %3s: %s\n"),
438 get_pname(), slotstr, errstr);
440 g_fprintf(stderr, _("slot %3s: time %-14s label %s\n"),
441 slotstr, device->volume_time, device->volume_label);
443 /* update the changer db */
444 changer_label(slotstr, device->volume_label);
449 g_object_unref(device);
455 show_current(int argc, G_GNUC_UNUSED char ** argv) {
461 g_fprintf(stderr, _("%s: scanning current slot in tape-changer rack:\n"),
463 changer_current(NULL, scan_init, show_slots_slot);
467 show_slots(int argc, G_GNUC_UNUSED char ** argv) {
472 changer_find(NULL, scan_init_print, show_slots_slot, NULL);
476 /* ---------------------------- */
479 amtape_taper_scan(G_GNUC_UNUSED int argc, G_GNUC_UNUSED char ** argv) {
483 char *timestamp = NULL;
484 char *tapedev = NULL;
489 g_fprintf(stderr, _("%s: scanning for "), get_pname());
491 tp = lookup_last_reusable_tape(0);
495 searchlabel = stralloc(tp->label);
496 g_fprintf(stderr, _("tape label %s or "), searchlabel);
498 g_fprintf(stderr, _("a new tape.\n"));
500 result = taper_scan(searchlabel, &label, ×tamp, &tapedev, NULL,
501 FILE_taperscan_output_callback, stderr, NULL, NULL);
504 g_fprintf(stderr, _("Could not find a non-active Amanda tape.\n"));
506 g_fprintf(stderr, _("Tape with label `%s' is now loaded.\n"), label);
508 } else if (result == 3) {
509 g_fprintf(stderr, _("New tape loaded, it will be labelled `%s'.\n"),
512 g_fprintf(stderr, _("Tape with label `%s' is now loaded.\n"), label);
521 /* ---------------------------- */
524 show_device(G_GNUC_UNUSED int argc,
525 G_GNUC_UNUSED char ** argv) {
526 char *slot = NULL, *device = NULL;
528 if(changer_loadslot(_("current"), &slot, &device)) {
529 error(_("Could not load current slot.\n"));
533 g_printf("%s\n", device);