1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 1991-1998 University of Maryland at College Park
4  * Copyright (c) 2007-2013 Zmanda, Inc.  All Rights Reserved.
5  * All Rights Reserved.
6  *
7  * Permission to use, copy, modify, distribute, and sell this software and its
8  * documentation for any purpose is hereby granted without fee, provided that
9  * the above copyright notice appear in all copies and that both that
10  * copyright notice and this permission notice appear in supporting
11  * documentation, and that the name of U.M. not be used in advertising or
12  * publicity pertaining to distribution of the software without specific,
13  * written prior permission.  U.M. makes no representations about the
14  * suitability of this software for any purpose.  It is provided "as is"
15  * without express or implied warranty.
16  *
17  * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
19  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
21  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23  *
24  * Authors: the Amanda Development Team.  Its members are listed in a
25  * file named AUTHORS, in the root directory of this distribution.
26  */
27 /*
28  * $Id: amtrmlog.c,v 1.17 2006/07/25 18:27:57 martinea Exp $
29  *
30  * trims number of index files to only those still in system.  Well
31  * actually, it keeps a few extra, plus goes back to the last level 0
32  * dump.
33  */
34 
35 #include "amanda.h"
36 #include "arglist.h"
37 #include "conffile.h"
38 #include "diskfile.h"
39 #include "tapefile.h"
40 #include "find.h"
41 
42 int amtrmidx_debug = 0;
43 
44 int main(int argc, char **argv);
45 
46 int
main(int argc,char ** argv)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_overrides_t *cfg_ovr = NULL;
69 
70     if (argc > 1 && argv && argv[1] && g_str_equal(argv[1], "--version")) {
71 	printf("amtrmlog-%s\n", VERSION);
72 	return (0);
73     }
74 
75     /*
76      * Configure program for internationalization:
77      *   1) Only set the message locale for now.
78      *   2) Set textdomain for all amanda related programs to "amanda"
79      *      We don't want to be forced to support dozens of message catalogs.
80      */
81     setlocale(LC_MESSAGES, "C");
82     textdomain("amanda");
83 
84     safe_fd(-1, 0);
85     safe_cd();
86 
87     set_pname("amtrmlog");
88 
89     /* Don't die when child closes pipe */
90     signal(SIGPIPE, SIG_IGN);
91 
92     cfg_ovr = extract_commandline_config_overrides(&argc, &argv);
93 
94     if (argc > 1 && strcmp(argv[1], "-t") == 0) {
95 	amtrmidx_debug = 1;
96 	argc--;
97 	argv++;
98     }
99 
100     if (argc < 2) {
101 	g_fprintf(stderr, _("Usage: %s [-t] <config> [-o configoption]*\n"), argv[0]);
102 	return 1;
103     }
104 
105     dbopen(DBG_SUBDIR_SERVER);
106     dbprintf(_("%s: version %s\n"), argv[0], VERSION);
107 
108     set_config_overrides(cfg_ovr);
109     config_init(CONFIG_INIT_EXPLICIT_NAME, argv[1]);
110 
111     conf_diskfile = config_dir_relative(getconf_str(CNF_DISKFILE));
112     read_diskfile(conf_diskfile, &diskl);
113     amfree(conf_diskfile);
114 
115     if (config_errors(NULL) >= CFGERR_WARNINGS) {
116 	config_print_errors();
117 	if (config_errors(NULL) >= CFGERR_ERRORS) {
118 	    g_critical(_("errors processing config file"));
119 	}
120     }
121 
122     check_running_as(RUNNING_AS_DUMPUSER);
123 
124     dbrename(get_config_name(), DBG_SUBDIR_SERVER);
125 
126     conf_tapelist = config_dir_relative(getconf_str(CNF_TAPELIST));
127     if (read_tapelist(conf_tapelist)) {
128 	error(_("could not load tapelist \"%s\""), conf_tapelist);
129 	/*NOTREACHED*/
130     }
131     amfree(conf_tapelist);
132 
133     today = time((time_t *)NULL);
134     dumpcycle = getconf_int(CNF_DUMPCYCLE);
135     if (dumpcycle > 30)
136 	dumpcycle = 30;
137     date_keep = today - (dumpcycle * 86400);
138 
139     output_find_log = find_log();
140 
141     /* determine how many log to keep */
142     no_keep = getconf_int(CNF_TAPECYCLE) * 2;
143     if (no_keep > 30)
144 	no_keep = 30;
145 
146     dbprintf(plural(_("Keeping %d log file\n"),
147 		    _("Keeping %d log files\n"), no_keep),
148 	     no_keep);
149 
150     conf_logdir = config_dir_relative(getconf_str(CNF_LOGDIR));
151     olddir = vstralloc(conf_logdir, "/oldlog", NULL);
152     if (mkpdir(olddir, 0700, (uid_t)-1, (gid_t)-1) != 0) {
153 	error(_("could not create parents of %s: %s"), olddir, strerror(errno));
154 	/*NOTREACHED*/
155     }
156     if (mkdir(olddir, 0700) != 0 && errno != EEXIST) {
157 	error(_("could not create %s: %s"), olddir, strerror(errno));
158 	/*NOTREACHED*/
159     }
160 
161     if (stat(olddir,&stat_old) == -1) {
162 	error(_("can't stat oldlog directory \"%s\": %s"), olddir, strerror(errno));
163 	/*NOTREACHED*/
164     }
165 
166     if (!S_ISDIR(stat_old.st_mode)) {
167 	error(_("Oldlog directory \"%s\" is not a directory"), olddir);
168 	/*NOTREACHED*/
169     }
170 
171     if ((dir = opendir(conf_logdir)) == NULL) {
172 	error(_("could not open log directory \"%s\": %s"), conf_logdir,strerror(errno));
173 	/*NOTREACHED*/
174     }
175     while ((adir=readdir(dir)) != NULL) {
176 	if (strncmp(adir->d_name, "log.", 4)==0) {
177 	    useful=0;
178 	    for (name=output_find_log; *name != NULL; name++) {
179 		if((strlen(adir->d_name) >= 13 &&
180 		    strlen(*name) >= 13 &&
181 		    adir->d_name[12] == '.' && (*name)[12] == '.' &&
182 		    strncmp(adir->d_name,*name,12) == 0) ||
183 		   strncmp(adir->d_name,*name,18) == 0) {
184 		    useful=1;
185 		    break;
186 		}
187 	    }
188 	    logname=newvstralloc(logname,
189 				 conf_logdir, "/" ,adir->d_name, NULL);
190 	    if (stat(logname,&stat_log) == 0) {
191 		if ((time_t)stat_log.st_mtime > date_keep) {
192 		    useful = 1;
193 		}
194 	    }
195 	    if (useful == 0) {
196 		char *d;
197 		char *datestamp;
198 		char *amdumpfile;
199 
200 		oldfile = newvstralloc(oldfile,
201 				       conf_logdir, "/", adir->d_name, NULL);
202 		newfile = newvstralloc(newfile,
203 				       olddir, "/", adir->d_name, NULL);
204 		if (rename(oldfile,newfile) != 0) {
205 		    error(_("could not rename \"%s\" to \"%s\": %s"),
206 			  oldfile, newfile, strerror(errno));
207 		    /*NOTREACHED*/
208 		}
209 
210 		datestamp = g_strdup(adir->d_name);
211 		d = strrchr(datestamp+4, '.');
212 		if (*d) *d = '\0';
213 		amdumpfile = g_strconcat(conf_logdir, "/amdump.", datestamp+4, NULL);
214 		if (unlink(amdumpfile) != 0 && errno != ENOENT) {
215 		    g_debug("Failed to unlink '%s': %s", amdumpfile, strerror(errno));
216 		}
217 		g_free(datestamp);
218 		g_free(amdumpfile);
219 	    }
220 	}
221     }
222     closedir(dir);
223     for (name = output_find_log; *name != NULL; name++) {
224 	amfree(*name);
225     }
226     amfree(output_find_log);
227     amfree(logname);
228     amfree(oldfile);
229     amfree(newfile);
230     amfree(olddir);
231     amfree(conf_logdir);
232     clear_tapelist();
233     unload_disklist();
234     diskl.head = NULL;
235     diskl.tail = NULL;
236 
237     dbclose();
238 
239     return 0;
240 }
241