7875815975622c4bf9539c0076ce9fd259baf4f2
[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 | CONFIG_INIT_FATAL,
81                 cfg_opt);
82     apply_config_overwrites(cfg_ovr);
83
84     safe_cd(); /* must happen after config_init */
85
86     check_running_as(RUNNING_AS_DUMPUSER);
87
88     dbrename(config_name, DBG_SUBDIR_SERVER);
89
90     conf_logdir = config_dir_relative(getconf_str(CNF_LOGDIR));
91     logfname = vstralloc(conf_logdir, "/", "log", NULL);
92     amfree(conf_logdir);
93
94     if((logfile = fopen(logfname, "r")) == NULL) {
95         error(_("could not open log %s: %s"), logfname, strerror(errno));
96         /*NOTREACHED*/
97     }
98     amfree(logfname);
99
100     erroutput_type |= ERR_AMANDALOG;
101     set_logerror(logerror);
102
103     while(get_logline(logfile)) {
104         if(curlog == L_START) {
105             handle_start();
106             if(datestamp != NULL) {
107                 break;
108             }
109         }
110     }
111     afclose(logfile);
112  
113     log_rename(datestamp);
114
115     amfree(datestamp);
116
117     dbclose();
118
119     return 0;
120 }
121
122 void handle_start(void)
123 {
124     static int started = 0;
125     char *s, *fp;
126     int ch;
127
128     if(!started) {
129         s = curstr;
130         ch = *s++;
131
132         skip_whitespace(s, ch);
133         if(ch == '\0' || strncmp_const_skip(s - 1, "date", s, ch) != 0) {
134             return;                             /* ignore bogus line */
135         }
136
137         skip_whitespace(s, ch);
138         if(ch == '\0') {
139             return;
140         }
141         fp = s - 1;
142         skip_non_whitespace(s, ch);
143         s[-1] = '\0';
144         datestamp = newstralloc(datestamp, fp);
145         s[-1] = (char)ch;
146
147         started = 1;
148     }
149 }