Imported Upstream version 2.5.0
[debian/amanda] / server-src / amtrmlog.c
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 1991-1998 University of Maryland at College Park
4  * All Rights Reserved.
5  *
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.
15  *
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.
22  *
23  * Authors: the Amanda Development Team.  Its members are listed in a
24  * file named AUTHORS, in the root directory of this distribution.
25  */
26 /*
27  * $Id: amtrmlog.c,v 1.10 2006/01/14 04:37:19 paddy_s Exp $
28  *
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
31  * dump.
32  */
33
34 #include "amanda.h"
35 #include "arglist.h"
36 #ifdef HAVE_NETINET_IN_SYSTM_H
37 #include <netinet/in_systm.h>
38 #endif
39 #include "conffile.h"
40 #include "diskfile.h"
41 #include "tapefile.h"
42 #include "find.h"
43 #include "version.h"
44
45 int main P((int, char **));
46
47 int main(argc, argv)
48 int argc;
49 char **argv;
50 {
51     disklist_t diskl;
52     int no_keep;                        /* files per system to keep */
53     char **output_find_log;
54     DIR *dir;
55     struct dirent *adir;
56     char **name;
57     int useful;
58     char *olddir;
59     char *oldfile = NULL, *newfile = NULL;
60     time_t today, date_keep;
61     char *logname = NULL;
62     struct stat stat_log;
63     struct stat stat_old;
64     char *conffile;
65     char *conf_diskfile;
66     char *conf_tapelist;
67     char *conf_logdir;
68     int amtrmidx_debug = 0;
69
70     safe_fd(-1, 0);
71     safe_cd();
72
73     set_pname("amtrmlog");
74
75     /* Don't die when child closes pipe */
76     signal(SIGPIPE, SIG_IGN);
77
78     if (argc > 1 && strcmp(argv[1], "-t") == 0) {
79         amtrmidx_debug = 1;
80         argc--;
81         argv++;
82     }
83
84     if (argc < 2) {
85         fprintf(stderr, "Usage: %s [-t] <config>\n", argv[0]);
86         return 1;
87     }
88
89     dbopen();
90     dbprintf(("%s: version %s\n", argv[0], version()));
91
92     config_name = argv[1];
93
94     config_dir = vstralloc(CONFIG_DIR, "/", config_name, "/", NULL);
95     conffile = stralloc2(config_dir, CONFFILE_NAME);
96     if (read_conffile(conffile))
97         error("errors processing amanda config file \"%s\"", conffile);
98     amfree(conffile);
99
100     conf_diskfile = getconf_str(CNF_DISKFILE);
101     if (*conf_diskfile == '/') {
102         conf_diskfile = stralloc(conf_diskfile);
103     } else {
104         conf_diskfile = stralloc2(config_dir, conf_diskfile);
105     }
106     if (read_diskfile(conf_diskfile, &diskl) < 0)
107         error("could not load disklist \"%s\"", conf_diskfile);
108     amfree(conf_diskfile);
109
110     conf_tapelist = getconf_str(CNF_TAPELIST);
111     if (*conf_tapelist == '/') {
112         conf_tapelist = stralloc(conf_tapelist);
113     } else {
114         conf_tapelist = stralloc2(config_dir, conf_tapelist);
115     }
116     if (read_tapelist(conf_tapelist))
117         error("could not load tapelist \"%s\"", conf_tapelist);
118     amfree(conf_tapelist);
119
120     today = time((time_t *)NULL);
121     date_keep = today - (getconf_int(CNF_DUMPCYCLE)*86400);
122
123     output_find_log = find_log();
124
125     /* determine how many log to keep */
126     no_keep = getconf_int(CNF_TAPECYCLE) * 2;
127     dbprintf(("Keeping %d log file%s\n", no_keep, (no_keep == 1) ? "" : "s"));
128
129     conf_logdir = getconf_str(CNF_LOGDIR);
130     if (*conf_logdir == '/') {
131         conf_logdir = stralloc(conf_logdir);
132     } else {
133         conf_logdir = stralloc2(config_dir, conf_logdir);
134     }
135     olddir = vstralloc(conf_logdir, "/oldlog", NULL);
136     if (mkpdir(olddir, 02700, (uid_t)-1, (gid_t)-1) != 0)
137         error("could not create parents of %s: %s", olddir, strerror(errno));
138     if (mkdir(olddir, 02700) != 0 && errno != EEXIST)
139         error("could not create %s: %s", olddir, strerror(errno));
140
141     if (stat(olddir,&stat_old) == -1) {
142         error("can't stat oldlog directory \"%s\": %s", olddir, strerror(errno));
143     }
144
145     if (!S_ISDIR(stat_old.st_mode)) {
146         error("Oldlog directory \"%s\" is not a directory", olddir);
147     }
148
149     if ((dir = opendir(conf_logdir)) == NULL)
150         error("could not open log directory \"%s\": %s", conf_logdir,strerror(errno));
151     while ((adir=readdir(dir)) != NULL) {
152         if(strncmp(adir->d_name,"log.",4)==0) {
153             useful=0;
154             for (name=output_find_log;*name !=NULL; name++) {
155                 if(strncmp(adir->d_name,*name,12)==0) {
156                     useful=1;
157                 }
158             }
159             logname=newvstralloc(logname,
160                                  conf_logdir, "/" ,adir->d_name, NULL);
161             if(stat(logname,&stat_log)==0) {
162                 if(stat_log.st_mtime > date_keep) {
163                     useful = 1;
164                 }
165             }
166             if(useful == 0) {
167                 oldfile = newvstralloc(oldfile,
168                                        conf_logdir, "/", adir->d_name, NULL);
169                 newfile = newvstralloc(newfile,
170                                        olddir, "/", adir->d_name, NULL);
171                 if (rename(oldfile,newfile) != 0)
172                     error("could not rename \"%s\" to \"%s\": %s",
173                           oldfile, newfile, strerror(errno));
174             }
175         }
176     }
177     closedir(dir);
178     for (name = output_find_log; *name != NULL; name++) {
179         amfree(*name);
180     }
181     amfree(output_find_log);
182     amfree(logname);
183     amfree(oldfile);
184     amfree(newfile);
185     amfree(olddir);
186     amfree(config_dir);
187     amfree(conf_logdir);
188
189     dbclose();
190
191     return 0;
192 }