Imported Upstream version 3.3.2
[debian/amanda] / server-src / tapefile.c
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 1991-1998 University of Maryland at College Park
4  * All Rights Reserved.
5  *
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.
15  *
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.
22  *
23  * Authors: the Amanda Development Team.  Its members are listed in a
24  * file named AUTHORS, in the root directory of this distribution.
25  */
26 /*
27  * $Id: tapefile.c,v 1.37 2006/07/21 00:25:52 martinea Exp $
28  *
29  * routines to read and write the amanda active tape list
30  */
31 #include "amanda.h"
32 #include "match.h"
33 #include "tapefile.h"
34 #include "conffile.h"
35
36 static tape_t *tape_list = NULL;
37
38 /* local functions */
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);
42
43 int
44 read_tapelist(
45     char *tapefile)
46 {
47     tape_t *tp;
48     FILE *tapef;
49     int pos;
50     char *line = NULL;
51     int status = 0;
52
53     clear_tapelist();
54     if((tapef = fopen(tapefile,"r")) == NULL) {
55         if (errno == ENOENT) {
56             /* no tapelist is equivalent to an empty tapelist */
57             return 0;
58         } else {
59             g_debug("Error opening '%s': %s", tapefile, strerror(errno));
60             return 1;
61         }
62     }
63
64     while((line = agets(tapef)) != NULL) {
65         if (line[0] == '\0') {
66             amfree(line);
67             continue;
68         }
69         tp = parse_tapeline(&status, line);
70         amfree(line);
71         if(tp == NULL && status != 0)
72             return 1;
73         if(tp != NULL)
74             tape_list = insert(tape_list, tp);
75     }
76     afclose(tapef);
77
78     for(pos=1,tp=tape_list; tp != NULL; pos++,tp=tp->next) {
79         tp->position = pos;
80     }
81
82     return 0;
83 }
84
85 int
86 write_tapelist(
87     char *tapefile)
88 {
89     tape_t *tp;
90     FILE *tapef;
91     char *newtapefile;
92     int rc;
93
94     newtapefile = stralloc2(tapefile, ".new");
95
96     if((tapef = fopen(newtapefile,"w")) == NULL) {
97         amfree(newtapefile);
98         return 1;
99     }
100
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");
105         if (tp->barcode)
106             g_fprintf(tapef, " BARCODE:%s", tp->barcode);
107         if (tp->meta)
108             g_fprintf(tapef, " META:%s", tp->meta);
109         if (tp->blocksize)
110             g_fprintf(tapef, " BLOCKSIZE:%jd", (intmax_t)tp->blocksize);
111         if (tp->comment)
112             g_fprintf(tapef, " #%s", tp->comment);
113         g_fprintf(tapef, "\n");
114     }
115
116     if (fclose(tapef) == EOF) {
117         g_fprintf(stderr,_("error [closing %s: %s]"), newtapefile, strerror(errno));
118         amfree(newtapefile);
119         return 1;
120     }
121     rc = rename(newtapefile, tapefile);
122     amfree(newtapefile);
123
124     return(rc != 0);
125 }
126
127 void
128 clear_tapelist(void)
129 {
130     tape_t *tp, *next;
131
132     for(tp = tape_list; tp; tp = next) {
133         amfree(tp->label);
134         amfree(tp->datestamp);
135         amfree(tp->barcode);
136         amfree(tp->meta);
137         amfree(tp->comment);
138         next = tp->next;
139         amfree(tp);
140     }
141     tape_list = NULL;
142 }
143
144 tape_t *
145 lookup_tapelabel(
146     const char *label)
147 {
148     tape_t *tp;
149
150     for(tp = tape_list; tp != NULL; tp = tp->next) {
151         if(strcmp(label, tp->label) == 0) return tp;
152     }
153     return NULL;
154 }
155
156
157
158 tape_t *
159 lookup_tapepos(
160     int pos)
161 {
162     tape_t *tp;
163
164     for(tp = tape_list; tp != NULL; tp = tp->next) {
165         if(tp->position == pos) return tp;
166     }
167     return NULL;
168 }
169
170
171 tape_t *
172 lookup_tapedate(
173     char *datestamp)
174 {
175     tape_t *tp;
176
177     for(tp = tape_list; tp != NULL; tp = tp->next) {
178         if(strcmp(tp->datestamp, datestamp) == 0) return tp;
179     }
180     return NULL;
181 }
182
183 int
184 lookup_nb_tape(void)
185 {
186     tape_t *tp;
187     int pos=0;
188
189     for(tp = tape_list; tp != NULL; tp = tp->next) {
190         pos=tp->position;
191     }
192     return pos;
193 }
194
195
196 char *
197 get_last_reusable_tape_label(
198      int skip)
199 {
200     tape_t *tp = lookup_last_reusable_tape(skip);
201     return (tp != NULL) ? tp->label : NULL;
202 }
203
204 tape_t *
205 lookup_last_reusable_tape(
206      int skip)
207 {
208     tape_t *tp, **tpsave;
209     int count=0;
210     int s;
211     int tapecycle = getconf_int(CNF_TAPECYCLE);
212     char *labelstr = getconf_str (CNF_LABELSTR);
213
214     /*
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.
219      */
220     tpsave = alloc((skip + 1) * SIZEOF(*tpsave));
221     for(s = 0; s <= skip; s++) {
222         tpsave[s] = NULL;
223     }
224     for(tp = tape_list; tp != NULL; tp = tp->next) {
225         if(tp->reuse == 1 && strcmp(tp->datestamp,"0") != 0 && match (labelstr, tp->label)) {
226             count++;
227             for(s = skip; s > 0; s--) {
228                 tpsave[s] = tpsave[s - 1];
229             }
230             tpsave[0] = tp;
231         }
232     }
233     s = tapecycle - count;
234     if(s < 0) s = 0;
235     if(count < tapecycle - skip) tp = NULL;
236     else tp = tpsave[skip - s];
237     amfree(tpsave);
238     return tp;
239 }
240
241 int
242 reusable_tape(
243     tape_t *tp)
244 {
245     int count = 0;
246
247     if(tp == NULL) return 0;
248     if(tp->reuse == 0) return 0;
249     if( strcmp(tp->datestamp,"0") == 0) return 1;
250     while(tp != NULL) {
251         if(tp->reuse == 1) count++;
252         tp = tp->prev;
253     }
254     return (count >= getconf_int(CNF_TAPECYCLE));
255 }
256
257 void
258 remove_tapelabel(
259     char *label)
260 {
261     tape_t *tp, *prev, *next;
262
263     tp = lookup_tapelabel(label);
264     if(tp != NULL) {
265         prev = tp->prev;
266         next = tp->next;
267         /*@ignore@*/
268         if(prev != NULL)
269             prev->next = next;
270         else /* begin of list */
271             tape_list = next;
272         if(next != NULL)
273             next->prev = prev;
274         /*@end@*/
275         while (next != NULL) {
276             next->position--;
277             next = next->next;
278         }
279         amfree(tp->datestamp);
280         amfree(tp->label);
281         amfree(tp->meta);
282         amfree(tp->comment);
283         amfree(tp->barcode);
284         amfree(tp);
285     }
286 }
287
288 tape_t *
289 add_tapelabel(
290     char *datestamp,
291     char *label,
292     char *comment)
293 {
294     tape_t *cur, *new;
295
296     /* insert a new record to the front of the list */
297
298     new = g_new0(tape_t, 1);
299
300     new->datestamp = stralloc(datestamp);
301     new->position = 0;
302     new->reuse = 1;
303     new->label = stralloc(label);
304     new->comment = comment? stralloc(comment) : NULL;
305
306     new->prev  = NULL;
307     if(tape_list != NULL) tape_list->prev = new;
308     new->next = tape_list;
309     tape_list = new;
310
311     /* scan list, updating positions */
312     cur = tape_list;
313     while(cur != NULL) {
314         cur->position++;
315         cur = cur->next;
316     }
317
318     return new;
319 }
320
321 int
322 guess_runs_from_tapelist(void)
323 {
324     tape_t *tp;
325     int i, ntapes, tape_ndays, dumpcycle, runtapes, runs;
326     time_t tape_time, today;
327
328     today = time(0);
329     dumpcycle = getconf_int(CNF_DUMPCYCLE);
330     runtapes = getconf_int(CNF_RUNTAPES);
331     if(runtapes == 0) runtapes = 1;     /* just in case */
332
333     ntapes = 0;
334     tape_ndays = 0;
335     for(i = 1; i < getconf_int(CNF_TAPECYCLE); i++) {
336         if((tp = lookup_tapepos(i)) == NULL) break;
337
338         tape_time  = stamp2time(tp->datestamp);
339         tape_ndays = (int)days_diff(tape_time, today);
340
341         if(tape_ndays < dumpcycle) ntapes++;
342         else break;
343     }
344
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;
349     }
350     else if(ntapes == 0) {
351         /* no dumps within the last dumpcycle, guess as above */
352         ntapes = dumpcycle * runtapes;
353     }
354
355     runs = (ntapes + runtapes - 1) / runtapes;
356     if (runs <= 0)
357       runs = 1;
358     return runs;
359 }
360
361 static tape_t *
362 parse_tapeline(
363     int *status,
364     char *line)
365 {
366     tape_t *tp = NULL;
367     char *s, *s1;
368     int ch;
369
370     *status = 0;
371
372     s = line;
373     ch = *s++;
374
375     skip_whitespace(s, ch);
376     if(ch == '\0') {
377         return NULL;
378     }
379
380     tp = g_new0(tape_t, 1);
381
382     s1 = s - 1;
383     skip_non_whitespace(s, ch);
384     s[-1] = '\0';
385     tp->datestamp = stralloc(s1);
386
387     skip_whitespace(s, ch);
388     s1 = s - 1;
389     skip_non_whitespace(s, ch);
390     s[-1] = '\0';
391     tp->label = stralloc(s1);
392
393     skip_whitespace(s, ch);
394     tp->reuse = 1;
395     if(strncmp_const(s - 1, "reuse") == 0) {
396         tp->reuse = 1;
397         s1 = s - 1;
398         skip_non_whitespace(s, ch);
399         s[-1] = '\0';
400         skip_whitespace(s, ch);
401     }
402     if(strncmp_const(s - 1, "no-reuse") == 0) {
403         tp->reuse = 0;
404         s1 = s - 1;
405         skip_non_whitespace(s, ch);
406         s[-1] = '\0';
407         skip_whitespace(s, ch);
408     }
409
410     if (strncmp_const(s - 1, "BARCODE:") == 0) {
411         s1 = s - 1 + 8;
412         skip_non_whitespace(s, ch);
413         s[-1] = '\0';
414         skip_whitespace(s, ch);
415         tp->barcode = stralloc(s1);
416     }
417
418     if (strncmp_const(s - 1, "META:") == 0) {
419         s1 = s - 1 + 5;
420         skip_non_whitespace(s, ch);
421         s[-1] = '\0';
422         skip_whitespace(s, ch);
423         tp->meta = stralloc(s1);
424     }
425
426     if (strncmp_const(s - 1, "BLOCKSIZE:") == 0) {
427         s1 = s - 1 + 10;
428         skip_non_whitespace(s, ch);
429         s[-1] = '\0';
430         skip_whitespace(s, ch);
431         tp->blocksize = atol(s1);
432     }
433     if (*(s - 1) == '#') {
434         tp->comment = stralloc(s); /* skip leading '#' */
435     }
436
437     return tp;
438 }
439
440
441 /* insert in reversed datestamp order */
442 /*@ignore@*/
443 static tape_t *
444 insert(
445     tape_t *list,
446     tape_t *tp)
447 {
448     tape_t *prev, *cur;
449
450     prev = NULL;
451     cur = list;
452
453     while(cur != NULL && strcmp(cur->datestamp, tp->datestamp) >= 0) {
454         prev = cur;
455         cur = cur->next;
456     }
457     tp->prev = prev;
458     tp->next = cur;
459     if(prev == NULL) {
460         list = tp;
461 #ifndef __lint
462     } else {
463         prev->next = tp;
464 #endif
465     }
466     if(cur !=NULL)
467         cur->prev = tp;
468
469     return list;
470 }
471 /*@end@*/
472
473 /*
474  * Converts datestamp (an char of the form YYYYMMDD or YYYYMMDDHHMMSS) into a real
475  * time_t value.
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.
479  */
480
481 static time_t
482 stamp2time(
483     char *datestamp)
484 {
485     struct tm *tm;
486     time_t now;
487     char date[9];
488     int dateint;
489
490     strncpy(date, datestamp, 8);
491     date[8] = '\0';
492     dateint = atoi(date);
493     now = time(0);
494     tm = localtime(&now);       /* initialize sec/min/hour & gmtoff */
495
496     if (!tm) {
497         tm = alloc(SIZEOF(struct tm));
498         tm->tm_sec   = 0;
499         tm->tm_min   = 0;
500         tm->tm_hour  = 0;
501         tm->tm_wday  = 0;
502         tm->tm_yday  = 0;
503         tm->tm_isdst = 0;
504     }
505
506
507     tm->tm_year = ( dateint          / 10000) - 1900;
508     tm->tm_mon  = ((dateint % 10000) /   100) - 1;
509     tm->tm_mday = ((dateint %   100)        );
510
511     return mktime(tm);
512 }
513
514 char *
515 list_new_tapes(
516     int nb)
517 {
518     tape_t *lasttp, *iter;
519     char *result = NULL;
520
521     /* Find latest reusable new tape */
522     lasttp = lookup_tapepos(lookup_nb_tape());
523     while (lasttp && lasttp->reuse == 0)
524         lasttp = lasttp->prev;
525
526     if(lasttp && nb > 0 && strcmp(lasttp->datestamp,"0") == 0) {
527         int c = 0;
528         iter = lasttp;
529         /* count the number of tapes we *actually* used */
530         while(iter && nb > 0 && strcmp(iter->datestamp,"0") == 0) {
531             if (iter->reuse) {
532                 c++;
533                 nb--;
534             }
535             iter = iter->prev;
536         }
537
538         if(c == 1) {
539             result = g_strdup_printf(
540                         _("The next new tape already labelled is: %s."),
541                         lasttp->label);
542         } else {
543             result = g_strdup_printf(
544                         _("The next %d new tapes already labelled are: %s"),
545                         c, lasttp->label);
546             iter = lasttp->prev;
547             c--;
548             while(iter && c > 0 && strcmp(iter->datestamp,"0") == 0) {
549                 if (iter->reuse) {
550                     result = vstrextend(&result, ", ", iter->label, NULL);
551                     c--;
552                 }
553                 iter = iter->prev;
554             }
555         }
556     }
557     return result;
558 }
559
560 void
561 print_new_tapes(
562     FILE *output,
563     int   nb)
564 {
565     char *result = list_new_tapes(nb);
566
567     if (result) {
568         g_fprintf(output,"%s\n", result);
569         amfree(result);
570     }
571 }