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) {
54 if (errno == ENOENT) {
55 /* no tapelist is equivalent to an empty tapelist */
58 g_debug("Error opening '%s': %s", tapefile, strerror(errno));
63 while((line = agets(tapef)) != NULL) {
64 if (line[0] == '\0') {
68 tp = parse_tapeline(&status, line);
70 if(tp == NULL && status != 0)
73 tape_list = insert(tape_list, tp);
77 for(pos=1,tp=tape_list; tp != NULL; pos++,tp=tp->next) {
93 newtapefile = stralloc2(tapefile, ".new");
95 if((tapef = fopen(newtapefile,"w")) == NULL) {
100 for(tp = tape_list; tp != NULL; tp = tp->next) {
101 g_fprintf(tapef, "%s %s", tp->datestamp, tp->label);
102 if(tp->reuse) g_fprintf(tapef, " reuse");
103 else g_fprintf(tapef, " no-reuse");
105 g_fprintf(tapef, " #%s", tp->comment);
106 g_fprintf(tapef, "\n");
109 if (fclose(tapef) == EOF) {
110 g_fprintf(stderr,_("error [closing %s: %s]"), newtapefile, strerror(errno));
114 rc = rename(newtapefile, tapefile);
125 for(tp = tape_list; tp; tp = next) {
127 amfree(tp->datestamp);
140 for(tp = tape_list; tp != NULL; tp = tp->next) {
141 if(strcmp(label, tp->label) == 0) return tp;
154 for(tp = tape_list; tp != NULL; tp = tp->next) {
155 if(tp->position == pos) return tp;
167 for(tp = tape_list; tp != NULL; tp = tp->next) {
168 if(strcmp(tp->datestamp, datestamp) == 0) return tp;
179 for(tp = tape_list; tp != NULL; tp = tp->next) {
186 lookup_last_reusable_tape(
189 tape_t *tp, **tpsave;
192 int tapecycle = getconf_int(CNF_TAPECYCLE);
193 char *labelstr = getconf_str (CNF_LABELSTR);
196 * The idea here is we keep the last "several" reusable tapes we
197 * find in a stack and then return the n-th oldest one to the
198 * caller. If skip is zero, the oldest is returned, if it is
199 * one, the next oldest, two, the next to next oldest and so on.
201 tpsave = alloc((skip + 1) * SIZEOF(*tpsave));
202 for(s = 0; s <= skip; s++) {
205 for(tp = tape_list; tp != NULL; tp = tp->next) {
206 if(tp->reuse == 1 && strcmp(tp->datestamp,"0") != 0 && match (labelstr, tp->label)) {
208 for(s = skip; s > 0; s--) {
209 tpsave[s] = tpsave[s - 1];
214 s = tapecycle - count;
216 if(count < tapecycle - skip) tp = NULL;
217 else tp = tpsave[skip - s];
228 if(tp == NULL) return 0;
229 if(tp->reuse == 0) return 0;
230 if( strcmp(tp->datestamp,"0") == 0) return 1;
232 if(tp->reuse == 1) count++;
235 return (count >= getconf_int(CNF_TAPECYCLE));
242 tape_t *tp, *prev, *next;
244 tp = lookup_tapelabel(label);
251 else /* begin of list */
256 while (next != NULL) {
260 amfree(tp->datestamp);
274 /* insert a new record to the front of the list */
276 new = (tape_t *) alloc(SIZEOF(tape_t));
278 new->datestamp = stralloc(datestamp);
281 new->label = stralloc(label);
282 new->comment = comment? stralloc(comment) : NULL;
285 if(tape_list != NULL) tape_list->prev = new;
286 new->next = tape_list;
289 /* scan list, updating positions */
300 guess_runs_from_tapelist(void)
303 int i, ntapes, tape_ndays, dumpcycle, runtapes, runs;
304 time_t tape_time, today;
307 dumpcycle = getconf_int(CNF_DUMPCYCLE);
308 runtapes = getconf_int(CNF_RUNTAPES);
309 if(runtapes == 0) runtapes = 1; /* just in case */
313 for(i = 1; i < getconf_int(CNF_TAPECYCLE); i++) {
314 if((tp = lookup_tapepos(i)) == NULL) break;
316 tape_time = stamp2time(tp->datestamp);
317 tape_ndays = (int)days_diff(tape_time, today);
319 if(tape_ndays < dumpcycle) ntapes++;
323 if(tape_ndays < dumpcycle) {
324 /* scale for best guess */
325 if(tape_ndays == 0) ntapes = dumpcycle * runtapes;
326 else ntapes = ntapes * dumpcycle / tape_ndays;
328 else if(ntapes == 0) {
329 /* no dumps within the last dumpcycle, guess as above */
330 ntapes = dumpcycle * runtapes;
333 runs = (ntapes + runtapes - 1) / runtapes;
349 tp = (tape_t *) alloc(SIZEOF(tape_t));
357 skip_whitespace(s, ch);
363 skip_non_whitespace(s, ch);
365 tp->datestamp = stralloc(s1);
367 skip_whitespace(s, ch);
369 skip_non_whitespace(s, ch);
371 tp->label = stralloc(s1);
373 skip_whitespace(s, ch);
375 if(strncmp_const(s - 1, "reuse") == 0) {
378 skip_non_whitespace(s, ch);
380 skip_whitespace(s, ch);
382 if(strncmp_const(s - 1, "no-reuse") == 0) {
385 skip_non_whitespace(s, ch);
387 skip_whitespace(s, ch);
390 if (*(s - 1) == '#') {
391 tp->comment = stralloc(s); /* skip leading '#' */
400 /* insert in reversed datestamp order */
412 while(cur != NULL && strcmp(cur->datestamp, tp->datestamp) >= 0) {
433 * Converts datestamp (an char of the form YYYYMMDD or YYYYMMDDHHMMSS) into a real
435 * Since the datestamp contains no timezone or hh/mm/ss information, the
436 * value is approximate. This is ok for our purposes, since we round off
437 * scheduling calculations to the nearest day.
449 strncpy(date, datestamp, 8);
451 dateint = atoi(date);
453 tm = localtime(&now); /* initialize sec/min/hour & gmtoff */
456 tm = alloc(SIZEOF(struct tm));
466 tm->tm_year = ( dateint / 10000) - 1900;
467 tm->tm_mon = ((dateint % 10000) / 100) - 1;
468 tm->tm_mday = ((dateint % 100) );
478 tape_t *lasttp, *iter;
480 /* Find latest reusable new tape */
481 lasttp = lookup_tapepos(lookup_nb_tape());
482 while (lasttp && lasttp->reuse == 0)
483 lasttp = lasttp->prev;
485 if(lasttp && nb > 0 && strcmp(lasttp->datestamp,"0") == 0) {
488 /* count the number of tapes we *actually* used */
489 while(iter && nb > 0 && strcmp(iter->datestamp,"0") == 0) {
499 _("The next new tape already labelled is: %s.\n"),
503 _("The next %d new tapes already labelled are: %s"),
507 while(iter && c > 0 && strcmp(iter->datestamp,"0") == 0) {
509 g_fprintf(output, ", %s", iter->label);
514 g_fprintf(output, ".\n");