35e504d72ae4bdcb86f4aaa8c4280d76fca591ab
[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.7 2005/09/20 21:32:26 jrjackson 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 P((void));
41
42 int main(argc, argv)
43 int argc;
44 char **argv;
45 {
46     char *conffile;
47     char *logfname;
48     char *conf_logdir;
49     FILE *logfile;
50     unsigned long malloc_hist_1, malloc_size_1;
51     unsigned long malloc_hist_2, malloc_size_2;
52     char my_cwd[STR_SIZE];
53
54     safe_fd(-1, 0);
55
56     set_pname("amlogroll");
57
58     malloc_size_1 = malloc_inuse(&malloc_hist_1);
59
60     /* Process options */
61     
62     erroutput_type = ERR_INTERACTIVE;
63
64     if (getcwd(my_cwd, sizeof(my_cwd)) == NULL) {
65         error("cannot determine current working directory");
66     }
67
68     if (argc < 2) {
69         config_dir = stralloc2(my_cwd, "/");
70         if ((config_name = strrchr(my_cwd, '/')) != NULL) {
71             config_name = stralloc(config_name + 1);
72         }
73     } else {
74         config_name = stralloc(argv[1]);
75         config_dir = vstralloc(CONFIG_DIR, "/", config_name, "/", NULL);
76     }
77
78     safe_cd();
79
80     /* read configuration files */
81
82     conffile = stralloc2(config_dir, CONFFILE_NAME);
83     if(read_conffile(conffile)) {
84         error("errors processing config file \"%s\"", conffile);
85     }
86     amfree(conffile);
87
88     conf_logdir = getconf_str(CNF_LOGDIR);
89     if (*conf_logdir == '/') {
90         conf_logdir = stralloc(conf_logdir);
91     } else {
92         conf_logdir = stralloc2(config_dir, conf_logdir);
93     }
94     logfname = vstralloc(conf_logdir, "/", "log", NULL);
95     amfree(conf_logdir);
96
97     if((logfile = fopen(logfname, "r")) == NULL) {
98         error("could not open log %s: %s", logfname, strerror(errno));
99     }
100     amfree(logfname);
101
102     erroutput_type |= ERR_AMANDALOG;
103     set_logerror(logerror);
104
105     while(get_logline(logfile)) {
106         if(curlog == L_START) {
107             handle_start();
108             if(datestamp != NULL) {
109                 break;
110             }
111         }
112     }
113     afclose(logfile);
114  
115     log_rename(datestamp);
116
117     amfree(datestamp);
118     amfree(config_dir);
119     amfree(config_name);
120
121     malloc_size_2 = malloc_inuse(&malloc_hist_2);
122
123     if(malloc_size_1 != malloc_size_2) {
124         malloc_list(fileno(stderr), malloc_hist_1, malloc_hist_2);
125     }
126
127     return 0;
128 }
129
130 void handle_start()
131 {
132     static int started = 0;
133     char *s, *fp;
134     int ch;
135
136     if(!started) {
137         s = curstr;
138         ch = *s++;
139
140         skip_whitespace(s, ch);
141 #define sc "date"
142         if(ch == '\0' || strncmp(s - 1, sc, sizeof(sc)-1) != 0) {
143             return;                             /* ignore bogus line */
144         }
145         s += sizeof(sc)-1;
146         ch = s[-1];
147 #undef sc
148         skip_whitespace(s, ch);
149         if(ch == '\0') {
150             return;
151         }
152         fp = s - 1;
153         skip_non_whitespace(s, ch);
154         s[-1] = '\0';
155         datestamp = newstralloc(datestamp, fp);
156         s[-1] = ch;
157
158         started = 1;
159     }
160 }