2 * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3 * Copyright (c) 1991-1998, 2000 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.
29 * implements the "extract" command in amrecover
34 #include "amrecover.h"
35 #include "fileheader.h"
46 #include "client_util.h"
49 typedef struct EXTRACT_LIST_ITEM {
51 struct EXTRACT_LIST_ITEM *next;
55 typedef struct EXTRACT_LIST {
56 char *date; /* date tape created */
57 int level; /* level of dump */
58 char *tape; /* tape label */
59 off_t fileno; /* fileno on tape */
60 EXTRACT_LIST_ITEM *files; /* files to get off tape */
62 struct EXTRACT_LIST *next;
66 typedef struct ctl_data_s {
72 data_path_t data_path;
74 backup_support_option_t *bsu;
83 security_stream_t *fd;
84 } amidxtaped_streams[] = {
90 #define NSTREAMS (int)(sizeof(amidxtaped_streams) / sizeof(amidxtaped_streams[0]))
93 static void amidxtaped_response(void *, pkt_t *, security_handle_t *);
94 static void stop_amidxtaped(void);
95 static char *dump_device_name = NULL;
97 static char *amidxtaped_line = NULL;
98 extern char *localhost;
99 static char header_buf[32768];
100 static int header_size = 0;
103 /* global pid storage for interrupt handler */
104 pid_t extract_restore_child_pid = -1;
106 static EXTRACT_LIST *extract_list = NULL;
107 static const security_driver_t *amidxtaped_secdrv;
109 unsigned short samba_extract_method = SAMBA_TAR;
111 #define READ_TIMEOUT 240*60
113 EXTRACT_LIST *first_tape_list(void);
114 EXTRACT_LIST *next_tape_list(EXTRACT_LIST *list);
115 static int is_empty_dir(char *fname);
116 int is_extract_list_nonempty(void);
117 int length_of_tape_list(EXTRACT_LIST *tape_list);
118 void add_file(char *path, char *regex);
119 void add_glob(char *glob);
120 void add_regex(char *regex);
121 void clear_extract_list(void);
122 void clean_tape_list(EXTRACT_LIST *tape_list);
123 void clean_extract_list(void);
124 void check_file_overwrite(char *filename);
125 void delete_file(char *path, char *regex);
126 void delete_glob(char *glob);
127 void delete_regex(char *regex);
128 void delete_tape_list(EXTRACT_LIST *tape_list);
129 void display_extract_list(char *file);
130 void extract_files(void);
131 void read_file_header(char *buffer,
135 static int add_extract_item(DIR_ITEM *ditem);
136 static int delete_extract_item(DIR_ITEM *ditem);
137 static int extract_files_setup(char *label, off_t fsf);
138 static int okay_to_continue(int allow_tape,
141 static ssize_t read_buffer(int datafd,
145 static void clear_tape_list(EXTRACT_LIST *tape_list);
146 static void extract_files_child(ctl_data_t *ctl_data);
147 static void send_to_tape_server(security_stream_t *stream, char *cmd);
148 int writer_intermediary(EXTRACT_LIST *elist);
149 int get_amidxtaped_line(void);
150 static void read_amidxtaped_data(void *, void *, ssize_t);
151 static char *merge_path(char *path1, char *path2);
152 static gboolean ask_file_overwrite(ctl_data_t *ctl_data);
153 static void start_processing_data(ctl_data_t *ctl_data);
156 * Function: ssize_t read_buffer(datafd, buffer, buflen, timeout_s)
159 * read data from input file desciptor waiting up to timeout_s
160 * seconds before returning data.
163 * datafd - File descriptor to read from.
164 * buffer - Buffer to read into.
165 * buflen - Maximum number of bytes to read into buffer.
166 * timeout_s - Seconds to wait before returning what was already read.
169 * >0 - Number of data bytes in buffer.
171 * -1 - errno == ETIMEDOUT if no data available in specified time.
172 * errno == ENFILE if datafd is invalid.
173 * otherwise errno is set by select or read..
184 SELECT_ARG_TYPE readset;
185 struct timeval timeout;
190 if(datafd < 0 || datafd >= (int)FD_SETSIZE) {
191 errno = EMFILE; /* out of range */
196 spaceleft = (ssize_t)buflen;
200 FD_SET(datafd, &readset);
201 timeout.tv_sec = timeout_s;
203 nfound = select(datafd+1, &readset, NULL, NULL, &timeout);
205 /* Select returned an error. */
206 g_fprintf(stderr,_("select error: %s\n"), strerror(errno));
212 /* Select timed out. */
213 if (timeout_s != 0) {
214 /* Not polling: a real read timeout */
215 g_fprintf(stderr,_("timeout waiting for restore\n"));
216 g_fprintf(stderr,_("increase READ_TIMEOUT in recover-src/extract_list.c if your tape is slow\n"));
223 if(!FD_ISSET(datafd, &readset))
226 /* Select says data is available, so read it. */
227 size = read(datafd, dataptr, (size_t)spaceleft);
229 if ((errno == EINTR) || (errno == EAGAIN)) {
232 if (errno != EPIPE) {
233 g_fprintf(stderr, _("read_buffer: read error - %s"),
241 } while ((size > 0) && (spaceleft > 0));
243 return ((((ssize_t)buflen-spaceleft) > 0) ? ((ssize_t)buflen-spaceleft) : size);
248 first_tape_list(void)
255 /*@keep@*/EXTRACT_LIST *list)
264 EXTRACT_LIST * tape_list)
266 EXTRACT_LIST_ITEM *this, *next;
269 this = tape_list->files;
277 tape_list->files = NULL;
281 /* remove a tape list from the extract list, clearing the tape list
282 beforehand if necessary */
285 EXTRACT_LIST *tape_list)
287 EXTRACT_LIST *this, *prev;
289 if (tape_list == NULL)
292 /* is it first on the list? */
293 if (tape_list == extract_list)
295 extract_list = tape_list->next;
296 clear_tape_list(tape_list);
297 amfree(tape_list->date);
298 amfree(tape_list->tape);
303 /* so not first on list - find it and delete */
305 this = extract_list->next;
308 if (this == tape_list)
310 prev->next = tape_list->next;
311 clear_tape_list(tape_list);
312 amfree(tape_list->date);
313 amfree(tape_list->tape);
324 /* return the number of files on a tape's list */
327 EXTRACT_LIST *tape_list)
329 EXTRACT_LIST_ITEM *fn;
333 for (fn = tape_list->files; fn != NULL; fn = fn->next)
341 clear_extract_list(void)
343 while (extract_list != NULL)
344 delete_tape_list(extract_list);
350 EXTRACT_LIST *tape_list)
352 EXTRACT_LIST_ITEM *fn1, *pfn1, *ofn1;
353 EXTRACT_LIST_ITEM *fn2, *pfn2, *ofn2;
358 fn1 = tape_list->files;
359 while (fn1 != NULL) {
364 while (fn2 != NULL && remove_fn1 == 0) {
366 if(strcmp(fn1->path, fn2->path) == 0) {
368 } else if (strncmp(fn1->path, fn2->path, strlen(fn1->path)) == 0 &&
369 ((strlen(fn2->path) > strlen(fn1->path) &&
370 fn2->path[strlen(fn1->path)] == '/') ||
371 (fn1->path[strlen(fn1->path)-1] == '/'))) {
373 } else if (strncmp(fn2->path, fn1->path, strlen(fn2->path)) == 0 &&
374 ((strlen(fn1->path) > strlen(fn2->path) &&
375 fn1->path[strlen(fn2->path)] == '/') ||
376 (fn2->path[strlen(fn2->path)-1] == '/'))) {
382 dbprintf(_("removing path %s, it is included in %s\n"),
383 fn2->path, fn1->path);
389 } else if (remove_fn1 == 0) {
395 if(remove_fn1 != 0) {
396 /* fn2->path is always valid */
397 /*@i@*/ dbprintf(_("removing path %s, it is included in %s\n"),
398 /*@i@*/ fn1->path, fn2->path);
403 amfree(tape_list->files);
404 tape_list->files = fn1;
422 char *npath = g_path_get_basename(path);
423 *dir = g_path_get_dirname(path);
424 if (strcmp(*dir, ".") == 0) {
431 clean_extract_list(void)
435 for (this = extract_list; this != NULL; this = this->next)
436 clean_tape_list(this);
440 int add_to_unlink_list(char *path);
441 int do_unlink_list(void);
442 void free_unlink_list(void);
444 typedef struct s_unlink_list {
446 struct s_unlink_list *next;
448 t_unlink_list *unlink_list = NULL;
457 unlink_list = alloc(SIZEOF(*unlink_list));
458 unlink_list->path = stralloc(path);
459 unlink_list->next = NULL;
461 for (ul = unlink_list; ul != NULL; ul = ul->next) {
462 if (strcmp(ul->path, path) == 0)
465 ul = alloc(SIZEOF(*ul));
466 ul->path = stralloc(path);
467 ul->next = unlink_list;
479 for (ul = unlink_list; ul != NULL; ul = ul->next) {
480 if (unlink(ul->path) < 0) {
481 g_fprintf(stderr,_("Can't unlink %s: %s\n"), ul->path, strerror(errno));
490 free_unlink_list(void)
492 t_unlink_list *ul, *ul1;
494 for (ul = unlink_list; ul != NULL; ul = ul1) {
506 check_file_overwrite(
510 EXTRACT_LIST_ITEM *fn;
511 struct stat stat_buf;
515 for (this = extract_list; this != NULL; this = this->next) {
516 for (fn = this->files; fn != NULL ; fn = fn->next) {
518 /* Check path component of fn->path */
520 path = stralloc2(dir, fn->path);
521 if (path[strlen(path)-1] == '/') {
522 path[strlen(path)-1] = '\0';
525 s = path + strlen(dir) + 1;
526 while((s = strchr(s, '/'))) {
528 if (lstat(path, &stat_buf) == 0) {
529 if(!S_ISDIR(stat_buf.st_mode)) {
530 if (add_to_unlink_list(path)) {
531 g_printf(_("WARNING: %s is not a directory, "
532 "it will be deleted.\n"),
537 else if (errno != ENOENT) {
538 g_printf(_("Can't stat %s: %s\n"), path, strerror(errno));
547 filename = stralloc2(dir, fn->path);
548 if (filename[strlen(filename)-1] == '/') {
549 filename[strlen(filename)-1] = '\0';
552 if (lstat(filename, &stat_buf) == 0) {
553 if(S_ISDIR(stat_buf.st_mode)) {
554 if(!is_empty_dir(filename)) {
555 g_printf(_("WARNING: All existing files in %s "
556 "will be deleted.\n"), filename);
558 } else if(S_ISREG(stat_buf.st_mode)) {
559 g_printf(_("WARNING: Existing file %s will be overwritten\n"),
562 if (add_to_unlink_list(filename)) {
563 g_printf(_("WARNING: Existing entry %s will be deleted\n"),
567 } else if (errno != ENOENT) {
568 g_printf(_("Can't stat %s: %s\n"), filename, strerror(errno));
576 /* returns -1 if error */
577 /* returns 0 on succes */
578 /* returns 1 if already added */
583 EXTRACT_LIST *this, *this1;
584 EXTRACT_LIST_ITEM *that, *curr;
585 char *ditem_path = NULL;
587 ditem_path = stralloc(ditem->path);
588 clean_pathname(ditem_path);
590 for (this = extract_list; this != NULL; this = this->next)
592 /* see if this is the list for the tape */
593 if (this->level == ditem->level && strcmp(this->tape, ditem->tape) == 0)
595 /* yes, so add to list */
599 if (strcmp(curr->path,ditem_path) == 0) {
605 that = (EXTRACT_LIST_ITEM *)alloc(sizeof(EXTRACT_LIST_ITEM));
606 that->path = stralloc(ditem_path);
607 that->next = this->files;
608 this->files = that; /* add at front since easiest */
614 /* so this is the first time we have seen this tape */
615 this = (EXTRACT_LIST *)alloc(sizeof(EXTRACT_LIST));
616 this->tape = stralloc(ditem->tape);
617 this->level = ditem->level;
618 this->fileno = ditem->fileno;
619 this->date = stralloc(ditem->date);
620 that = (EXTRACT_LIST_ITEM *)alloc(sizeof(EXTRACT_LIST_ITEM));
621 that->path = stralloc(ditem_path);
625 /* add this in date increasing order */
626 /* because restore must be done in this order */
627 /* add at begining */
628 if(extract_list==NULL || strcmp(this->date,extract_list->date) < 0)
630 this->next = extract_list;
635 for (this1 = extract_list; this1->next != NULL; this1 = this1->next)
637 /* add in the middle */
638 if(strcmp(this->date,this1->next->date) < 0)
640 this->next = this1->next;
654 /* returns -1 if error */
655 /* returns 0 on deletion */
656 /* returns 1 if not there */
662 EXTRACT_LIST_ITEM *that, *prev;
663 char *ditem_path = NULL;
665 ditem_path = stralloc(ditem->path);
666 clean_pathname(ditem_path);
668 for (this = extract_list; this != NULL; this = this->next)
670 /* see if this is the list for the tape */
671 if (this->level == ditem->level && strcmp(this->tape, ditem->tape) == 0)
673 /* yes, so find file on list */
675 if (strcmp(that->path, ditem_path) == 0)
678 this->files = that->next;
681 /* if list empty delete it */
682 if (this->files == NULL)
683 delete_tape_list(this);
691 if (strcmp(that->path, ditem_path) == 0)
693 prev->next = that->next;
717 int len = strlen(path1);
718 if (path1[len-1] == '/' && path2[0] == '/') {
719 result = stralloc2(path1, path2+1);
720 } else if (path1[len-1] != '/' && path2[0] != '/') {
721 result = vstralloc(path1, "/", path2, NULL);
723 result = stralloc2(path1, path2);
740 if (disk_path == NULL) {
741 g_printf(_("Must select directory before adding files\n"));
745 uqglob = unquote_string(glob);
746 glob = file_of_path(uqglob, &dir);
748 sdir = merge_path(mount_point, disk_path);
749 result = cd_glob(dir, 0);
753 regex = glob_to_regex(glob);
754 dbprintf(_("add_glob (%s) -> %s\n"), uqglob, regex);
755 if ((s = validate_regexp(regex)) != NULL) {
756 g_printf(_("%s is not a valid shell wildcard pattern: "), glob);
760 * glob_to_regex() anchors the beginning of the pattern with ^,
761 * but we will be tacking it onto the end of the current directory
762 * in add_file, so strip that off. Also, it anchors the end with
763 * $, but we need to match an optional trailing /, so tack that on
766 regex_path = stralloc(regex + 1);
767 regex_path[strlen(regex_path) - 1] = '\0';
768 strappend(regex_path, "[/]*$");
769 add_file(uqglob, regex_path);
773 set_directory(sdir, 0);
793 if (disk_path == NULL) {
794 g_printf(_("Must select directory before adding files\n"));
798 uqregex = unquote_string(regex);
799 newregex = file_of_path(uqregex, &dir);
801 sdir = merge_path(mount_point, disk_path);
802 result = cd_regex(dir, 0);
807 if ((s = validate_regexp(newregex)) != NULL) {
808 g_printf(_("\"%s\" is not a valid regular expression: "), newregex);
811 add_file(uqregex, newregex);
814 set_directory(sdir, 0);
827 DIR_ITEM *ditem, lditem;
828 char *path_on_disk = NULL;
833 char *dir, *dir_undo, dir_undo_ch = '\0';
834 char *ditem_path = NULL;
835 char *qditem_path = NULL;
838 char *s, *fp, *quoted;
843 if (disk_path == NULL) {
844 g_printf(_("Must select directory before adding files\n"));
847 memset(&lditem, 0, sizeof(lditem)); /* Prevent use of bogus data... */
849 dbprintf(_("add_file: Looking for \"%s\"\n"), regex);
851 if(strcmp(regex, "/[/]*$") == 0) { /* "/" behave like "." */
854 else if(strcmp(regex, "[^/]*[/]*$") == 0) { /* "*" */
855 regex = "([^/.]|\\.[^/]+|[^/.][^/]*)[/]*$";
857 /* remove "/" at end of path */
858 j = (ssize_t)(strlen(regex) - 1);
859 while(j >= 0 && regex[j] == '/')
863 /* convert path (assumed in cwd) to one on disk */
864 if (strcmp(disk_path, "/") == 0) {
866 /* No mods needed if already starts with '/' */
867 path_on_disk = stralloc(regex);
870 path_on_disk = stralloc2("/", regex);
873 char *clean_disk_path = clean_regex(disk_path, 0);
874 path_on_disk = vstralloc(clean_disk_path, "/", regex, NULL);
875 amfree(clean_disk_path);
878 dbprintf(_("add_file: Converted path=\"%s\" to path_on_disk=\"%s\"\n"),
879 regex, path_on_disk);
883 for (ditem=get_dir_list(); ditem!=NULL; ditem=get_next_dir_item(ditem))
886 quoted = quote_string(ditem->path);
887 dbprintf(_("add_file: Pondering ditem->path=%s\n"), quoted);
889 if (match(path_on_disk, ditem->path))
892 j = (ssize_t)strlen(ditem->path);
893 if((j > 0 && ditem->path[j-1] == '/')
894 || (j > 1 && ditem->path[j-2] == '/' && ditem->path[j-1] == '.'))
895 { /* It is a directory */
896 ditem_path = newstralloc(ditem_path, ditem->path);
897 clean_pathname(ditem_path);
899 qditem_path = quote_string(ditem_path);
900 cmd = newstralloc2(cmd, "ORLD ", qditem_path);
902 if(send_command(cmd) == -1) {
905 amfree(path_on_disk);
911 if ((i = get_reply_line()) == -1) {
913 amfree(path_on_disk);
916 if(i==0) { /* assume something wrong */
918 amfree(path_on_disk);
925 lditem.path = newstralloc(lditem.path, ditem->path);
926 /* skip the last line -- duplicate of the preamble */
928 while ((i = get_reply_line()) != 0) {
931 amfree(path_on_disk);
936 if(dir_undo) *dir_undo = dir_undo_ch;
938 cmd = stralloc(l); /* save for error report */
940 continue; /* throw the rest of the lines away */
943 if (!server_happy()) {
949 if(strncmp_const_skip(l, "201-", s, ch) != 0) {
950 err = _("bad reply: not 201-");
955 skip_whitespace(s, ch);
957 err = _("bad reply: missing date field");
961 skip_non_whitespace(s, ch);
963 lditem.date = newstralloc(lditem.date, fp);
966 skip_whitespace(s, ch);
967 if(ch == '\0' || sscanf(s - 1, "%d", &lditem.level) != 1) {
968 err = _("bad reply: cannot parse level field");
973 skip_whitespace(s, ch);
975 err = _("bad reply: missing tape field");
979 skip_quoted_string(s, ch);
982 lditem.tape = unquote_string(fp);
985 if(am_has_feature(indexsrv_features, fe_amindexd_fileno_in_ORLD)) {
986 long long fileno_ = (long long)0;
987 skip_whitespace(s, ch);
989 sscanf(s - 1, "%lld", &fileno_) != 1) {
990 err = _("bad reply: cannot parse fileno field");
993 lditem.fileno = (off_t)fileno_;
997 skip_whitespace(s, ch);
999 err = _("bad reply: missing directory field");
1003 skip_quoted_string(s, ch);
1005 dir_undo_ch = *dir_undo;
1008 switch(add_extract_item(&lditem)) {
1010 g_printf(_("System error\n"));
1011 dbprintf(_("add_file: (Failed) System error\n"));
1015 quoted = quote_string(lditem.path);
1016 g_printf(_("Added dir %s at date %s\n"),
1017 quoted, lditem.date);
1018 dbprintf(_("add_file: (Successful) Added dir %s at date %s\n"),
1019 quoted, lditem.date);
1028 if(!server_happy()) {
1035 } else if(added == 0) {
1036 quoted = quote_string(ditem_path);
1037 g_printf(_("dir %s already added\n"), quoted);
1038 dbprintf(_("add_file: dir %s already added\n"), quoted);
1042 else /* It is a file */
1044 switch(add_extract_item(ditem)) {
1046 g_printf(_("System error\n"));
1047 dbprintf(_("add_file: (Failed) System error\n"));
1051 quoted = quote_string(ditem->path);
1052 g_printf(_("Added file %s\n"), quoted);
1053 dbprintf(_("add_file: (Successful) Added %s\n"), quoted);
1058 quoted = quote_string(ditem->path);
1059 g_printf(_("File %s already added\n"), quoted);
1060 dbprintf(_("add_file: file %s already added\n"), quoted);
1069 amfree(path_on_disk);
1071 amfree(lditem.path);
1072 amfree(lditem.date);
1073 amfree(lditem.tape);
1076 quoted = quote_string(path);
1077 g_printf(_("File %s doesn't exist in directory\n"), quoted);
1078 dbprintf(_("add_file: (Failed) File %s doesn't exist in directory\n"),
1098 if (disk_path == NULL) {
1099 g_printf(_("Must select directory before adding files\n"));
1103 uqglob = unquote_string(glob);
1104 newglob = file_of_path(uqglob, &dir);
1106 sdir = merge_path(mount_point, disk_path);
1107 result = cd_glob(dir, 0);
1111 regex = glob_to_regex(newglob);
1112 dbprintf(_("delete_glob (%s) -> %s\n"), newglob, regex);
1113 if ((s = validate_regexp(regex)) != NULL) {
1114 g_printf(_("\"%s\" is not a valid shell wildcard pattern: "),
1119 * glob_to_regex() anchors the beginning of the pattern with ^,
1120 * but we will be tacking it onto the end of the current directory
1121 * in add_file, so strip that off. Also, it anchors the end with
1122 * $, but we need to match an optional trailing /, so tack that on
1125 regex_path = stralloc(regex + 1);
1126 regex_path[strlen(regex_path) - 1] = '\0';
1127 strappend(regex_path, "[/]*$");
1128 delete_file(uqglob, regex_path);
1132 set_directory(sdir, 0);
1152 if (disk_path == NULL) {
1153 g_printf(_("Must select directory before adding files\n"));
1157 uqregex = unquote_string(regex);
1158 newregex = file_of_path(uqregex, &dir);
1160 sdir = merge_path(mount_point, disk_path);
1161 result = cd_regex(dir, 0);
1166 if ((s = validate_regexp(newregex)) != NULL) {
1167 g_printf(_("\"%s\" is not a valid regular expression: "), newregex);
1170 delete_file(newregex, regex);
1173 set_directory(sdir, 0);
1186 DIR_ITEM *ditem, lditem;
1187 char *path_on_disk = NULL;
1193 char *tape, *tape_undo, tape_undo_ch = '\0';
1194 char *dir_undo, dir_undo_ch = '\0';
1197 char *ditem_path = NULL;
1206 if (disk_path == NULL) {
1207 g_printf(_("Must select directory before deleting files\n"));
1210 memset(&lditem, 0, sizeof(lditem)); /* Prevent use of bogus data... */
1212 dbprintf(_("delete_file: Looking for \"%s\"\n"), path);
1214 if (strcmp(regex, "[^/]*[/]*$") == 0) {
1215 /* Looking for * find everything but single . */
1216 regex = "([^/.]|\\.[^/]+|[^/.][^/]*)[/]*$";
1218 /* remove "/" at end of path */
1219 j = (ssize_t)(strlen(regex) - 1);
1220 while(j >= 0 && regex[j] == '/') regex[j--] = '\0';
1223 /* convert path (assumed in cwd) to one on disk */
1224 if (strcmp(disk_path, "/") == 0) {
1225 if (*regex == '/') {
1226 if (strcmp(regex, "/[/]*$") == 0) {
1227 /* We want "/" to match the directory itself: "/." */
1228 path_on_disk = stralloc("/\\.[/]*$");
1230 /* No mods needed if already starts with '/' */
1231 path_on_disk = stralloc(regex);
1235 path_on_disk = stralloc2("/", regex);
1238 char *clean_disk_path = clean_regex(disk_path, 0);
1239 path_on_disk = vstralloc(clean_disk_path, "/", regex, NULL);
1240 amfree(clean_disk_path);
1243 dbprintf(_("delete_file: Converted path=\"%s\" to path_on_disk=\"%s\"\n"),
1244 regex, path_on_disk);
1246 for (ditem=get_dir_list(); ditem!=NULL; ditem=get_next_dir_item(ditem))
1248 quoted = quote_string(ditem->path);
1249 dbprintf(_("delete_file: Pondering ditem->path=%s\n"), quoted);
1251 if (match(path_on_disk, ditem->path))
1254 j = (ssize_t)strlen(ditem->path);
1255 if((j > 0 && ditem->path[j-1] == '/')
1256 || (j > 1 && ditem->path[j-2] == '/' && ditem->path[j-1] == '.'))
1257 { /* It is a directory */
1258 ditem_path = newstralloc(ditem_path, ditem->path);
1259 clean_pathname(ditem_path);
1261 qditem_path = quote_string(ditem_path);
1262 cmd = newstralloc2(cmd, "ORLD ", qditem_path);
1263 amfree(qditem_path);
1264 if(send_command(cmd) == -1) {
1267 amfree(path_on_disk);
1272 if ((i = get_reply_line()) == -1) {
1274 amfree(path_on_disk);
1277 if(i==0) /* assume something wrong */
1280 amfree(path_on_disk);
1282 g_printf("%s\n", l);
1286 lditem.path = newstralloc(lditem.path, ditem->path);
1288 tape_undo = dir_undo = NULL;
1289 /* skip the last line -- duplicate of the preamble */
1290 while ((i = get_reply_line()) != 0)
1294 amfree(path_on_disk);
1299 if(tape_undo) *tape_undo = tape_undo_ch;
1300 if(dir_undo) *dir_undo = dir_undo_ch;
1301 tape_undo = dir_undo = NULL;
1302 cmd = stralloc(l); /* save for the error report */
1304 continue; /* throw the rest of the lines away */
1307 if (!server_happy()) {
1313 if(strncmp_const_skip(l, "201-", s, ch) != 0) {
1314 err = _("bad reply: not 201-");
1319 skip_whitespace(s, ch);
1321 err = _("bad reply: missing date field");
1325 skip_non_whitespace(s, ch);
1328 skip_whitespace(s, ch);
1329 if(ch == '\0' || sscanf(s - 1, "%d", &level) != 1) {
1330 err = _("bad reply: cannot parse level field");
1333 skip_integer(s, ch);
1335 skip_whitespace(s, ch);
1337 err = _("bad reply: missing tape field");
1341 skip_non_whitespace(s, ch);
1343 tape_undo_ch = *tape_undo;
1346 if(am_has_feature(indexsrv_features, fe_amindexd_fileno_in_ORLD)) {
1347 long long fileno_ = (long long)0;
1348 skip_whitespace(s, ch);
1350 sscanf(s - 1, "%lld", &fileno_) != 1) {
1351 err = _("bad reply: cannot parse fileno field");
1354 fileno = (off_t)fileno_;
1355 skip_integer(s, ch);
1358 skip_whitespace(s, ch);
1360 err = _("bad reply: missing directory field");
1363 skip_non_whitespace(s, ch);
1365 dir_undo_ch = *dir_undo;
1368 lditem.date = newstralloc(lditem.date, date);
1370 lditem.tape = newstralloc(lditem.tape, tape);
1371 switch(delete_extract_item(&lditem)) {
1373 g_printf(_("System error\n"));
1374 dbprintf(_("delete_file: (Failed) System error\n"));
1377 g_printf(_("Deleted dir %s at date %s\n"), ditem_path, date);
1378 dbprintf(_("delete_file: (Successful) Deleted dir %s at date %s\n"),
1386 if(!server_happy()) {
1393 } else if(deleted == 0) {
1394 g_printf(_("Warning - dir '%s' not on tape list\n"),
1396 dbprintf(_("delete_file: dir '%s' not on tape list\n"),
1402 switch(delete_extract_item(ditem)) {
1404 g_printf(_("System error\n"));
1405 dbprintf(_("delete_file: (Failed) System error\n"));
1408 g_printf(_("Deleted %s\n"), ditem->path);
1409 dbprintf(_("delete_file: (Successful) Deleted %s\n"),
1413 g_printf(_("Warning - file '%s' not on tape list\n"),
1415 dbprintf(_("delete_file: file '%s' not on tape list\n"),
1424 amfree(path_on_disk);
1427 g_printf(_("File %s doesn't exist in directory\n"), path);
1428 dbprintf(_("delete_file: (Failed) File %s doesn't exist in directory\n"),
1434 /* print extract list into file. If NULL ptr passed print to screen */
1436 display_extract_list(
1440 EXTRACT_LIST_ITEM *that;
1443 char *pager_command;
1448 if ((pager = getenv("PAGER")) == NULL)
1453 * Set up the pager command so if the pager is terminated, we do
1454 * not get a SIGPIPE back.
1456 pager_command = stralloc2(pager, " ; /bin/cat > /dev/null");
1457 if ((fp = popen(pager_command, "w")) == NULL)
1459 g_printf(_("Warning - can't pipe through %s\n"), pager);
1462 amfree(pager_command);
1466 uqfile = unquote_string(file);
1467 if ((fp = fopen(uqfile, "w")) == NULL)
1469 g_printf(_("Can't open file %s to print extract list into\n"), file);
1476 for (this = extract_list; this != NULL; this = this->next)
1478 g_fprintf(fp, _("TAPE %s LEVEL %d DATE %s\n"),
1479 this->tape, this->level, this->date);
1480 for (that = this->files; that != NULL; that = that->next)
1481 g_fprintf(fp, "\t%s\n", that->path);
1487 g_printf(_("Extract list written to file %s\n"), file);
1498 struct dirent *entry;
1501 if((dir = opendir(fname)) == NULL)
1505 while(!gotentry && (entry = readdir(dir)) != NULL) {
1506 gotentry = !is_dot_or_dotdot(entry->d_name);
1514 /* returns 0 if extract list empty and 1 if it isn't */
1516 is_extract_list_nonempty(void)
1518 return (extract_list != NULL);
1522 /* prints continue prompt and waits for response,
1523 returns 0 if don't, non-0 if do */
1540 prompt = _("New device name [?]: ");
1541 } else if (allow_tape && allow_skip) {
1542 prompt = _("Continue [?/Y/n/s/d]? ");
1543 } else if (allow_tape && !allow_skip) {
1544 prompt = _("Continue [?/Y/n/d]? ");
1545 } else if (allow_retry) {
1546 prompt = _("Continue [?/Y/n/r]? ");
1548 prompt = _("Continue [?/Y/n]? ");
1550 fputs(prompt, stdout);
1551 fflush(stdout); fflush(stderr);
1553 if ((line = agets(stdin)) == NULL) {
1563 dbprintf("User prompt: '%s'; response: '%s'\n", prompt, line);
1566 while ((ch = *s++) != '\0' && g_ascii_isspace(ch)) {
1567 (void)ch; /* Quiet empty loop compiler warning */
1571 g_printf(_("Enter a new device name or \"default\"\n"));
1573 g_printf(_("Enter \"y\"es to continue, \"n\"o to stop"));
1575 g_printf(_(", \"s\"kip this tape"));
1578 g_printf(_(" or \"r\"etry this tape"));
1581 g_printf(_(" or \"d\" to change to a new device"));
1585 } else if (get_device) {
1586 char *tmp = stralloc(tape_server_name);
1588 if (strncmp_const(s - 1, "default") == 0) {
1589 set_device(tmp, NULL); /* default device, existing host */
1590 } else if (s[-1] != '\0') {
1591 set_device(tmp, s - 1); /* specified device, existing host */
1593 g_printf(_("No change.\n"));
1599 } else if (ch == '\0' || ch == 'Y' || ch == 'y') {
1601 } else if (allow_tape && (ch == 'D' || ch == 'd' || ch == 'T' || ch == 't')) {
1602 get_device = 1; /* ('T' and 't' are for backward-compatibility) */
1603 } else if (ch == 'N' || ch == 'n') {
1605 } else if (allow_retry && (ch == 'R' || ch == 'r')) {
1607 } else if (allow_skip && (ch == 'S' || ch == 's')) {
1618 send_to_tape_server(
1619 security_stream_t * stream,
1622 char *msg = stralloc2(cmd, "\r\n");
1624 g_debug("send_to_tape_server: %s\n", cmd);
1625 if (security_stream_write(stream, msg, strlen(msg)) < 0)
1627 error(_("Error writing to tape server"));
1635 /* start up connection to tape server and set commands to initiate
1636 transfer of dump image.
1637 Return tape server socket on success, -1 on error. */
1639 extract_files_setup(
1643 char *disk_regex = NULL;
1644 char *host_regex = NULL;
1645 char *clean_datestamp, *ch, *ch1;
1650 amidxtaped_secdrv = security_getdriver(authopt);
1651 if (amidxtaped_secdrv == NULL) {
1652 error(_("no '%s' security driver available for host '%s'"),
1653 authopt, tape_server_name);
1656 /* We assume that amidxtaped support fe_amidxtaped_options_features */
1657 /* and fe_amidxtaped_options_auth */
1658 /* We should send a noop to really know */
1659 req = vstralloc("SERVICE amidxtaped\n",
1660 "OPTIONS ", "features=", our_features_string, ";",
1661 "auth=", authopt, ";",
1663 protocol_sendreq(tape_server_name, amidxtaped_secdrv,
1664 generic_client_get_security_conf, req, STARTUP_TIMEOUT,
1665 amidxtaped_response, &response_error);
1668 if(response_error != 0) {
1672 disk_regex = make_exact_disk_expression(disk_name);
1673 host_regex = make_exact_host_expression(dump_hostname);
1675 clean_datestamp = stralloc(dump_datestamp);
1676 for(ch=ch1=clean_datestamp;*ch1 != '\0';ch1++) {
1683 /* push our feature list off to the tape server */
1684 /* XXX assumes that index server and tape server are equivalent, ew */
1686 if(am_has_feature(indexsrv_features, fe_amidxtaped_exchange_features)){
1687 tt = newstralloc2(tt, "FEATURES=", our_features_string);
1688 send_to_tape_server(amidxtaped_streams[CTLFD].fd, tt);
1689 get_amidxtaped_line();
1690 if(strncmp_const(amidxtaped_line,"FEATURES=") == 0) {
1691 tapesrv_features = am_string_to_feature(amidxtaped_line+9);
1693 g_fprintf(stderr, _("amrecover - expecting FEATURES line from amidxtaped\n"));
1697 amfree(clean_datestamp);
1701 *tapesrv_features = *indexsrv_features;
1705 if(am_has_feature(indexsrv_features, fe_amidxtaped_header) &&
1706 am_has_feature(indexsrv_features, fe_amidxtaped_device) &&
1707 am_has_feature(indexsrv_features, fe_amidxtaped_host) &&
1708 am_has_feature(indexsrv_features, fe_amidxtaped_disk) &&
1709 am_has_feature(indexsrv_features, fe_amidxtaped_datestamp)) {
1711 if(am_has_feature(indexsrv_features, fe_amidxtaped_config)) {
1712 tt = newstralloc2(tt, "CONFIG=", get_config_name());
1713 send_to_tape_server(amidxtaped_streams[CTLFD].fd, tt);
1715 if(am_has_feature(indexsrv_features, fe_amidxtaped_label) &&
1716 label && label[0] != '/') {
1717 tt = newstralloc2(tt,"LABEL=",label);
1718 send_to_tape_server(amidxtaped_streams[CTLFD].fd, tt);
1720 if(am_has_feature(indexsrv_features, fe_amidxtaped_fsf)) {
1722 g_snprintf(v_fsf, 99, "%lld", (long long)fsf);
1723 tt = newstralloc2(tt, "FSF=",v_fsf);
1724 send_to_tape_server(amidxtaped_streams[CTLFD].fd, tt);
1726 send_to_tape_server(amidxtaped_streams[CTLFD].fd, "HEADER");
1727 tt = newstralloc2(tt, "DEVICE=", dump_device_name);
1728 send_to_tape_server(amidxtaped_streams[CTLFD].fd, tt);
1729 tt = newstralloc2(tt, "HOST=", host_regex);
1730 send_to_tape_server(amidxtaped_streams[CTLFD].fd, tt);
1731 tt = newstralloc2(tt, "DISK=", disk_regex);
1732 send_to_tape_server(amidxtaped_streams[CTLFD].fd, tt);
1733 tt = newstralloc2(tt, "DATESTAMP=", clean_datestamp);
1734 send_to_tape_server(amidxtaped_streams[CTLFD].fd, tt);
1735 send_to_tape_server(amidxtaped_streams[CTLFD].fd, "END");
1738 else if(am_has_feature(indexsrv_features, fe_amidxtaped_nargs)) {
1739 /* send to the tape server what tape file we want */
1748 send_to_tape_server(amidxtaped_streams[CTLFD].fd, "6");
1749 send_to_tape_server(amidxtaped_streams[CTLFD].fd, "-h");
1750 send_to_tape_server(amidxtaped_streams[CTLFD].fd, "-p");
1751 send_to_tape_server(amidxtaped_streams[CTLFD].fd, dump_device_name);
1752 send_to_tape_server(amidxtaped_streams[CTLFD].fd, host_regex);
1753 send_to_tape_server(amidxtaped_streams[CTLFD].fd, disk_regex);
1754 send_to_tape_server(amidxtaped_streams[CTLFD].fd, clean_datestamp);
1756 dbprintf(_("Started amidxtaped with arguments \"6 -h -p %s %s %s %s\"\n"),
1757 dump_device_name, host_regex, disk_regex, clean_datestamp);
1762 amfree(clean_datestamp);
1769 * Reads the first block of a tape file.
1780 bytes_read = read_buffer(tapedev, buffer, buflen, READ_TIMEOUT);
1781 if(bytes_read < 0) {
1782 error(_("error reading header (%s), check amidxtaped.*.debug on server"),
1787 if((size_t)bytes_read < buflen) {
1788 g_fprintf(stderr, plural(_("%s: short block %d byte\n"),
1789 _("%s: short block %d bytes\n"), bytes_read),
1790 get_pname(), (int)bytes_read);
1791 print_header(stdout, file);
1792 error(_("Can't read file header"));
1796 /* bytes_read == buflen */
1797 parse_file_header(buffer, file, (size_t)bytes_read);
1811 extract_files_child(
1812 ctl_data_t *ctl_data)
1817 GPtrArray *argv_ptr = g_ptr_array_new();
1819 EXTRACT_LIST_ITEM *fn;
1820 enum dumptypes dumptype = IS_UNKNOWN;
1823 guint passwd_field = 999999999;
1825 char *domain = NULL, *smbpass = NULL;
1828 /* code executed by child to do extraction */
1831 /* make in_fd be our stdin */
1832 if (dup2(ctl_data->child_pipe[0], STDIN_FILENO) == -1)
1834 error(_("dup2 failed in extract_files_child: %s"), strerror(errno));
1838 if(ctl_data->file.type != F_DUMPFILE) {
1839 dump_dumpfile_t(&ctl_data->file);
1840 error(_("bad header"));
1844 if (ctl_data->file.program != NULL) {
1845 if (strcmp(ctl_data->file.program, "APPLICATION") == 0)
1846 dumptype = IS_APPLICATION_API;
1848 if (strcmp(ctl_data->file.program, GNUTAR) == 0)
1849 dumptype = IS_GNUTAR;
1852 if (dumptype == IS_UNKNOWN) {
1853 len_program = strlen(ctl_data->file.program);
1854 if(len_program >= 3 &&
1855 strcmp(&ctl_data->file.program[len_program-3],"tar") == 0)
1860 if (dumptype == IS_UNKNOWN && strcmp(ctl_data->file.program, SAMBA_CLIENT) ==0) {
1861 if (samba_extract_method == SAMBA_TAR)
1862 dumptype = IS_SAMBA_TAR;
1864 dumptype = IS_SAMBA;
1869 /* form the arguments to restore */
1870 files_off_tape = length_of_tape_list(ctl_data->elist);
1874 g_ptr_array_add(argv_ptr, stralloc("smbclient"));
1875 smbpass = findpass(ctl_data->file.disk, &domain);
1877 g_ptr_array_add(argv_ptr, stralloc(ctl_data->file.disk));
1878 g_ptr_array_add(argv_ptr, stralloc("-U"));
1879 passwd_field = argv_ptr->len;
1880 g_ptr_array_add(argv_ptr, stralloc(smbpass));
1882 g_ptr_array_add(argv_ptr, stralloc("-W"));
1883 g_ptr_array_add(argv_ptr, stralloc(domain));
1886 g_ptr_array_add(argv_ptr, stralloc("-d0"));
1887 g_ptr_array_add(argv_ptr, stralloc("-Tx"));
1888 g_ptr_array_add(argv_ptr, stralloc("-")); /* data on stdin */
1893 g_ptr_array_add(argv_ptr, stralloc("tar"));
1894 /* ignore trailing zero blocks on input (this was the default until tar-1.21) */
1895 g_ptr_array_add(argv_ptr, stralloc("--ignore-zeros"));
1896 g_ptr_array_add(argv_ptr, stralloc("--numeric-owner"));
1897 g_ptr_array_add(argv_ptr, stralloc("-xpGvf"));
1898 g_ptr_array_add(argv_ptr, stralloc("-")); /* data on stdin */
1901 g_ptr_array_add(argv_ptr, stralloc("tar"));
1902 g_ptr_array_add(argv_ptr, stralloc("-xpvf"));
1903 g_ptr_array_add(argv_ptr, stralloc("-")); /* data on stdin */
1907 g_ptr_array_add(argv_ptr, stralloc("restore"));
1909 restore_args[j++] = stralloc("-xB");
1910 g_ptr_array_add(argv_ptr, stralloc("-xB"));
1912 #if defined(XFSDUMP)
1913 if (strcmp(ctl_data->file.program, XFSDUMP) == 0) {
1914 g_ptr_array_add(argv_ptr, stralloc("-v"));
1915 g_ptr_array_add(argv_ptr, stralloc("silent"));
1919 if (strcmp(ctl_data->file.program, VDUMP) == 0) {
1920 g_ptr_array_add(argv_ptr, stralloc("xf"));
1921 g_ptr_array_add(argv_ptr, stralloc("-")); /* data on stdin */
1925 g_ptr_array_add(argv_ptr, stralloc("xbf"));
1926 g_ptr_array_add(argv_ptr, stralloc("2")); /* read in units of 1K */
1927 g_ptr_array_add(argv_ptr, stralloc("-")); /* data on stdin */
1931 case IS_APPLICATION_API:
1932 g_ptr_array_add(argv_ptr, stralloc(ctl_data->file.application));
1933 g_ptr_array_add(argv_ptr, stralloc("restore"));
1934 g_ptr_array_add(argv_ptr, stralloc("--config"));
1935 g_ptr_array_add(argv_ptr, stralloc(get_config_name()));
1936 g_ptr_array_add(argv_ptr, stralloc("--disk"));
1937 g_ptr_array_add(argv_ptr, stralloc(ctl_data->file.disk));
1938 if (dump_dle && dump_dle->device) {
1939 g_ptr_array_add(argv_ptr, stralloc("--device"));
1940 g_ptr_array_add(argv_ptr, stralloc(dump_dle->device));
1942 if (ctl_data->data_path == DATA_PATH_DIRECTTCP) {
1943 g_ptr_array_add(argv_ptr, stralloc("--data-path"));
1944 g_ptr_array_add(argv_ptr, stralloc("DIRECTTCP"));
1945 g_ptr_array_add(argv_ptr, stralloc("--direct-tcp"));
1946 g_ptr_array_add(argv_ptr, stralloc(ctl_data->addrs));
1948 if (ctl_data->bsu && ctl_data->bsu->smb_recover_mode &&
1949 samba_extract_method == SAMBA_SMBCLIENT){
1950 g_ptr_array_add(argv_ptr, stralloc("--recover-mode"));
1951 g_ptr_array_add(argv_ptr, stralloc("smb"));
1953 g_ptr_array_add(argv_ptr, stralloc("--level"));
1954 g_ptr_array_add(argv_ptr, g_strdup_printf("%d", ctl_data->elist->level));
1959 merge_properties(dump_dle->application_property, proplist);
1960 application_property_add_to_argv(argv_ptr, dump_dle, NULL,
1962 for (scriptlist = dump_dle->scriptlist; scriptlist != NULL;
1963 scriptlist = scriptlist->next) {
1964 script = (script_t *)scriptlist->data;
1965 if (script->result && script->result->proplist) {
1966 property_add_to_argv(argv_ptr, script->result->proplist);
1970 } else if (proplist) {
1971 property_add_to_argv(argv_ptr, proplist);
1976 for (i = 0, fn = ctl_data->elist->files; i < files_off_tape;
1980 case IS_APPLICATION_API:
1985 if (strcmp(fn->path, "/") == 0)
1986 g_ptr_array_add(argv_ptr, stralloc("."));
1988 g_ptr_array_add(argv_ptr, stralloc2(".", fn->path));
1992 #if defined(XFSDUMP)
1993 if (strcmp(ctl_data->file.program, XFSDUMP) == 0) {
1995 * xfsrestore needs a -s option before each file to be
1996 * restored, and also wants them to be relative paths.
1998 g_ptr_array_add(argv_ptr, stralloc("-s"));
1999 g_ptr_array_add(argv_ptr, stralloc(fn->path + 1));
2003 g_ptr_array_add(argv_ptr, stralloc(fn->path));
2008 #if defined(XFSDUMP)
2009 if (strcmp(ctl_data->file.program, XFSDUMP) == 0) {
2010 g_ptr_array_add(argv_ptr, stralloc("-"));
2011 g_ptr_array_add(argv_ptr, stralloc("."));
2014 g_ptr_array_add(argv_ptr, NULL);
2019 cmd = stralloc(SAMBA_CLIENT);
2022 /* fall through to ... */
2028 g_fprintf(stderr, _("warning: GNUTAR program not available.\n"));
2029 cmd = stralloc("tar");
2031 cmd = stralloc(GNUTAR);
2038 if (strcmp(ctl_data->file.program, DUMP) == 0) {
2039 cmd = stralloc(RESTORE);
2043 if (strcmp(ctl_data->file.program, VDUMP) == 0) {
2044 cmd = stralloc(VRESTORE);
2048 if (strcmp(ctl_data->file.program, VXDUMP) == 0) {
2049 cmd = stralloc(VXRESTORE);
2052 #if defined(XFSDUMP)
2053 if (strcmp(ctl_data->file.program, XFSDUMP) == 0) {
2054 cmd = stralloc(XFSRESTORE);
2058 g_fprintf(stderr, _("warning: restore program for %s not available.\n"),
2059 ctl_data->file.program);
2060 cmd = stralloc("restore");
2063 case IS_APPLICATION_API:
2064 cmd = vstralloc(APPLICATION_DIR, "/", ctl_data->file.application, NULL);
2068 dbprintf(_("Exec'ing %s with arguments:\n"), cmd);
2069 for (j = 0; j < argv_ptr->len - 1; j++) {
2070 if (j == passwd_field)
2071 dbprintf("\tXXXXX\n");
2073 dbprintf(_("\t%s\n"), (char *)g_ptr_array_index(argv_ptr, j));
2076 (void)execv(cmd, (char **)argv_ptr->pdata);
2077 /* only get here if exec failed */
2079 g_ptr_array_free_full(argv_ptr);
2081 perror(_("amrecover couldn't exec"));
2082 g_fprintf(stderr, _(" problem executing %s\n"), cmd);
2090 * Interpose something between the process writing out the dump (writing it to
2091 * some extraction program, really) and the socket from which we're reading, so
2092 * that we can do things like prompt for human interaction for multiple tapes.
2095 writer_intermediary(
2096 EXTRACT_LIST * elist)
2098 ctl_data_t ctl_data;
2099 amwait_t extractor_status;
2101 ctl_data.header_done = 0;
2102 ctl_data.child_pipe[0] = -1;
2103 ctl_data.child_pipe[1] = -1;
2105 ctl_data.elist = elist;
2106 fh_init(&ctl_data.file);
2107 ctl_data.data_path = DATA_PATH_AMANDA;
2108 ctl_data.addrs = NULL;
2109 ctl_data.bsu = NULL;
2110 ctl_data.bytes_read = 0;
2113 security_stream_read(amidxtaped_streams[DATAFD].fd,
2114 read_amidxtaped_data, &ctl_data);
2116 while(get_amidxtaped_line() >= 0) {
2117 char desired_tape[MAX_TAPE_LABEL_BUF];
2118 g_debug("get amidxtaped line: %s", amidxtaped_line);
2120 /* if prompted for a tape, relay said prompt to the user */
2121 if(sscanf(amidxtaped_line, "FEEDME %132s\n", desired_tape) == 1) {
2123 g_printf(_("Load tape %s now\n"), desired_tape);
2124 dbprintf(_("Requesting tape %s from user\n"), desired_tape);
2125 done = okay_to_continue(am_has_feature(indexsrv_features,
2126 fe_amrecover_feedme_tape),
2129 if (am_has_feature(indexsrv_features,
2130 fe_amrecover_feedme_tape)) {
2131 char *reply = stralloc2("TAPE ", tape_device_name);
2132 send_to_tape_server(amidxtaped_streams[CTLFD].fd, reply);
2135 send_to_tape_server(amidxtaped_streams[CTLFD].fd, "OK");
2138 send_to_tape_server(amidxtaped_streams[CTLFD].fd, "ERROR");
2141 } else if (strncmp_const(amidxtaped_line, "USE-DATAPATH ") == 0) {
2142 if (strncmp_const(amidxtaped_line+13, "AMANDA") == 0) {
2143 ctl_data.data_path = DATA_PATH_AMANDA;
2144 g_debug("Using AMANDA data-path");
2145 } else if (strncmp_const(amidxtaped_line+13, "DIRECT-TCP") == 0) {
2146 ctl_data.data_path = DATA_PATH_DIRECTTCP;
2147 ctl_data.addrs = stralloc(amidxtaped_line+24);
2148 g_debug("Using DIRECT-TCP data-path with %s", ctl_data.addrs);
2150 start_processing_data(&ctl_data);
2151 } else if(strncmp_const(amidxtaped_line, "MESSAGE ") == 0) {
2152 g_printf("%s\n",&amidxtaped_line[8]);
2154 g_fprintf(stderr, _("Strange message from tape server: %s"),
2160 /* CTL might be close before DATA */
2162 dumpfile_free_data(&ctl_data.file);
2163 amfree(ctl_data.addrs);
2164 amfree(ctl_data.bsu);
2165 if (ctl_data.child_pipe[1] != -1)
2166 aclose(ctl_data.child_pipe[1]);
2168 if (ctl_data.header_done == 0) {
2169 g_printf(_("Got no header and data from server, check in amidxtaped.*.debug and amandad.*.debug files on server\n"));
2172 if (ctl_data.pid != -1) {
2173 waitpid(ctl_data.pid, &extractor_status, 0);
2174 if(WEXITSTATUS(extractor_status) != 0){
2175 int ret = WEXITSTATUS(extractor_status);
2176 if(ret == 255) ret = -1;
2177 g_printf(_("Extractor child exited with status %d\n"), ret);
2181 g_debug("bytes read: %jd", (intmax_t)ctl_data.bytes_read);
2185 /* exec restore to do the actual restoration */
2187 /* does the actual extraction of files */
2189 * The original design had the dump image being returned exactly as it
2190 * appears on the tape, and this routine getting from the index server
2191 * whether or not it is compressed, on the assumption that the tape
2192 * server may not know how to uncompress it. But
2193 * - Amrestore can't do that. It returns either compressed or uncompressed
2194 * (always). Amrestore assumes it can uncompress files. It is thus a good
2195 * idea to run the tape server on a machine with gzip.
2196 * - The information about compression in the disklist is really only
2197 * for future dumps. It is possible to change compression on a drive
2198 * so the information in the disklist may not necessarily relate to
2199 * the dump image on the tape.
2200 * Consequently the design was changed to assuming that amrestore can
2201 * uncompress any dump image and have it return an uncompressed file
2207 EXTRACT_LIST *elist;
2211 tapelist_t *tlist = NULL, *a_tlist;
2212 g_option_t g_options;
2213 levellist_t all_level = NULL;
2216 if (!is_extract_list_nonempty())
2218 g_printf(_("Extract list empty - No files to extract!\n"));
2222 clean_extract_list();
2224 /* get tape device name from index server if none specified */
2225 if (tape_server_name == NULL) {
2226 tape_server_name = newstralloc(tape_server_name, server_name);
2228 if (tape_device_name == NULL) {
2229 if (send_command("TAPE") == -1)
2231 if (get_reply_line() == -1)
2234 if (!server_happy())
2236 g_printf("%s\n", l);
2239 /* skip reply number */
2240 tape_device_name = newstralloc(tape_device_name, l+4);
2243 if (strcmp(tape_device_name, "/dev/null") == 0)
2245 g_printf(_("amrecover: warning: using %s as the tape device will not work\n"),
2250 for (elist = first_tape_list(); elist != NULL; elist = next_tape_list(elist)) {
2251 if(elist->tape[0]!='/') {
2253 g_printf(_("\nExtracting files using tape drive %s on host %s.\n"),
2254 tape_device_name, tape_server_name);
2255 g_printf(_("The following tapes are needed:"));
2260 tlist = unmarshal_tapelist_str(elist->tape);
2261 for(a_tlist = tlist ; a_tlist != NULL; a_tlist = a_tlist->next)
2262 g_printf(" %s", a_tlist->label);
2264 free_tapelist(tlist);
2268 for (elist = first_tape_list(); elist != NULL; elist = next_tape_list(elist)) {
2269 if(elist->tape[0]=='/') {
2271 g_printf(_("\nExtracting files from holding disk on host %s.\n"),
2273 g_printf(_("The following files are needed:"));
2278 tlist = unmarshal_tapelist_str(elist->tape);
2279 for(a_tlist = tlist; a_tlist != NULL; a_tlist = a_tlist->next)
2280 g_printf(" %s", a_tlist->label);
2282 free_tapelist(tlist);
2287 g_options.config = get_config_name();
2288 g_options.hostname = dump_hostname;
2289 for (elist = first_tape_list(); elist != NULL;
2290 elist = next_tape_list(elist)) {
2291 level_t *level = g_new0(level_t, 1);
2292 level->level = elist->level;
2293 all_level = g_slist_append(all_level, level);
2296 slist_free_full(dump_dle->levellist, g_free);
2297 dump_dle->levellist = all_level;
2298 run_client_scripts(EXECUTE_ON_PRE_RECOVER, &g_options, dump_dle,
2300 dump_dle->levellist = NULL;
2303 while ((elist = first_tape_list()) != NULL)
2305 if(elist->tape[0]=='/') {
2306 dump_device_name = newstralloc(dump_device_name, elist->tape);
2307 g_printf(_("Extracting from file "));
2308 tlist = unmarshal_tapelist_str(dump_device_name);
2309 for(a_tlist = tlist; a_tlist != NULL; a_tlist = a_tlist->next)
2310 g_printf(" %s", a_tlist->label);
2312 free_tapelist(tlist);
2315 g_printf(_("Extracting files using tape drive %s on host %s.\n"),
2316 tape_device_name, tape_server_name);
2317 tlist = unmarshal_tapelist_str(elist->tape);
2318 g_printf(_("Load tape %s now\n"), tlist->label);
2319 dbprintf(_("Requesting tape %s from user\n"), tlist->label);
2320 free_tapelist(tlist);
2321 otc = okay_to_continue(1,1,0);
2324 else if (otc == SKIP_TAPE) {
2325 delete_tape_list(elist); /* skip this tape */
2328 dump_device_name = newstralloc(dump_device_name, tape_device_name);
2330 dump_datestamp = newstralloc(dump_datestamp, elist->date);
2332 if (last_level != -1 && dump_dle) {
2335 level = g_new0(level_t, 1);
2336 level->level = last_level;
2337 dump_dle->levellist = g_slist_append(dump_dle->levellist, level);
2339 level = g_new0(level_t, 1);
2340 level->level = elist->level;
2341 dump_dle->levellist = g_slist_append(dump_dle->levellist, level);
2342 run_client_scripts(EXECUTE_ON_INTER_LEVEL_RECOVER, &g_options,
2344 slist_free_full(dump_dle->levellist, g_free);
2345 dump_dle->levellist = NULL;
2348 /* connect to the tape handler daemon on the tape drive server */
2349 if ((extract_files_setup(elist->tape, elist->fileno)) == -1)
2351 g_fprintf(stderr, _("amrecover - can't talk to tape server: %s\n"),
2358 level = g_new0(level_t, 1);
2359 level->level = elist->level;
2360 dump_dle->levellist = g_slist_append(dump_dle->levellist, level);
2361 run_client_scripts(EXECUTE_ON_PRE_LEVEL_RECOVER, &g_options,
2364 last_level = elist->level;
2366 /* if the server have fe_amrecover_feedme_tape, it has asked for
2367 * the tape itself, even if the restore didn't succeed, we should
2370 if(writer_intermediary(elist) == 0 ||
2371 am_has_feature(indexsrv_features, fe_amrecover_feedme_tape))
2372 delete_tape_list(elist); /* tape done so delete from list */
2374 am_release_feature_set(tapesrv_features);
2378 run_client_scripts(EXECUTE_ON_POST_LEVEL_RECOVER, &g_options,
2380 slist_free_full(dump_dle->levellist, g_free);
2381 dump_dle->levellist = NULL;
2385 dump_dle->levellist = all_level;
2386 run_client_scripts(EXECUTE_ON_POST_RECOVER, &g_options, dump_dle,
2388 slist_free_full(dump_dle->levellist, g_free);
2390 dump_dle->levellist = NULL;
2395 amidxtaped_response(
2398 security_handle_t * sech)
2400 int ports[NSTREAMS], *response_error = datap, i;
2405 assert(response_error != NULL);
2406 assert(sech != NULL);
2407 memset(ports, -1, SIZEOF(ports));
2410 errstr = newvstrallocf(errstr, _("[request failed: %s]"), security_geterror(sech));
2411 *response_error = 1;
2414 security_close_connection(sech, dump_hostname);
2416 if (pkt->type == P_NAK) {
2417 #if defined(PACKET_DEBUG)
2418 g_fprintf(stderr, _("got nak response:\n----\n%s\n----\n\n"), pkt->body);
2421 tok = strtok(pkt->body, " ");
2422 if (tok == NULL || strcmp(tok, "ERROR") != 0)
2425 tok = strtok(NULL, "\n");
2427 errstr = newvstralloc(errstr, "NAK: ", tok, NULL);
2428 *response_error = 1;
2431 errstr = newstralloc(errstr, "request NAK");
2432 *response_error = 2;
2437 if (pkt->type != P_REP) {
2438 errstr = newvstrallocf(errstr, _("received strange packet type %s: %s"),
2439 pkt_type2str(pkt->type), pkt->body);
2440 *response_error = 1;
2444 #if defined(PACKET_DEBUG)
2445 g_fprintf(stderr, _("got response:\n----\n%s\n----\n\n"), pkt->body);
2448 for(i = 0; i < NSTREAMS; i++) {
2450 amidxtaped_streams[i].fd = NULL;
2454 while((tok = strtok(p, " \n")) != NULL) {
2458 * Error response packets have "ERROR" followed by the error message
2459 * followed by a newline.
2461 if (strcmp(tok, "ERROR") == 0) {
2462 tok = strtok(NULL, "\n");
2464 tok = _("[bogus error packet]");
2465 errstr = newstralloc(errstr, tok);
2466 *response_error = 2;
2472 * Regular packets have CONNECT followed by three streams
2474 if (strcmp(tok, "CONNECT") == 0) {
2477 * Parse the three stream specifiers out of the packet.
2479 for (i = 0; i < NSTREAMS; i++) {
2480 tok = strtok(NULL, " ");
2481 if (tok == NULL || strcmp(tok, amidxtaped_streams[i].name) != 0) {
2482 extra = vstrallocf(_("CONNECT token is \"%s\": expected \"%s\""),
2483 tok ? tok : "(null)",
2484 amidxtaped_streams[i].name);
2487 tok = strtok(NULL, " \n");
2488 if (tok == NULL || sscanf(tok, "%d", &ports[i]) != 1) {
2489 extra = vstrallocf(_("CONNECT %s token is \"%s\": expected a port number"),
2490 amidxtaped_streams[i].name,
2491 tok ? tok : "(null)");
2499 * OPTIONS [options string] '\n'
2501 if (strcmp(tok, "OPTIONS") == 0) {
2502 tok = strtok(NULL, "\n");
2504 extra = stralloc(_("OPTIONS token is missing"));
2508 while((p = strchr(tok, ';')) != NULL) {
2510 if(strncmp_const(tok, "features=") == 0) {
2511 tok += sizeof("features=") - 1;
2512 am_release_feature_set(their_features);
2513 if((their_features = am_string_to_feature(tok)) == NULL) {
2514 errstr = newvstralloc(errstr,
2515 _("OPTIONS: bad features value: "),
2527 extra = vstrallocf("next token is \"%s\": expected \"CONNECT\", \"ERROR\" or \"OPTIONS\""),
2528 tok ? tok : _("(null)"));
2534 * Connect the streams to their remote ports
2536 for (i = 0; i < NSTREAMS; i++) {
2539 amidxtaped_streams[i].fd = security_stream_client(sech, ports[i]);
2540 dbprintf(_("amidxtaped_streams[%d].fd = %p\n"),i, amidxtaped_streams[i].fd);
2541 if (amidxtaped_streams[i].fd == NULL) {
2542 errstr = newvstrallocf(errstr,\
2543 _("[could not connect %s stream: %s]"),
2544 amidxtaped_streams[i].name,
2545 security_geterror(sech));
2550 * Authenticate the streams
2552 for (i = 0; i < NSTREAMS; i++) {
2553 if (amidxtaped_streams[i].fd == NULL)
2555 if (security_stream_auth(amidxtaped_streams[i].fd) < 0) {
2556 errstr = newvstrallocf(errstr,
2557 _("[could not authenticate %s stream: %s]"),
2558 amidxtaped_streams[i].name,
2559 security_stream_geterror(amidxtaped_streams[i].fd));
2565 * The CTLFD and DATAFD streams are mandatory. If we didn't get
2568 if (amidxtaped_streams[CTLFD].fd == NULL) {
2569 errstr = newvstrallocf(errstr, _("[couldn't open CTL streams]"));
2572 if (amidxtaped_streams[DATAFD].fd == NULL) {
2573 errstr = newvstrallocf(errstr, _("[couldn't open DATA streams]"));
2577 /* everything worked */
2578 *response_error = 0;
2583 errstr = newvstrallocf(errstr,
2584 _("[parse of reply message failed: %s]"), extra);
2586 errstr = newvstrallocf(errstr,
2587 _("[parse of reply message failed: (no additional information)"));
2590 *response_error = 2;
2595 *response_error = 1;
2599 * This is called when everything needs to shut down so event_loop()
2603 stop_amidxtaped(void)
2607 for (i = 0; i < NSTREAMS; i++) {
2608 if (amidxtaped_streams[i].fd != NULL) {
2609 security_stream_close(amidxtaped_streams[i].fd);
2610 amidxtaped_streams[i].fd = NULL;
2615 static char* ctl_buffer = NULL;
2616 /* gets a "line" from server and put in server_line */
2617 /* server_line is terminated with \0, \r\n is striped */
2618 /* returns -1 if error */
2621 get_amidxtaped_line(void)
2627 amfree(amidxtaped_line);
2629 ctl_buffer = stralloc("");
2631 while (!strstr(ctl_buffer,"\r\n")) {
2632 if (amidxtaped_streams[CTLFD].fd == NULL)
2635 size = security_stream_read_sync(amidxtaped_streams[CTLFD].fd, &buf);
2639 else if(size == 0) {
2642 newbuf = alloc(strlen(ctl_buffer)+size+1);
2643 strncpy(newbuf, ctl_buffer, (size_t)(strlen(ctl_buffer) + size + 1));
2644 memcpy(newbuf+strlen(ctl_buffer), buf, (size_t)size);
2645 newbuf[strlen(ctl_buffer)+size] = '\0';
2647 ctl_buffer = newbuf;
2651 s = strstr(ctl_buffer,"\r\n");
2653 newbuf = stralloc(s+2);
2654 amidxtaped_line = stralloc(ctl_buffer);
2656 ctl_buffer = newbuf;
2662 read_amidxtaped_data(
2667 ctl_data_t *ctl_data = (ctl_data_t *)cookie;
2668 assert(cookie != NULL);
2671 errstr = newstralloc2(errstr, _("amidxtaped read: "),
2672 security_stream_geterror(amidxtaped_streams[DATAFD].fd));
2677 * EOF. Stop and return.
2680 security_stream_close(amidxtaped_streams[DATAFD].fd);
2681 amidxtaped_streams[DATAFD].fd = NULL;
2683 * If the mesg fd has also shut down, then we're done.
2688 assert(buf != NULL);
2690 if (ctl_data->header_done == 0) {
2691 GPtrArray *errarray;
2692 g_option_t g_options;
2693 data_path_t data_path_set = DATA_PATH_AMANDA;
2696 to_move = MIN(32768-header_size, size);
2697 memcpy(header_buf+header_size, buf, to_move);
2698 header_size += to_move;
2700 g_debug("read header %zd => %d", size, header_size);
2701 if (header_size < 32768) {
2702 security_stream_read(amidxtaped_streams[DATAFD].fd,
2703 read_amidxtaped_data, cookie);
2705 } else if (header_size > 32768) {
2706 error("header_size is %d\n", header_size);
2708 assert (to_move == size);
2709 /* parse the file header */
2710 fh_init(&ctl_data->file);
2711 parse_file_header(header_buf, &ctl_data->file, (size_t)header_size);
2713 /* call backup_support_option */
2714 g_options.config = get_config_name();
2715 g_options.hostname = dump_hostname;
2716 if (strcmp(ctl_data->file.program, "APPLICATION") == 0) {
2718 ctl_data->bsu = backup_support_option(ctl_data->file.application,
2720 ctl_data->file.disk,
2724 ctl_data->bsu = backup_support_option(ctl_data->file.application,
2726 ctl_data->file.disk, NULL,
2729 if (!ctl_data->bsu) {
2731 for (i=0; i < errarray->len; i++) {
2733 line = g_ptr_array_index(errarray, i);
2734 g_fprintf(stderr, "%s\n", line);
2736 g_ptr_array_free_full(errarray);
2739 data_path_set = ctl_data->bsu->data_path_set;
2741 /* handle backup_support_option failure */
2743 ctl_data->header_done = 1;
2744 if (!ask_file_overwrite(ctl_data)) {
2745 if (am_has_feature(tapesrv_features, fe_amidxtaped_abort)) {
2746 send_to_tape_server(amidxtaped_streams[CTLFD].fd, "ABORT");
2752 if (am_has_feature(tapesrv_features, fe_amidxtaped_datapath)) {
2754 /* send DATA-PATH request */
2755 msg = stralloc("AVAIL-DATAPATH");
2756 if (data_path_set & DATA_PATH_AMANDA)
2757 vstrextend(&msg, " AMANDA", NULL);
2758 if (data_path_set & DATA_PATH_DIRECTTCP)
2759 vstrextend(&msg, " DIRECT-TCP", NULL);
2760 send_to_tape_server(amidxtaped_streams[CTLFD].fd, msg);
2763 start_processing_data(ctl_data);
2766 ctl_data->bytes_read += size;
2767 /* Only the data is sent to the child */
2769 * We ignore errors while writing to the index file.
2771 (void)full_write(ctl_data->child_pipe[1], buf, (size_t)size);
2772 security_stream_read(amidxtaped_streams[DATAFD].fd,
2773 read_amidxtaped_data, cookie);
2779 ctl_data_t *ctl_data)
2781 char *restore_dir = NULL;
2783 if (ctl_data->file.dumplevel == 0) {
2784 property_t *property = g_hash_table_lookup(proplist, "directory");
2785 if (property && property->values && property->values->data) {
2786 /* take first property value */
2787 restore_dir = strdup(property->values->data);
2789 if (samba_extract_method == SAMBA_SMBCLIENT ||
2791 ctl_data->bsu->recover_path == RECOVER_PATH_REMOTE)) {
2793 restore_dir = g_strdup(ctl_data->file.disk);
2795 g_printf(_("Restoring files into target host %s\n"), restore_dir);
2798 restore_dir = g_get_current_dir();
2800 g_printf(_("Restoring files into directory %s\n"), restore_dir);
2803 /* Collect files to delete befause of a bug in gnutar */
2804 if (strcmp(ctl_data->file.program, "GNUTAR") == 0 ||
2805 (strcmp(ctl_data->file.program, "APPLICATION") == 0 &&
2806 strcmp(ctl_data->file.application, "amgtar") == 0)) {
2807 check_file_overwrite(restore_dir);
2809 g_printf(_("All existing files in %s can be deleted\n"),
2813 if (!okay_to_continue(0,0,0)) {
2815 amfree(restore_dir);
2820 /* delete the files for gnutar */
2822 if (!do_unlink_list()) {
2823 g_fprintf(stderr, _("Can't recover because I can't cleanup the restore directory (%s)\n"),
2826 amfree(restore_dir);
2831 amfree(restore_dir);
2837 start_processing_data(
2838 ctl_data_t *ctl_data)
2840 if (pipe(ctl_data->child_pipe) == -1) {
2841 error(_("extract_list - error setting up pipe to extractor: %s\n"),
2846 /* okay, ready to extract. fork a child to do the actual work */
2847 if ((ctl_data->pid = fork()) == 0) {
2848 /* this is the child process */
2849 /* never gets out of this clause */
2850 aclose(ctl_data->child_pipe[1]);
2851 extract_files_child(ctl_data);
2855 if (ctl_data->pid == -1) {
2856 errstr = newstralloc(errstr, _("writer_intermediary - error forking child"));
2857 g_printf(_("writer_intermediary - error forking child"));
2860 aclose(ctl_data->child_pipe[0]);
2861 security_stream_read(amidxtaped_streams[DATAFD].fd, read_amidxtaped_data,
2863 if (am_has_feature(tapesrv_features, fe_amidxtaped_datapath)) {
2864 send_to_tape_server(amidxtaped_streams[CTLFD].fd, "DATAPATH-OK");