⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 client_util.c

📁 开源备份软件源码 AMANDA, the Advanced Maryland Automatic Network Disk Archiver, is a backup system that a
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * Amanda, The Advanced Maryland Automatic Network Disk Archiver * Copyright (c) 1991-1998 University of Maryland at College Park * All Rights Reserved. * * Permission to use, copy, modify, distribute, and sell this software and its * documentation for any purpose is hereby granted without fee, provided that * the above copyright notice appear in all copies and that both that * copyright notice and this permission notice appear in supporting * documentation, and that the name of U.M. not be used in advertising or * publicity pertaining to distribution of the software without specific, * written prior permission.  U.M. makes no representations about the * suitability of this software for any purpose.  It is provided "as is" * without express or implied warranty. * * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M. * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * * Authors: the Amanda Development Team.  Its members are listed in a * file named AUTHORS, in the root directory of this distribution. *//*  * $Id: client_util.c,v 1.34 2006/05/25 01:47:11 johnfranks Exp $ * */#include "amanda.h"#include "conffile.h"#include "client_util.h"#include "getfsent.h"#include "util.h"#include "timestamp.h"#include "pipespawn.h"#define MAXMAXDUMPS 16static int add_exclude(FILE *file_exclude, char *aexc, int verbose);static int add_include(char *disk, char *device, FILE *file_include, char *ainc, int verbose);static char *build_name(char *disk, char *exin, int verbose);static char *get_name(char *diskname, char *exin, time_t t, int n);char *fixup_relative(    char *	name,    char *	device){    char *newname;    if(*name != '/') {	char *dirname = amname_to_dirname(device);	newname = vstralloc(dirname, "/", name , NULL);	amfree(dirname);    }    else {	newname = stralloc(name);    }    return newname;}static char *get_name(    char *	diskname,    char *	exin,    time_t	t,    int		n){    char number[NUM_STR_SIZE];    char *filename;    char *ts;    ts = get_timestamp_from_time(t);    if(n == 0)	number[0] = '\0';    else	g_snprintf(number, SIZEOF(number), "%03d", n - 1);	    filename = vstralloc(get_pname(), ".", diskname, ".", ts, number, ".",			 exin, NULL);    amfree(ts);    return filename;}static char *build_name(    char *	disk,    char *	exin,    int		verbose){    int n;    int fd;    char *filename = NULL;    char *afilename = NULL;    char *diskname;    time_t curtime;    char *dbgdir;    char *e = NULL;    DIR *d;    struct dirent *entry;    char *test_name;    size_t match_len, d_name_len;    char *quoted;    time(&curtime);    diskname = sanitise_filename(disk);    dbgdir = stralloc2(AMANDA_TMPDIR, "/");    if((d = opendir(AMANDA_TMPDIR)) == NULL) {	error(_("open debug directory \"%s\": %s"),		AMANDA_TMPDIR, strerror(errno));	/*NOTREACHED*/    }    test_name = get_name(diskname, exin,			 curtime - (AMANDA_DEBUG_DAYS * 24 * 60 * 60), 0);    match_len = strlen(get_pname()) + strlen(diskname) + 2;    while((entry = readdir(d)) != NULL) {	if(is_dot_or_dotdot(entry->d_name)) {	    continue;	}	d_name_len = strlen(entry->d_name);	if(strncmp(test_name, entry->d_name, match_len) != 0	   || d_name_len < match_len + 14 + 8	   || strcmp(entry->d_name+ d_name_len - 7, exin) != 0) {	    continue;				/* not one of our files */	}	if(strcmp(entry->d_name, test_name) < 0) {	    e = newvstralloc(e, dbgdir, entry->d_name, NULL);	    (void) unlink(e);                   /* get rid of old file */	}    }    amfree(test_name);    amfree(e);    closedir(d);    n=0;    do {	filename = get_name(diskname, exin, curtime, n);	afilename = newvstralloc(afilename, dbgdir, filename, NULL);	if((fd=open(afilename, O_WRONLY|O_CREAT|O_APPEND, 0600)) < 0){	    amfree(afilename);	    n++;	}	else {	    close(fd);	}	amfree(filename);    } while(!afilename && n < 1000);    if(afilename == NULL) {	filename = get_name(diskname, exin, curtime, 0);	afilename = newvstralloc(afilename, dbgdir, filename, NULL);	quoted = quote_string(afilename);	dbprintf(_("Cannot create %s (%s)\n"), quoted, strerror(errno));	if(verbose) {	    g_printf(_("ERROR [cannot create %s (%s)]\n"),			quoted, strerror(errno));	}	amfree(quoted);	amfree(afilename);	amfree(filename);    }    amfree(dbgdir);    amfree(diskname);    return afilename;}static intadd_exclude(    FILE *	file_exclude,    char *	aexc,    int		verbose){    size_t l;    char *quoted, *file;    (void)verbose;	/* Quiet unused parameter warning */    l = strlen(aexc);    if(aexc[l-1] == '\n') {	aexc[l-1] = '\0';	l--;    }    file = quoted = quote_string(aexc);    if (*file == '"') {	file[strlen(file) - 1] = '\0';	file++;    }    g_fprintf(file_exclude, "%s\n", file);    amfree(quoted);    return 1;}static intadd_include(    char *	disk,    char *	device,    FILE *	file_include,    char *	ainc,    int		verbose){    size_t l;    int nb_exp=0;    char *quoted, *file;    (void)disk;	/* Quiet unused parameter warning */    l = strlen(ainc);    if(ainc[l-1] == '\n') {	ainc[l-1] = '\0';	l--;    }    if (strncmp(ainc, "./", 2) != 0) {        quoted = quote_string(ainc);        dbprintf(_("include must start with './' (%s)\n"), quoted);	if(verbose) {	    g_printf(_("ERROR [include must start with './' (%s)]\n"), quoted);	}	amfree(quoted);    }    else {	char *incname = ainc+2;	if(strchr(incname, '/')) {            file = quoted = quote_string(ainc);	    if (*file == '"') {		file[strlen(file) - 1] = '\0';		file++;	    }	    g_fprintf(file_include, "%s\n", file);	    amfree(quoted);	    nb_exp++;	}	else {	    char *regex;	    DIR *d;	    struct dirent *entry;	    regex = glob_to_regex(incname);	    if((d = opendir(device)) == NULL) {		quoted = quote_string(device);		dbprintf(_("Can't open disk %s\n"), quoted);		if(verbose) {		    g_printf(_("ERROR [Can't open disk %s]\n"), quoted);		}		amfree(quoted);	    }	    else {		while((entry = readdir(d)) != NULL) {		    if(is_dot_or_dotdot(entry->d_name)) {			continue;		    }		    if(match(regex, entry->d_name)) {			incname = vstralloc("./", entry->d_name, NULL);			file = quoted = quote_string(incname);			if (*file == '"') {			    file[strlen(file) - 1] = '\0';			    file++;			}			g_fprintf(file_include, "%s\n", file);			amfree(quoted);			amfree(incname);			nb_exp++;		    }		}		closedir(d);	    }	    amfree(regex);	}    }    return nb_exp;}char *build_exclude(    char *	disk,    char *	device,    option_t *	options,    int		verbose){    char *filename;    FILE *file_exclude;    FILE *exclude;    char *aexc;    sle_t *excl;    int nb_exclude = 0;    char *quoted;    if(options->exclude_file) nb_exclude += options->exclude_file->nb_element;    if(options->exclude_list) nb_exclude += options->exclude_list->nb_element;    if(nb_exclude == 0) return NULL;    if((filename = build_name(disk, "exclude", verbose)) != NULL) {	if((file_exclude = fopen(filename,"w")) != NULL) {	    if(options->exclude_file) {		for(excl = options->exclude_file->first; excl != NULL;		    excl = excl->next) {		    add_exclude(file_exclude, excl->name,				verbose && options->exclude_optional == 0);		}	    }	    if(options->exclude_list) {		for(excl = options->exclude_list->first; excl != NULL;		    excl = excl->next) {		    char *exclname = fixup_relative(excl->name, device);		    if((exclude = fopen(exclname, "r")) != NULL) {			while ((aexc = agets(exclude)) != NULL) {			    if (aexc[0] == '\0') {				amfree(aexc);				continue;			    }			    add_exclude(file_exclude, aexc,				        verbose && options->exclude_optional == 0);			    amfree(aexc);			}			fclose(exclude);		    }		    else {			quoted = quote_string(exclname);			dbprintf(_("Can't open exclude file %s (%s)\n"),				  quoted, strerror(errno));			if(verbose && (options->exclude_optional == 0 ||				       errno != ENOENT)) {			    g_printf(_("ERROR [Can't open exclude file %s (%s)]\n"),				   quoted, strerror(errno));			}			amfree(quoted);		    }		    amfree(exclname);		}	    }            fclose(file_exclude);	}	else {	    quoted = quote_string(filename);	    dbprintf(_("Can't create exclude file %s (%s)\n"),		      quoted, strerror(errno));	    if(verbose) {		g_printf(_("ERROR [Can't create exclude file %s (%s)]\n"),			quoted, strerror(errno));	    }	    amfree(quoted);	}    }    return filename;}char *build_include(    char *	disk,    char *	device,    option_t *	options,    int		verbose){    char *filename;    FILE *file_include;    FILE *include;    char *ainc = NULL;    sle_t *incl;    int nb_include = 0;    int nb_exp = 0;    char *quoted;    if(options->include_file) nb_include += options->include_file->nb_element;    if(options->include_list) nb_include += options->include_list->nb_element;    if(nb_include == 0) return NULL;    if((filename = build_name(disk, "include", verbose)) != NULL) {	if((file_include = fopen(filename,"w")) != NULL) {	    if(options->include_file) {		for(incl = options->include_file->first; incl != NULL;		    incl = incl->next) {		    nb_exp += add_include(disk, device, file_include,				  incl->name,				  verbose && options->include_optional == 0);		}	    }	    if(options->include_list) {		for(incl = options->include_list->first; incl != NULL;		    incl = incl->next) {		    char *inclname = fixup_relative(incl->name, device);		    if((include = fopen(inclname, "r")) != NULL) {			while ((ainc = agets(include)) != NULL) {			    if (ainc[0] == '\0') {				amfree(ainc);				continue;			    }			    nb_exp += add_include(disk, device,						  file_include, ainc,						  verbose && options->include_optional == 0);			    amfree(ainc);			}			fclose(include);		    }		    else {			quoted = quote_string(inclname);			dbprintf(_("Can't open include file %s (%s)\n"),				  quoted, strerror(errno));			if(verbose && (options->include_optional == 0 ||				       errno != ENOENT)) {			    g_printf(_("ERROR [Can't open include file %s (%s)]\n"),				   quoted, strerror(errno));			}			amfree(quoted);		   }		   amfree(inclname);		}	    }            fclose(file_include);	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -