Imported Upstream version 2.4.5
[debian/amanda] / server-src / amtrmidx.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: amtrmidx.c,v 1.21.4.1.4.2.2.5.2.1 2004/02/12 18:28:37 martinea Exp $
28  *
29  * trims number of index files to only those still in system.  Well
30  * actually, it keeps a few extra, plus goes back to the last level 0
31  * dump.
32  */
33
34 #include "amanda.h"
35 #include "arglist.h"
36 #ifdef HAVE_NETINET_IN_SYSTM_H
37 #include <netinet/in_systm.h>
38 #endif
39 #include "conffile.h"
40 #include "diskfile.h"
41 #include "tapefile.h"
42 #include "find.h"
43 #include "version.h"
44
45 static int sort_by_name_reversed(a, b)
46 const void *a;
47 const void *b;
48 {
49     char **ap = (char **) a;
50     char **bp = (char **) b;
51
52     return -1 * strcmp(*ap, *bp);
53 }
54
55 int main(argc, argv)
56 int argc;
57 char **argv;
58 {
59     disk_t *diskp;
60     disklist_t *diskl;
61     int i;
62     char *conffile;
63     char *conf_diskfile;
64     char *conf_tapelist;
65     char *conf_indexdir;
66     int fd;
67     find_result_t *output_find;
68     time_t tmp_time;
69     int amtrmidx_debug = 0;
70
71     for(fd = 3; fd < FD_SETSIZE; fd++) {
72         /*
73          * Make sure nobody spoofs us with a lot of extra open files
74          * that would cause an open we do to get a very high file
75          * descriptor, which in turn might be used as an index into
76          * an array (e.g. an fd_set).
77          */
78         close(fd);
79     }
80
81     safe_cd();
82
83     set_pname("amtrmidx");
84
85     dbopen();
86     dbprintf(("%s: version %s\n", argv[0], version()));
87
88     if (argc > 1 && strcmp(argv[1], "-t") == 0) {
89         amtrmidx_debug = 1;
90         argc--;
91         argv++;
92     }
93
94     if (argc < 2) {
95         fprintf(stderr, "Usage: %s [-t] <config>\n", argv[0]);
96         return 1;
97     }
98
99     config_name = argv[1];
100
101     config_dir = vstralloc(CONFIG_DIR, "/", config_name, "/", NULL);
102     conffile = stralloc2(config_dir, CONFFILE_NAME);
103     if(read_conffile(conffile)) {
104         error("errors processing config file \"%s\"", conffile);
105     }
106     amfree(conffile);
107     conf_diskfile = getconf_str(CNF_DISKFILE);
108     if(*conf_diskfile == '/') {
109         conf_diskfile = stralloc(conf_diskfile);
110     } else {
111         conf_diskfile = stralloc2(config_dir, conf_diskfile);
112     }
113     if((diskl = read_diskfile(conf_diskfile)) == NULL) {
114         error("could not load disklist \"%s\".", conf_diskfile);
115     }
116     amfree(conf_diskfile);
117     conf_tapelist = getconf_str(CNF_TAPELIST);
118     if(*conf_tapelist == '/') {
119         conf_tapelist = stralloc(conf_tapelist);
120     } else {
121         conf_tapelist = stralloc2(config_dir, conf_tapelist);
122     }
123     if(read_tapelist(conf_tapelist)) {
124         error("could not load tapelist \"%s\"", conf_tapelist);
125     }
126     amfree(conf_tapelist);
127
128     output_find = find_dump(1, diskl);
129
130     conf_indexdir = getconf_str(CNF_INDEXDIR);
131     if(*conf_indexdir == '/') {
132         conf_indexdir = stralloc(conf_indexdir);
133     } else {
134         conf_indexdir = stralloc2(config_dir, conf_indexdir);
135     }
136
137     /* now go through the list of disks and find which have indexes */
138     time(&tmp_time);
139     tmp_time -= 7*24*60*60;                     /* back one week */
140     for (diskp = diskl->head; diskp != NULL; diskp = diskp->next) {
141         if (diskp->index) {
142             char *indexdir;
143             DIR *d;
144             struct dirent *f;
145             char **names;
146             int name_length;
147             int name_count;
148             char *host;
149             char *disk;
150
151             dbprintf(("%s %s\n", diskp->host->hostname, diskp->name));
152
153             /* get listing of indices, newest first */
154             host = sanitise_filename(diskp->host->hostname);
155             disk = sanitise_filename(diskp->name);
156             indexdir = vstralloc (conf_indexdir, "/",
157                                   host, "/",
158                                   disk, "/",
159                                   NULL);
160             amfree(host);
161             amfree(disk);
162             if((d = opendir(indexdir)) == NULL) {
163                 dbprintf(("could not open index directory \"%s\"\n", indexdir));
164                 amfree(indexdir);
165                 continue;
166             }
167             name_length = 100;
168             names = (char **)alloc(name_length * sizeof(char *));
169             name_count = 0;
170             while ((f = readdir(d)) != NULL) {
171                 int l;
172
173                 if(is_dot_or_dotdot(f->d_name)) {
174                     continue;
175                 }
176                 for(i = 0; i < sizeof("YYYYMMDD")-1; i++) {
177                     if(! isdigit((int)(f->d_name[i]))) {
178                         break;
179                     }
180                 }
181                 if(i < sizeof("YYYYMMDD")-1
182                     || f->d_name[i] != '_'
183                     || ! isdigit((int)(f->d_name[i+1]))) {
184                     continue;                   /* not an index file */
185                 }
186                 /*
187                  * Clear out old index temp files.
188                  */
189                 l = strlen(f->d_name) - (sizeof(".tmp")-1);
190                 if(l > sizeof("YYYYMMDD_L")-1
191                     && strcmp (f->d_name + l, ".tmp") == 0) {
192                     struct stat sbuf;
193                     char *path;
194
195                     path = stralloc2(indexdir, f->d_name);
196                     if(lstat(path, &sbuf) != -1
197                         && (sbuf.st_mode & S_IFMT) == S_IFREG
198                         && sbuf.st_mtime < tmp_time) {
199                         dbprintf(("rm %s\n", path));
200                         if(amtrmidx_debug == 0 && unlink(path) == -1) {
201                             dbprintf(("Error removing \"%s\": %s\n",
202                                       path, strerror(errno)));
203                         }
204                     }
205                     amfree(path);
206                     continue;
207                 }
208                 if(name_count >= name_length) {
209                     char **new_names;
210
211                     new_names = alloc((name_length + 100) * sizeof(char *));
212                     memcpy(new_names, names, name_length * sizeof(char *));
213                     name_length += 100;
214                     amfree(names);
215                     names = new_names;
216                 }
217                 names[name_count++] = stralloc(f->d_name);
218             }
219             closedir(d);
220             qsort(names, name_count, sizeof(char *), sort_by_name_reversed);
221
222             /*
223              * Search for the first full dump past the minimum number
224              * of index files to keep.
225              */
226             for(i = 0; i < name_count; i++) {
227                 if(!dump_exist(output_find,
228                                          diskp->host->hostname,diskp->name,
229                                          atoi(names[i]),
230                                          names[i][sizeof("YYYYMMDD_L")-1-1] - '0')) {
231                     char *path;
232                     path = stralloc2(indexdir, names[i]);
233                     dbprintf(("rm %s\n", path));
234                     if(amtrmidx_debug == 0 && unlink(path) == -1) {
235                         dbprintf(("Error removing \"%s\": %s\n",
236                                   path, strerror(errno)));
237                     }
238                     amfree(path);
239                 }
240                 amfree(names[i]);
241             }
242             amfree(names);
243             amfree(indexdir);
244         }
245     }
246
247     amfree(conf_indexdir);
248     amfree(config_dir);
249     free_find_result(&output_find);
250
251     dbclose();
252
253     return 0;
254 }