2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 1991-1998 University of Maryland at College Park
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.
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.
23 * Authors: the Amanda Development Team. Its members are listed in a
24 * file named AUTHORS, in the root directory of this distribution.
27 * $Id: amtrmidx.c,v 1.42 2006/07/25 18:27:57 martinea Exp $
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
43 static int sort_by_name_reversed(const void *a, const void *b);
45 int main(int argc, char **argv);
47 static int sort_by_name_reversed(
51 char **ap = (char **) a;
52 char **bp = (char **) b;
54 return -1 * strcmp(*ap, *bp);
69 find_result_t *output_find;
71 int amtrmidx_debug = 0;
72 config_overwrites_t *cfg_ovr = NULL;
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.
80 setlocale(LC_MESSAGES, "C");
86 set_pname("amtrmidx");
88 /* Don't die when child closes pipe */
89 signal(SIGPIPE, SIG_IGN);
91 dbopen(DBG_SUBDIR_SERVER);
92 dbprintf(_("%s: version %s\n"), argv[0], version());
94 cfg_ovr = extract_commandline_config_overwrites(&argc, &argv);
96 if (my_argc > 1 && strcmp(my_argv[1], "-t") == 0) {
103 g_fprintf(stderr, _("Usage: %s [-t] <config> [-o configoption]*\n"), argv[0]);
107 config_init(CONFIG_INIT_EXPLICIT_NAME | CONFIG_INIT_FATAL | CONFIG_INIT_USE_CWD,
109 apply_config_overwrites(cfg_ovr);
111 check_running_as(RUNNING_AS_DUMPUSER);
113 dbrename(config_name, DBG_SUBDIR_SERVER);
115 conf_diskfile = config_dir_relative(getconf_str(CNF_DISKFILE));
116 if (read_diskfile(conf_diskfile, &diskl) < 0) {
117 error(_("could not load disklist \"%s\""), conf_diskfile);
120 amfree(conf_diskfile);
122 conf_tapelist = config_dir_relative(getconf_str(CNF_TAPELIST));
123 if(read_tapelist(conf_tapelist)) {
124 error(_("could not load tapelist \"%s\""), conf_tapelist);
127 amfree(conf_tapelist);
129 output_find = find_dump(&diskl);
131 conf_indexdir = config_dir_relative(getconf_str(CNF_INDEXDIR));
133 /* now go through the list of disks and find which have indexes */
135 tmp_time -= 7*24*60*60; /* back one week */
136 for (diskp = diskl.head; diskp != NULL; diskp = diskp->next)
140 char *indexdir, *qindexdir;
151 /* get listing of indices, newest first */
152 host = sanitise_filename(diskp->host->hostname);
153 disk = sanitise_filename(diskp->name);
154 qdisk = quote_string(diskp->name);
155 indexdir = vstralloc(conf_indexdir, "/",
159 qindexdir = quote_string(indexdir);
161 dbprintf("%s %s -> %s\n", diskp->host->hostname,
166 if ((d = opendir(indexdir)) == NULL) {
167 dbprintf(_("could not open index directory %s\n"), qindexdir);
173 names = (char **)alloc(name_length * SIZEOF(char *));
175 while ((f = readdir(d)) != NULL) {
178 if(is_dot_or_dotdot(f->d_name)) {
181 for(i = 0; i < SIZEOF("YYYYMMDDHHMMSS")-1; i++) {
182 if(! isdigit((int)(f->d_name[i]))) {
187 /* len_date=8 for YYYYMMDD */
188 /* len_date=14 for YYYYMMDDHHMMSS */
189 if((len_date != 8 && len_date != 14)
190 || f->d_name[len_date] != '_'
191 || ! isdigit((int)(f->d_name[len_date+1]))) {
192 continue; /* not an index file */
195 * Clear out old index temp files.
197 l = strlen(f->d_name) - (SIZEOF(".tmp")-1);
198 if ((l > (len_date + 1))
199 && (strcmp(f->d_name + l, ".tmp")==0)) {
203 path = stralloc2(indexdir, f->d_name);
204 qpath = quote_string(path);
205 if(lstat(path, &sbuf) != -1
206 && ((sbuf.st_mode & S_IFMT) == S_IFREG)
207 && ((time_t)sbuf.st_mtime < tmp_time)) {
208 dbprintf("rm %s\n", qpath);
209 if(amtrmidx_debug == 0 && unlink(path) == -1) {
210 dbprintf(_("Error removing %s: %s\n"),
211 qpath, strerror(errno));
218 if(name_count >= name_length) {
221 new_names = alloc((name_length * 2) * SIZEOF(char *));
222 memcpy(new_names, names, name_length * SIZEOF(char *));
227 names[name_count++] = stralloc(f->d_name);
230 qsort(names, name_count, SIZEOF(char *), sort_by_name_reversed);
233 * Search for the first full dump past the minimum number
234 * of index files to keep.
236 for(i = 0; i < name_count; i++) {
241 for(len_date = 0; len_date < SIZEOF("YYYYMMDDHHMMSS")-1; len_date++) {
242 if(! isdigit((int)(names[i][len_date]))) {
247 datestamp = stralloc(names[i]);
248 datestamp[len_date] = '\0';
249 level = names[i][len_date+1] - '0';
250 if(!dump_exist(output_find, diskp->host->hostname,
251 diskp->name, datestamp, level)) {
253 path = stralloc2(indexdir, names[i]);
254 qpath = quote_string(path);
255 dbprintf("rm %s\n", qpath);
256 if(amtrmidx_debug == 0 && unlink(path) == -1) {
257 dbprintf(_("Error removing %s: %s\n"),
258 qpath, strerror(errno));
272 amfree(conf_indexdir);
273 free_find_result(&output_find);
275 free_disklist(&diskl);