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);
150 for(tp = tape_list; tp != NULL; tp = tp->next) {
151 if(strcmp(label, tp->label) == 0) return tp;
164 for(tp = tape_list; tp != NULL; tp = tp->next) {
165 if(tp->position == pos) return tp;
177 for(tp = tape_list; tp != NULL; tp = tp->next) {
178 if(strcmp(tp->datestamp, datestamp) == 0) return tp;
189 for(tp = tape_list; tp != NULL; tp = tp->next) {
197 get_last_reusable_tape_label(
200 tape_t *tp = lookup_last_reusable_tape(skip);
201 return (tp != NULL) ? tp->label : NULL;
205 lookup_last_reusable_tape(
208 tape_t *tp, **tpsave;
211 int tapecycle = getconf_int(CNF_TAPECYCLE);
212 char *labelstr = getconf_str (CNF_LABELSTR);
215 * The idea here is we keep the last "several" reusable tapes we
216 * find in a stack and then return the n-th oldest one to the
217 * caller. If skip is zero, the oldest is returned, if it is
218 * one, the next oldest, two, the next to next oldest and so on.
220 tpsave = alloc((skip + 1) * SIZEOF(*tpsave));
221 for(s = 0; s <= skip; s++) {
224 for(tp = tape_list; tp != NULL; tp = tp->next) {
225 if(tp->reuse == 1 && strcmp(tp->datestamp,"0") != 0 && match (labelstr, tp->label)) {
227 for(s = skip; s > 0; s--) {
228 tpsave[s] = tpsave[s - 1];
233 s = tapecycle - count;
235 if(count < tapecycle - skip) tp = NULL;
236 else tp = tpsave[skip - s];
247 if(tp == NULL) return 0;
248 if(tp->reuse == 0) return 0;
249 if( strcmp(tp->datestamp,"0") == 0) return 1;
251 if(tp->reuse == 1) count++;
254 return (count >= getconf_int(CNF_TAPECYCLE));
261 tape_t *tp, *prev, *next;
263 tp = lookup_tapelabel(label);
270 else /* begin of list */
275 while (next != NULL) {
279 amfree(tp->datestamp);
296 /* insert a new record to the front of the list */
298 new = g_new0(tape_t, 1);
300 new->datestamp = stralloc(datestamp);
303 new->label = stralloc(label);
304 new->comment = comment? stralloc(comment) : NULL;
307 if(tape_list != NULL) tape_list->prev = new;
308 new->next = tape_list;
311 /* scan list, updating positions */
322 guess_runs_from_tapelist(void)
325 int i, ntapes, tape_ndays, dumpcycle, runtapes, runs;
326 time_t tape_time, today;
329 dumpcycle = getconf_int(CNF_DUMPCYCLE);
330 runtapes = getconf_int(CNF_RUNTAPES);
331 if(runtapes == 0) runtapes = 1; /* just in case */
335 for(i = 1; i < getconf_int(CNF_TAPECYCLE); i++) {
336 if((tp = lookup_tapepos(i)) == NULL) break;
338 tape_time = stamp2time(tp->datestamp);
339 tape_ndays = (int)days_diff(tape_time, today);
341 if(tape_ndays < dumpcycle) ntapes++;
345 if(tape_ndays < dumpcycle) {
346 /* scale for best guess */
347 if(tape_ndays == 0) ntapes = dumpcycle * runtapes;
348 else ntapes = ntapes * dumpcycle / tape_ndays;
350 else if(ntapes == 0) {
351 /* no dumps within the last dumpcycle, guess as above */
352 ntapes = dumpcycle * runtapes;
355 runs = (ntapes + runtapes - 1) / runtapes;
375 skip_whitespace(s, ch);
380 tp = g_new0(tape_t, 1);
383 skip_non_whitespace(s, ch);
385 tp->datestamp = stralloc(s1);
387 skip_whitespace(s, ch);
389 skip_non_whitespace(s, ch);
391 tp->label = stralloc(s1);
393 skip_whitespace(s, ch);
395 if(strncmp_const(s - 1, "reuse") == 0) {
398 skip_non_whitespace(s, ch);
400 skip_whitespace(s, ch);
402 if(strncmp_const(s - 1, "no-reuse") == 0) {
405 skip_non_whitespace(s, ch);
407 skip_whitespace(s, ch);
410 if (strncmp_const(s - 1, "BARCODE:") == 0) {
412 skip_non_whitespace(s, ch);
414 skip_whitespace(s, ch);
415 tp->barcode = stralloc(s1);
418 if (strncmp_const(s - 1, "META:") == 0) {
420 skip_non_whitespace(s, ch);
422 skip_whitespace(s, ch);
423 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);
433 if (*(s - 1) == '#') {
434 tp->comment = stralloc(s); /* skip leading '#' */
441 /* insert in reversed datestamp order */
453 while(cur != NULL && strcmp(cur->datestamp, tp->datestamp) >= 0) {
474 * Converts datestamp (an char of the form YYYYMMDD or YYYYMMDDHHMMSS) into a real
476 * Since the datestamp contains no timezone or hh/mm/ss information, the
477 * value is approximate. This is ok for our purposes, since we round off
478 * scheduling calculations to the nearest day.
490 strncpy(date, datestamp, 8);
492 dateint = atoi(date);
494 tm = localtime(&now); /* initialize sec/min/hour & gmtoff */
497 tm = alloc(SIZEOF(struct tm));
507 tm->tm_year = ( dateint / 10000) - 1900;
508 tm->tm_mon = ((dateint % 10000) / 100) - 1;
509 tm->tm_mday = ((dateint % 100) );
518 tape_t *lasttp, *iter;
521 /* Find latest reusable new tape */
522 lasttp = lookup_tapepos(lookup_nb_tape());
523 while (lasttp && lasttp->reuse == 0)
524 lasttp = lasttp->prev;
526 if(lasttp && nb > 0 && strcmp(lasttp->datestamp,"0") == 0) {
529 /* count the number of tapes we *actually* used */
530 while(iter && nb > 0 && strcmp(iter->datestamp,"0") == 0) {
539 result = g_strdup_printf(
540 _("The next new tape already labelled is: %s."),
543 result = g_strdup_printf(
544 _("The next %d new tapes already labelled are: %s"),
548 while(iter && c > 0 && strcmp(iter->datestamp,"0") == 0) {
550 result = vstrextend(&result, ", ", iter->label, NULL);
565 char *result = list_new_tapes(nb);
568 g_fprintf(output,"%s\n", result);