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