📄 extract_list.c
字号:
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);}void add_file( char * path, char * regex){ DIR_ITEM *ditem, lditem; char *path_on_disk = NULL; char *path_on_disk_slash = 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 *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); } path_on_disk_slash = stralloc2(path_on_disk, "/"); 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) || match(path_on_disk_slash, 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); cmd = newstralloc2(cmd, "ORLD ", ditem_path); if(send_command(cmd) == -1) { amfree(cmd); amfree(ditem_path); amfree(path_on_disk); amfree(path_on_disk_slash); exit(1); } amfree(cmd); cmd = NULL; /* skip preamble */ if ((i = get_reply_line()) == -1) { amfree(ditem_path); amfree(path_on_disk); amfree(path_on_disk_slash); exit(1); } if(i==0) { /* assume something wrong */ amfree(ditem_path); amfree(path_on_disk); amfree(path_on_disk_slash); 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); amfree(path_on_disk_slash); 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); break; } } } } if (cmd != NULL) amfree(cmd); amfree(ditem_path); amfree(path_on_disk); amfree(path_on_disk_slash); 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); } amfree(regex); amfree(uqglob);}voiddelete_regex( char * regex){ char *s; char *uqregex = unquote_string(regex); if ((s = validate_regexp(regex)) != NULL) { g_printf(_("\"%s\" is not a valid regular expression: "), regex); puts(s); } else { delete_file(uqregex, uqregex); } amfree(uqregex);}voiddelete_file( char * path, char * regex){ DIR_ITEM *ditem, lditem; char *path_on_disk = NULL; char *path_on_disk_slash = NULL; char *cmd = NULL; char *err = NULL; int i; ssize_t j; char *date; char *tape, *tape_undo, tape_undo_ch = '\0'; char *dir_undo, dir_undo_ch = '\0'; int level = 0; off_t fileno; char *ditem_path = NULL; char *l = NULL; int deleted; char *s; int ch; int found_one; char *quoted; if (disk_path == NULL) { g_printf(_("Must select directory before deleting files\n")); return; } memset(&lditem, 0, sizeof(lditem)); /* Prevent use of bogus data... */ dbprintf(_("delete_file: Looking for \"%s\"\n"), path); if (strcmp(regex, "[^/]*[/]*$") == 0) { /* Looking for * find everything but single . */ 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 == '/') { if (strcmp(regex, "/[/]*$") == 0) { /* We want "/" to match the directory itself: "/." */ path_on_disk = stralloc("/\\.[/]*$"); } else { /* 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); } path_on_disk_slash = stralloc2(path_on_disk, "/"); dbprintf(_("delete_file: Converted path=\"%s\" to path_on_disk=\"%s\"\n"), regex, path_on_disk); found_one = 0; for (ditem=get_dir_list(); ditem!=NULL; ditem=get_next_dir_item(ditem)) { quoted = quote_string(ditem->path); dbprintf(_("delete_file: Pondering ditem->path=%s\n"), quoted); amfree(quoted); if (match(path_on_disk, ditem->path) || match(path_on_disk_slash, 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); cmd = newstralloc2(cmd, "ORLD ", ditem_path); if(send_command(cmd) == -1) { amfree(cmd); amfree(ditem_path); amfree(path_on_disk); amfree(path_on_disk_slash); exit(1); } amfree(cmd); /* skip preamble */ if ((i = get_reply_line()) == -1) { amfree(ditem_path); amfree(path_on_disk); amfree(path_on_disk_slash); exit(1); } if(i==0) /* assume something wrong */ { amfree(ditem_path); amfree(path_on_disk); amfree(path_on_disk_slash); l = reply_line(); g_printf("%s\n", l); return; } deleted=0; lditem.path = newstralloc(lditem.path, ditem->path); amfree(cmd); tape_undo = dir_undo = NULL; /* skip the last line -- duplicate of the preamble */ while ((i = get_reply_line()) != 0) { if (i == -1) { amfree(ditem_path); amfree(path_on_disk); amfree(path_on_disk_slash); exit(1); } if(err) { if(cmd == NULL) { if(tape_undo) *tape_undo = tape_undo_ch; if(dir_undo) *dir_undo = dir_undo_ch; tape_undo = dir_undo = NULL; cmd = stralloc(l); /* save for the 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; } date = s - 1; skip_non_whitespace(s, ch); *(s - 1) = '\0'; skip_whitespace(s, ch); if(ch == '\0' || sscanf(s - 1, "%d", &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; } tape = s - 1; skip_non_whitespace(s, ch); tape_undo = s - 1; tape_undo_ch = *tape_undo; *tape_undo = '\0'; 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; } fileno = (off_t)fileno_; skip_integer(s, ch); } skip_whitespace(s, ch); if(ch == '\0') { err = _("bad reply: missing directory field"); continue; } skip_non_whitespace(s, ch); dir_undo = s - 1; dir_undo_ch = *dir_undo; *dir_undo = '\0'; lditem.date = newstralloc(lditem.date, date); lditem.level=level; lditem.tape = newstralloc(lditem.tape, tape); switch(delete_extract_item(&lditem)) { case -1: g_printf(_("System error\n")); dbprintf(_("delete_file: (Failed) System error\n")); break; case 0: g_printf(_("Deleted dir %s at date %s\n"), ditem_path, date); dbprintf(_("delete_file: (Successful) Deleted dir %s at date %s\n"), ditem_path, date); deleted=1; break; case 1: break; } } if(!server_happy()) { puts(reply_line()); } else if(err) { if (*err) puts(err); if (cmd) puts(cmd); } else if(deleted == 0) { g_printf(_("Warning - dir '%s' not on tape list\n"), ditem_path); dbprintf(_("delete_file: dir '%s' not on tape list\n"), ditem_path); } } else { switch(delete_extract_item(ditem)) { case -1: g_printf(_("System error\n")); dbprintf(_("delete_file: (Failed) System error\n")); break; case 0: g_printf(_("Deleted %s\n"), ditem->path); dbprintf(_("delete_file: (Successful) Deleted %s\n"), ditem->path); break; case 1: g_printf(_("Warning - file '%s' not on tape list\n"), ditem->path); dbprintf(_("delete_file: file '%s' not on tape list\n"),
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -