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
35 static tape_t *tape_list = NULL;
38 static tape_t *parse_tapeline(int *status, char *line);
39 static tape_t *insert(tape_t *list, tape_t *tp);
40 static time_t stamp2time(char *datestamp);
53 if((tapef = fopen(tapefile,"r")) == NULL) {
57 while((line = agets(tapef)) != NULL) {
58 if (line[0] == '\0') {
62 tp = parse_tapeline(&status, line);
64 if(tp == NULL && status != 0)
67 tape_list = insert(tape_list, tp);
71 for(pos=1,tp=tape_list; tp != NULL; pos++,tp=tp->next) {
87 newtapefile = stralloc2(tapefile, ".new");
89 if((tapef = fopen(newtapefile,"w")) == NULL) {
94 for(tp = tape_list; tp != NULL; tp = tp->next) {
95 g_fprintf(tapef, "%s %s", tp->datestamp, tp->label);
96 if(tp->reuse) g_fprintf(tapef, " reuse");
97 else g_fprintf(tapef, " no-reuse");
98 g_fprintf(tapef, "\n");
101 if (fclose(tapef) == EOF) {
102 g_fprintf(stderr,_("error [closing %s: %s]"), newtapefile, strerror(errno));
106 rc = rename(newtapefile, tapefile);
117 for(tp = tape_list; tp; tp = next) {
119 amfree(tp->datestamp);
132 for(tp = tape_list; tp != NULL; tp = tp->next) {
133 if(strcmp(label, tp->label) == 0) return tp;
146 for(tp = tape_list; tp != NULL; tp = tp->next) {
147 if(tp->position == pos) return tp;
159 for(tp = tape_list; tp != NULL; tp = tp->next) {
160 if(strcmp(tp->datestamp, datestamp) == 0) return tp;
171 for(tp = tape_list; tp != NULL; tp = tp->next) {
178 lookup_last_reusable_tape(
181 tape_t *tp, **tpsave;
184 int tapecycle = getconf_int(CNF_TAPECYCLE);
185 char *labelstr = getconf_str (CNF_LABELSTR);
188 * The idea here is we keep the last "several" reusable tapes we
189 * find in a stack and then return the n-th oldest one to the
190 * caller. If skip is zero, the oldest is returned, if it is
191 * one, the next oldest, two, the next to next oldest and so on.
193 tpsave = alloc((skip + 1) * SIZEOF(*tpsave));
194 for(s = 0; s <= skip; s++) {
197 for(tp = tape_list; tp != NULL; tp = tp->next) {
198 if(tp->reuse == 1 && strcmp(tp->datestamp,"0") != 0 && match (labelstr, tp->label)) {
200 for(s = skip; s > 0; s--) {
201 tpsave[s] = tpsave[s - 1];
206 s = tapecycle - count;
208 if(count < tapecycle - skip) tp = NULL;
209 else tp = tpsave[skip - s];
220 if(tp == NULL) return 0;
221 if(tp->reuse == 0) return 0;
222 if( strcmp(tp->datestamp,"0") == 0) return 1;
224 if(tp->reuse == 1) count++;
227 return (count >= getconf_int(CNF_TAPECYCLE));
234 tape_t *tp, *prev, *next;
236 tp = lookup_tapelabel(label);
243 else /* begin of list */
248 while (next != NULL) {
252 amfree(tp->datestamp);
265 /* insert a new record to the front of the list */
267 new = (tape_t *) alloc(SIZEOF(tape_t));
269 new->datestamp = stralloc(datestamp);
272 new->label = stralloc(label);
275 if(tape_list != NULL) tape_list->prev = new;
276 new->next = tape_list;
279 /* scan list, updating positions */
290 guess_runs_from_tapelist(void)
293 int i, ntapes, tape_ndays, dumpcycle, runtapes, runs;
294 time_t tape_time, today;
297 dumpcycle = getconf_int(CNF_DUMPCYCLE);
298 runtapes = getconf_int(CNF_RUNTAPES);
299 if(runtapes == 0) runtapes = 1; /* just in case */
303 for(i = 1; i < getconf_int(CNF_TAPECYCLE); i++) {
304 if((tp = lookup_tapepos(i)) == NULL) break;
306 tape_time = stamp2time(tp->datestamp);
307 tape_ndays = (int)days_diff(tape_time, today);
309 if(tape_ndays < dumpcycle) ntapes++;
313 if(tape_ndays < dumpcycle) {
314 /* scale for best guess */
315 if(tape_ndays == 0) ntapes = dumpcycle * runtapes;
316 else ntapes = ntapes * dumpcycle / tape_ndays;
318 else if(ntapes == 0) {
319 /* no dumps within the last dumpcycle, guess as above */
320 ntapes = dumpcycle * runtapes;
323 runs = (ntapes + runtapes - 1) / runtapes;
339 tp = (tape_t *) alloc(SIZEOF(tape_t));
347 skip_whitespace(s, ch);
353 skip_non_whitespace(s, ch);
355 tp->datestamp = stralloc(s1);
357 skip_whitespace(s, ch);
359 skip_non_whitespace(s, ch);
361 tp->label = stralloc(s1);
363 skip_whitespace(s, ch);
365 if(strncmp_const(s - 1, "reuse") == 0)
367 if(strncmp_const(s - 1, "no-reuse") == 0)
374 /* insert in reversed datestamp order */
386 while(cur != NULL && strcmp(cur->datestamp, tp->datestamp) >= 0) {
407 * Converts datestamp (an char of the form YYYYMMDD or YYYYMMDDHHMMSS) into a real
409 * Since the datestamp contains no timezone or hh/mm/ss information, the
410 * value is approximate. This is ok for our purposes, since we round off
411 * scheduling calculations to the nearest day.
423 strncpy(date, datestamp, 8);
425 dateint = atoi(date);
427 tm = localtime(&now); /* initialize sec/min/hour & gmtoff */
430 tm = alloc(SIZEOF(struct tm));
440 tm->tm_year = ( dateint / 10000) - 1900;
441 tm->tm_mon = ((dateint % 10000) / 100) - 1;
442 tm->tm_mday = ((dateint % 100) );
452 tape_t *lasttp, *iter;
454 /* Find latest reusable new tape */
455 lasttp = lookup_tapepos(lookup_nb_tape());
456 while (lasttp && lasttp->reuse == 0)
457 lasttp = lasttp->prev;
459 if(lasttp && nb > 0 && strcmp(lasttp->datestamp,"0") == 0) {
462 /* count the number of tapes we *actually* used */
463 while(iter && nb > 0 && strcmp(iter->datestamp,"0") == 0) {
473 _("The next new tape already labelled is: %s.\n"),
477 _("The next %d new tapes already labelled are: %s"),
481 while(iter && c > 0 && strcmp(iter->datestamp,"0") == 0) {
483 g_fprintf(output, ", %s", iter->label);
488 g_fprintf(output, ".\n");