Imported Upstream version 3.1.0
[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
37 char *datestamp;
38
39 void handle_start(void);
40 int main(int argc, char **argv);
41
42 int
43 main(
44     int         argc,
45     char **     argv)
46 {
47     char *logfname;
48     char *conf_logdir;
49     FILE *logfile;
50     config_overrides_t *cfg_ovr = NULL;
51     char *cfg_opt = NULL;
52
53     /*
54      * Configure program for internationalization:
55      *   1) Only set the message locale for now.
56      *   2) Set textdomain for all amanda related programs to "amanda"
57      *      We don't want to be forced to support dozens of message catalogs.
58      */  
59     setlocale(LC_MESSAGES, "C");
60     textdomain("amanda"); 
61
62     safe_fd(-1, 0);
63
64     set_pname("amlogroll");
65
66     dbopen(DBG_SUBDIR_SERVER);
67
68     add_amanda_log_handler(amanda_log_stderr);
69
70     /* Process options */
71     cfg_ovr = extract_commandline_config_overrides(&argc, &argv);
72
73     if (argc >= 2) {
74         cfg_opt = argv[1];
75     }
76
77     /* read configuration files */
78
79     set_config_overrides(cfg_ovr);
80     config_init(CONFIG_INIT_EXPLICIT_NAME | CONFIG_INIT_USE_CWD, cfg_opt);
81
82     if (config_errors(NULL) >= CFGERR_WARNINGS) {
83         config_print_errors();
84         if (config_errors(NULL) >= CFGERR_ERRORS) {
85             g_critical(_("errors processing config file"));
86         }
87     }
88
89     safe_cd(); /* must happen after config_init */
90
91     check_running_as(RUNNING_AS_DUMPUSER);
92
93     dbrename(get_config_name(), DBG_SUBDIR_SERVER);
94
95     conf_logdir = config_dir_relative(getconf_str(CNF_LOGDIR));
96     logfname = vstralloc(conf_logdir, "/", "log", NULL);
97     amfree(conf_logdir);
98
99     if((logfile = fopen(logfname, "r")) == NULL) {
100         error(_("could not open log %s: %s"), logfname, strerror(errno));
101         /*NOTREACHED*/
102     }
103     amfree(logfname);
104
105     add_amanda_log_handler(amanda_log_trace_log);
106
107     while(get_logline(logfile)) {
108         if(curlog == L_START) {
109             handle_start();
110             if(datestamp != NULL) {
111                 break;
112             }
113         }
114     }
115     afclose(logfile);
116  
117     log_rename(datestamp);
118
119     amfree(datestamp);
120
121     dbclose();
122
123     return 0;
124 }
125
126 void handle_start(void)
127 {
128     static int started = 0;
129     char *s, *fp;
130     int ch;
131
132     if(!started) {
133         s = curstr;
134         ch = *s++;
135
136         skip_whitespace(s, ch);
137         if(ch == '\0' || strncmp_const_skip(s - 1, "date", s, ch) != 0) {
138             return;                             /* ignore bogus line */
139         }
140
141         skip_whitespace(s, ch);
142         if(ch == '\0') {
143             return;
144         }
145         fp = s - 1;
146         skip_non_whitespace(s, ch);
147         s[-1] = '\0';
148         datestamp = newstralloc(datestamp, fp);
149         s[-1] = (char)ch;
150
151         started = 1;
152     }
153 }