2 * Copyright (c) 2005 Zmanda Inc. All Rights Reserved.
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.
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
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
17 * Contact information: Zmanda Inc, 505 N Mathlida Ave, Suite 120
18 * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
20 * Author: Dustin J. Mitchell <dustin@zmanda.com>
25 * Utility routines for handling command lines.
42 rv = calloc(1, sizeof(*rv));
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);
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);
66 GSList *dumpspec_list)
68 /* first free all of the individual dumpspecs */
69 g_slist_foreach_nodata(dumpspec_list, dumpspec_free);
71 /* then free the list itself */
72 g_slist_free(dumpspec_list);
76 cmdline_parse_dumpspecs(
81 dumpspec_t *dumpspec = NULL;
86 enum { ARG_GET_HOST, ARG_GET_DISK, ARG_GET_DATESTAMP, ARG_GET_LEVEL } arg_state = ARG_GET_HOST;
88 while (optind < argc) {
92 arg_state = ARG_GET_DISK;
94 && (errstr=validate_regexp(name)) != NULL) {
95 error(_("bad hostname regex \"%s\": %s\n"), name, errstr);
97 dumpspec = dumpspec_new(name, NULL, NULL, NULL);
98 list = g_slist_append(list, (gpointer)dumpspec);
102 arg_state = ARG_GET_DATESTAMP;
104 && (errstr=validate_regexp(name)) != NULL) {
105 error(_("bad diskname regex \"%s\": %s\n"), name, errstr);
107 dumpspec->disk = stralloc(name);
110 case ARG_GET_DATESTAMP:
111 arg_state = ARG_GET_LEVEL;
112 if (!(flags & CMDLINE_PARSE_DATESTAMP)) continue;
114 && (errstr=validate_regexp(name)) != NULL) {
115 error(_("bad datestamp regex \"%s\": %s\n"), name, errstr);
117 dumpspec->datestamp = stralloc(name);
121 arg_state = ARG_GET_HOST;
122 if (!(flags & CMDLINE_PARSE_LEVEL)) continue;
124 && (errstr=validate_regexp(name)) != NULL) {
125 error(_("bad level regex \"%s\": %s\n"), name, errstr);
127 dumpspec->level = stralloc(name);
134 /* if nothing was processed and the caller has requested it,
135 * then add an "empty" element */
136 if (list == NULL && (flags & CMDLINE_EMPTY_TO_WILDCARD)) {
137 dumpspec = dumpspec_new("", "",
138 (flags & CMDLINE_PARSE_DATESTAMP)?"":NULL,
139 (flags & CMDLINE_PARSE_LEVEL)?"":NULL);
140 list = g_slist_append(list, (gpointer)dumpspec);
147 cmdline_format_dumpspec(
148 dumpspec_t *dumpspec)
150 if (!dumpspec) return NULL;
151 return cmdline_format_dumpspec_components(
158 /* Quote str for shell interpretation, being conservative.
159 * Any non-alphanumeric charcacters other than '.' and '/'
160 * trigger surrounding single quotes, and single quotes and
161 * backslashes within those single quotes are escaped.
164 quote_dumpspec_string(char *str)
169 int need_single_quotes = 0;
172 return stralloc("''"); /* special-case the empty string */
174 for (p = str; *p; p++) {
175 if (!isalnum((int)*p) && *p != '.' && *p != '/') need_single_quotes=1;
176 if (*p == '\'' || *p == '\\') len++; /* extra byte for '\' */
179 if (need_single_quotes) len += 2;
181 q = rv = malloc(len+1);
182 if (need_single_quotes) *(q++) = '\'';
183 for (p = str; *p; p++) {
184 if (*p == '\'' || *p == '\\') *(q++) = '\\';
187 if (need_single_quotes) *(q++) = '\'';
194 cmdline_format_dumpspec_components(
202 host = host? quote_dumpspec_string(host):NULL;
203 disk = disk? quote_dumpspec_string(disk):NULL;
204 datestamp = datestamp? quote_dumpspec_string(datestamp):NULL;
205 level = level? quote_dumpspec_string(level):NULL;
211 rv = newvstralloc(rv, rv, " ", disk, NULL);
213 rv = newvstralloc(rv, rv, " ", datestamp, NULL);
215 rv = newvstralloc(rv, rv, " ", level, NULL);
221 if (host) amfree(host);
222 if (disk) amfree(disk);
223 if (datestamp) amfree(datestamp);
224 if (level) amfree(level);
230 cmdline_match_holding(
231 GSList *dumpspec_list)
235 GSList *holding_files;
236 GSList *matching_files = NULL;
239 holding_files = holding_get_files(NULL, 1);
241 for (hi = holding_files; hi != NULL; hi = hi->next) {
243 if (!holding_file_get_dumpfile((char *)hi->data, &file)) continue;
244 if (file.type != F_DUMPFILE) continue;
245 for (li = dumpspec_list; li != NULL; li = li->next) {
246 de = (dumpspec_t *)(li->data);
247 if (de->host && de->host[0] && !match_host(de->host, file.name)) continue;
248 if (de->disk && de->disk[0] && !match_disk(de->disk, file.disk)) continue;
249 if (de->datestamp && de->datestamp[0] && !match_datestamp(de->datestamp, file.datestamp)) continue;
250 matching_files = g_slist_append(matching_files, g_strdup((char *)hi->data));
255 g_slist_free_full(holding_files);
257 return matching_files;