Imported Upstream version 2.6.0p1
[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 #include "conffile.h"
37 #include "diskfile.h"
38 #include "tapefile.h"
39 #include "find.h"
40 #include "version.h"
41
42 int amtrmidx_debug = 0;
43
44 int main(int argc, char **argv);
45
46 int
47 main(
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 *conf_diskfile;
65     char *conf_tapelist;
66     char *conf_logdir;
67     int dumpcycle;
68     config_overwrites_t *cfg_ovr = NULL;
69
70     /*
71      * Configure program for internationalization:
72      *   1) Only set the message locale for now.
73      *   2) Set textdomain for all amanda related programs to "amanda"
74      *      We don't want to be forced to support dozens of message catalogs.
75      */  
76     setlocale(LC_MESSAGES, "C");
77     textdomain("amanda"); 
78
79     safe_fd(-1, 0);
80     safe_cd();
81
82     set_pname("amtrmlog");
83
84     /* Don't die when child closes pipe */
85     signal(SIGPIPE, SIG_IGN);
86
87     cfg_ovr = extract_commandline_config_overwrites(&argc, &argv);
88
89     if (argc > 1 && strcmp(argv[1], "-t") == 0) {
90         amtrmidx_debug = 1;
91         argc--;
92         argv++;
93     }
94
95     if (argc < 2) {
96         g_fprintf(stderr, _("Usage: %s [-t] <config> [-o configoption]*\n"), argv[0]);
97         return 1;
98     }
99
100     dbopen(DBG_SUBDIR_SERVER);
101     dbprintf(_("%s: version %s\n"), argv[0], version());
102
103     config_init(CONFIG_INIT_EXPLICIT_NAME | CONFIG_INIT_FATAL,
104                 argv[1]);
105     apply_config_overwrites(cfg_ovr);
106
107     check_running_as(RUNNING_AS_DUMPUSER);
108
109     dbrename(config_name, DBG_SUBDIR_SERVER);
110
111     conf_diskfile = config_dir_relative(getconf_str(CNF_DISKFILE));
112     if (read_diskfile(conf_diskfile, &diskl) < 0) {
113         error(_("could not load disklist \"%s\""), conf_diskfile);
114         /*NOTREACHED*/
115     }
116     amfree(conf_diskfile);
117
118     conf_tapelist = config_dir_relative(getconf_str(CNF_TAPELIST));
119     if (read_tapelist(conf_tapelist)) {
120         error(_("could not load tapelist \"%s\""), conf_tapelist);
121         /*NOTREACHED*/
122     }
123     amfree(conf_tapelist);
124
125     today = time((time_t *)NULL);
126     dumpcycle = getconf_int(CNF_DUMPCYCLE);
127     if(dumpcycle > 5000)
128         dumpcycle = 5000;
129     date_keep = today - (dumpcycle * 86400);
130
131     output_find_log = find_log();
132
133     /* determine how many log to keep */
134     no_keep = getconf_int(CNF_TAPECYCLE) * 2;
135     dbprintf(plural(_("Keeping %d log file\n"),
136                     _("Keeping %d log files\n"), no_keep),
137              no_keep);
138
139     conf_logdir = config_dir_relative(getconf_str(CNF_LOGDIR));
140     olddir = vstralloc(conf_logdir, "/oldlog", NULL);
141     if (mkpdir(olddir, 0700, (uid_t)-1, (gid_t)-1) != 0) {
142         error(_("could not create parents of %s: %s"), olddir, strerror(errno));
143         /*NOTREACHED*/
144     }
145     if (mkdir(olddir, 0700) != 0 && errno != EEXIST) {
146         error(_("could not create %s: %s"), olddir, strerror(errno));
147         /*NOTREACHED*/
148     }
149
150     if (stat(olddir,&stat_old) == -1) {
151         error(_("can't stat oldlog directory \"%s\": %s"), olddir, strerror(errno));
152         /*NOTREACHED*/
153     }
154
155     if (!S_ISDIR(stat_old.st_mode)) {
156         error(_("Oldlog directory \"%s\" is not a directory"), olddir);
157         /*NOTREACHED*/
158     }
159
160     if ((dir = opendir(conf_logdir)) == NULL) {
161         error(_("could not open log directory \"%s\": %s"), conf_logdir,strerror(errno));
162         /*NOTREACHED*/
163     }
164     while ((adir=readdir(dir)) != NULL) {
165         if(strncmp(adir->d_name,"log.",4)==0) {
166             useful=0;
167             for (name=output_find_log;*name !=NULL; name++) {
168                 if((strlen(adir->d_name) >= 13 &&
169                     strlen(*name) >= 13 &&
170                     adir->d_name[12] == '.' && (*name)[12] == '.' &&
171                     strncmp(adir->d_name,*name,12)==0) ||
172                    strncmp(adir->d_name,*name,18)==0) {
173                     useful=1;
174                     break;
175                 }
176             }
177             logname=newvstralloc(logname,
178                                  conf_logdir, "/" ,adir->d_name, NULL);
179             if(stat(logname,&stat_log)==0) {
180                 if((time_t)stat_log.st_mtime > date_keep) {
181                     useful = 1;
182                 }
183             }
184             if(useful == 0) {
185                 oldfile = newvstralloc(oldfile,
186                                        conf_logdir, "/", adir->d_name, NULL);
187                 newfile = newvstralloc(newfile,
188                                        olddir, "/", adir->d_name, NULL);
189                 if (rename(oldfile,newfile) != 0) {
190                     error(_("could not rename \"%s\" to \"%s\": %s"),
191                           oldfile, newfile, strerror(errno));
192                     /*NOTREACHED*/
193                 }
194             }
195         }
196     }
197     closedir(dir);
198     for (name = output_find_log; *name != NULL; name++) {
199         amfree(*name);
200     }
201     amfree(output_find_log);
202     amfree(logname);
203     amfree(oldfile);
204     amfree(newfile);
205     amfree(olddir);
206     amfree(conf_logdir);
207     clear_tapelist();
208     free_disklist(&diskl);
209
210     dbclose();
211
212     return 0;
213 }