f00c348e482f1d839027bcd931f2bd2649261f60
[debian/amanda] / server-src / amlogroll.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  * Author: James da Silva, Systems Design and Analysis Group
24  *                         Computer Science Department
25  *                         University of Maryland at College Park
26  */
27 /*
28  * $Id: amlogroll.c,v 1.14 2006/07/25 18:27:57 martinea Exp $
29  *
30  * rename a live log file to the datestamped name.
31  */
32
33 #include "amanda.h"
34 #include "conffile.h"
35 #include "logfile.h"
36 #include "version.h"
37
38 char *datestamp;
39
40 void handle_start(void);
41 int main(int argc, char **argv);
42
43 int main(int argc, char **argv)
44 {
45     char *conffile;
46     char *logfname;
47     char *conf_logdir;
48     FILE *logfile;
49     unsigned long malloc_hist_1, malloc_size_1;
50     unsigned long malloc_hist_2, malloc_size_2;
51     char my_cwd[STR_SIZE];
52     int    new_argc,   my_argc;
53     char **new_argv, **my_argv;
54
55     safe_fd(-1, 0);
56
57     set_pname("amlogroll");
58
59     dbopen(DBG_SUBDIR_SERVER);
60
61     malloc_size_1 = malloc_inuse(&malloc_hist_1);
62
63     /* Process options */
64     
65     erroutput_type = ERR_INTERACTIVE;
66
67     if (getcwd(my_cwd, SIZEOF(my_cwd)) == NULL) {
68         error("cannot determine current working directory");
69         /*NOTREACHED*/
70     }
71
72     parse_server_conf(argc, argv, &new_argc, &new_argv);
73     my_argc = new_argc;
74     my_argv = new_argv;
75
76     if (my_argc < 2) {
77         config_dir = stralloc2(my_cwd, "/");
78         if ((config_name = strrchr(my_cwd, '/')) != NULL) {
79             config_name = stralloc(config_name + 1);
80         }
81     } else {
82         config_name = stralloc(my_argv[1]);
83         config_dir = vstralloc(CONFIG_DIR, "/", config_name, "/", NULL);
84     }
85
86     safe_cd();
87
88     /* read configuration files */
89
90     conffile = stralloc2(config_dir, CONFFILE_NAME);
91     if(read_conffile(conffile)) {
92         error("errors processing config file \"%s\"", conffile);
93         /*NOTREACHED*/
94     }
95     amfree(conffile);
96
97     dbrename(config_name, DBG_SUBDIR_SERVER);
98
99     report_bad_conf_arg();
100
101     conf_logdir = getconf_str(CNF_LOGDIR);
102     if (*conf_logdir == '/') {
103         conf_logdir = stralloc(conf_logdir);
104     } else {
105         conf_logdir = stralloc2(config_dir, conf_logdir);
106     }
107     logfname = vstralloc(conf_logdir, "/", "log", NULL);
108     amfree(conf_logdir);
109
110     if((logfile = fopen(logfname, "r")) == NULL) {
111         error("could not open log %s: %s", logfname, strerror(errno));
112         /*NOTREACHED*/
113     }
114     amfree(logfname);
115
116     erroutput_type |= ERR_AMANDALOG;
117     set_logerror(logerror);
118
119     while(get_logline(logfile)) {
120         if(curlog == L_START) {
121             handle_start();
122             if(datestamp != NULL) {
123                 break;
124             }
125         }
126     }
127     afclose(logfile);
128  
129     log_rename(datestamp);
130
131     amfree(datestamp);
132     amfree(config_dir);
133     amfree(config_name);
134     free_new_argv(new_argc, new_argv);
135     free_server_config();
136
137     malloc_size_2 = malloc_inuse(&malloc_hist_2);
138
139     if(malloc_size_1 != malloc_size_2) {
140         malloc_list(fileno(stderr), malloc_hist_1, malloc_hist_2);
141     }
142
143     dbclose();
144
145     return 0;
146 }
147
148 void handle_start(void)
149 {
150     static int started = 0;
151     char *s, *fp;
152     int ch;
153
154     if(!started) {
155         s = curstr;
156         ch = *s++;
157
158         skip_whitespace(s, ch);
159 #define sc "date"
160         if(ch == '\0' || strncmp(s - 1, sc, SIZEOF(sc)-1) != 0) {
161             return;                             /* ignore bogus line */
162         }
163         s += SIZEOF(sc) - 1;
164         ch = s[-1];
165 #undef sc
166         skip_whitespace(s, ch);
167         if(ch == '\0') {
168             return;
169         }
170         fp = s - 1;
171         skip_non_whitespace(s, ch);
172         s[-1] = '\0';
173         datestamp = newstralloc(datestamp, fp);
174         s[-1] = (char)ch;
175
176         started = 1;
177     }
178 }