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: amtrmlog.c,v 1.17 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
41 int amtrmidx_debug = 0;
43 int main(int argc, char **argv);
51 int no_keep; /* files per system to keep */
52 char **output_find_log;
58 char *oldfile = NULL, *newfile = NULL;
59 time_t today, date_keep;
67 config_overrides_t *cfg_ovr = NULL;
70 * Configure program for internationalization:
71 * 1) Only set the message locale for now.
72 * 2) Set textdomain for all amanda related programs to "amanda"
73 * We don't want to be forced to support dozens of message catalogs.
75 setlocale(LC_MESSAGES, "C");
81 set_pname("amtrmlog");
83 /* Don't die when child closes pipe */
84 signal(SIGPIPE, SIG_IGN);
86 cfg_ovr = extract_commandline_config_overrides(&argc, &argv);
88 if (argc > 1 && strcmp(argv[1], "-t") == 0) {
95 g_fprintf(stderr, _("Usage: %s [-t] <config> [-o configoption]*\n"), argv[0]);
99 dbopen(DBG_SUBDIR_SERVER);
100 dbprintf(_("%s: version %s\n"), argv[0], VERSION);
102 set_config_overrides(cfg_ovr);
103 config_init(CONFIG_INIT_EXPLICIT_NAME, argv[1]);
105 conf_diskfile = config_dir_relative(getconf_str(CNF_DISKFILE));
106 read_diskfile(conf_diskfile, &diskl);
107 amfree(conf_diskfile);
109 if (config_errors(NULL) >= CFGERR_WARNINGS) {
110 config_print_errors();
111 if (config_errors(NULL) >= CFGERR_ERRORS) {
112 g_critical(_("errors processing config file"));
116 check_running_as(RUNNING_AS_DUMPUSER);
118 dbrename(get_config_name(), DBG_SUBDIR_SERVER);
120 conf_tapelist = config_dir_relative(getconf_str(CNF_TAPELIST));
121 if (read_tapelist(conf_tapelist)) {
122 error(_("could not load tapelist \"%s\""), conf_tapelist);
125 amfree(conf_tapelist);
127 today = time((time_t *)NULL);
128 dumpcycle = getconf_int(CNF_DUMPCYCLE);
131 date_keep = today - (dumpcycle * 86400);
133 output_find_log = find_log();
135 /* determine how many log to keep */
136 no_keep = getconf_int(CNF_TAPECYCLE) * 2;
137 dbprintf(plural(_("Keeping %d log file\n"),
138 _("Keeping %d log files\n"), no_keep),
141 conf_logdir = config_dir_relative(getconf_str(CNF_LOGDIR));
142 olddir = vstralloc(conf_logdir, "/oldlog", NULL);
143 if (mkpdir(olddir, 0700, (uid_t)-1, (gid_t)-1) != 0) {
144 error(_("could not create parents of %s: %s"), olddir, strerror(errno));
147 if (mkdir(olddir, 0700) != 0 && errno != EEXIST) {
148 error(_("could not create %s: %s"), olddir, strerror(errno));
152 if (stat(olddir,&stat_old) == -1) {
153 error(_("can't stat oldlog directory \"%s\": %s"), olddir, strerror(errno));
157 if (!S_ISDIR(stat_old.st_mode)) {
158 error(_("Oldlog directory \"%s\" is not a directory"), olddir);
162 if ((dir = opendir(conf_logdir)) == NULL) {
163 error(_("could not open log directory \"%s\": %s"), conf_logdir,strerror(errno));
166 while ((adir=readdir(dir)) != NULL) {
167 if(strncmp(adir->d_name,"log.",4)==0) {
169 for (name=output_find_log;*name !=NULL; name++) {
170 if((strlen(adir->d_name) >= 13 &&
171 strlen(*name) >= 13 &&
172 adir->d_name[12] == '.' && (*name)[12] == '.' &&
173 strncmp(adir->d_name,*name,12)==0) ||
174 strncmp(adir->d_name,*name,18)==0) {
179 logname=newvstralloc(logname,
180 conf_logdir, "/" ,adir->d_name, NULL);
181 if(stat(logname,&stat_log)==0) {
182 if((time_t)stat_log.st_mtime > date_keep) {
187 oldfile = newvstralloc(oldfile,
188 conf_logdir, "/", adir->d_name, NULL);
189 newfile = newvstralloc(newfile,
190 olddir, "/", adir->d_name, NULL);
191 if (rename(oldfile,newfile) != 0) {
192 error(_("could not rename \"%s\" to \"%s\": %s"),
193 oldfile, newfile, strerror(errno));
200 for (name = output_find_log; *name != NULL; name++) {
203 amfree(output_find_log);
210 free_disklist(&diskl);