6c04a6fd7454e633d4c4468658b73c5986b51467
[debian/amanda] / server-src / disk_history.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 /* $Id: disk_history.c,v 1.13 2006/05/25 01:47:19 johnfranks Exp $
27  *
28  * functions for obtaining backup history
29  */
30
31 #include "amanda.h"
32 #include "disk_history.h"
33
34 static DUMP_ITEM *disk_hist = NULL;
35
36 void
37 clear_list(void)
38 {
39     DUMP_ITEM *item, *this;
40
41     item = disk_hist;
42     while (item != NULL)
43     {
44         this = item;
45         item = item->next;
46         amfree(this->hostname);
47         while(this->tapes != NULL) {
48             tapelist_t *tapes = this->tapes;
49             this->tapes = tapes->next;
50             amfree(tapes->label);
51             amfree(tapes->files);
52             amfree(tapes);
53         }
54         amfree(this);
55     }
56     disk_hist = NULL;
57 }
58
59 /* add item, maintain list ordered by oldest date last */
60
61 void
62 add_dump(
63     char *      hostname,
64     char *      date,
65     int         level,
66     char *      tape,
67     off_t       file,
68     int         partnum,
69     int         maxpart)
70 {
71     DUMP_ITEM *new, *item, *before;
72     int isafile = 0;
73
74     if(tape[0] == '/')
75         isafile = 1; /* XXX kludgey, like this whole thing */
76
77     /* See if we already have partnum=partnum-1 */
78     if (partnum > 1) {
79         int partnum_minus_1 = 0;
80         for(item = disk_hist, before = NULL; item;
81             before = item, item = item->next) {
82             if (!strcmp(item->date, date) &&
83                     item->level == level && item->is_split) {
84                 tapelist_t *cur_tape;
85                 for (cur_tape = item->tapes; cur_tape;
86                                              cur_tape = cur_tape->next) {
87                     int files;
88                     for(files=0; files<cur_tape->numfiles; files++) {
89                         if (cur_tape->partnum[files] == partnum - 1)
90                             partnum_minus_1 = 1;
91                     }
92                 }
93                 if (partnum_minus_1 == 1) {
94                     item->tapes = append_to_tapelist(item->tapes, tape, file,
95                                                      partnum, isafile);
96                     if (maxpart > item->maxpart)
97                         item->maxpart = maxpart;
98                 } else {
99                     /* some part are missing, remove the item from disk_hist */
100                     if (before)
101                         before->next = item->next;
102                     else
103                         disk_hist = item->next;
104                     /* free item */
105                     free_tapelist(item->tapes);
106                     amfree(item->hostname);
107                     amfree(item);
108                 }
109                 return;
110             }
111         }
112         return;
113     }
114
115     new = (DUMP_ITEM *)alloc(SIZEOF(DUMP_ITEM));
116     strncpy(new->date, date, SIZEOF(new->date)-1);
117     new->date[SIZEOF(new->date)-1] = '\0';
118     new->level = level;
119     strncpy(new->tape, tape, SIZEOF(new->tape)-1);
120     new->tape[SIZEOF(new->tape)-1] = '\0';
121     new->file = file;
122     new->maxpart = maxpart;
123     if(partnum == -1)
124         new->is_split = 0;
125     else
126         new->is_split = 1;
127     new->tapes = NULL;
128     new->hostname = stralloc(hostname);
129
130     new->tapes = append_to_tapelist(new->tapes, tape, file, partnum, isafile);
131
132     if (disk_hist == NULL)
133     {
134         disk_hist = new;
135         new->next = NULL;
136         return;
137     }
138
139     /* prepend this item to the history list, if it's newer */
140     /* XXX this should probably handle them being on the same date with
141        datestamp_uax or something */
142     if (strcmp(disk_hist->date, new->date) <= 0)
143     {
144         new->next = disk_hist;
145         disk_hist = new;
146         return;
147     }
148
149     /* append this item to the history list, if it's older */
150     before = disk_hist;
151     item = disk_hist->next;
152     while ((item != NULL) && (strcmp(item->date, new->date) > 0))
153     {
154         before = item;
155         item = item->next;
156     }
157     new->next = item;
158     before->next = new;
159 }
160
161 void
162 clean_dump(void)
163 {
164     DUMP_ITEM *item, *before;
165
166     /* check if the maxpart part is avaliable */
167     for(item = disk_hist, before = NULL; item;
168                                          before = item, item = item->next) {
169         int found_maxpart = 0;
170         tapelist_t *cur_tape;
171
172         if (item->maxpart > 1) {
173             for (cur_tape = item->tapes; cur_tape; cur_tape = cur_tape->next) {
174                 int files;
175                 for(files=0; files<cur_tape->numfiles; files++) {
176                     if (cur_tape->partnum[files] == item->maxpart) {
177                         found_maxpart = 1;
178                     }
179                 }
180             }
181             if (found_maxpart == 0) {
182                 DUMP_ITEM *myitem = item; 
183
184                 if (before)
185                     before->next = item->next;
186                 else
187                     disk_hist = item->next;
188                 item = item->next;
189                 /* free myitem */
190                 free_tapelist(myitem->tapes);
191                 amfree(myitem->hostname);
192                 amfree(myitem);
193                 if (item == NULL)
194                     break;
195             }
196         }
197     }
198 }
199
200 DUMP_ITEM *
201 first_dump(void)
202 {
203     return disk_hist;
204 }