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
42 static int sort_by_name_reversed(const void *a, const void *b);
44 int main(int argc, char **argv);
46 static int sort_by_name_reversed(
50 char **ap = (char **) a;
51 char **bp = (char **) b;
53 return -1 * strcmp(*ap, *bp);
68 find_result_t *output_find;
70 int amtrmidx_debug = 0;
71 config_overrides_t *cfg_ovr = NULL;
74 * Configure program for internationalization:
75 * 1) Only set the message locale for now.
76 * 2) Set textdomain for all amanda related programs to "amanda"
77 * We don't want to be forced to support dozens of message catalogs.
79 setlocale(LC_MESSAGES, "C");
85 set_pname("amtrmidx");
87 /* Don't die when child closes pipe */
88 signal(SIGPIPE, SIG_IGN);
90 dbopen(DBG_SUBDIR_SERVER);
91 dbprintf(_("%s: version %s\n"), argv[0], VERSION);
93 cfg_ovr = extract_commandline_config_overrides(&argc, &argv);
95 if (argc > 1 && strcmp(argv[1], "-t") == 0) {
102 g_fprintf(stderr, _("Usage: %s [-t] <config> [-o configoption]*\n"), argv[0]);
106 set_config_overrides(cfg_ovr);
107 config_init(CONFIG_INIT_EXPLICIT_NAME | CONFIG_INIT_USE_CWD, argv[1]);
109 conf_diskfile = config_dir_relative(getconf_str(CNF_DISKFILE));
110 read_diskfile(conf_diskfile, &diskl);
111 amfree(conf_diskfile);
113 if (config_errors(NULL) >= CFGERR_WARNINGS) {
114 config_print_errors();
115 if (config_errors(NULL) >= CFGERR_ERRORS) {
116 g_critical(_("errors processing config file"));
120 check_running_as(RUNNING_AS_DUMPUSER);
122 dbrename(get_config_name(), DBG_SUBDIR_SERVER);
124 conf_tapelist = config_dir_relative(getconf_str(CNF_TAPELIST));
125 if(read_tapelist(conf_tapelist)) {
126 error(_("could not load tapelist \"%s\""), conf_tapelist);
129 amfree(conf_tapelist);
131 output_find = find_dump(&diskl);
133 conf_indexdir = config_dir_relative(getconf_str(CNF_INDEXDIR));
135 /* now go through the list of disks and find which have indexes */
137 tmp_time -= 7*24*60*60; /* back one week */
138 for (diskp = diskl.head; diskp != NULL; diskp = diskp->next)
142 char *indexdir, *qindexdir;
152 GSList *matching_dp = NULL;
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, "/",
162 qindexdir = quote_string(indexdir);
164 /* find all dles that use the same indexdir */
165 for (dp = diskl.head; dp != NULL; dp = dp->next) {
166 char *dp_host, *dp_disk;
168 dp_host = sanitise_filename(dp->host->hostname);
169 dp_disk = sanitise_filename(dp->name);
170 if (strcmp(host, dp_host) == 0 &&
171 strcmp(disk, dp_disk) == 0) {
172 matching_dp = g_slist_append(matching_dp, dp);
178 dbprintf("%s %s -> %s\n", diskp->host->hostname,
183 if ((d = opendir(indexdir)) == NULL) {
184 dbprintf(_("could not open index directory %s\n"), qindexdir);
187 g_slist_free(matching_dp);
191 names = (char **)alloc(name_length * SIZEOF(char *));
193 while ((f = readdir(d)) != NULL) {
196 if(is_dot_or_dotdot(f->d_name)) {
199 for(i = 0; i < SIZEOF("YYYYMMDDHHMMSS")-1; i++) {
200 if(! isdigit((int)(f->d_name[i]))) {
205 /* len_date=8 for YYYYMMDD */
206 /* len_date=14 for YYYYMMDDHHMMSS */
207 if((len_date != 8 && len_date != 14)
208 || f->d_name[len_date] != '_'
209 || ! isdigit((int)(f->d_name[len_date+1]))) {
210 continue; /* not an index file */
213 * Clear out old index temp files.
215 l = strlen(f->d_name) - (SIZEOF(".tmp")-1);
216 if ((l > (len_date + 1))
217 && (strcmp(f->d_name + l, ".tmp")==0)) {
221 path = stralloc2(indexdir, f->d_name);
222 qpath = quote_string(path);
223 if(lstat(path, &sbuf) != -1
224 && ((sbuf.st_mode & S_IFMT) == S_IFREG)
225 && ((time_t)sbuf.st_mtime < tmp_time)) {
226 dbprintf("rm %s\n", qpath);
227 if(amtrmidx_debug == 0 && unlink(path) == -1) {
228 dbprintf(_("Error removing %s: %s\n"),
229 qpath, strerror(errno));
236 if(name_count >= name_length) {
239 new_names = alloc((name_length * 2) * SIZEOF(char *));
240 memcpy(new_names, names, name_length * SIZEOF(char *));
245 names[name_count++] = stralloc(f->d_name);
248 qsort(names, name_count, SIZEOF(char *), sort_by_name_reversed);
251 * Search for the first full dump past the minimum number
252 * of index files to keep.
254 for(i = 0; i < name_count; i++) {
261 for(len_date = 0; len_date < SIZEOF("YYYYMMDDHHMMSS")-1; len_date++) {
262 if(! isdigit((int)(names[i][len_date]))) {
267 datestamp = stralloc(names[i]);
268 datestamp[len_date] = '\0';
269 if (sscanf(&names[i][len_date+1], "%d", &level) != 1)
271 for (mdp = matching_dp; mdp != NULL; mdp = mdp->next) {
273 if (dump_exist(output_find, dp->host->hostname,
274 dp->name, datestamp, level)) {
280 path = stralloc2(indexdir, names[i]);
281 qpath = quote_string(path);
282 dbprintf("rm %s\n", qpath);
283 if(amtrmidx_debug == 0 && unlink(path) == -1) {
284 dbprintf(_("Error removing %s: %s\n"),
285 qpath, strerror(errno));
293 g_slist_free(matching_dp);
300 amfree(conf_indexdir);
301 free_find_result(&output_find);
303 free_disklist(&diskl);