2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 1991-1998 University of Maryland at College Park
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.
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.
23 * Authors: the Amanda Development Team. Its members are listed in a
24 * file named AUTHORS, in the root directory of this distribution.
27 * $Id: client_util.c,v 1.1.2.27.2.1 2005/10/11 14:50:00 martinea Exp $
31 #include "client_util.h"
35 #define MAXMAXDUMPS 16
37 static char *fixup_relative(name, device)
43 char *dirname = amname_to_dirname(device);
44 newname = vstralloc(dirname, "/", name , NULL);
48 newname = stralloc(name);
54 static char *get_name(diskname, exin, t, n)
55 char *diskname, *exin;
59 char number[NUM_STR_SIZE];
63 ts = construct_timestamp(&t);
67 ap_snprintf(number, sizeof(number), "%03d", n - 1);
69 filename = vstralloc(get_pname(), ".", diskname, ".", ts, number, ".",
76 static char *build_name(disk, exin, verbose)
81 char *filename = NULL;
82 char *afilename = NULL;
89 char *test_name = NULL;
90 int match_len, d_name_len;
94 diskname = sanitise_filename(disk);
96 dbgdir = stralloc2(AMANDA_TMPDIR, "/");
97 if((d = opendir(AMANDA_TMPDIR)) == NULL) {
98 error("open debug directory \"%s\": %s",
99 AMANDA_TMPDIR, strerror(errno));
101 test_name = get_name(diskname, exin,
102 curtime - (AMANDA_DEBUG_DAYS * 24 * 60 * 60), 0);
103 match_len = strlen(get_pname()) + strlen(diskname) + 2;
104 while((entry = readdir(d)) != NULL) {
105 if(is_dot_or_dotdot(entry->d_name)) {
108 d_name_len = strlen(entry->d_name);
109 if(strncmp(test_name, entry->d_name, match_len) != 0
110 || d_name_len < match_len + 14 + 8
111 || strcmp(entry->d_name+ d_name_len - 7, exin) != 0) {
112 continue; /* not one of our files */
114 if(strcmp(entry->d_name, test_name) < 0) {
115 e = newvstralloc(e, dbgdir, entry->d_name, NULL);
116 (void) unlink(e); /* get rid of old file */
125 filename = get_name(diskname, exin, curtime, n);
126 afilename = newvstralloc(afilename, dbgdir, filename, NULL);
127 if((fd=open(afilename, O_WRONLY|O_CREAT|O_EXCL|O_APPEND, 0600)) < 0){
135 } while(!afilename && n < 1000);
137 if(afilename == NULL) {
138 filename = get_name(diskname, exin, curtime, 0);
139 afilename = newvstralloc(afilename, dbgdir, filename, NULL);
140 dbprintf(("%s: Cannot create '%s'\n", debug_prefix(NULL), afilename));
142 printf("ERROR [cannot create: %s]\n", afilename);
154 static int add_exclude(file_exclude, aexc, verbose)
162 if(aexc[l-1] == '\n') {
166 fprintf(file_exclude, "%s\n", aexc);
170 static int add_include(disk, device, file_include, ainc, verbose)
180 if(ainc[l-1] == '\n') {
185 dbprintf(("%s: include must be at least 3 character long: %s\n",
186 debug_prefix(NULL), ainc));
188 printf("ERROR [include must be at least 3 character long: %s]\n", ainc);
191 else if(ainc[0] != '.' && ainc[0] != '\0' && ainc[1] != '/') {
192 dbprintf(("%s: include must start with './': %s\n",
193 debug_prefix(NULL), ainc));
195 printf("ERROR [include must start with './': %s]\n", ainc);
199 char *incname = ainc+2;
200 if(strchr(incname, '/')) {
201 fprintf(file_include, "./%s\n", incname);
207 struct dirent *entry;
209 regex = glob_to_regex(incname);
210 if((d = opendir(device)) == NULL) {
211 dbprintf(("%s: Can't open disk '%s']\n",
212 debug_prefix(NULL), device));
214 printf("ERROR [Can't open disk '%s']\n", device);
218 while((entry = readdir(d)) != NULL) {
219 if(is_dot_or_dotdot(entry->d_name)) {
222 if(match(regex, entry->d_name)) {
223 fprintf(file_include, "./%s\n", entry->d_name);
234 char *build_exclude(disk, device, options, verbose)
246 if(options->exclude_file) nb_exclude += options->exclude_file->nb_element;
247 if(options->exclude_list) nb_exclude += options->exclude_list->nb_element;
249 if(nb_exclude == 0) return NULL;
251 if((filename = build_name(disk, "exclude", verbose)) != NULL) {
252 if((file_exclude = fopen(filename,"w")) != NULL) {
254 if(options->exclude_file) {
255 for(excl = options->exclude_file->first; excl != NULL;
257 add_exclude(file_exclude, excl->name,
258 verbose && options->exclude_optional == 0);
262 if(options->exclude_list) {
263 for(excl = options->exclude_list->first; excl != NULL;
265 char *exclname = fixup_relative(excl->name, device);
266 if((exclude = fopen(exclname, "r")) != NULL) {
267 while ((aexc = agets(exclude)) != NULL) {
268 add_exclude(file_exclude, aexc,
269 verbose && options->exclude_optional == 0);
275 dbprintf(("%s: Can't open exclude file '%s': %s\n",
279 if(verbose && (options->exclude_optional == 0 ||
281 printf("ERROR [Can't open exclude file '%s': %s]\n",
282 exclname, strerror(errno));
287 fclose(file_exclude);
290 dbprintf(("%s: Can't create exclude file '%s': %s\n",
295 printf("ERROR [Can't create exclude file '%s': %s]\n", filename,
303 char *build_include(disk, device, options, verbose)
317 if(options->include_file) nb_include += options->include_file->nb_element;
318 if(options->include_list) nb_include += options->include_list->nb_element;
320 if(nb_include == 0) return NULL;
322 if((filename = build_name(disk, "include", verbose)) != NULL) {
323 if((file_include = fopen(filename,"w")) != NULL) {
325 if(options->include_file) {
326 for(incl = options->include_file->first; incl != NULL;
328 nb_exp += add_include(disk, device, file_include,
330 verbose && options->include_optional == 0);
334 if(options->include_list) {
335 for(incl = options->include_list->first; incl != NULL;
337 char *inclname = fixup_relative(incl->name, device);
338 if((include = fopen(inclname, "r")) != NULL) {
339 while ((ainc = agets(include)) != NULL) {
340 nb_exp += add_include(disk, device,
342 verbose && options->include_optional == 0);
348 dbprintf(("%s: Can't open include file '%s': %s\n",
352 if(verbose && (options->include_optional == 0 ||
354 printf("ERROR [Can't open include file '%s': %s]\n",
355 inclname, strerror(errno));
360 fclose(file_include);
363 dbprintf(("%s: Can't create include file '%s': %s\n",
368 printf("ERROR [Can't create include file '%s': %s]\n", filename,
374 dbprintf(("%s: No include for '%s'\n", debug_prefix(NULL), disk));
375 if(verbose && options->include_optional == 0)
376 printf("ERROR [No include for '%s']\n", disk);
383 void init_options(options)
387 options->compress = NO_COMPR;
388 options->no_record = 0;
389 options->bsd_auth = 0;
390 options->createindex = 0;
392 options->krb4_auth = 0;
393 options->kencrypt = 0;
395 options->exclude_file = NULL;
396 options->exclude_list = NULL;
397 options->include_file = NULL;
398 options->include_list = NULL;
399 options->exclude_optional = 0;
400 options->include_optional = 0;
404 option_t *parse_options(str, disk, device, fs, verbose)
414 options = alloc(sizeof(option_t));
415 init_options(options);
416 options->str = stralloc(str);
421 while (tok != NULL) {
422 if(am_has_feature(fs, fe_options_auth)
423 && strncmp(tok, "auth=", 5) == 0) {
429 dbprintf(("%s: multiple auth option\n",
430 debug_prefix(NULL)));
432 printf("ERROR [multiple auth option]\n");
435 if(strcasecmp(tok + 5, "bsd") == 0) {
436 options->bsd_auth = 1;
439 else if(strcasecmp(tok + 5, "krb4") == 0) {
440 options->krb4_auth = 1;
444 dbprintf(("%s: unknown auth= value \"%s\"\n",
445 debug_prefix(NULL), tok + 5));
447 printf("ERROR [unknown auth= value \"%s\"]\n", tok + 5);
451 else if(strcmp(tok, "compress-fast") == 0) {
452 if(options->compress != NO_COMPR) {
453 dbprintf(("%s: multiple compress option\n",
454 debug_prefix(NULL)));
456 printf("ERROR [multiple compress option]\n");
459 options->compress = COMPR_FAST;
461 else if(strcmp(tok, "compress-best") == 0) {
462 if(options->compress != NO_COMPR) {
463 dbprintf(("%s: multiple compress option\n",
464 debug_prefix(NULL)));
466 printf("ERROR [multiple compress option]\n");
469 options->compress = COMPR_BEST;
471 else if(strcmp(tok, "srvcomp-fast") == 0) {
472 if(options->compress != NO_COMPR) {
473 dbprintf(("%s: multiple compress option\n",
474 debug_prefix(NULL)));
476 printf("ERROR [multiple compress option]\n");
479 options->compress = COMPR_SERVER_FAST;
481 else if(strcmp(tok, "srvcomp-best") == 0) {
482 if(options->compress != NO_COMPR) {
483 dbprintf(("%s: multiple compress option\n",
484 debug_prefix(NULL)));
486 printf("ERROR [multiple compress option]\n");
489 options->compress = COMPR_SERVER_BEST;
491 else if(strcmp(tok, "no-record") == 0) {
492 if(options->no_record != 0) {
493 dbprintf(("%s: multiple no-record option\n",
494 debug_prefix(NULL)));
496 printf("ERROR [multiple no-record option]\n");
499 options->no_record = 1;
501 else if(strcmp(tok, "bsd-auth") == 0) {
507 dbprintf(("%s: multiple auth option\n",
508 debug_prefix(NULL)));
510 printf("ERROR [multiple auth option]\n");
513 options->bsd_auth = 1;
515 else if(strcmp(tok, "index") == 0) {
516 if(options->createindex != 0) {
517 dbprintf(("%s: multiple index option\n",
518 debug_prefix(NULL)));
520 printf("ERROR [multiple index option]\n");
523 options->createindex = 1;
526 else if(strcmp(tok, "krb4-auth") == 0) {
527 if(options->bsd_auth + options->krb4_auth > 0) {
528 dbprintf(("%s: multiple auth option\n",
529 debug_prefix(NULL)));
531 printf("ERROR [multiple auth option]\n");
534 options->krb4_auth = 1;
536 else if(strcmp(tok, "kencrypt") == 0) {
537 if(options->kencrypt != 0) {
538 dbprintf(("%s: multiple kencrypt option\n",
539 debug_prefix(NULL)));
541 printf("ERROR [multiple kencrypt option]\n");
544 options->kencrypt = 1;
547 else if(strcmp(tok, "exclude-optional") == 0) {
548 if(options->exclude_optional != 0) {
549 dbprintf(("%s: multiple exclude-optional option\n",
550 debug_prefix(NULL)));
552 printf("ERROR [multiple exclude-optional option]\n");
555 options->exclude_optional = 1;
557 else if(strcmp(tok, "include-optional") == 0) {
558 if(options->include_optional != 0) {
559 dbprintf(("%s: multiple include-optional option\n",
560 debug_prefix(NULL)));
562 printf("ERROR [multiple include-optional option]\n");
565 options->include_optional = 1;
567 else if(strncmp(tok,"exclude-file=", 13) == 0) {
569 options->exclude_file = append_sl(options->exclude_file,exc);
571 else if(strncmp(tok,"exclude-list=", 13) == 0) {
573 options->exclude_list = append_sl(options->exclude_list, exc);
575 else if(strncmp(tok,"include-file=", 13) == 0) {
577 options->include_file = append_sl(options->include_file,exc);
579 else if(strncmp(tok,"include-list=", 13) == 0) {
581 options->include_list = append_sl(options->include_list, exc);
583 else if(strcmp(tok,"|") == 0) {
586 dbprintf(("%s: unknown option \"%s\"\n",
587 debug_prefix(NULL), tok));
589 printf("ERROR [unknown option \"%s\"]\n", tok);
592 tok = strtok(NULL, ";");
599 void init_g_options(g_options)
600 g_option_t *g_options;
602 g_options->features = NULL;
603 g_options->hostname = NULL;
604 g_options->maxdumps = 0;
608 g_option_t *parse_g_options(str, verbose)
612 g_option_t *g_options;
616 g_options = alloc(sizeof(g_option_t));
617 init_g_options(g_options);
618 g_options->str = stralloc(str);
623 while (tok != NULL) {
624 if(strncmp(tok,"features=", 9) == 0) {
625 if(g_options->features != NULL) {
626 dbprintf(("%s: multiple features option\n",
627 debug_prefix(NULL)));
629 printf("ERROR [multiple features option]\n");
632 if((g_options->features = am_string_to_feature(tok+9)) == NULL) {
633 dbprintf(("%s: bad features value \"%s\n",
634 debug_prefix(NULL), tok+10));
636 printf("ERROR [bad features value \"%s\"]\n", tok+10);
640 else if(strncmp(tok,"hostname=", 9) == 0) {
641 if(g_options->hostname != NULL) {
642 dbprintf(("%s: multiple hostname option\n",
643 debug_prefix(NULL)));
645 printf("ERROR [multiple hostname option]\n");
648 g_options->hostname = stralloc(tok+9);
650 else if(strncmp(tok,"maxdumps=", 9) == 0) {
651 if(g_options->maxdumps != 0) {
652 dbprintf(("%s: multiple maxdumps option\n",
653 debug_prefix(NULL)));
655 printf("ERROR [multiple maxdumps option]\n");
658 if(sscanf(tok+9, "%d;", &new_maxdumps) == 1) {
659 if (new_maxdumps > MAXMAXDUMPS) {
660 g_options->maxdumps = MAXMAXDUMPS;
662 else if (new_maxdumps > 0) {
663 g_options->maxdumps = new_maxdumps;
666 dbprintf(("%s: bad maxdumps value \"%s\"\n",
667 debug_prefix(NULL), tok+9));
669 printf("ERROR [bad maxdumps value \"%s\"]\n",
675 dbprintf(("%s: bad maxdumps value \"%s\"\n",
676 debug_prefix(NULL), tok+9));
678 printf("ERROR [bad maxdumps value \"%s\"]\n",
684 dbprintf(("%s: unknown option \"%s\"\n",
685 debug_prefix(NULL), tok));
687 printf("ERROR [unknown option \"%s\"]\n", tok);
690 tok = strtok(NULL, ";");
692 if(g_options->features == NULL) {
693 g_options->features = am_set_default_feature_set();
695 if(g_options->maxdumps == 0) /* default */
696 g_options->maxdumps = 1;