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.34 2006/01/14 04:37:19 paddy_s 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
36 #ifdef HAVE_NETINET_IN_SYSTM_H
37 #include <netinet/in_systm.h>
45 static int sort_by_name_reversed(a, b)
49 char **ap = (char **) a;
50 char **bp = (char **) b;
52 return -1 * strcmp(*ap, *bp);
55 int main P((int, char **));
68 find_result_t *output_find;
70 int amtrmidx_debug = 0;
75 set_pname("amtrmidx");
77 /* Don't die when child closes pipe */
78 signal(SIGPIPE, SIG_IGN);
81 dbprintf(("%s: version %s\n", argv[0], version()));
83 if (argc > 1 && strcmp(argv[1], "-t") == 0) {
90 fprintf(stderr, "Usage: %s [-t] <config>\n", argv[0]);
94 config_name = argv[1];
96 config_dir = vstralloc(CONFIG_DIR, "/", config_name, "/", NULL);
97 conffile = stralloc2(config_dir, CONFFILE_NAME);
98 if (read_conffile(conffile))
99 error("errors processing config file \"%s\"", conffile);
102 conf_diskfile = getconf_str(CNF_DISKFILE);
103 if(*conf_diskfile == '/') {
104 conf_diskfile = stralloc(conf_diskfile);
106 conf_diskfile = stralloc2(config_dir, conf_diskfile);
108 if (read_diskfile(conf_diskfile, &diskl) < 0)
109 error("could not load disklist \"%s\"", conf_diskfile);
110 amfree(conf_diskfile);
112 conf_tapelist = getconf_str(CNF_TAPELIST);
113 if(*conf_tapelist == '/') {
114 conf_tapelist = stralloc(conf_tapelist);
116 conf_tapelist = stralloc2(config_dir, conf_tapelist);
118 if(read_tapelist(conf_tapelist))
119 error("could not load tapelist \"%s\"", conf_tapelist);
120 amfree(conf_tapelist);
122 output_find = find_dump(1, &diskl);
124 conf_indexdir = getconf_str(CNF_INDEXDIR);
125 if(*conf_indexdir == '/') {
126 conf_indexdir = stralloc(conf_indexdir);
128 conf_indexdir = stralloc2(config_dir, conf_indexdir);
131 /* now go through the list of disks and find which have indexes */
133 tmp_time -= 7*24*60*60; /* back one week */
134 for (diskp = diskl.head; diskp != NULL; diskp = diskp->next)
147 dbprintf(("%s %s\n", diskp->host->hostname, diskp->name));
149 /* get listing of indices, newest first */
150 host = sanitise_filename(diskp->host->hostname);
151 disk = sanitise_filename(diskp->name);
152 indexdir = vstralloc(conf_indexdir, "/",
158 if ((d = opendir(indexdir)) == NULL) {
159 dbprintf(("could not open index directory \"%s\"\n", indexdir));
164 names = (char **)alloc(name_length * sizeof(char *));
166 while ((f = readdir(d)) != NULL) {
169 if(is_dot_or_dotdot(f->d_name)) {
172 for(i = 0; i < sizeof("YYYYMMDD")-1; i++) {
173 if(! isdigit((int)(f->d_name[i]))) {
177 if(i < sizeof("YYYYMMDD")-1
178 || f->d_name[i] != '_'
179 || ! isdigit((int)(f->d_name[i+1]))) {
180 continue; /* not an index file */
183 * Clear out old index temp files.
185 l = strlen(f->d_name) - (sizeof(".tmp")-1);
186 if(l > sizeof("YYYYMMDD_L")-1
187 && strcmp (f->d_name + l, ".tmp") == 0) {
191 path = stralloc2(indexdir, f->d_name);
192 if(lstat(path, &sbuf) != -1
193 && (sbuf.st_mode & S_IFMT) == S_IFREG
194 && sbuf.st_mtime < tmp_time) {
195 dbprintf(("rm %s\n", path));
196 if(amtrmidx_debug == 0 && unlink(path) == -1) {
197 dbprintf(("Error removing \"%s\": %s\n",
198 path, strerror(errno)));
204 if(name_count >= name_length) {
207 new_names = alloc((name_length + 100) * sizeof(char *));
208 memcpy(new_names, names, name_length * sizeof(char *));
213 names[name_count++] = stralloc(f->d_name);
216 qsort(names, name_count, sizeof(char *), sort_by_name_reversed);
219 * Search for the first full dump past the minimum number
220 * of index files to keep.
222 for(i = 0; i < name_count; i++) {
223 if(!dump_exist(output_find,
224 diskp->host->hostname,diskp->name,
226 names[i][sizeof("YYYYMMDD_L")-1-1] - '0')) {
228 path = stralloc2(indexdir, names[i]);
229 dbprintf(("rm %s\n", path));
230 if(amtrmidx_debug == 0 && unlink(path) == -1) {
231 dbprintf(("Error removing \"%s\": %s\n",
232 path, strerror(errno)));
243 amfree(conf_indexdir);
245 free_find_result(&output_find);