Imported Upstream version 2.5.1
[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.17 2006/07/25 18:27:57 martinea 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 amtrmidx_debug = 0;
46
47 int main(int argc, char **argv);
48
49 int
50 main(
51     int         argc,
52     char **     argv)
53 {
54     disklist_t diskl;
55     int no_keep;                        /* files per system to keep */
56     char **output_find_log;
57     DIR *dir;
58     struct dirent *adir;
59     char **name;
60     int useful;
61     char *olddir;
62     char *oldfile = NULL, *newfile = NULL;
63     time_t today, date_keep;
64     char *logname = NULL;
65     struct stat stat_log;
66     struct stat stat_old;
67     char *conffile;
68     char *conf_diskfile;
69     char *conf_tapelist;
70     char *conf_logdir;
71     int dumpcycle;
72     int    new_argc,   my_argc;
73     char **new_argv, **my_argv;
74
75     safe_fd(-1, 0);
76     safe_cd();
77
78     set_pname("amtrmlog");
79
80     /* Don't die when child closes pipe */
81     signal(SIGPIPE, SIG_IGN);
82
83     parse_server_conf(argc, argv, &new_argc, &new_argv);
84     my_argc = new_argc;
85     my_argv = new_argv;
86
87     if (my_argc > 1 && strcmp(my_argv[1], "-t") == 0) {
88         amtrmidx_debug = 1;
89         my_argc--;
90         my_argv++;
91     }
92
93     if (my_argc < 2) {
94         fprintf(stderr, "Usage: %s [-t] <config> [-o configoption]*\n", my_argv[0]);
95         return 1;
96     }
97
98     dbopen(DBG_SUBDIR_SERVER);
99     dbprintf(("%s: version %s\n", my_argv[0], version()));
100
101     config_name = my_argv[1];
102
103     config_dir = vstralloc(CONFIG_DIR, "/", config_name, "/", NULL);
104     conffile = stralloc2(config_dir, CONFFILE_NAME);
105     if (read_conffile(conffile)) {
106         error("errors processing amanda config file \"%s\"", conffile);
107         /*NOTREACHED*/
108     }
109     amfree(conffile);
110
111     dbrename(config_name, DBG_SUBDIR_SERVER);
112
113     report_bad_conf_arg();
114
115     conf_diskfile = getconf_str(CNF_DISKFILE);
116     if (*conf_diskfile == '/') {
117         conf_diskfile = stralloc(conf_diskfile);
118     } else {
119         conf_diskfile = stralloc2(config_dir, conf_diskfile);
120     }
121     if (read_diskfile(conf_diskfile, &diskl) < 0) {
122         error("could not load disklist \"%s\"", conf_diskfile);
123         /*NOTREACHED*/
124     }
125     amfree(conf_diskfile);
126
127     conf_tapelist = getconf_str(CNF_TAPELIST);
128     if (*conf_tapelist == '/') {
129         conf_tapelist = stralloc(conf_tapelist);
130     } else {
131         conf_tapelist = stralloc2(config_dir, conf_tapelist);
132     }
133     if (read_tapelist(conf_tapelist)) {
134         error("could not load tapelist \"%s\"", conf_tapelist);
135         /*NOTREACHED*/
136     }
137     amfree(conf_tapelist);
138
139     today = time((time_t *)NULL);
140     dumpcycle = getconf_int(CNF_DUMPCYCLE);
141     if(dumpcycle > 5000)
142         dumpcycle = 5000;
143     date_keep = today - (dumpcycle * 86400);
144
145     output_find_log = find_log();
146
147     /* determine how many log to keep */
148     no_keep = getconf_int(CNF_TAPECYCLE) * 2;
149     dbprintf(("Keeping %d log file%s\n", no_keep, (no_keep == 1) ? "" : "s"));
150
151     conf_logdir = getconf_str(CNF_LOGDIR);
152     if (*conf_logdir == '/') {
153         conf_logdir = stralloc(conf_logdir);
154     } else {
155         conf_logdir = stralloc2(config_dir, conf_logdir);
156     }
157     olddir = vstralloc(conf_logdir, "/oldlog", NULL);
158     if (mkpdir(olddir, 02700, (uid_t)-1, (gid_t)-1) != 0) {
159         error("could not create parents of %s: %s", olddir, strerror(errno));
160         /*NOTREACHED*/
161     }
162     if (mkdir(olddir, 02700) != 0 && errno != EEXIST) {
163         error("could not create %s: %s", olddir, strerror(errno));
164         /*NOTREACHED*/
165     }
166
167     if (stat(olddir,&stat_old) == -1) {
168         error("can't stat oldlog directory \"%s\": %s", olddir, strerror(errno));
169         /*NOTREACHED*/
170     }
171
172     if (!S_ISDIR(stat_old.st_mode)) {
173         error("Oldlog directory \"%s\" is not a directory", olddir);
174         /*NOTREACHED*/
175     }
176
177     if ((dir = opendir(conf_logdir)) == NULL) {
178         error("could not open log directory \"%s\": %s", conf_logdir,strerror(errno));
179         /*NOTREACHED*/
180     }
181     while ((adir=readdir(dir)) != NULL) {
182         if(strncmp(adir->d_name,"log.",4)==0) {
183             useful=0;
184             for (name=output_find_log;*name !=NULL; name++) {
185                 if((strlen(adir->d_name) >= 13 &&
186                     strlen(*name) >= 13 &&
187                     adir->d_name[12] == '.' && (*name)[12] == '.' &&
188                     strncmp(adir->d_name,*name,12)==0) ||
189                    strncmp(adir->d_name,*name,18)==0) {
190                     useful=1;
191                     break;
192                 }
193             }
194             logname=newvstralloc(logname,
195                                  conf_logdir, "/" ,adir->d_name, NULL);
196             if(stat(logname,&stat_log)==0) {
197                 if((time_t)stat_log.st_mtime > date_keep) {
198                     useful = 1;
199                 }
200             }
201             if(useful == 0) {
202                 oldfile = newvstralloc(oldfile,
203                                        conf_logdir, "/", adir->d_name, NULL);
204                 newfile = newvstralloc(newfile,
205                                        olddir, "/", adir->d_name, NULL);
206                 if (rename(oldfile,newfile) != 0) {
207                     error("could not rename \"%s\" to \"%s\": %s",
208                           oldfile, newfile, strerror(errno));
209                     /*NOTREACHED*/
210                 }
211             }
212         }
213     }
214     closedir(dir);
215     for (name = output_find_log; *name != NULL; name++) {
216         amfree(*name);
217     }
218     amfree(output_find_log);
219     amfree(logname);
220     amfree(oldfile);
221     amfree(newfile);
222     amfree(olddir);
223     amfree(config_dir);
224     amfree(conf_logdir);
225     clear_tapelist();
226     free_disklist(&diskl);
227     free_new_argv(new_argc, new_argv);
228     free_server_config();
229
230     dbclose();
231
232     return 0;
233 }