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