Imported Upstream version 2.5.0
[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.34 2006/01/14 04:37:19 paddy_s 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 P((int, char **));
56
57 int main(argc, argv)
58 int argc;
59 char **argv;
60 {
61     disk_t *diskp;
62     disklist_t diskl;
63     int i;
64     char *conffile;
65     char *conf_diskfile;
66     char *conf_tapelist;
67     char *conf_indexdir;
68     find_result_t *output_find;
69     time_t tmp_time;
70     int amtrmidx_debug = 0;
71
72     safe_fd(-1, 0);
73     safe_cd();
74
75     set_pname("amtrmidx");
76
77     /* Don't die when child closes pipe */
78     signal(SIGPIPE, SIG_IGN);
79
80     dbopen();
81     dbprintf(("%s: version %s\n", argv[0], version()));
82
83     if (argc > 1 && strcmp(argv[1], "-t") == 0) {
84         amtrmidx_debug = 1;
85         argc--;
86         argv++;
87     }
88
89     if (argc < 2) {
90         fprintf(stderr, "Usage: %s [-t] <config>\n", argv[0]);
91         return 1;
92     }
93
94     config_name = argv[1];
95
96     config_dir = vstralloc(CONFIG_DIR, "/", config_name, "/", NULL);
97     conffile = stralloc2(config_dir, CONFFILE_NAME);
98     if (read_conffile(conffile))
99         error("errors processing config file \"%s\"", conffile);
100     amfree(conffile);
101
102     conf_diskfile = getconf_str(CNF_DISKFILE);
103     if(*conf_diskfile == '/') {
104         conf_diskfile = stralloc(conf_diskfile);
105     } else {
106         conf_diskfile = stralloc2(config_dir, conf_diskfile);
107     }
108     if (read_diskfile(conf_diskfile, &diskl) < 0)
109         error("could not load disklist \"%s\"", conf_diskfile);
110     amfree(conf_diskfile);
111
112     conf_tapelist = getconf_str(CNF_TAPELIST);
113     if(*conf_tapelist == '/') {
114         conf_tapelist = stralloc(conf_tapelist);
115     } else {
116         conf_tapelist = stralloc2(config_dir, conf_tapelist);
117     }
118     if(read_tapelist(conf_tapelist))
119         error("could not load tapelist \"%s\"", conf_tapelist);
120     amfree(conf_tapelist);
121
122     output_find = find_dump(1, &diskl);
123
124     conf_indexdir = getconf_str(CNF_INDEXDIR);
125     if(*conf_indexdir == '/') {
126         conf_indexdir = stralloc(conf_indexdir);
127     } else {
128         conf_indexdir = stralloc2(config_dir, conf_indexdir);
129     }
130
131     /* now go through the list of disks and find which have indexes */
132     time(&tmp_time);
133     tmp_time -= 7*24*60*60;                     /* back one week */
134     for (diskp = diskl.head; diskp != NULL; diskp = diskp->next)
135     {
136         if (diskp->index)
137         {
138             char *indexdir;
139             DIR *d;
140             struct dirent *f;
141             char **names;
142             int name_length;
143             int name_count;
144             char *host;
145             char *disk;
146
147             dbprintf(("%s %s\n", diskp->host->hostname, diskp->name));
148
149             /* get listing of indices, newest first */
150             host = sanitise_filename(diskp->host->hostname);
151             disk = sanitise_filename(diskp->name);
152             indexdir = vstralloc(conf_indexdir, "/",
153                                  host, "/",
154                                  disk, "/",
155                                  NULL);
156             amfree(host);
157             amfree(disk);
158             if ((d = opendir(indexdir)) == NULL) {
159                 dbprintf(("could not open index directory \"%s\"\n", indexdir));
160                 amfree(indexdir);
161                 continue;
162             }
163             name_length = 100;
164             names = (char **)alloc(name_length * sizeof(char *));
165             name_count = 0;
166             while ((f = readdir(d)) != NULL) {
167                 int l;
168
169                 if(is_dot_or_dotdot(f->d_name)) {
170                     continue;
171                 }
172                 for(i = 0; i < sizeof("YYYYMMDD")-1; i++) {
173                     if(! isdigit((int)(f->d_name[i]))) {
174                         break;
175                     }
176                 }
177                 if(i < sizeof("YYYYMMDD")-1
178                     || f->d_name[i] != '_'
179                     || ! isdigit((int)(f->d_name[i+1]))) {
180                     continue;                   /* not an index file */
181                 }
182                 /*
183                  * Clear out old index temp files.
184                  */
185                 l = strlen(f->d_name) - (sizeof(".tmp")-1);
186                 if(l > sizeof("YYYYMMDD_L")-1
187                     && strcmp (f->d_name + l, ".tmp") == 0) {
188                     struct stat sbuf;
189                     char *path;
190
191                     path = stralloc2(indexdir, f->d_name);
192                     if(lstat(path, &sbuf) != -1
193                         && (sbuf.st_mode & S_IFMT) == S_IFREG
194                         && sbuf.st_mtime < tmp_time) {
195                         dbprintf(("rm %s\n", path));
196                         if(amtrmidx_debug == 0 && unlink(path) == -1) {
197                             dbprintf(("Error removing \"%s\": %s\n",
198                                       path, strerror(errno)));
199                         }
200                     }
201                     amfree(path);
202                     continue;
203                 }
204                 if(name_count >= name_length) {
205                     char **new_names;
206
207                     new_names = alloc((name_length + 100) * sizeof(char *));
208                     memcpy(new_names, names, name_length * sizeof(char *));
209                     name_length += 100;
210                     amfree(names);
211                     names = new_names;
212                 }
213                 names[name_count++] = stralloc(f->d_name);
214             }
215             closedir(d);
216             qsort(names, name_count, sizeof(char *), sort_by_name_reversed);
217
218             /*
219              * Search for the first full dump past the minimum number
220              * of index files to keep.
221              */
222             for(i = 0; i < name_count; i++) {
223                 if(!dump_exist(output_find,
224                                          diskp->host->hostname,diskp->name,
225                                          atoi(names[i]),
226                                          names[i][sizeof("YYYYMMDD_L")-1-1] - '0')) {
227                     char *path;
228                     path = stralloc2(indexdir, names[i]);
229                     dbprintf(("rm %s\n", path));
230                     if(amtrmidx_debug == 0 && unlink(path) == -1) {
231                         dbprintf(("Error removing \"%s\": %s\n",
232                                   path, strerror(errno)));
233                     }
234                     amfree(path);
235                 }
236                 amfree(names[i]);
237             }
238             amfree(names);
239             amfree(indexdir);
240         }
241     }
242
243     amfree(conf_indexdir);
244     amfree(config_dir);
245     free_find_result(&output_find);
246
247     dbclose();
248
249     return 0;
250 }