4a08dad0f1cfab5d11002535a7adb14010f90e83
[debian/amanda] / server-src / cmdline.c
1 /*
2  * Copyright (c) 2007, 2008, 2009, 2010 Zmanda, Inc.  All Rights Reserved.
3  * 
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 2 as published
6  * by the Free Software Foundation.
7  * 
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
11  * for more details.
12  * 
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16  * 
17  * Contact information: Zmanda Inc, 465 S. Mathilda Ave., Suite 300
18  * Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
19  *
20  * Author: Dustin J. Mitchell <dustin@zmanda.com>
21  */
22 /*
23  * $Id$
24  *
25  * Utility routines for handling command lines.
26  */
27
28 #include <ctype.h>
29 #include "amanda.h"
30 #include "match.h"
31 #include "cmdline.h"
32 #include "holding.h"
33
34 dumpspec_t *
35 dumpspec_new(
36     char *host, 
37     char *disk, 
38     char *datestamp,
39     char *level)
40 {
41     dumpspec_t *rv;
42
43     rv = g_new0(dumpspec_t, 1);
44     if (host) rv->host = stralloc(host);
45     if (disk) rv->disk = stralloc(disk);
46     if (datestamp) rv->datestamp = stralloc(datestamp);
47     if (level) rv->level = stralloc(level);
48
49     return rv;
50 }
51
52 void
53 dumpspec_free(
54     dumpspec_t *dumpspec)
55 {
56     if (!dumpspec) return;
57     if (dumpspec->host) free(dumpspec->host);
58     if (dumpspec->disk) free(dumpspec->disk);
59     if (dumpspec->datestamp) free(dumpspec->datestamp);
60     if (dumpspec->level) free(dumpspec->level);
61     free(dumpspec);
62 }
63
64 void
65 dumpspec_list_free(
66     GSList *dumpspec_list)
67 {
68     /* first free all of the individual dumpspecs */
69     g_slist_foreach_nodata(dumpspec_list, dumpspec_free);
70
71     /* then free the list itself */
72     g_slist_free(dumpspec_list);
73 }
74
75 GSList *
76 cmdline_parse_dumpspecs(
77     int argc,
78     char **argv,
79     int flags)
80 {
81     dumpspec_t *dumpspec = NULL;
82     GSList *list = NULL;
83     char *errstr;
84     char *name;
85     int optind = 0;
86     enum { ARG_GET_HOST, ARG_GET_DISK, ARG_GET_DATESTAMP, ARG_GET_LEVEL } arg_state = ARG_GET_HOST;
87
88     while (optind < argc) {
89         name = argv[optind];
90         switch (arg_state) {
91             case ARG_GET_HOST:
92                 arg_state = ARG_GET_DISK;
93                 dumpspec = dumpspec_new(name, NULL, NULL, NULL);
94                 list = g_slist_append(list, (gpointer)dumpspec);
95                 break;
96
97             case ARG_GET_DISK:
98                 arg_state = ARG_GET_DATESTAMP;
99                 dumpspec->disk = stralloc(name);
100                 break;
101
102             case ARG_GET_DATESTAMP:
103                 arg_state = ARG_GET_LEVEL;
104                 if (!(flags & CMDLINE_PARSE_DATESTAMP)) continue;
105                 dumpspec->datestamp = stralloc(name);
106                 break;
107
108             case ARG_GET_LEVEL:
109                 arg_state = ARG_GET_HOST;
110                 if (!(flags & CMDLINE_PARSE_LEVEL)) continue;
111                 if (name[0] != '\0'
112                     && (errstr=validate_regexp(name)) != NULL) {
113                     error(_("bad level regex \"%s\": %s\n"), name, errstr);
114                 }
115                 dumpspec->level = stralloc(name);
116                 break;
117         }
118
119         optind++;
120     }
121
122     /* if nothing was processed and the caller has requested it, 
123      * then add an "empty" element */
124     if (list == NULL && (flags & CMDLINE_EMPTY_TO_WILDCARD)) {
125         dumpspec = dumpspec_new("", "", 
126                 (flags & CMDLINE_PARSE_DATESTAMP)?"":NULL,
127                 (flags & CMDLINE_PARSE_LEVEL)?"":NULL);
128         list = g_slist_append(list, (gpointer)dumpspec);
129     }
130
131     return list;
132 }
133
134 char *
135 cmdline_format_dumpspec(
136     dumpspec_t *dumpspec)
137 {
138     if (!dumpspec) return NULL;
139     return cmdline_format_dumpspec_components(
140         dumpspec->host,
141         dumpspec->disk,
142         dumpspec->datestamp,
143         dumpspec->level);
144 }
145
146 /* Quote str for shell interpretation, being conservative.
147  * Any non-alphanumeric charcacters other than '.' and '/'
148  * trigger surrounding single quotes, and single quotes and
149  * backslashes within those single quotes are escaped.
150  */
151 static char *
152 quote_dumpspec_string(char *str)
153 {
154     char *rv;
155     char *p, *q;
156     int len = 0;
157     int need_single_quotes = 0;
158
159     if (!str[0])
160         return stralloc("''"); /* special-case the empty string */
161
162     for (p = str; *p; p++) {
163         if (!isalnum((int)*p) && *p != '.' && *p != '/') need_single_quotes=1;
164         if (*p == '\'' || *p == '\\') len++; /* extra byte for '\' */
165         len++;
166     }
167     if (need_single_quotes) len += 2;
168
169     q = rv = malloc(len+1);
170     if (need_single_quotes) *(q++) = '\'';
171     for (p = str; *p; p++) {
172         if (*p == '\'' || *p == '\\') *(q++) = '\\';
173         *(q++) = *p;
174     }
175     if (need_single_quotes) *(q++) = '\'';
176     *(q++) = '\0';
177
178     return rv;
179 }
180
181 char *
182 cmdline_format_dumpspec_components(
183     char *host,
184     char *disk,
185     char *datestamp,
186     char *level)
187 {
188     char *rv = NULL;
189
190     host = host? quote_dumpspec_string(host):NULL;
191     disk = disk? quote_dumpspec_string(disk):NULL;
192     datestamp = datestamp? quote_dumpspec_string(datestamp):NULL;
193     level = level? quote_dumpspec_string(level):NULL;
194
195     if (host) {
196         rv = host;
197         host = NULL;
198         if (disk) {
199             rv = newvstralloc(rv, rv, " ", disk, NULL);
200             if (datestamp) {
201                 rv = newvstralloc(rv, rv, " ", datestamp, NULL);
202                 if (level) {
203                     rv = newvstralloc(rv, rv, " ", level, NULL);
204                 }
205             }
206         }
207     }
208
209     if (host) amfree(host);
210     if (disk) amfree(disk);
211     if (datestamp) amfree(datestamp);
212     if (level) amfree(level);
213
214     return rv;
215 }
216
217 GSList *
218 cmdline_match_holding(
219     GSList *dumpspec_list)
220 {
221     dumpspec_t *de;
222     GSList *li, *hi;
223     GSList *holding_files;
224     GSList *matching_files = NULL;
225     dumpfile_t file;
226
227     holding_files = holding_get_files(NULL, 1);
228
229     for (hi = holding_files; hi != NULL; hi = hi->next) {
230         /* TODO add level */
231         if (!holding_file_get_dumpfile((char *)hi->data, &file)) continue;
232         if (file.type != F_DUMPFILE) {
233             dumpfile_free_data(&file);
234             continue;
235         }
236         for (li = dumpspec_list; li != NULL; li = li->next) {
237             de = (dumpspec_t *)(li->data);
238             if (de->host && de->host[0] && !match_host(de->host, file.name)) continue;
239             if (de->disk && de->disk[0] && !match_disk(de->disk, file.disk)) continue;
240             if (de->datestamp && de->datestamp[0] && !match_datestamp(de->datestamp, file.datestamp)) continue;
241             matching_files = g_slist_append(matching_files, g_strdup((char *)hi->data));
242             break;
243         }
244         dumpfile_free_data(&file);
245     }
246
247     g_slist_free_full(holding_files);
248
249     return matching_files;
250 }