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

📄 extract_list.c

📁 开源备份软件源码 AMANDA, the Advanced Maryland Automatic Network Disk Archiver, is a backup system that a
💻 C
📖 第 1 页 / 共 5 页
字号:
		}		*s = '/';		s++;	    }	    amfree(path);	    /* Check fn->path */	    filename = stralloc2(dir, fn->path);	    if (filename[strlen(filename)-1] == '/') {		filename[strlen(filename)-1] = '\0';	    }	    if (lstat(filename, &stat_buf) == 0) {		if(S_ISDIR(stat_buf.st_mode)) {		    if(!is_empty_dir(filename)) {			g_printf(_("WARNING: All existing files in %s "			       "will be deleted.\n"), filename);		    }		} else if(S_ISREG(stat_buf.st_mode)) {		    g_printf(_("WARNING: Existing file %s will be overwritten\n"),			   filename);		} else {		    if (add_to_unlink_list(filename)) {			g_printf(_("WARNING: Existing entry %s will be deleted\n"),			       filename);		    }		}	    } else if (errno != ENOENT) {		g_printf(_("Can't stat %s: %s\n"), filename, strerror(errno));	    }	    amfree(filename);	}    }}/* returns -1 if error *//* returns  0 on succes *//* returns  1 if already added */static intadd_extract_item(    DIR_ITEM *ditem){    EXTRACT_LIST *this, *this1;    EXTRACT_LIST_ITEM *that, *curr;    char *ditem_path = NULL;    ditem_path = stralloc(ditem->path);    clean_pathname(ditem_path);    for (this = extract_list; this != NULL; this = this->next)    {	/* see if this is the list for the tape */		if (this->level == ditem->level && strcmp(this->tape, ditem->tape) == 0)	{	    /* yes, so add to list */	    curr=this->files;	    while(curr!=NULL)	    {		if (strcmp(curr->path,ditem_path) == 0) {		    amfree(ditem_path);		    return 1;		}		curr=curr->next;	    }	    that = (EXTRACT_LIST_ITEM *)alloc(sizeof(EXTRACT_LIST_ITEM));            that->path = stralloc(ditem_path);	    that->next = this->files;	    this->files = that;		/* add at front since easiest */	    amfree(ditem_path);	    return 0;	}    }    /* so this is the first time we have seen this tape */    this = (EXTRACT_LIST *)alloc(sizeof(EXTRACT_LIST));    this->tape = stralloc(ditem->tape);    this->level = ditem->level;    this->fileno = ditem->fileno;    this->date = stralloc(ditem->date);    that = (EXTRACT_LIST_ITEM *)alloc(sizeof(EXTRACT_LIST_ITEM));    that->path = stralloc(ditem_path);    that->next = NULL;    this->files = that;    /* add this in date increasing order          */    /* because restore must be done in this order */    /* add at begining */    if(extract_list==NULL || strcmp(this->date,extract_list->date) < 0)     {	this->next = extract_list;	extract_list = this;	amfree(ditem_path);	return 0;    }    for (this1 = extract_list; this1->next != NULL; this1 = this1->next)    {	/* add in the middle */	if(strcmp(this->date,this1->next->date) < 0)	{	    this->next = this1->next;	    this1->next = this;	    amfree(ditem_path);	    return 0;	}    }    /* add at end */    this->next = NULL;    this1->next = this;    amfree(ditem_path);    return 0;}/* returns -1 if error *//* returns  0 on deletion *//* returns  1 if not there */static intdelete_extract_item(    DIR_ITEM *ditem){    EXTRACT_LIST *this;    EXTRACT_LIST_ITEM *that, *prev;    char *ditem_path = NULL;    ditem_path = stralloc(ditem->path);    clean_pathname(ditem_path);    for (this = extract_list; this != NULL; this = this->next)    {	/* see if this is the list for the tape */		if (this->level == ditem->level && strcmp(this->tape, ditem->tape) == 0)	{	    /* yes, so find file on list */	    that = this->files;	    if (strcmp(that->path, ditem_path) == 0)	    {		/* first on list */		this->files = that->next;                amfree(that->path);		amfree(that);		/* if list empty delete it */		if (this->files == NULL)		    delete_tape_list(this);		amfree(ditem_path);		return 0;	    }	    prev = that;	    that = that->next;	    while (that != NULL)	    {		if (strcmp(that->path, ditem_path) == 0)		{		    prev->next = that->next;                    amfree(that->path);		    amfree(that);		    amfree(ditem_path);		    return 0;		}		prev = that;		that = that->next;	    }	    amfree(ditem_path);	    return 1;	}    }    amfree(ditem_path);    return 1;}voidadd_glob(    char *	glob){    char *regex;    char *regex_path;    char *s;    char *uqglob = unquote_string(glob);     regex = glob_to_regex(uqglob);    dbprintf(_("add_glob (%s) -> %s\n"), uqglob, regex);    if ((s = validate_regexp(regex)) != NULL) {	g_printf(_("%s is not a valid shell wildcard pattern: "), glob);	puts(s);    } else {        /*         * glob_to_regex() anchors the beginning of the pattern with ^,         * but we will be tacking it onto the end of the current directory         * in add_file, so strip that off.  Also, it anchors the end with         * $, but we need to match an optional trailing /, so tack that on         * the end.         */        regex_path = stralloc(regex + 1);        regex_path[strlen(regex_path) - 1] = '\0';        strappend(regex_path, "[/]*$");        add_file(uqglob, regex_path);        amfree(regex_path);    }    amfree(regex);    amfree(uqglob);}voidadd_regex(    char *	regex){    char *s;    char *uqregex = unquote_string(regex);     if ((s = validate_regexp(uqregex)) != NULL) {	g_printf(_("\"%s\" is not a valid regular expression: "), regex);	puts(s);    } else {        add_file(uqregex, regex);    }    amfree(uqregex);}voidadd_file(    char *	path,    char *	regex){    DIR_ITEM *ditem, lditem;    char *path_on_disk = NULL;    char *cmd = NULL;    char *err = NULL;    int i;    ssize_t j;    char *dir, *dir_undo, dir_undo_ch = '\0';    char *ditem_path = NULL;    char *qditem_path = NULL;    char *l = NULL;    int  added;    char *s, *fp, *quoted;    int ch;    int found_one;    int dir_entries;    if (disk_path == NULL) {	g_printf(_("Must select directory before adding files\n"));	return;    }    memset(&lditem, 0, sizeof(lditem)); /* Prevent use of bogus data... */    dbprintf(_("add_file: Looking for \"%s\"\n"), regex);    if(strcmp(regex, "/[/]*$") == 0) {	/* "/" behave like "." */	regex = "\\.[/]*$";    }    else if(strcmp(regex, "[^/]*[/]*$") == 0) {		/* "*" */	regex = "([^/.]|\\.[^/]+|[^/.][^/]*)[/]*$";    } else {	/* remove "/" at end of path */	j = (ssize_t)(strlen(regex) - 1);	while(j >= 0 && regex[j] == '/')	    regex[j--] = '\0';    }    /* convert path (assumed in cwd) to one on disk */    if (strcmp(disk_path, "/") == 0) {        if (*regex == '/') {	    /* No mods needed if already starts with '/' */	    path_on_disk = stralloc(regex);	} else {	    /* Prepend '/' */	    path_on_disk = stralloc2("/", regex);	}    } else {	char *clean_disk_path = clean_regex(disk_path);	path_on_disk = vstralloc(clean_disk_path, "/", regex, NULL);	amfree(clean_disk_path);    }    dbprintf(_("add_file: Converted path=\"%s\" to path_on_disk=\"%s\"\n"),	      regex, path_on_disk);    found_one = 0;    dir_entries = 0;    for (ditem=get_dir_list(); ditem!=NULL; ditem=get_next_dir_item(ditem))    {	dir_entries++;	quoted = quote_string(ditem->path);	dbprintf(_("add_file: Pondering ditem->path=%s\n"), quoted);	amfree(quoted);	if (match(path_on_disk, ditem->path))	{	    found_one = 1;	    j = (ssize_t)strlen(ditem->path);	    if((j > 0 && ditem->path[j-1] == '/')	       || (j > 1 && ditem->path[j-2] == '/' && ditem->path[j-1] == '.'))	    {	/* It is a directory */		ditem_path = newstralloc(ditem_path, ditem->path);		clean_pathname(ditem_path);		qditem_path = quote_string(ditem_path);		cmd = newstralloc2(cmd, "ORLD ", qditem_path);		amfree(qditem_path);		if(send_command(cmd) == -1) {		    amfree(cmd);		    amfree(ditem_path);		    amfree(path_on_disk);		    exit(1);		}		amfree(cmd);		cmd = NULL;		/* skip preamble */		if ((i = get_reply_line()) == -1) {		    amfree(ditem_path);		    amfree(path_on_disk);		    exit(1);		}		if(i==0) {		/* assume something wrong */		    amfree(ditem_path);		    amfree(path_on_disk);		    l = reply_line();		    g_printf("%s\n", l);		    return;		}		dir_undo = NULL;		added=0;                lditem.path = newstralloc(lditem.path, ditem->path);		/* skip the last line -- duplicate of the preamble */		while ((i = get_reply_line()) != 0) {		    if (i == -1) {			amfree(ditem_path);		        amfree(path_on_disk);			exit(1);		    }		    if(err) {			if(cmd == NULL) {			    if(dir_undo) *dir_undo = dir_undo_ch;			    dir_undo = NULL;			    cmd = stralloc(l);	/* save for error report */			}			continue;	/* throw the rest of the lines away */		    }		    l=reply_line();		    if (!server_happy()) {			puts(l);			continue;		    }		    s = l;		    if(strncmp_const_skip(l, "201-", s, ch) != 0) {			err = _("bad reply: not 201-");			continue;		    }		    ch = *s++;		    skip_whitespace(s, ch);		    if(ch == '\0') {			err = _("bad reply: missing date field");			continue;		    }                    fp = s-1;                    skip_non_whitespace(s, ch);                    s[-1] = '\0';                    lditem.date = newstralloc(lditem.date, fp);                    s[-1] = (char)ch;		    skip_whitespace(s, ch);		    if(ch == '\0' || sscanf(s - 1, "%d", &lditem.level) != 1) {			err = _("bad reply: cannot parse level field");			continue;		    }		    skip_integer(s, ch);		    skip_whitespace(s, ch);		    if(ch == '\0') {			err = _("bad reply: missing tape field");			continue;		    }                    fp = s-1;                    skip_non_whitespace(s, ch);                    s[-1] = '\0';                    lditem.tape = newstralloc(lditem.tape, fp);                    s[-1] = (char)ch;		    if(am_has_feature(indexsrv_features, fe_amindexd_fileno_in_ORLD)) {			long long fileno_ = (long long)0;			skip_whitespace(s, ch);			if(ch == '\0' ||			   sscanf(s - 1, "%lld", &fileno_) != 1) {			    err = _("bad reply: cannot parse fileno field");			    continue;			}			lditem.fileno = (off_t)fileno_;			skip_integer(s, ch);		    }		    skip_whitespace(s, ch);		    if(ch == '\0') {			err = _("bad reply: missing directory field");			continue;		    }		    dir = s - 1;		    skip_quoted_string(s, ch);		    dir_undo = s - 1;		    dir_undo_ch = *dir_undo;		    *dir_undo = '\0';		    switch(add_extract_item(&lditem)) {		    case -1:			g_printf(_("System error\n"));			dbprintf(_("add_file: (Failed) System error\n"));			break;		    case  0:			quoted = quote_string(lditem.path);			g_printf(_("Added dir %s at date %s\n"),			       quoted, lditem.date);			dbprintf(_("add_file: (Successful) Added dir %s at date %s\n"),				  quoted, lditem.date);			amfree(quoted);			added=1;			break;		    case  1:			break;		    }		}		if(!server_happy()) {		    puts(reply_line());		} else if(err) {		    if (*err)			puts(err);		    if (cmd)			puts(cmd);		} else if(added == 0) {		    quoted = quote_string(ditem_path);		    g_printf(_("dir %s already added\n"), quoted);		    dbprintf(_("add_file: dir %s already added\n"), quoted);		    amfree(quoted);		}	    }	    else /* It is a file */	    {		switch(add_extract_item(ditem)) {		case -1:		    g_printf(_("System error\n"));		    dbprintf(_("add_file: (Failed) System error\n"));		    break;		case  0:		    quoted = quote_string(ditem->path);		    g_printf(_("Added file %s\n"), quoted);		    dbprintf(_("add_file: (Successful) Added %s\n"), quoted);		    amfree(quoted);		    break;		case  1:		    quoted = quote_string(ditem->path);		    g_printf(_("File %s already added\n"), quoted);		    dbprintf(_("add_file: file %s already added\n"), quoted);		    amfree(quoted);		}	    }	}    }    amfree(cmd);    amfree(ditem_path);    amfree(path_on_disk);    amfree(lditem.path);    amfree(lditem.date);    amfree(lditem.tape);    if(! found_one) {	quoted = quote_string(path);	g_printf(_("File %s doesn't exist in directory\n"), quoted);	dbprintf(_("add_file: (Failed) File %s doesn't exist in directory\n"),	          quoted);	amfree(quoted);    }}voiddelete_glob(    char *	glob){    char *regex;    char *regex_path;    char *s;    char *uqglob = unquote_string(glob);     regex = glob_to_regex(uqglob);    dbprintf(_("delete_glob (%s) -> %s\n"), uqglob, regex);    if ((s = validate_regexp(regex)) != NULL) {	g_printf(_("\"%s\" is not a valid shell wildcard pattern: "), glob);	puts(s);    } else {        /*         * glob_to_regex() anchors the beginning of the pattern with ^,         * but we will be tacking it onto the end of the current directory         * in add_file, so strip that off.  Also, it anchors the end with         * $, but we need to match an optional trailing /, so tack that on         * the end.         */        regex_path = stralloc(regex + 1);        regex_path[strlen(regex_path) - 1] = '\0';        strappend(regex_path, "[/]*$");        delete_file(uqglob, regex_path);        amfree(regex_path);

⌨️ 快捷键说明

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