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 next load tape from next slot") },
79 T_("slot advance advance to next slot but do not load") },
81 T_("slot first load tape from first slot") },
82 { "label", load_label,
83 T_("label <label> find and load labeled tape") },
84 { "taper", amtape_taper_scan,
85 T_("taper perform taper's scan alg.") },
86 { "device", show_device,
87 T_("device show current tape device") },
88 { "update", show_slots,
89 T_("update update the label matchingdatabase")},
91 #define NCMDS (int)(sizeof(cmdtab) / sizeof(cmdtab[0]))
98 g_fprintf(stderr, _("Usage: amtape%s <conf> <command> {<args>} [-o configoption]*\n"), versionsuffix());
99 g_fprintf(stderr, _("\tValid commands are:\n"));
100 for (i = 0; i < NCMDS; i++)
101 g_fprintf(stderr, "\t\t%s\n", _(cmdtab[i].usage));
113 config_overwrites_t *cfg_ovr = NULL;
116 * Configure program for internationalization:
117 * 1) Only set the message locale for now.
118 * 2) Set textdomain for all amanda related programs to "amanda"
119 * We don't want to be forced to support dozens of message catalogs.
121 setlocale(LC_MESSAGES, "C");
122 textdomain("amanda");
129 /* Don't die when child closes pipe */
130 signal(SIGPIPE, SIG_IGN);
132 dbopen(DBG_SUBDIR_SERVER);
134 erroutput_type = ERR_INTERACTIVE;
136 cfg_ovr = extract_commandline_config_overwrites(&argc, &argv);
137 if(argc < 3) usage();
139 config_init(CONFIG_INIT_EXPLICIT_NAME, argv[1]);
140 apply_config_overwrites(cfg_ovr);
142 if (config_errors(NULL) >= CFGERR_WARNINGS) {
143 config_print_errors();
144 if (config_errors(NULL) >= CFGERR_ERRORS) {
145 g_critical(_("errors processing config file"));
149 check_running_as(RUNNING_AS_DUMPUSER);
151 dbrename(get_config_name(), DBG_SUBDIR_SERVER);
153 conf_tapelist = config_dir_relative(getconf_str(CNF_TAPELIST));
154 if (read_tapelist(conf_tapelist)) {
155 error(_("could not load tapelist \"%s\""), conf_tapelist);
158 amfree(conf_tapelist);
160 if((have_changer = changer_init()) == 0) {
161 error(_("no tpchanger specified in \"%s\""), get_config_filename());
163 } else if (have_changer != 1) {
164 error(_("changer initialization failed: %s"), strerror(errno));
168 /* switch on command name */
170 argc -= 2; argv += 2;
171 for (i = 0; i < NCMDS; i++)
172 if (strcmp(argv[0], cmdtab[i].name) == 0) {
173 (*cmdtab[i].fn)(argc, argv);
177 g_fprintf(stderr, _("%s: unknown command \"%s\"\n"), get_pname(), argv[0]);
181 amfree(changer_resultstr);
187 /* ---------------------------- */
190 reset_changer(G_GNUC_UNUSED int argc,
191 G_GNUC_UNUSED char ** argv) {
192 char *slotstr = NULL;
194 switch(changer_reset(&slotstr)) {
196 g_fprintf(stderr, _("%s: changer is reset, slot %s is loaded.\n"),
197 get_pname(), slotstr);
200 g_fprintf(stderr, _("%s: changer is reset, but slot %s not loaded: %s\n"),
201 get_pname(), slotstr, changer_resultstr);
204 error(_("could not reset changer: %s"), changer_resultstr);
211 /* ---------------------------- */
213 clean_tape(G_GNUC_UNUSED int argc,
214 G_GNUC_UNUSED char ** argv) {
217 if(changer_clean(&devstr) == 0) {
218 g_fprintf(stderr, _("%s: device %s is clean.\n"), get_pname(), devstr);
220 g_fprintf(stderr, _("%s: device %s not clean: %s\n"),
221 get_pname(), devstr ? devstr : "??", changer_resultstr);
227 /* ---------------------------- */
229 eject_tape(G_GNUC_UNUSED int argc,
230 G_GNUC_UNUSED char ** argv) {
231 char *slotstr = NULL;
233 if(changer_eject(&slotstr) == 0) {
234 g_fprintf(stderr, _("%s: slot %3s is ejected.\n"), get_pname(), slotstr);
236 g_fprintf(stderr, _("%s: slot %3s not ejected: %s\n"),
237 get_pname(), slotstr ? slotstr : "??", changer_resultstr);
243 /* ---------------------------- */
250 char *slotstr = NULL, *devicename = NULL;
258 if (strcmp(argv[1], "advance") == 0) {
262 if(changer_loadslot(argv[1], &slotstr, &devicename)) {
263 error(_("could not load slot %s: %s"), slotstr, changer_resultstr);
267 device = device_open(devicename);
268 g_assert(device != NULL);
269 if (device->status != DEVICE_STATUS_SUCCESS) {
271 _("%s: could not open device %s: %s"), get_pname(),
272 devicename, device_error(device));
274 g_object_unref(device);
276 g_fprintf(stderr, _("%s: changed to slot %s on %s\n"), get_pname(), slotstr, devicename);
282 /* ---------------------------- */
284 /* This initalizes the ChangerStatus structure for all commands that
285 use changer_find; namely, show_slots, load_label, and
288 scan_init(G_GNUC_UNUSED void * data, int rc, G_GNUC_UNUSED int numslots,
289 G_GNUC_UNUSED int backwards, G_GNUC_UNUSED int searchable) {
291 error(_("could not get changer info: %s"), changer_resultstr);
298 static int scan_init_print(void * data, int rc, int numslots,
299 int backwards, int searchable) {
301 g_fprintf(stderr, _("%s: scanning all %d slots in tape-changer rack:\n"),
302 get_pname(), numslots);
304 return scan_init(data, rc, numslots, backwards, searchable);
310 } LabelChangerStatus;
312 /* This is the 'user_slot' callback for the 'load label' command. */
320 LabelChangerStatus * status = ud;
322 DeviceStatusFlags device_status;
325 error(_("could not load slot %s: %s"), slotstr, changer_resultstr);
326 g_assert_not_reached();
327 } else if (rc == 1) {
328 g_fprintf(stderr, _("%s: slot %3s: %s\n"),
329 get_pname(), slotstr, changer_resultstr);
333 device = device_open(device_name);
334 g_assert(device != NULL);
335 if (device->status != DEVICE_STATUS_SUCCESS) {
336 g_fprintf(stderr, _("%s: slot %3s: Could not open device: %s.\n"),
337 get_pname(), slotstr, device_error(device));
341 if (!device_configure(device, TRUE)) {
342 g_fprintf(stderr, _("%s: slot %3s: Could not configure device: %s.\n"),
343 get_pname(), slotstr, device_error(device));
347 device_status = device_read_label(device);
348 if (device_status != DEVICE_STATUS_SUCCESS) {
349 g_fprintf(stderr, _("%s: slot %3s: %s\n"),
350 get_pname(), slotstr, device_error_or_status(device));
351 g_object_unref(device);
355 g_fprintf(stderr, _("%s: slot %3s: time %-14s label %s"),
356 get_pname(), slotstr, device->volume_time, device->volume_label);
358 if(strcmp(device->volume_label, status->searchlabel) != 0) {
359 g_fprintf(stderr, _(" (wrong tape)\n"));
360 g_object_unref(device);
363 g_fprintf(stderr, _(" (exact label match)\n"));
364 g_object_unref(device);
369 g_assert_not_reached();
372 /* This does the 'load label' command. */
378 LabelChangerStatus status;
385 status.searchlabel = argv[1];
387 g_fprintf(stderr, _("%s: scanning for tape with label %s\n"),
388 get_pname(), status.searchlabel);
392 changer_find(&status, scan_init, loadlabel_slot, status.searchlabel);
395 g_fprintf(stderr, _("%s: label %s is now loaded.\n"),
396 get_pname(), status.searchlabel);
398 g_fprintf(stderr, _("%s: could not find label %s in tape rack.\n"),
399 get_pname(), status.searchlabel);
403 /* ---------------------------- */
405 /* This is the user_slot function for "amtape show". */
407 show_slots_slot(G_GNUC_UNUSED void * data, int rc, char * slotstr,
413 error(_("could not load slot %s: %s"), slotstr, changer_resultstr);
414 g_assert_not_reached();
418 g_fprintf(stderr, _("slot %s: %s\n"), slotstr, changer_resultstr);
422 device = device_open(device_name);
423 g_assert(device != NULL);
424 if (device->status != DEVICE_STATUS_SUCCESS) {
425 g_fprintf(stderr, _("%s: slot %3s: Could not open device: %s.\n"),
426 get_pname(), slotstr, device_error(device));
428 DeviceStatusFlags device_status;
430 if (!device_configure(device, TRUE)) {
431 g_fprintf(stderr, _("%s: slot %3s: %s\n"),
432 get_pname(), slotstr, device_error_or_status(device));
435 device_status = device_read_label(device);
437 if (device_status != DEVICE_STATUS_SUCCESS) {
438 g_fprintf(stderr, _("%s: slot %3s: %s\n"),
439 get_pname(), slotstr, device_error_or_status(device));
441 g_fprintf(stderr, _("slot %3s: time %-14s label %s\n"),
442 slotstr, device->volume_time, device->volume_label);
444 /* update the changer db */
445 changer_label(slotstr, device->volume_label);
450 g_object_unref(device);
456 show_current(int argc, G_GNUC_UNUSED char ** argv) {
462 g_fprintf(stderr, _("%s: scanning current slot in tape-changer rack:\n"),
464 changer_current(NULL, scan_init, show_slots_slot);
468 show_slots(int argc, G_GNUC_UNUSED char ** argv) {
473 changer_find(NULL, scan_init_print, show_slots_slot, NULL);
477 /* ---------------------------- */
480 amtape_taper_scan(G_GNUC_UNUSED int argc, G_GNUC_UNUSED char ** argv) {
484 char *timestamp = NULL;
485 char *tapedev = NULL;
490 g_fprintf(stderr, _("%s: scanning for "), get_pname());
492 tp = lookup_last_reusable_tape(0);
496 searchlabel = stralloc(tp->label);
497 g_fprintf(stderr, _("tape label %s or "), searchlabel);
499 g_fprintf(stderr, _("a new tape.\n"));
501 result = taper_scan(searchlabel, &label, ×tamp, &tapedev, NULL,
502 FILE_taperscan_output_callback, stderr, NULL, NULL);
505 g_fprintf(stderr, _("Could not find a non-active Amanda tape.\n"));
507 g_fprintf(stderr, _("Tape with label `%s' is now loaded.\n"), label);
509 } else if (result == 3) {
510 g_fprintf(stderr, _("New tape loaded, it will be labelled `%s'.\n"),
513 g_fprintf(stderr, _("Tape with label `%s' is now loaded.\n"), label);
522 /* ---------------------------- */
525 show_device(G_GNUC_UNUSED int argc,
526 G_GNUC_UNUSED char ** argv) {
527 char *slot = NULL, *device = NULL;
529 if(changer_loadslot(_("current"), &slot, &device)) {
530 error(_("could not load current slot: %s\n"), changer_resultstr);
534 g_printf("%s\n", device);