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.1.2.3.4.1.2.2.2.1 2005/09/20 21:31:52 jrjackson 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>
50 int no_keep; /* files per system to keep */
51 char **output_find_log;
57 char *oldfile = NULL, *newfile = NULL;
58 time_t today, date_keep;
66 int amtrmidx_debug = 0;
71 set_pname("amtrmlog");
73 if (argc > 1 && strcmp(argv[1], "-t") == 0) {
80 fprintf(stderr, "Usage: %s [-t] <config>\n", argv[0]);
85 dbprintf(("%s: version %s\n", argv[0], version()));
87 config_name = argv[1];
89 config_dir = vstralloc(CONFIG_DIR, "/", config_name, "/", NULL);
90 conffile = stralloc2(config_dir, CONFFILE_NAME);
91 if(read_conffile(conffile)) {
92 error("errors processing config file \"%s\"", conffile);
95 conf_diskfile = getconf_str(CNF_DISKFILE);
96 if (*conf_diskfile == '/') {
97 conf_diskfile = stralloc(conf_diskfile);
99 conf_diskfile = stralloc2(config_dir, conf_diskfile);
101 if((diskl = read_diskfile(conf_diskfile)) == NULL) {
102 error("could not load disklist %s", conf_diskfile);
104 amfree(conf_diskfile);
105 conf_tapelist = getconf_str(CNF_TAPELIST);
106 if (*conf_tapelist == '/') {
107 conf_tapelist = stralloc(conf_tapelist);
109 conf_tapelist = stralloc2(config_dir, conf_tapelist);
111 if (read_tapelist(conf_tapelist)) {
112 error("could not load tapelist \"%s\"", conf_tapelist);
114 amfree(conf_tapelist);
116 today = time((time_t *)NULL);
117 date_keep = today - (getconf_int(CNF_DUMPCYCLE)*86400);
119 output_find_log = find_log(NULL,0,NULL);
121 /* determine how many log to keep */
122 no_keep = getconf_int(CNF_TAPECYCLE) * 2;
123 dbprintf(("Keeping %d log file%s\n", no_keep, (no_keep == 1) ? "" : "s"));
125 conf_logdir = getconf_str(CNF_LOGDIR);
126 if (*conf_logdir == '/') {
127 conf_logdir = stralloc(conf_logdir);
129 conf_logdir = stralloc2(config_dir, conf_logdir);
131 olddir = vstralloc(conf_logdir, "/oldlog", NULL);
132 if (mkpdir(olddir, 02700, (uid_t)-1, (gid_t)-1) != 0) {
133 error("could not create parents of %s: %s", olddir, strerror(errno));
135 if (mkdir(olddir, 02700) != 0 && errno != EEXIST) {
136 error("could not create %s: %s", olddir, strerror(errno));
139 if (stat(olddir,&stat_old) == -1) {
140 error("can't stat oldlog directory \"%s\": %s", olddir, strerror(errno));
143 if (!S_ISDIR(stat_old.st_mode)) {
144 error("Oldlog directory \"%s\" is not a directory", olddir);
146 if ((dir = opendir(conf_logdir)) == NULL) {
147 error("could not open log directory \"%s\": %s", conf_logdir, strerror(errno));
149 while ((adir = readdir(dir)) != NULL) {
150 if(strncmp(adir->d_name,"log.",4)==0) {
152 for (name = output_find_log; *name != NULL; name++) {
153 if(strncmp(adir->d_name, *name, 12) == 0) {
157 logname = newvstralloc(logname,
158 conf_logdir, "/", adir->d_name, NULL);
159 if(stat(logname, &stat_log) == 0) {
160 if(stat_log.st_mtime > date_keep) {
165 oldfile = newvstralloc(oldfile,
166 conf_logdir, "/", adir->d_name, NULL);
167 newfile = newvstralloc(newfile,
168 olddir, "/", adir->d_name, NULL);
169 if(rename(oldfile, newfile) != 0) {
170 error("could not rename \"%s\" to \"%s\": %s",
171 oldfile, newfile, strerror(errno));
177 for (name = output_find_log; *name != NULL; name++) {
180 amfree(output_find_log);