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: tapefile.c,v 1.37 2006/07/21 00:25:52 martinea Exp $
29 * routines to read and write the amanda active tape list
36 static tape_t *tape_list = NULL;
39 static tape_t *parse_tapeline(int *status, char *line);
40 static tape_t *insert(tape_t *list, tape_t *tp);
41 static time_t stamp2time(char *datestamp);
54 if((tapef = fopen(tapefile,"r")) == NULL) {
55 if (errno == ENOENT) {
56 /* no tapelist is equivalent to an empty tapelist */
59 g_debug("Error opening '%s': %s", tapefile, strerror(errno));
64 while((line = agets(tapef)) != NULL) {
65 if (line[0] == '\0') {
69 tp = parse_tapeline(&status, line);
71 if(tp == NULL && status != 0)
74 tape_list = insert(tape_list, tp);
78 for(pos=1,tp=tape_list; tp != NULL; pos++,tp=tp->next) {
94 newtapefile = stralloc2(tapefile, ".new");
96 if((tapef = fopen(newtapefile,"w")) == NULL) {
101 for(tp = tape_list; tp != NULL; tp = tp->next) {
102 g_fprintf(tapef, "%s %s", tp->datestamp, tp->label);
103 if(tp->reuse) g_fprintf(tapef, " reuse");
104 else g_fprintf(tapef, " no-reuse");
106 g_fprintf(tapef, " BARCODE:%s", tp->barcode);
108 g_fprintf(tapef, " META:%s", tp->meta);
110 g_fprintf(tapef, " #%s", tp->comment);
111 g_fprintf(tapef, "\n");
114 if (fclose(tapef) == EOF) {
115 g_fprintf(stderr,_("error [closing %s: %s]"), newtapefile, strerror(errno));
119 rc = rename(newtapefile, tapefile);
130 for(tp = tape_list; tp; tp = next) {
132 amfree(tp->datestamp);
145 for(tp = tape_list; tp != NULL; tp = tp->next) {
146 if(strcmp(label, tp->label) == 0) return tp;
159 for(tp = tape_list; tp != NULL; tp = tp->next) {
160 if(tp->position == pos) return tp;
172 for(tp = tape_list; tp != NULL; tp = tp->next) {
173 if(strcmp(tp->datestamp, datestamp) == 0) return tp;
184 for(tp = tape_list; tp != NULL; tp = tp->next) {
192 get_last_reusable_tape_label(
195 tape_t *tp = lookup_last_reusable_tape(skip);
196 return (tp != NULL) ? tp->label : NULL;
200 lookup_last_reusable_tape(
203 tape_t *tp, **tpsave;
206 int tapecycle = getconf_int(CNF_TAPECYCLE);
207 char *labelstr = getconf_str (CNF_LABELSTR);
210 * The idea here is we keep the last "several" reusable tapes we
211 * find in a stack and then return the n-th oldest one to the
212 * caller. If skip is zero, the oldest is returned, if it is
213 * one, the next oldest, two, the next to next oldest and so on.
215 tpsave = alloc((skip + 1) * SIZEOF(*tpsave));
216 for(s = 0; s <= skip; s++) {
219 for(tp = tape_list; tp != NULL; tp = tp->next) {
220 if(tp->reuse == 1 && strcmp(tp->datestamp,"0") != 0 && match (labelstr, tp->label)) {
222 for(s = skip; s > 0; s--) {
223 tpsave[s] = tpsave[s - 1];
228 s = tapecycle - count;
230 if(count < tapecycle - skip) tp = NULL;
231 else tp = tpsave[skip - s];
242 if(tp == NULL) return 0;
243 if(tp->reuse == 0) return 0;
244 if( strcmp(tp->datestamp,"0") == 0) return 1;
246 if(tp->reuse == 1) count++;
249 return (count >= getconf_int(CNF_TAPECYCLE));
256 tape_t *tp, *prev, *next;
258 tp = lookup_tapelabel(label);
265 else /* begin of list */
270 while (next != NULL) {
274 amfree(tp->datestamp);
288 /* insert a new record to the front of the list */
290 new = (tape_t *) alloc(SIZEOF(tape_t));
292 new->datestamp = stralloc(datestamp);
295 new->label = stralloc(label);
296 new->comment = comment? stralloc(comment) : NULL;
299 if(tape_list != NULL) tape_list->prev = new;
300 new->next = tape_list;
303 /* scan list, updating positions */
314 guess_runs_from_tapelist(void)
317 int i, ntapes, tape_ndays, dumpcycle, runtapes, runs;
318 time_t tape_time, today;
321 dumpcycle = getconf_int(CNF_DUMPCYCLE);
322 runtapes = getconf_int(CNF_RUNTAPES);
323 if(runtapes == 0) runtapes = 1; /* just in case */
327 for(i = 1; i < getconf_int(CNF_TAPECYCLE); i++) {
328 if((tp = lookup_tapepos(i)) == NULL) break;
330 tape_time = stamp2time(tp->datestamp);
331 tape_ndays = (int)days_diff(tape_time, today);
333 if(tape_ndays < dumpcycle) ntapes++;
337 if(tape_ndays < dumpcycle) {
338 /* scale for best guess */
339 if(tape_ndays == 0) ntapes = dumpcycle * runtapes;
340 else ntapes = ntapes * dumpcycle / tape_ndays;
342 else if(ntapes == 0) {
343 /* no dumps within the last dumpcycle, guess as above */
344 ntapes = dumpcycle * runtapes;
347 runs = (ntapes + runtapes - 1) / runtapes;
363 tp = (tape_t *) alloc(SIZEOF(tape_t));
371 skip_whitespace(s, ch);
377 skip_non_whitespace(s, ch);
379 tp->datestamp = stralloc(s1);
381 skip_whitespace(s, ch);
383 skip_non_whitespace(s, ch);
385 tp->label = stralloc(s1);
387 skip_whitespace(s, ch);
389 if(strncmp_const(s - 1, "reuse") == 0) {
392 skip_non_whitespace(s, ch);
394 skip_whitespace(s, ch);
396 if(strncmp_const(s - 1, "no-reuse") == 0) {
399 skip_non_whitespace(s, ch);
401 skip_whitespace(s, ch);
404 if (strncmp_const(s - 1, "BARCODE:") == 0) {
406 skip_non_whitespace(s, ch);
408 skip_whitespace(s, ch);
409 tp->barcode = stralloc(s1);
414 if (strncmp_const(s - 1, "META:") == 0) {
416 skip_non_whitespace(s, ch);
418 skip_whitespace(s, ch);
419 tp->meta = stralloc(s1);
424 if (*(s - 1) == '#') {
425 tp->comment = stralloc(s); /* skip leading '#' */
434 /* insert in reversed datestamp order */
446 while(cur != NULL && strcmp(cur->datestamp, tp->datestamp) >= 0) {
467 * Converts datestamp (an char of the form YYYYMMDD or YYYYMMDDHHMMSS) into a real
469 * Since the datestamp contains no timezone or hh/mm/ss information, the
470 * value is approximate. This is ok for our purposes, since we round off
471 * scheduling calculations to the nearest day.
483 strncpy(date, datestamp, 8);
485 dateint = atoi(date);
487 tm = localtime(&now); /* initialize sec/min/hour & gmtoff */
490 tm = alloc(SIZEOF(struct tm));
500 tm->tm_year = ( dateint / 10000) - 1900;
501 tm->tm_mon = ((dateint % 10000) / 100) - 1;
502 tm->tm_mday = ((dateint % 100) );
511 tape_t *lasttp, *iter;
514 /* Find latest reusable new tape */
515 lasttp = lookup_tapepos(lookup_nb_tape());
516 while (lasttp && lasttp->reuse == 0)
517 lasttp = lasttp->prev;
519 if(lasttp && nb > 0 && strcmp(lasttp->datestamp,"0") == 0) {
522 /* count the number of tapes we *actually* used */
523 while(iter && nb > 0 && strcmp(iter->datestamp,"0") == 0) {
532 result = g_strdup_printf(
533 _("The next new tape already labelled is: %s."),
536 result = g_strdup_printf(
537 _("The next %d new tapes already labelled are: %s"),
541 while(iter && c > 0 && strcmp(iter->datestamp,"0") == 0) {
543 result = vstrextend(&result, ", ", iter->label, NULL);
558 char *result = list_new_tapes(nb);
561 g_fprintf(output,"%s\n", result);