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