Imported Upstream version 2.6.0
[debian/amanda] / server-src / amlabel.c
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 1991-1998, 2000 University of Maryland at College Park
4  * All Rights Reserved.
5  *
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.
15  *
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.
22  *
23  * Authors: the Amanda Development Team.  Its members are listed in a
24  * file named AUTHORS, in the root directory of this distribution.
25  */
26 /*
27  * $Id: amlabel.c,v 1.53 2006/07/25 18:27:57 martinea Exp $
28  *
29  * write an Amanda label on a tape
30  */
31 #include "amanda.h"
32 #include "conffile.h"
33 #include "tapefile.h"
34 #include "changer.h"
35 #include <device.h>
36 #include <timestamp.h>
37 #include <taperscan.h>
38
39 /* local functions */
40
41 int main(int argc, char **argv);
42
43 static void usage(void) {
44     g_fprintf(stderr, _("Usage: %s [-f] <conf> <label> [slot <slot-number>] [-o configoption]*\n"),
45             get_pname());
46     exit(1);
47 }
48
49 static void print_read_label_status_error(ReadLabelStatusFlags status) {
50     char ** status_strv;
51
52     if (status == READ_LABEL_STATUS_SUCCESS)
53         return;
54
55     status_strv = g_flags_nick_to_strv(status,
56                                        READ_LABEL_STATUS_FLAGS_TYPE);
57     g_assert(g_strv_length(status_strv) > 0);
58     if (g_strv_length(status_strv) == 1) {
59         g_printf("Error was %s.\n", *status_strv);
60     } else {
61         char * status_list = g_english_strjoinv(status_strv, "or");
62         g_printf("Error was one of %s.\n", status_list);
63         amfree(status_list);
64     }
65     g_strfreev(status_strv);
66 }
67
68 int
69 main(
70     int         argc,
71     char **     argv)
72 {
73     char *conf_tapelist;
74     char *outslot = NULL;
75     char *label, *tapename = NULL;
76     char *labelstr, *slotstr;
77     char *conf_tapelist_old;
78     int have_changer;
79     int force, tape_ok;
80     tapetype_t *tape;
81     size_t tt_blocksize_kb;
82     int slotcommand;
83     Device * device;
84     ReadLabelStatusFlags label_status;
85     char *cfg_opt = NULL;
86     config_overwrites_t *cfg_ovr = NULL;
87
88     /*
89      * Configure program for internationalization:
90      *   1) Only set the message locale for now.
91      *   2) Set textdomain for all amanda related programs to "amanda"
92      *      We don't want to be forced to support dozens of message catalogs.
93      */  
94     setlocale(LC_MESSAGES, "C");
95     textdomain("amanda"); 
96
97     safe_fd(-1, 0);
98     safe_cd();
99
100     set_pname("amlabel");
101
102     dbopen(DBG_SUBDIR_SERVER);
103     device_api_init();
104
105     /* Don't die when child closes pipe */
106     signal(SIGPIPE, SIG_IGN);
107
108     erroutput_type = ERR_INTERACTIVE;
109
110     cfg_ovr = extract_commandline_config_overwrites(&argc, &argv);
111
112     if(argc > 1 && strcmp(argv[1],"-f") == 0)
113          force=1;
114     else force=0;
115
116     if(argc != 3+force && argc != 5+force)
117         usage();
118
119     cfg_opt = argv[1+force];
120     label = argv[2+force];
121
122     if(argc == 5+force) {
123         if(strcmp(argv[3+force], "slot"))
124             usage();
125         slotstr = argv[4+force];
126         slotcommand = 1;
127     } else {
128         slotstr = "current";
129         slotcommand = 0;
130     }
131
132     config_init(CONFIG_INIT_EXPLICIT_NAME | CONFIG_INIT_FATAL,
133                 cfg_opt);
134     apply_config_overwrites(cfg_ovr);
135
136     check_running_as(RUNNING_AS_DUMPUSER);
137
138     dbrename(config_name, DBG_SUBDIR_SERVER);
139
140     conf_tapelist = config_dir_relative(getconf_str(CNF_TAPELIST));
141     if (read_tapelist(conf_tapelist)) {
142         error(_("could not load tapelist \"%s\""), conf_tapelist);
143         /*NOTREACHED*/
144     }
145
146     labelstr = getconf_str(CNF_LABELSTR);
147
148     if(!match(labelstr, label)) {
149         error(_("label %s doesn't match labelstr \"%s\""), label, labelstr);
150         /*NOTREACHED*/
151     }
152
153     if((lookup_tapelabel(label))!=NULL) {
154         if(!force) {
155             error(_("label %s already on a tape\n"),label);
156             /*NOTREACHED*/
157         }
158     }
159     tape = lookup_tapetype(getconf_str(CNF_TAPETYPE));
160     tt_blocksize_kb = (size_t)tapetype_get_blocksize(tape);
161
162     if((have_changer = changer_init()) == 0) {
163         if(slotcommand) {
164             g_fprintf(stderr,
165              _("%s: no tpchanger specified in \"%s\", so slot command invalid\n"),
166                     argv[0], config_filename);
167             usage();
168         }
169         tapename = getconf_str(CNF_TAPEDEV);
170         if (tapename == NULL) {
171             error(_("No tapedev specified"));
172         }
173     } else if(have_changer != 1) {
174         error(_("changer initialization failed: %s"), strerror(errno));
175         /*NOTREACHED*/
176     } else {
177         if(changer_loadslot(slotstr, &outslot, &tapename)) {
178             error(_("could not load slot \"%s\": %s"), slotstr, changer_resultstr);
179             /*NOTREACHED*/
180         }
181
182         g_printf(_("labeling tape in slot %s (%s):\n"), outslot, tapename);
183     }
184
185     tape_ok=1;
186     g_printf("Reading label...\n");fflush(stdout);
187     device = device_open(tapename);
188     if (device == NULL) {
189         error("Could not open device %s.\n", tapename);
190     }
191     
192     device_set_startup_properties_from_config(device);
193     label_status = device_read_label(device);
194
195     if (label_status & READ_LABEL_STATUS_VOLUME_UNLABELED) {
196         g_printf("Found an unlabeled tape.\n");
197     } else if (label_status != READ_LABEL_STATUS_SUCCESS) {
198         g_printf("Reading the tape label failed: \n  ");
199         print_read_label_status_error(label_status);
200         tape_ok = 0;
201     } else {
202         /* got an amanda tape */
203         g_printf(_("Found Amanda tape %s"),device->volume_label);
204         if(match(labelstr, device->volume_label) == 0) {
205             g_printf(_(", but it is not from configuration %s."), config_name);
206             if(!force)
207                 tape_ok=0;
208         } else {
209             if((lookup_tapelabel(device->volume_label)) != NULL) {
210                 g_printf(_(", tape is active"));
211                 if(!force)
212                     tape_ok=0;
213             }
214         }
215         g_printf("\n");
216     }
217
218     if(tape_ok) {
219         char *timestamp = NULL;
220
221         g_printf(_("Writing label %s..\n"), label); fflush(stdout);
222         
223         timestamp = get_undef_timestamp();
224         if (!device_start(device, ACCESS_WRITE, label, timestamp)) {
225             error(_("Error writing label.\n"));
226             g_assert_not_reached();
227         } else if (!device_finish(device)) {
228             error(_("Error closing device.\n"));
229             g_assert_not_reached();
230         }
231         amfree(timestamp);
232
233         g_printf(_("Checking label...\n")); fflush(stdout);
234
235         label_status = device_read_label(device);
236         if (label_status != READ_LABEL_STATUS_SUCCESS) {
237             g_printf("Checking the tape label failed: \n  ");
238             print_read_label_status_error(label_status);
239             exit(EXIT_FAILURE);
240         } else if (device->volume_label == NULL) {
241             error(_("no label found.\n"));
242             g_assert_not_reached();
243         } else if (strcmp(device->volume_label, label) != 0) {
244             error(_("Read back a different label: Got %s, but expected %s\n"),
245                   device->volume_label, label);
246             g_assert_not_reached();
247         } else if (get_timestamp_state(device->volume_time) !=
248                    TIME_STATE_UNDEF) {
249             error(_("Read the right label, but the wrong timestamp: "
250                     "Got %s, expected X.\n"), device->volume_time);
251             g_assert_not_reached();
252         }
253         
254         /* write tape list */
255         
256         /* make a copy */
257         conf_tapelist_old = stralloc2(conf_tapelist, ".amlabel");
258         if(write_tapelist(conf_tapelist_old)) {
259             error(_("couldn't write tapelist: %s"), strerror(errno));
260             /*NOTREACHED*/
261         }
262         amfree(conf_tapelist_old);
263         
264         /* XXX add cur_tape number to tape list structure */
265         remove_tapelabel(label);
266         add_tapelabel("0", label);
267         if(write_tapelist(conf_tapelist)) {
268             error(_("couldn't write tapelist: %s"), strerror(errno));
269             /*NOTREACHED*/
270         }
271         
272         g_printf(_("Success!\n"));
273     } else {
274         g_printf(_("\ntape not labeled\n"));
275     }
276     
277     g_object_unref(device);
278     device = NULL;
279
280     clear_tapelist();
281     amfree(outslot);
282     amfree(conf_tapelist);
283     config_name=NULL;
284     dbclose();
285
286     return 0;
287 }