Imported Upstream version 3.3.3
[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  * Copyright (c) 2007-2012 Zmanda, Inc.  All Rights Reserved.
5  * All Rights Reserved.
6  *
7  * Permission to use, copy, modify, distribute, and sell this software and its
8  * documentation for any purpose is hereby granted without fee, provided that
9  * the above copyright notice appear in all copies and that both that
10  * copyright notice and this permission notice appear in supporting
11  * documentation, and that the name of U.M. not be used in advertising or
12  * publicity pertaining to distribution of the software without specific,
13  * written prior permission.  U.M. makes no representations about the
14  * suitability of this software for any purpose.  It is provided "as is"
15  * without express or implied warranty.
16  *
17  * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
19  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
21  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23  *
24  * Authors: the Amanda Development Team.  Its members are listed in a
25  * file named AUTHORS, in the root directory of this distribution.
26  */
27 /*
28  * $Id: amtrmidx.c,v 1.42 2006/07/25 18:27:57 martinea Exp $
29  *
30  * trims number of index files to only those still in system.  Well
31  * actually, it keeps a few extra, plus goes back to the last level 0
32  * dump.
33  */
34
35 #include "amanda.h"
36 #include "arglist.h"
37 #include "conffile.h"
38 #include "diskfile.h"
39 #include "tapefile.h"
40 #include "find.h"
41 #include "util.h"
42
43 static int sort_by_name_reversed(const void *a, const void *b);
44
45 int main(int argc, char **argv);
46
47 static int sort_by_name_reversed(
48     const void *a,
49     const void *b)
50 {
51     char **ap = (char **) a;
52     char **bp = (char **) b;
53
54     return -1 * strcmp(*ap, *bp);
55 }
56
57
58 int
59 main(
60     int         argc,
61     char **     argv)
62 {
63     disk_t *diskp;
64     disklist_t diskl;
65     size_t i;
66     char *conf_diskfile;
67     char *conf_tapelist;
68     char *conf_indexdir;
69     find_result_t *output_find;
70     time_t tmp_time;
71     int amtrmidx_debug = 0;
72     config_overrides_t *cfg_ovr = NULL;
73
74     if (argc > 1 && argv && argv[1] && g_str_equal(argv[1], "--version")) {
75         printf("amtrmidx-%s\n", VERSION);
76         return (0);
77     }
78
79     /*
80      * Configure program for internationalization:
81      *   1) Only set the message locale for now.
82      *   2) Set textdomain for all amanda related programs to "amanda"
83      *      We don't want to be forced to support dozens of message catalogs.
84      */  
85     setlocale(LC_MESSAGES, "C");
86     textdomain("amanda"); 
87
88     safe_fd(-1, 0);
89     safe_cd();
90
91     set_pname("amtrmidx");
92
93     /* Don't die when child closes pipe */
94     signal(SIGPIPE, SIG_IGN);
95
96     dbopen(DBG_SUBDIR_SERVER);
97     dbprintf(_("%s: version %s\n"), argv[0], VERSION);
98
99     cfg_ovr = extract_commandline_config_overrides(&argc, &argv);
100
101     if (argc > 1 && strcmp(argv[1], "-t") == 0) {
102         amtrmidx_debug = 1;
103         argc--;
104         argv++;
105     }
106
107     if (argc < 2) {
108         g_fprintf(stderr, _("Usage: %s [-t] <config> [-o configoption]*\n"), argv[0]);
109         return 1;
110     }
111
112     set_config_overrides(cfg_ovr);
113     config_init(CONFIG_INIT_EXPLICIT_NAME | CONFIG_INIT_USE_CWD, argv[1]);
114
115     conf_diskfile = config_dir_relative(getconf_str(CNF_DISKFILE));
116     read_diskfile(conf_diskfile, &diskl);
117     amfree(conf_diskfile);
118
119     if (config_errors(NULL) >= CFGERR_WARNINGS) {
120         config_print_errors();
121         if (config_errors(NULL) >= CFGERR_ERRORS) {
122             g_critical(_("errors processing config file"));
123         }
124     }
125
126     check_running_as(RUNNING_AS_DUMPUSER);
127
128     dbrename(get_config_name(), DBG_SUBDIR_SERVER);
129
130     conf_tapelist = config_dir_relative(getconf_str(CNF_TAPELIST));
131     if(read_tapelist(conf_tapelist)) {
132         error(_("could not load tapelist \"%s\""), conf_tapelist);
133         /*NOTREACHED*/
134     }
135     amfree(conf_tapelist);
136
137     output_find = find_dump(&diskl);
138
139     conf_indexdir = config_dir_relative(getconf_str(CNF_INDEXDIR));
140
141     /* now go through the list of disks and find which have indexes */
142     time(&tmp_time);
143     tmp_time -= 7*24*60*60;                     /* back one week */
144     for (diskp = diskl.head; diskp != NULL; diskp = diskp->next)
145     {
146         if (diskp->index)
147         {
148             char *indexdir, *qindexdir;
149             DIR *d;
150             struct dirent *f;
151             char **names;
152             size_t name_length;
153             size_t name_count;
154             char *host;
155             char *disk, *qdisk;
156             size_t len_date;
157             disk_t *dp;
158             GSList *matching_dp = NULL;
159
160             /* get listing of indices, newest first */
161             host = sanitise_filename(diskp->host->hostname);
162             disk = sanitise_filename(diskp->name);
163             qdisk = quote_string(diskp->name);
164             indexdir = vstralloc(conf_indexdir, "/",
165                                  host, "/",
166                                  disk, "/",
167                                  NULL);
168             qindexdir = quote_string(indexdir);
169
170             /* find all dles that use the same indexdir */
171             for (dp = diskl.head; dp != NULL; dp = dp->next) {
172                 char *dp_host, *dp_disk;
173
174                 dp_host = sanitise_filename(dp->host->hostname);
175                 dp_disk = sanitise_filename(dp->name);
176                 if (strcmp(host, dp_host) == 0 &&
177                     strcmp(disk, dp_disk) == 0) {
178                     matching_dp = g_slist_append(matching_dp, dp);
179                 }
180                 amfree(dp_host);
181                 amfree(dp_disk);
182             }
183
184             dbprintf("%s %s -> %s\n", diskp->host->hostname,
185                         qdisk, qindexdir);
186             amfree(host);
187             amfree(qdisk);
188             amfree(disk);
189             if ((d = opendir(indexdir)) == NULL) {
190                 dbprintf(_("could not open index directory %s\n"), qindexdir);
191                 amfree(indexdir);
192                 amfree(qindexdir);
193                 g_slist_free(matching_dp);
194                 continue;
195             }
196             name_length = 100;
197             names = (char **)alloc(name_length * SIZEOF(char *));
198             name_count = 0;
199             while ((f = readdir(d)) != NULL) {
200                 size_t l;
201
202                 if(is_dot_or_dotdot(f->d_name)) {
203                     continue;
204                 }
205                 for(i = 0; i < SIZEOF("YYYYMMDDHHMMSS")-1; i++) {
206                     if(! isdigit((int)(f->d_name[i]))) {
207                         break;
208                     }
209                 }
210                 len_date = i;
211                 /* len_date=8  for YYYYMMDD       */
212                 /* len_date=14 for YYYYMMDDHHMMSS */
213                 if((len_date != 8 && len_date != 14)
214                     || f->d_name[len_date] != '_'
215                     || ! isdigit((int)(f->d_name[len_date+1]))) {
216                     continue;                   /* not an index file */
217                 }
218                 /*
219                  * Clear out old index temp files.
220                  */
221                 l = strlen(f->d_name) - (SIZEOF(".tmp")-1);
222                 if ((l > (len_date + 1))
223                         && (strcmp(f->d_name + l, ".tmp")==0)) {
224                     struct stat sbuf;
225                     char *path, *qpath;
226
227                     path = stralloc2(indexdir, f->d_name);
228                     qpath = quote_string(path);
229                     if(lstat(path, &sbuf) != -1
230                         && ((sbuf.st_mode & S_IFMT) == S_IFREG)
231                         && ((time_t)sbuf.st_mtime < tmp_time)) {
232                         dbprintf("rm %s\n", qpath);
233                         if(amtrmidx_debug == 0 && unlink(path) == -1) {
234                             dbprintf(_("Error removing %s: %s\n"),
235                                       qpath, strerror(errno));
236                         }
237                     }
238                     amfree(qpath);
239                     amfree(path);
240                     continue;
241                 }
242                 if(name_count >= name_length) {
243                     char **new_names;
244
245                     new_names = alloc((name_length * 2) * SIZEOF(char *));
246                     memcpy(new_names, names, name_length * SIZEOF(char *));
247                     amfree(names);
248                     names = new_names;
249                     name_length *= 2;
250                 }
251                 names[name_count++] = stralloc(f->d_name);
252             }
253             closedir(d);
254             qsort(names, name_count, SIZEOF(char *), sort_by_name_reversed);
255
256             /*
257              * Search for the first full dump past the minimum number
258              * of index files to keep.
259              */
260             for(i = 0; i < name_count; i++) {
261                 char *datestamp;
262                 int level;
263                 size_t len_date;
264                 int matching = 0;
265                 GSList *mdp;
266
267                 for(len_date = 0; len_date < SIZEOF("YYYYMMDDHHMMSS")-1; len_date++) {
268                     if(! isdigit((int)(names[i][len_date]))) {
269                         break;
270                     }
271                 }
272
273                 datestamp = stralloc(names[i]);
274                 datestamp[len_date] = '\0';
275                 if (sscanf(&names[i][len_date+1], "%d", &level) != 1)
276                     level = 0;
277                 for (mdp = matching_dp; mdp != NULL; mdp = mdp->next) {
278                     dp = mdp->data;
279                     if (dump_exist(output_find, dp->host->hostname,
280                                    dp->name, datestamp, level)) {
281                         matching = 1;
282                     }
283                 }
284                 if (!matching) {
285                     char *path, *qpath;
286                     path = stralloc2(indexdir, names[i]);
287                     qpath = quote_string(path);
288                     dbprintf("rm %s\n", qpath);
289                     if(amtrmidx_debug == 0 && unlink(path) == -1) {
290                         dbprintf(_("Error removing %s: %s\n"),
291                                   qpath, strerror(errno));
292                     }
293                     amfree(qpath);
294                     amfree(path);
295                 }
296                 amfree(datestamp);
297                 amfree(names[i]);
298             }
299             g_slist_free(matching_dp);
300             amfree(names);
301             amfree(indexdir);
302             amfree(qindexdir);
303         }
304     }
305
306     amfree(conf_indexdir);
307     free_find_result(&output_find);
308     clear_tapelist();
309     free_disklist(&diskl);
310
311     dbclose();
312
313     return 0;
314 }