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;
73 if (argc > 1 && argv && argv[1] && g_str_equal(argv[1], "--version")) {
74 printf("amtrmidx-%s\n", VERSION);
79 * Configure program for internationalization:
80 * 1) Only set the message locale for now.
81 * 2) Set textdomain for all amanda related programs to "amanda"
82 * We don't want to be forced to support dozens of message catalogs.
84 setlocale(LC_MESSAGES, "C");
90 set_pname("amtrmidx");
92 /* Don't die when child closes pipe */
93 signal(SIGPIPE, SIG_IGN);
95 dbopen(DBG_SUBDIR_SERVER);
96 dbprintf(_("%s: version %s\n"), argv[0], VERSION);
98 cfg_ovr = extract_commandline_config_overrides(&argc, &argv);
100 if (argc > 1 && strcmp(argv[1], "-t") == 0) {
107 g_fprintf(stderr, _("Usage: %s [-t] <config> [-o configoption]*\n"), argv[0]);
111 set_config_overrides(cfg_ovr);
112 config_init(CONFIG_INIT_EXPLICIT_NAME | CONFIG_INIT_USE_CWD, argv[1]);
114 conf_diskfile = config_dir_relative(getconf_str(CNF_DISKFILE));
115 read_diskfile(conf_diskfile, &diskl);
116 amfree(conf_diskfile);
118 if (config_errors(NULL) >= CFGERR_WARNINGS) {
119 config_print_errors();
120 if (config_errors(NULL) >= CFGERR_ERRORS) {
121 g_critical(_("errors processing config file"));
125 check_running_as(RUNNING_AS_DUMPUSER);
127 dbrename(get_config_name(), DBG_SUBDIR_SERVER);
129 conf_tapelist = config_dir_relative(getconf_str(CNF_TAPELIST));
130 if(read_tapelist(conf_tapelist)) {
131 error(_("could not load tapelist \"%s\""), conf_tapelist);
134 amfree(conf_tapelist);
136 output_find = find_dump(&diskl);
138 conf_indexdir = config_dir_relative(getconf_str(CNF_INDEXDIR));
140 /* now go through the list of disks and find which have indexes */
142 tmp_time -= 7*24*60*60; /* back one week */
143 for (diskp = diskl.head; diskp != NULL; diskp = diskp->next)
147 char *indexdir, *qindexdir;
157 GSList *matching_dp = NULL;
159 /* get listing of indices, newest first */
160 host = sanitise_filename(diskp->host->hostname);
161 disk = sanitise_filename(diskp->name);
162 qdisk = quote_string(diskp->name);
163 indexdir = vstralloc(conf_indexdir, "/",
167 qindexdir = quote_string(indexdir);
169 /* find all dles that use the same indexdir */
170 for (dp = diskl.head; dp != NULL; dp = dp->next) {
171 char *dp_host, *dp_disk;
173 dp_host = sanitise_filename(dp->host->hostname);
174 dp_disk = sanitise_filename(dp->name);
175 if (strcmp(host, dp_host) == 0 &&
176 strcmp(disk, dp_disk) == 0) {
177 matching_dp = g_slist_append(matching_dp, dp);
183 dbprintf("%s %s -> %s\n", diskp->host->hostname,
188 if ((d = opendir(indexdir)) == NULL) {
189 dbprintf(_("could not open index directory %s\n"), qindexdir);
192 g_slist_free(matching_dp);
196 names = (char **)alloc(name_length * SIZEOF(char *));
198 while ((f = readdir(d)) != NULL) {
201 if(is_dot_or_dotdot(f->d_name)) {
204 for(i = 0; i < SIZEOF("YYYYMMDDHHMMSS")-1; i++) {
205 if(! isdigit((int)(f->d_name[i]))) {
210 /* len_date=8 for YYYYMMDD */
211 /* len_date=14 for YYYYMMDDHHMMSS */
212 if((len_date != 8 && len_date != 14)
213 || f->d_name[len_date] != '_'
214 || ! isdigit((int)(f->d_name[len_date+1]))) {
215 continue; /* not an index file */
218 * Clear out old index temp files.
220 l = strlen(f->d_name) - (SIZEOF(".tmp")-1);
221 if ((l > (len_date + 1))
222 && (strcmp(f->d_name + l, ".tmp")==0)) {
226 path = stralloc2(indexdir, f->d_name);
227 qpath = quote_string(path);
228 if(lstat(path, &sbuf) != -1
229 && ((sbuf.st_mode & S_IFMT) == S_IFREG)
230 && ((time_t)sbuf.st_mtime < tmp_time)) {
231 dbprintf("rm %s\n", qpath);
232 if(amtrmidx_debug == 0 && unlink(path) == -1) {
233 dbprintf(_("Error removing %s: %s\n"),
234 qpath, strerror(errno));
241 if(name_count >= name_length) {
244 new_names = alloc((name_length * 2) * SIZEOF(char *));
245 memcpy(new_names, names, name_length * SIZEOF(char *));
250 names[name_count++] = stralloc(f->d_name);
253 qsort(names, name_count, SIZEOF(char *), sort_by_name_reversed);
256 * Search for the first full dump past the minimum number
257 * of index files to keep.
259 for(i = 0; i < name_count; i++) {
266 for(len_date = 0; len_date < SIZEOF("YYYYMMDDHHMMSS")-1; len_date++) {
267 if(! isdigit((int)(names[i][len_date]))) {
272 datestamp = stralloc(names[i]);
273 datestamp[len_date] = '\0';
274 if (sscanf(&names[i][len_date+1], "%d", &level) != 1)
276 for (mdp = matching_dp; mdp != NULL; mdp = mdp->next) {
278 if (dump_exist(output_find, dp->host->hostname,
279 dp->name, datestamp, level)) {
285 path = stralloc2(indexdir, names[i]);
286 qpath = quote_string(path);
287 dbprintf("rm %s\n", qpath);
288 if(amtrmidx_debug == 0 && unlink(path) == -1) {
289 dbprintf(_("Error removing %s: %s\n"),
290 qpath, strerror(errno));
298 g_slist_free(matching_dp);
305 amfree(conf_indexdir);
306 free_find_result(&output_find);
308 free_disklist(&diskl);