Imported Upstream version 2.5.1
[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.42 2006/07/25 18:27:57 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 #include "util.h"
45
46 static int sort_by_name_reversed(const void *a, const void *b);
47
48 int main(int argc, char **argv);
49
50 static int sort_by_name_reversed(
51     const void *a,
52     const void *b)
53 {
54     char **ap = (char **) a;
55     char **bp = (char **) b;
56
57     return -1 * strcmp(*ap, *bp);
58 }
59
60
61 int main(int argc, char **argv)
62 {
63     disk_t *diskp;
64     disklist_t diskl;
65     size_t i;
66     char *conffile;
67     char *conf_diskfile;
68     char *conf_tapelist;
69     char *conf_indexdir;
70     find_result_t *output_find;
71     time_t tmp_time;
72     int amtrmidx_debug = 0;
73     int    new_argc,   my_argc;
74     char **new_argv, **my_argv;
75
76     safe_fd(-1, 0);
77     safe_cd();
78
79     set_pname("amtrmidx");
80
81     /* Don't die when child closes pipe */
82     signal(SIGPIPE, SIG_IGN);
83
84     dbopen(DBG_SUBDIR_SERVER);
85     dbprintf(("%s: version %s\n", argv[0], version()));
86
87     parse_server_conf(argc, argv, &new_argc, &new_argv);
88     my_argc = new_argc;
89     my_argv = new_argv;
90
91     if (my_argc > 1 && strcmp(my_argv[1], "-t") == 0) {
92         amtrmidx_debug = 1;
93         my_argc--;
94         my_argv++;
95     }
96
97     if (my_argc < 2) {
98         fprintf(stderr, "Usage: %s [-t] <config> [-o configoption]*\n", my_argv[0]);
99         return 1;
100     }
101
102     config_name = my_argv[1];
103
104     config_dir = vstralloc(CONFIG_DIR, "/", config_name, "/", NULL);
105     conffile = stralloc2(config_dir, CONFFILE_NAME);
106     if (read_conffile(conffile)) {
107         error("errors processing config file \"%s\"", conffile);
108         /*NOTREACHED*/
109     }
110     amfree(conffile);
111
112     dbrename(config_name, DBG_SUBDIR_SERVER);
113
114     report_bad_conf_arg();
115
116     conf_diskfile = getconf_str(CNF_DISKFILE);
117     if(*conf_diskfile == '/') {
118         conf_diskfile = stralloc(conf_diskfile);
119     } else {
120         conf_diskfile = stralloc2(config_dir, conf_diskfile);
121     }
122     if (read_diskfile(conf_diskfile, &diskl) < 0) {
123         error("could not load disklist \"%s\"", conf_diskfile);
124         /*NOTREACHED*/
125     }
126     amfree(conf_diskfile);
127
128     conf_tapelist = getconf_str(CNF_TAPELIST);
129     if(*conf_tapelist == '/') {
130         conf_tapelist = stralloc(conf_tapelist);
131     } else {
132         conf_tapelist = stralloc2(config_dir, conf_tapelist);
133     }
134     if(read_tapelist(conf_tapelist)) {
135         error("could not load tapelist \"%s\"", conf_tapelist);
136         /*NOTREACHED*/
137     }
138     amfree(conf_tapelist);
139
140     output_find = find_dump(1, &diskl);
141
142     conf_indexdir = getconf_str(CNF_INDEXDIR);
143     if(*conf_indexdir == '/') {
144         conf_indexdir = stralloc(conf_indexdir);
145     } else {
146         conf_indexdir = stralloc2(config_dir, conf_indexdir);
147     }
148
149     /* now go through the list of disks and find which have indexes */
150     time(&tmp_time);
151     tmp_time -= 7*24*60*60;                     /* back one week */
152     for (diskp = diskl.head; diskp != NULL; diskp = diskp->next)
153     {
154         if (diskp->index)
155         {
156             char *indexdir, *qindexdir;
157             DIR *d;
158             struct dirent *f;
159             char **names;
160             size_t name_length;
161             size_t name_count;
162             char *host;
163             char *disk, *qdisk;
164             size_t len_date;
165
166
167             /* get listing of indices, newest first */
168             host = sanitise_filename(diskp->host->hostname);
169             disk = sanitise_filename(diskp->name);
170             qdisk = quote_string(diskp->name);
171             indexdir = vstralloc(conf_indexdir, "/",
172                                  host, "/",
173                                  disk, "/",
174                                  NULL);
175             qindexdir = quote_string(indexdir);
176
177             dbprintf(("%s %s -> %s\n", diskp->host->hostname,
178                         qdisk, qindexdir));
179             amfree(host);
180             amfree(qdisk);
181             amfree(disk);
182             if ((d = opendir(indexdir)) == NULL) {
183                 dbprintf(("could not open index directory %s\n", qindexdir));
184                 amfree(indexdir);
185                 amfree(qindexdir);
186                 continue;
187             }
188             name_length = 100;
189             names = (char **)alloc(name_length * SIZEOF(char *));
190             name_count = 0;
191             while ((f = readdir(d)) != NULL) {
192                 size_t l;
193
194                 if(is_dot_or_dotdot(f->d_name)) {
195                     continue;
196                 }
197                 for(i = 0; i < SIZEOF("YYYYMMDDHHMMSS")-1; i++) {
198                     if(! isdigit((int)(f->d_name[i]))) {
199                         break;
200                     }
201                 }
202                 len_date = i;
203                 /* len_date=8  for YYYYMMDD       */
204                 /* len_date=14 for YYYYMMDDHHMMSS */
205                 if((len_date != 8 && len_date != 14)
206                     || f->d_name[len_date] != '_'
207                     || ! isdigit((int)(f->d_name[len_date+1]))) {
208                     continue;                   /* not an index file */
209                 }
210                 /*
211                  * Clear out old index temp files.
212                  */
213                 l = strlen(f->d_name) - (SIZEOF(".tmp")-1);
214                 if ((l > (len_date + 1))
215                         && (strcmp(f->d_name + l, ".tmp")==0)) {
216                     struct stat sbuf;
217                     char *path, *qpath;
218
219                     path = stralloc2(indexdir, f->d_name);
220                     qpath = quote_string(path);
221                     if(lstat(path, &sbuf) != -1
222                         && ((sbuf.st_mode & S_IFMT) == S_IFREG)
223                         && ((time_t)sbuf.st_mtime < tmp_time)) {
224                         dbprintf(("rm %s\n", qpath));
225                         if(amtrmidx_debug == 0 && unlink(path) == -1) {
226                             dbprintf(("Error removing %s: %s\n",
227                                       qpath, strerror(errno)));
228                         }
229                     }
230                     amfree(qpath);
231                     amfree(path);
232                     continue;
233                 }
234                 if(name_count >= name_length) {
235                     char **new_names;
236
237                     new_names = alloc((name_length * 2) * SIZEOF(char *));
238                     memcpy(new_names, names, name_length * SIZEOF(char *));
239                     amfree(names);
240                     names = new_names;
241                     name_length *= 2;
242                 }
243                 names[name_count++] = stralloc(f->d_name);
244             }
245             closedir(d);
246             qsort(names, name_count, SIZEOF(char *), sort_by_name_reversed);
247
248             /*
249              * Search for the first full dump past the minimum number
250              * of index files to keep.
251              */
252             for(i = 0; i < name_count; i++) {
253                 char *datestamp;
254                 int level;
255                 size_t len_date;
256
257                 for(len_date = 0; len_date < SIZEOF("YYYYMMDDHHMMSS")-1; len_date++) {
258                     if(! isdigit((int)(names[i][len_date]))) {
259                         break;
260                     }
261                 }
262
263                 datestamp = stralloc(names[i]);
264                 datestamp[len_date] = '\0';
265                 level = names[i][len_date+1] - '0';
266                 if(!dump_exist(output_find, diskp->host->hostname,
267                                 diskp->name, datestamp, level)) {
268                     char *path, *qpath;
269                     path = stralloc2(indexdir, names[i]);
270                     qpath = quote_string(path);
271                     dbprintf(("rm %s\n", qpath));
272                     if(amtrmidx_debug == 0 && unlink(path) == -1) {
273                         dbprintf(("Error removing %s: %s\n",
274                                   qpath, strerror(errno)));
275                     }
276                     amfree(qpath);
277                     amfree(path);
278                 }
279                 amfree(datestamp);
280                 amfree(names[i]);
281             }
282             amfree(names);
283             amfree(indexdir);
284             amfree(qindexdir);
285         }
286     }
287
288     amfree(conf_indexdir);
289     amfree(config_dir);
290     free_find_result(&output_find);
291     clear_tapelist();
292     free_disklist(&diskl);
293     free_new_argv(new_argc, new_argv);
294     free_server_config();
295
296     dbclose();
297
298     return 0;
299 }