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, " #%s", tp->comment);
107 g_fprintf(tapef, "\n");
110 if (fclose(tapef) == EOF) {
111 g_fprintf(stderr,_("error [closing %s: %s]"), newtapefile, strerror(errno));
115 rc = rename(newtapefile, tapefile);
126 for(tp = tape_list; tp; tp = next) {
128 amfree(tp->datestamp);
141 for(tp = tape_list; tp != NULL; tp = tp->next) {
142 if(strcmp(label, tp->label) == 0) return tp;
155 for(tp = tape_list; tp != NULL; tp = tp->next) {
156 if(tp->position == pos) return tp;
168 for(tp = tape_list; tp != NULL; tp = tp->next) {
169 if(strcmp(tp->datestamp, datestamp) == 0) return tp;
180 for(tp = tape_list; tp != NULL; tp = tp->next) {
188 get_last_reusable_tape_label(
191 tape_t *tp = lookup_last_reusable_tape(skip);
192 return (tp != NULL) ? tp->label : NULL;
196 lookup_last_reusable_tape(
199 tape_t *tp, **tpsave;
202 int tapecycle = getconf_int(CNF_TAPECYCLE);
203 char *labelstr = getconf_str (CNF_LABELSTR);
206 * The idea here is we keep the last "several" reusable tapes we
207 * find in a stack and then return the n-th oldest one to the
208 * caller. If skip is zero, the oldest is returned, if it is
209 * one, the next oldest, two, the next to next oldest and so on.
211 tpsave = alloc((skip + 1) * SIZEOF(*tpsave));
212 for(s = 0; s <= skip; s++) {
215 for(tp = tape_list; tp != NULL; tp = tp->next) {
216 if(tp->reuse == 1 && strcmp(tp->datestamp,"0") != 0 && match (labelstr, tp->label)) {
218 for(s = skip; s > 0; s--) {
219 tpsave[s] = tpsave[s - 1];
224 s = tapecycle - count;
226 if(count < tapecycle - skip) tp = NULL;
227 else tp = tpsave[skip - s];
238 if(tp == NULL) return 0;
239 if(tp->reuse == 0) return 0;
240 if( strcmp(tp->datestamp,"0") == 0) return 1;
242 if(tp->reuse == 1) count++;
245 return (count >= getconf_int(CNF_TAPECYCLE));
252 tape_t *tp, *prev, *next;
254 tp = lookup_tapelabel(label);
261 else /* begin of list */
266 while (next != NULL) {
270 amfree(tp->datestamp);
284 /* insert a new record to the front of the list */
286 new = (tape_t *) alloc(SIZEOF(tape_t));
288 new->datestamp = stralloc(datestamp);
291 new->label = stralloc(label);
292 new->comment = comment? stralloc(comment) : NULL;
295 if(tape_list != NULL) tape_list->prev = new;
296 new->next = tape_list;
299 /* scan list, updating positions */
310 guess_runs_from_tapelist(void)
313 int i, ntapes, tape_ndays, dumpcycle, runtapes, runs;
314 time_t tape_time, today;
317 dumpcycle = getconf_int(CNF_DUMPCYCLE);
318 runtapes = getconf_int(CNF_RUNTAPES);
319 if(runtapes == 0) runtapes = 1; /* just in case */
323 for(i = 1; i < getconf_int(CNF_TAPECYCLE); i++) {
324 if((tp = lookup_tapepos(i)) == NULL) break;
326 tape_time = stamp2time(tp->datestamp);
327 tape_ndays = (int)days_diff(tape_time, today);
329 if(tape_ndays < dumpcycle) ntapes++;
333 if(tape_ndays < dumpcycle) {
334 /* scale for best guess */
335 if(tape_ndays == 0) ntapes = dumpcycle * runtapes;
336 else ntapes = ntapes * dumpcycle / tape_ndays;
338 else if(ntapes == 0) {
339 /* no dumps within the last dumpcycle, guess as above */
340 ntapes = dumpcycle * runtapes;
343 runs = (ntapes + runtapes - 1) / runtapes;
359 tp = (tape_t *) alloc(SIZEOF(tape_t));
367 skip_whitespace(s, ch);
373 skip_non_whitespace(s, ch);
375 tp->datestamp = stralloc(s1);
377 skip_whitespace(s, ch);
379 skip_non_whitespace(s, ch);
381 tp->label = stralloc(s1);
383 skip_whitespace(s, ch);
385 if(strncmp_const(s - 1, "reuse") == 0) {
388 skip_non_whitespace(s, ch);
390 skip_whitespace(s, ch);
392 if(strncmp_const(s - 1, "no-reuse") == 0) {
395 skip_non_whitespace(s, ch);
397 skip_whitespace(s, ch);
400 if (*(s - 1) == '#') {
401 tp->comment = stralloc(s); /* skip leading '#' */
410 /* insert in reversed datestamp order */
422 while(cur != NULL && strcmp(cur->datestamp, tp->datestamp) >= 0) {
443 * Converts datestamp (an char of the form YYYYMMDD or YYYYMMDDHHMMSS) into a real
445 * Since the datestamp contains no timezone or hh/mm/ss information, the
446 * value is approximate. This is ok for our purposes, since we round off
447 * scheduling calculations to the nearest day.
459 strncpy(date, datestamp, 8);
461 dateint = atoi(date);
463 tm = localtime(&now); /* initialize sec/min/hour & gmtoff */
466 tm = alloc(SIZEOF(struct tm));
476 tm->tm_year = ( dateint / 10000) - 1900;
477 tm->tm_mon = ((dateint % 10000) / 100) - 1;
478 tm->tm_mday = ((dateint % 100) );
487 tape_t *lasttp, *iter;
490 /* Find latest reusable new tape */
491 lasttp = lookup_tapepos(lookup_nb_tape());
492 while (lasttp && lasttp->reuse == 0)
493 lasttp = lasttp->prev;
495 if(lasttp && nb > 0 && strcmp(lasttp->datestamp,"0") == 0) {
498 /* count the number of tapes we *actually* used */
499 while(iter && nb > 0 && strcmp(iter->datestamp,"0") == 0) {
508 result = g_strdup_printf(
509 _("The next new tape already labelled is: %s."),
512 result = g_strdup_printf(
513 _("The next %d new tapes already labelled are: %s"),
517 while(iter && c > 0 && strcmp(iter->datestamp,"0") == 0) {
519 result = vstrextend(&result, ", ", iter->label, NULL);
534 char *result = list_new_tapes(nb);
537 g_fprintf(output,"%s\n", result);