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