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