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, " BLOCKSIZE:%jd", (intmax_t)tp->blocksize);
112 g_fprintf(tapef, " #%s", tp->comment);
113 g_fprintf(tapef, "\n");
116 if (fclose(tapef) == EOF) {
117 g_fprintf(stderr,_("error [closing %s: %s]"), newtapefile, strerror(errno));
121 rc = rename(newtapefile, tapefile);
132 for(tp = tape_list; tp; tp = next) {
134 amfree(tp->datestamp);
147 for(tp = tape_list; tp != NULL; tp = tp->next) {
148 if(strcmp(label, tp->label) == 0) return tp;
161 for(tp = tape_list; tp != NULL; tp = tp->next) {
162 if(tp->position == pos) return tp;
174 for(tp = tape_list; tp != NULL; tp = tp->next) {
175 if(strcmp(tp->datestamp, datestamp) == 0) return tp;
186 for(tp = tape_list; tp != NULL; tp = tp->next) {
194 get_last_reusable_tape_label(
197 tape_t *tp = lookup_last_reusable_tape(skip);
198 return (tp != NULL) ? tp->label : NULL;
202 lookup_last_reusable_tape(
205 tape_t *tp, **tpsave;
208 int tapecycle = getconf_int(CNF_TAPECYCLE);
209 char *labelstr = getconf_str (CNF_LABELSTR);
212 * The idea here is we keep the last "several" reusable tapes we
213 * find in a stack and then return the n-th oldest one to the
214 * caller. If skip is zero, the oldest is returned, if it is
215 * one, the next oldest, two, the next to next oldest and so on.
217 tpsave = alloc((skip + 1) * SIZEOF(*tpsave));
218 for(s = 0; s <= skip; s++) {
221 for(tp = tape_list; tp != NULL; tp = tp->next) {
222 if(tp->reuse == 1 && strcmp(tp->datestamp,"0") != 0 && match (labelstr, tp->label)) {
224 for(s = skip; s > 0; s--) {
225 tpsave[s] = tpsave[s - 1];
230 s = tapecycle - count;
232 if(count < tapecycle - skip) tp = NULL;
233 else tp = tpsave[skip - s];
244 if(tp == NULL) return 0;
245 if(tp->reuse == 0) return 0;
246 if( strcmp(tp->datestamp,"0") == 0) return 1;
248 if(tp->reuse == 1) count++;
251 return (count >= getconf_int(CNF_TAPECYCLE));
258 tape_t *tp, *prev, *next;
260 tp = lookup_tapelabel(label);
267 else /* begin of list */
272 while (next != NULL) {
276 amfree(tp->datestamp);
290 /* insert a new record to the front of the list */
292 new = (tape_t *) alloc(SIZEOF(tape_t));
294 new->datestamp = stralloc(datestamp);
297 new->label = stralloc(label);
298 new->comment = comment? stralloc(comment) : NULL;
301 if(tape_list != NULL) tape_list->prev = new;
302 new->next = tape_list;
305 /* scan list, updating positions */
316 guess_runs_from_tapelist(void)
319 int i, ntapes, tape_ndays, dumpcycle, runtapes, runs;
320 time_t tape_time, today;
323 dumpcycle = getconf_int(CNF_DUMPCYCLE);
324 runtapes = getconf_int(CNF_RUNTAPES);
325 if(runtapes == 0) runtapes = 1; /* just in case */
329 for(i = 1; i < getconf_int(CNF_TAPECYCLE); i++) {
330 if((tp = lookup_tapepos(i)) == NULL) break;
332 tape_time = stamp2time(tp->datestamp);
333 tape_ndays = (int)days_diff(tape_time, today);
335 if(tape_ndays < dumpcycle) ntapes++;
339 if(tape_ndays < dumpcycle) {
340 /* scale for best guess */
341 if(tape_ndays == 0) ntapes = dumpcycle * runtapes;
342 else ntapes = ntapes * dumpcycle / tape_ndays;
344 else if(ntapes == 0) {
345 /* no dumps within the last dumpcycle, guess as above */
346 ntapes = dumpcycle * runtapes;
349 runs = (ntapes + runtapes - 1) / runtapes;
365 tp = (tape_t *) alloc(SIZEOF(tape_t));
373 skip_whitespace(s, ch);
379 skip_non_whitespace(s, ch);
381 tp->datestamp = stralloc(s1);
383 skip_whitespace(s, ch);
385 skip_non_whitespace(s, ch);
387 tp->label = stralloc(s1);
389 skip_whitespace(s, ch);
391 if(strncmp_const(s - 1, "reuse") == 0) {
394 skip_non_whitespace(s, ch);
396 skip_whitespace(s, ch);
398 if(strncmp_const(s - 1, "no-reuse") == 0) {
401 skip_non_whitespace(s, ch);
403 skip_whitespace(s, ch);
406 if (strncmp_const(s - 1, "BARCODE:") == 0) {
408 skip_non_whitespace(s, ch);
410 skip_whitespace(s, ch);
411 tp->barcode = stralloc(s1);
416 if (strncmp_const(s - 1, "META:") == 0) {
418 skip_non_whitespace(s, ch);
420 skip_whitespace(s, ch);
421 tp->meta = stralloc(s1);
426 if (strncmp_const(s - 1, "BLOCKSIZE:") == 0) {
428 skip_non_whitespace(s, ch);
430 skip_whitespace(s, ch);
431 tp->blocksize = atol(s1);
435 if (*(s - 1) == '#') {
436 tp->comment = stralloc(s); /* skip leading '#' */
445 /* insert in reversed datestamp order */
457 while(cur != NULL && strcmp(cur->datestamp, tp->datestamp) >= 0) {
478 * Converts datestamp (an char of the form YYYYMMDD or YYYYMMDDHHMMSS) into a real
480 * Since the datestamp contains no timezone or hh/mm/ss information, the
481 * value is approximate. This is ok for our purposes, since we round off
482 * scheduling calculations to the nearest day.
494 strncpy(date, datestamp, 8);
496 dateint = atoi(date);
498 tm = localtime(&now); /* initialize sec/min/hour & gmtoff */
501 tm = alloc(SIZEOF(struct tm));
511 tm->tm_year = ( dateint / 10000) - 1900;
512 tm->tm_mon = ((dateint % 10000) / 100) - 1;
513 tm->tm_mday = ((dateint % 100) );
522 tape_t *lasttp, *iter;
525 /* Find latest reusable new tape */
526 lasttp = lookup_tapepos(lookup_nb_tape());
527 while (lasttp && lasttp->reuse == 0)
528 lasttp = lasttp->prev;
530 if(lasttp && nb > 0 && strcmp(lasttp->datestamp,"0") == 0) {
533 /* count the number of tapes we *actually* used */
534 while(iter && nb > 0 && strcmp(iter->datestamp,"0") == 0) {
543 result = g_strdup_printf(
544 _("The next new tape already labelled is: %s."),
547 result = g_strdup_printf(
548 _("The next %d new tapes already labelled are: %s"),
552 while(iter && c > 0 && strcmp(iter->datestamp,"0") == 0) {
554 result = vstrextend(&result, ", ", iter->label, NULL);
569 char *result = list_new_tapes(nb);
572 g_fprintf(output,"%s\n", result);