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"
48 #include "pipespawn.h"
50 typedef struct EXTRACT_LIST_ITEM {
52 struct EXTRACT_LIST_ITEM *next;
56 typedef struct EXTRACT_LIST {
57 char *date; /* date tape created */
58 int level; /* level of dump */
59 char *tape; /* tape label */
60 off_t fileno; /* fileno on tape */
61 EXTRACT_LIST_ITEM *files; /* files to get off tape */
63 struct EXTRACT_LIST *next;
67 typedef struct ctl_data_s {
73 data_path_t data_path;
75 backup_support_option_t *bsu;
84 security_stream_t *fd;
85 } amidxtaped_streams[] = {
91 #define NSTREAMS (int)(sizeof(amidxtaped_streams) / sizeof(amidxtaped_streams[0]))
94 static void amidxtaped_response(void *, pkt_t *, security_handle_t *);
95 static void stop_amidxtaped(void);
96 static char *dump_device_name = NULL;
98 static char *amidxtaped_line = NULL;
99 extern char *localhost;
100 static char header_buf[32768];
101 static int header_size = 0;
104 /* global pid storage for interrupt handler */
105 pid_t extract_restore_child_pid = -1;
107 static EXTRACT_LIST *extract_list = NULL;
108 static const security_driver_t *amidxtaped_secdrv;
110 unsigned short samba_extract_method = SAMBA_TAR;
112 #define READ_TIMEOUT 240*60
114 EXTRACT_LIST *first_tape_list(void);
115 EXTRACT_LIST *next_tape_list(EXTRACT_LIST *list);
116 static int is_empty_dir(char *fname);
117 int is_extract_list_nonempty(void);
118 int length_of_tape_list(EXTRACT_LIST *tape_list);
119 void add_file(char *path, char *regex);
120 void add_glob(char *glob);
121 void add_regex(char *regex);
122 void clear_extract_list(void);
123 void clean_tape_list(EXTRACT_LIST *tape_list);
124 void clean_extract_list(void);
125 void check_file_overwrite(char *filename);
126 void delete_file(char *path, char *regex);
127 void delete_glob(char *glob);
128 void delete_regex(char *regex);
129 void delete_tape_list(EXTRACT_LIST *tape_list);
130 void display_extract_list(char *file);
131 void extract_files(void);
132 void read_file_header(char *buffer,
136 static int add_extract_item(DIR_ITEM *ditem);
137 static int delete_extract_item(DIR_ITEM *ditem);
138 static int extract_files_setup(char *label, off_t fsf);
139 static int okay_to_continue(int allow_tape,
142 static ssize_t read_buffer(int datafd,
146 static void clear_tape_list(EXTRACT_LIST *tape_list);
147 static void extract_files_child(ctl_data_t *ctl_data);
148 static void send_to_tape_server(security_stream_t *stream, char *cmd);
149 int writer_intermediary(EXTRACT_LIST *elist);
150 int get_amidxtaped_line(void);
151 static void read_amidxtaped_data(void *, void *, ssize_t);
152 static char *merge_path(char *path1, char *path2);
153 static gboolean ask_file_overwrite(ctl_data_t *ctl_data);
154 static void start_processing_data(ctl_data_t *ctl_data);
157 * Function: ssize_t read_buffer(datafd, buffer, buflen, timeout_s)
160 * read data from input file desciptor waiting up to timeout_s
161 * seconds before returning data.
164 * datafd - File descriptor to read from.
165 * buffer - Buffer to read into.
166 * buflen - Maximum number of bytes to read into buffer.
167 * timeout_s - Seconds to wait before returning what was already read.
170 * >0 - Number of data bytes in buffer.
172 * -1 - errno == ETIMEDOUT if no data available in specified time.
173 * errno == ENFILE if datafd is invalid.
174 * otherwise errno is set by select or read..
185 SELECT_ARG_TYPE readset;
186 struct timeval timeout;
191 if(datafd < 0 || datafd >= (int)FD_SETSIZE) {
192 errno = EMFILE; /* out of range */
197 spaceleft = (ssize_t)buflen;
201 FD_SET(datafd, &readset);
202 timeout.tv_sec = timeout_s;
204 nfound = select(datafd+1, &readset, NULL, NULL, &timeout);
206 /* Select returned an error. */
207 g_fprintf(stderr,_("select error: %s\n"), strerror(errno));
213 /* Select timed out. */
214 if (timeout_s != 0) {
215 /* Not polling: a real read timeout */
216 g_fprintf(stderr,_("timeout waiting for restore\n"));
217 g_fprintf(stderr,_("increase READ_TIMEOUT in recover-src/extract_list.c if your tape is slow\n"));
224 if(!FD_ISSET(datafd, &readset))
227 /* Select says data is available, so read it. */
228 size = read(datafd, dataptr, (size_t)spaceleft);
230 if ((errno == EINTR) || (errno == EAGAIN)) {
233 if (errno != EPIPE) {
234 g_fprintf(stderr, _("read_buffer: read error - %s"),
242 } while ((size > 0) && (spaceleft > 0));
244 return ((((ssize_t)buflen-spaceleft) > 0) ? ((ssize_t)buflen-spaceleft) : size);
249 first_tape_list(void)
256 /*@keep@*/EXTRACT_LIST *list)
265 EXTRACT_LIST * tape_list)
267 EXTRACT_LIST_ITEM *this, *next;
270 this = tape_list->files;
278 tape_list->files = NULL;
282 /* remove a tape list from the extract list, clearing the tape list
283 beforehand if necessary */
286 EXTRACT_LIST *tape_list)
288 EXTRACT_LIST *this, *prev;
290 if (tape_list == NULL)
293 /* is it first on the list? */
294 if (tape_list == extract_list)
296 extract_list = tape_list->next;
297 clear_tape_list(tape_list);
298 amfree(tape_list->date);
299 amfree(tape_list->tape);
304 /* so not first on list - find it and delete */
306 this = extract_list->next;
309 if (this == tape_list)
311 prev->next = tape_list->next;
312 clear_tape_list(tape_list);
313 amfree(tape_list->date);
314 amfree(tape_list->tape);
325 /* return the number of files on a tape's list */
328 EXTRACT_LIST *tape_list)
330 EXTRACT_LIST_ITEM *fn;
334 for (fn = tape_list->files; fn != NULL; fn = fn->next)
342 clear_extract_list(void)
344 while (extract_list != NULL)
345 delete_tape_list(extract_list);
351 EXTRACT_LIST *tape_list)
353 EXTRACT_LIST_ITEM *fn1, *pfn1, *ofn1;
354 EXTRACT_LIST_ITEM *fn2, *pfn2, *ofn2;
359 fn1 = tape_list->files;
360 while (fn1 != NULL) {
365 while (fn2 != NULL && remove_fn1 == 0) {
367 if(strcmp(fn1->path, fn2->path) == 0) {
369 } else if (strncmp(fn1->path, fn2->path, strlen(fn1->path)) == 0 &&
370 ((strlen(fn2->path) > strlen(fn1->path) &&
371 fn2->path[strlen(fn1->path)] == '/') ||
372 (fn1->path[strlen(fn1->path)-1] == '/'))) {
374 } else if (strncmp(fn2->path, fn1->path, strlen(fn2->path)) == 0 &&
375 ((strlen(fn1->path) > strlen(fn2->path) &&
376 fn1->path[strlen(fn2->path)] == '/') ||
377 (fn2->path[strlen(fn2->path)-1] == '/'))) {
383 dbprintf(_("removing path %s, it is included in %s\n"),
384 fn2->path, fn1->path);
390 } else if (remove_fn1 == 0) {
396 if(remove_fn1 != 0) {
397 /* fn2->path is always valid */
398 /*@i@*/ dbprintf(_("removing path %s, it is included in %s\n"),
399 /*@i@*/ fn1->path, fn2->path);
404 amfree(tape_list->files);
405 tape_list->files = fn1;
423 char *npath = g_path_get_basename(path);
424 *dir = g_path_get_dirname(path);
425 if (strcmp(*dir, ".") == 0) {
432 clean_extract_list(void)
436 for (this = extract_list; this != NULL; this = this->next)
437 clean_tape_list(this);
441 int add_to_unlink_list(char *path);
442 int do_unlink_list(void);
443 void free_unlink_list(void);
445 typedef struct s_unlink_list {
447 struct s_unlink_list *next;
449 t_unlink_list *unlink_list = NULL;
458 unlink_list = alloc(SIZEOF(*unlink_list));
459 unlink_list->path = stralloc(path);
460 unlink_list->next = NULL;
462 for (ul = unlink_list; ul != NULL; ul = ul->next) {
463 if (strcmp(ul->path, path) == 0)
466 ul = alloc(SIZEOF(*ul));
467 ul->path = stralloc(path);
468 ul->next = unlink_list;
480 for (ul = unlink_list; ul != NULL; ul = ul->next) {
481 if (unlink(ul->path) < 0) {
482 g_fprintf(stderr,_("Can't unlink %s: %s\n"), ul->path, strerror(errno));
491 free_unlink_list(void)
493 t_unlink_list *ul, *ul1;
495 for (ul = unlink_list; ul != NULL; ul = ul1) {
507 check_file_overwrite(
511 EXTRACT_LIST_ITEM *fn;
512 struct stat stat_buf;
516 for (this = extract_list; this != NULL; this = this->next) {
517 for (fn = this->files; fn != NULL ; fn = fn->next) {
519 /* Check path component of fn->path */
521 path = stralloc2(dir, fn->path);
522 if (path[strlen(path)-1] == '/') {
523 path[strlen(path)-1] = '\0';
526 s = path + strlen(dir) + 1;
527 while((s = strchr(s, '/'))) {
529 if (lstat(path, &stat_buf) == 0) {
530 if(!S_ISDIR(stat_buf.st_mode)) {
531 if (add_to_unlink_list(path)) {
532 g_printf(_("WARNING: %s is not a directory, "
533 "it will be deleted.\n"),
538 else if (errno != ENOENT) {
539 g_printf(_("Can't stat %s: %s\n"), path, strerror(errno));
548 filename = stralloc2(dir, fn->path);
549 if (filename[strlen(filename)-1] == '/') {
550 filename[strlen(filename)-1] = '\0';
553 if (lstat(filename, &stat_buf) == 0) {
554 if(S_ISDIR(stat_buf.st_mode)) {
555 if(!is_empty_dir(filename)) {
556 g_printf(_("WARNING: All existing files in %s "
557 "will be deleted.\n"), filename);
559 } else if(S_ISREG(stat_buf.st_mode)) {
560 g_printf(_("WARNING: Existing file %s will be overwritten\n"),
563 if (add_to_unlink_list(filename)) {
564 g_printf(_("WARNING: Existing entry %s will be deleted\n"),
568 } else if (errno != ENOENT) {
569 g_printf(_("Can't stat %s: %s\n"), filename, strerror(errno));
577 /* returns -1 if error */
578 /* returns 0 on succes */
579 /* returns 1 if already added */
584 EXTRACT_LIST *this, *this1;
585 EXTRACT_LIST_ITEM *that, *curr;
586 char *ditem_path = NULL;
588 ditem_path = stralloc(ditem->path);
589 clean_pathname(ditem_path);
591 for (this = extract_list; this != NULL; this = this->next)
593 /* see if this is the list for the tape */
594 if (this->level == ditem->level && strcmp(this->tape, ditem->tape) == 0)
596 /* yes, so add to list */
600 if (strcmp(curr->path,ditem_path) == 0) {
606 that = (EXTRACT_LIST_ITEM *)alloc(sizeof(EXTRACT_LIST_ITEM));
607 that->path = stralloc(ditem_path);
608 that->next = this->files;
609 this->files = that; /* add at front since easiest */
615 /* so this is the first time we have seen this tape */
616 this = (EXTRACT_LIST *)alloc(sizeof(EXTRACT_LIST));
617 this->tape = stralloc(ditem->tape);
618 this->level = ditem->level;
619 this->fileno = ditem->fileno;
620 this->date = stralloc(ditem->date);
621 that = (EXTRACT_LIST_ITEM *)alloc(sizeof(EXTRACT_LIST_ITEM));
622 that->path = stralloc(ditem_path);
626 /* add this in date increasing order */
627 /* because restore must be done in this order */
628 /* add at begining */
629 if(extract_list==NULL || strcmp(this->date,extract_list->date) < 0)
631 this->next = extract_list;
636 for (this1 = extract_list; this1->next != NULL; this1 = this1->next)
638 /* add in the middle */
639 if(strcmp(this->date,this1->next->date) < 0)
641 this->next = this1->next;
655 /* returns -1 if error */
656 /* returns 0 on deletion */
657 /* returns 1 if not there */
663 EXTRACT_LIST_ITEM *that, *prev;
664 char *ditem_path = NULL;
666 ditem_path = stralloc(ditem->path);
667 clean_pathname(ditem_path);
669 for (this = extract_list; this != NULL; this = this->next)
671 /* see if this is the list for the tape */
672 if (this->level == ditem->level && strcmp(this->tape, ditem->tape) == 0)
674 /* yes, so find file on list */
676 if (strcmp(that->path, ditem_path) == 0)
679 this->files = that->next;
682 /* if list empty delete it */
683 if (this->files == NULL)
684 delete_tape_list(this);
692 if (strcmp(that->path, ditem_path) == 0)
694 prev->next = that->next;
718 int len = strlen(path1);
719 if (path1[len-1] == '/' && path2[0] == '/') {
720 result = stralloc2(path1, path2+1);
721 } else if (path1[len-1] != '/' && path2[0] != '/') {
722 result = vstralloc(path1, "/", path2, NULL);
724 result = stralloc2(path1, path2);
741 if (disk_path == NULL) {
742 g_printf(_("Must select directory before adding files\n"));
746 uqglob = unquote_string(glob);
747 glob = file_of_path(uqglob, &dir);
749 sdir = merge_path(mount_point, disk_path);
750 result = cd_glob(dir, 0);
754 regex = glob_to_regex(glob);
755 dbprintf(_("add_glob (%s) -> %s\n"), uqglob, regex);
756 if ((s = validate_regexp(regex)) != NULL) {
757 g_printf(_("%s is not a valid shell wildcard pattern: "), glob);
761 * glob_to_regex() anchors the beginning of the pattern with ^,
762 * but we will be tacking it onto the end of the current directory
763 * in add_file, so strip that off. Also, it anchors the end with
764 * $, but we need to match an optional trailing /, so tack that on
767 regex_path = stralloc(regex + 1);
768 regex_path[strlen(regex_path) - 1] = '\0';
769 strappend(regex_path, "[/]*$");
770 add_file(uqglob, regex_path);
774 set_directory(sdir, 0);
794 if (disk_path == NULL) {
795 g_printf(_("Must select directory before adding files\n"));
799 uqregex = unquote_string(regex);
800 newregex = file_of_path(uqregex, &dir);
802 sdir = merge_path(mount_point, disk_path);
803 result = cd_regex(dir, 0);
808 if ((s = validate_regexp(newregex)) != NULL) {
809 g_printf(_("\"%s\" is not a valid regular expression: "), newregex);
812 add_file(uqregex, newregex);
815 set_directory(sdir, 0);
828 DIR_ITEM *ditem, lditem;
829 char *path_on_disk = NULL;
834 char *dir, *dir_undo, dir_undo_ch = '\0';
835 char *ditem_path = NULL;
836 char *qditem_path = NULL;
839 char *s, *fp, *quoted;
844 if (disk_path == NULL) {
845 g_printf(_("Must select directory before adding files\n"));
848 memset(&lditem, 0, sizeof(lditem)); /* Prevent use of bogus data... */
850 dbprintf(_("add_file: Looking for \"%s\"\n"), regex);
852 if(strcmp(regex, "/[/]*$") == 0) { /* "/" behave like "." */
855 else if(strcmp(regex, "[^/]*[/]*$") == 0) { /* "*" */
856 regex = "([^/.]|\\.[^/]+|[^/.][^/]*)[/]*$";
858 /* remove "/" at end of path */
859 j = (ssize_t)(strlen(regex) - 1);
860 while(j >= 0 && regex[j] == '/')
864 /* convert path (assumed in cwd) to one on disk */
865 if (strcmp(disk_path, "/") == 0) {
867 /* No mods needed if already starts with '/' */
868 path_on_disk = stralloc(regex);
871 path_on_disk = stralloc2("/", regex);
874 char *clean_disk_path = clean_regex(disk_path, 0);
875 path_on_disk = vstralloc(clean_disk_path, "/", regex, NULL);
876 amfree(clean_disk_path);
879 dbprintf(_("add_file: Converted path=\"%s\" to path_on_disk=\"%s\"\n"),
880 regex, path_on_disk);
884 for (ditem=get_dir_list(); ditem!=NULL; ditem=get_next_dir_item(ditem))
887 quoted = quote_string(ditem->path);
888 dbprintf(_("add_file: Pondering ditem->path=%s\n"), quoted);
890 if (match(path_on_disk, ditem->path))
893 j = (ssize_t)strlen(ditem->path);
894 if((j > 0 && ditem->path[j-1] == '/')
895 || (j > 1 && ditem->path[j-2] == '/' && ditem->path[j-1] == '.'))
896 { /* It is a directory */
897 ditem_path = newstralloc(ditem_path, ditem->path);
898 clean_pathname(ditem_path);
900 qditem_path = quote_string(ditem_path);
901 cmd = newstralloc2(cmd, "ORLD ", qditem_path);
903 if(send_command(cmd) == -1) {
906 amfree(path_on_disk);
912 if ((i = get_reply_line()) == -1) {
914 amfree(path_on_disk);
917 if(i==0) { /* assume something wrong */
919 amfree(path_on_disk);
926 lditem.path = newstralloc(lditem.path, ditem->path);
927 /* skip the last line -- duplicate of the preamble */
929 while ((i = get_reply_line()) != 0) {
932 amfree(path_on_disk);
937 if(dir_undo) *dir_undo = dir_undo_ch;
939 cmd = stralloc(l); /* save for error report */
941 continue; /* throw the rest of the lines away */
944 if (!server_happy()) {
950 if(strncmp_const_skip(l, "201-", s, ch) != 0) {
951 err = _("bad reply: not 201-");
956 skip_whitespace(s, ch);
958 err = _("bad reply: missing date field");
962 skip_non_whitespace(s, ch);
964 lditem.date = newstralloc(lditem.date, fp);
967 skip_whitespace(s, ch);
968 if(ch == '\0' || sscanf(s - 1, "%d", &lditem.level) != 1) {
969 err = _("bad reply: cannot parse level field");
974 skip_whitespace(s, ch);
976 err = _("bad reply: missing tape field");
980 skip_quoted_string(s, ch);
983 lditem.tape = unquote_string(fp);
986 if(am_has_feature(indexsrv_features, fe_amindexd_fileno_in_ORLD)) {
987 long long fileno_ = (long long)0;
988 skip_whitespace(s, ch);
990 sscanf(s - 1, "%lld", &fileno_) != 1) {
991 err = _("bad reply: cannot parse fileno field");
994 lditem.fileno = (off_t)fileno_;
998 skip_whitespace(s, ch);
1000 err = _("bad reply: missing directory field");
1004 skip_quoted_string(s, ch);
1006 dir_undo_ch = *dir_undo;
1009 switch(add_extract_item(&lditem)) {
1011 g_printf(_("System error\n"));
1012 dbprintf(_("add_file: (Failed) System error\n"));
1016 quoted = quote_string(lditem.path);
1017 g_printf(_("Added dir %s at date %s\n"),
1018 quoted, lditem.date);
1019 dbprintf(_("add_file: (Successful) Added dir %s at date %s\n"),
1020 quoted, lditem.date);
1029 if(!server_happy()) {
1036 } else if(added == 0) {
1037 quoted = quote_string(ditem_path);
1038 g_printf(_("dir %s already added\n"), quoted);
1039 dbprintf(_("add_file: dir %s already added\n"), quoted);
1043 else /* It is a file */
1045 switch(add_extract_item(ditem)) {
1047 g_printf(_("System error\n"));
1048 dbprintf(_("add_file: (Failed) System error\n"));
1052 quoted = quote_string(ditem->path);
1053 g_printf(_("Added file %s\n"), quoted);
1054 dbprintf(_("add_file: (Successful) Added %s\n"), quoted);
1059 quoted = quote_string(ditem->path);
1060 g_printf(_("File %s already added\n"), quoted);
1061 dbprintf(_("add_file: file %s already added\n"), quoted);
1070 amfree(path_on_disk);
1072 amfree(lditem.path);
1073 amfree(lditem.date);
1074 amfree(lditem.tape);
1077 quoted = quote_string(path);
1078 g_printf(_("File %s doesn't exist in directory\n"), quoted);
1079 dbprintf(_("add_file: (Failed) File %s doesn't exist in directory\n"),
1099 if (disk_path == NULL) {
1100 g_printf(_("Must select directory before adding files\n"));
1104 uqglob = unquote_string(glob);
1105 newglob = file_of_path(uqglob, &dir);
1107 sdir = merge_path(mount_point, disk_path);
1108 result = cd_glob(dir, 0);
1112 regex = glob_to_regex(newglob);
1113 dbprintf(_("delete_glob (%s) -> %s\n"), newglob, regex);
1114 if ((s = validate_regexp(regex)) != NULL) {
1115 g_printf(_("\"%s\" is not a valid shell wildcard pattern: "),
1120 * glob_to_regex() anchors the beginning of the pattern with ^,
1121 * but we will be tacking it onto the end of the current directory
1122 * in add_file, so strip that off. Also, it anchors the end with
1123 * $, but we need to match an optional trailing /, so tack that on
1126 regex_path = stralloc(regex + 1);
1127 regex_path[strlen(regex_path) - 1] = '\0';
1128 strappend(regex_path, "[/]*$");
1129 delete_file(uqglob, regex_path);
1133 set_directory(sdir, 0);
1153 if (disk_path == NULL) {
1154 g_printf(_("Must select directory before adding files\n"));
1158 uqregex = unquote_string(regex);
1159 newregex = file_of_path(uqregex, &dir);
1161 sdir = merge_path(mount_point, disk_path);
1162 result = cd_regex(dir, 0);
1167 if ((s = validate_regexp(newregex)) != NULL) {
1168 g_printf(_("\"%s\" is not a valid regular expression: "), newregex);
1171 delete_file(newregex, regex);
1174 set_directory(sdir, 0);
1187 DIR_ITEM *ditem, lditem;
1188 char *path_on_disk = NULL;
1194 char *tape, *tape_undo, tape_undo_ch = '\0';
1195 char *dir_undo, dir_undo_ch = '\0';
1198 char *ditem_path = NULL;
1207 if (disk_path == NULL) {
1208 g_printf(_("Must select directory before deleting files\n"));
1211 memset(&lditem, 0, sizeof(lditem)); /* Prevent use of bogus data... */
1213 dbprintf(_("delete_file: Looking for \"%s\"\n"), path);
1215 if (strcmp(regex, "[^/]*[/]*$") == 0) {
1216 /* Looking for * find everything but single . */
1217 regex = "([^/.]|\\.[^/]+|[^/.][^/]*)[/]*$";
1219 /* remove "/" at end of path */
1220 j = (ssize_t)(strlen(regex) - 1);
1221 while(j >= 0 && regex[j] == '/') regex[j--] = '\0';
1224 /* convert path (assumed in cwd) to one on disk */
1225 if (strcmp(disk_path, "/") == 0) {
1226 if (*regex == '/') {
1227 if (strcmp(regex, "/[/]*$") == 0) {
1228 /* We want "/" to match the directory itself: "/." */
1229 path_on_disk = stralloc("/\\.[/]*$");
1231 /* No mods needed if already starts with '/' */
1232 path_on_disk = stralloc(regex);
1236 path_on_disk = stralloc2("/", regex);
1239 char *clean_disk_path = clean_regex(disk_path, 0);
1240 path_on_disk = vstralloc(clean_disk_path, "/", regex, NULL);
1241 amfree(clean_disk_path);
1244 dbprintf(_("delete_file: Converted path=\"%s\" to path_on_disk=\"%s\"\n"),
1245 regex, path_on_disk);
1247 for (ditem=get_dir_list(); ditem!=NULL; ditem=get_next_dir_item(ditem))
1249 quoted = quote_string(ditem->path);
1250 dbprintf(_("delete_file: Pondering ditem->path=%s\n"), quoted);
1252 if (match(path_on_disk, ditem->path))
1255 j = (ssize_t)strlen(ditem->path);
1256 if((j > 0 && ditem->path[j-1] == '/')
1257 || (j > 1 && ditem->path[j-2] == '/' && ditem->path[j-1] == '.'))
1258 { /* It is a directory */
1259 ditem_path = newstralloc(ditem_path, ditem->path);
1260 clean_pathname(ditem_path);
1262 qditem_path = quote_string(ditem_path);
1263 cmd = newstralloc2(cmd, "ORLD ", qditem_path);
1264 amfree(qditem_path);
1265 if(send_command(cmd) == -1) {
1268 amfree(path_on_disk);
1273 if ((i = get_reply_line()) == -1) {
1275 amfree(path_on_disk);
1278 if(i==0) /* assume something wrong */
1281 amfree(path_on_disk);
1283 g_printf("%s\n", l);
1287 lditem.path = newstralloc(lditem.path, ditem->path);
1289 tape_undo = dir_undo = NULL;
1290 /* skip the last line -- duplicate of the preamble */
1291 while ((i = get_reply_line()) != 0)
1295 amfree(path_on_disk);
1300 if(tape_undo) *tape_undo = tape_undo_ch;
1301 if(dir_undo) *dir_undo = dir_undo_ch;
1302 tape_undo = dir_undo = NULL;
1303 cmd = stralloc(l); /* save for the error report */
1305 continue; /* throw the rest of the lines away */
1308 if (!server_happy()) {
1314 if(strncmp_const_skip(l, "201-", s, ch) != 0) {
1315 err = _("bad reply: not 201-");
1320 skip_whitespace(s, ch);
1322 err = _("bad reply: missing date field");
1326 skip_non_whitespace(s, ch);
1329 skip_whitespace(s, ch);
1330 if(ch == '\0' || sscanf(s - 1, "%d", &level) != 1) {
1331 err = _("bad reply: cannot parse level field");
1334 skip_integer(s, ch);
1336 skip_whitespace(s, ch);
1338 err = _("bad reply: missing tape field");
1342 skip_non_whitespace(s, ch);
1344 tape_undo_ch = *tape_undo;
1347 if(am_has_feature(indexsrv_features, fe_amindexd_fileno_in_ORLD)) {
1348 long long fileno_ = (long long)0;
1349 skip_whitespace(s, ch);
1351 sscanf(s - 1, "%lld", &fileno_) != 1) {
1352 err = _("bad reply: cannot parse fileno field");
1355 fileno = (off_t)fileno_;
1356 skip_integer(s, ch);
1359 skip_whitespace(s, ch);
1361 err = _("bad reply: missing directory field");
1364 skip_non_whitespace(s, ch);
1366 dir_undo_ch = *dir_undo;
1369 lditem.date = newstralloc(lditem.date, date);
1371 lditem.tape = newstralloc(lditem.tape, tape);
1372 switch(delete_extract_item(&lditem)) {
1374 g_printf(_("System error\n"));
1375 dbprintf(_("delete_file: (Failed) System error\n"));
1378 g_printf(_("Deleted dir %s at date %s\n"), ditem_path, date);
1379 dbprintf(_("delete_file: (Successful) Deleted dir %s at date %s\n"),
1387 if(!server_happy()) {
1394 } else if(deleted == 0) {
1395 g_printf(_("Warning - dir '%s' not on tape list\n"),
1397 dbprintf(_("delete_file: dir '%s' not on tape list\n"),
1403 switch(delete_extract_item(ditem)) {
1405 g_printf(_("System error\n"));
1406 dbprintf(_("delete_file: (Failed) System error\n"));
1409 g_printf(_("Deleted %s\n"), ditem->path);
1410 dbprintf(_("delete_file: (Successful) Deleted %s\n"),
1414 g_printf(_("Warning - file '%s' not on tape list\n"),
1416 dbprintf(_("delete_file: file '%s' not on tape list\n"),
1425 amfree(path_on_disk);
1428 g_printf(_("File %s doesn't exist in directory\n"), path);
1429 dbprintf(_("delete_file: (Failed) File %s doesn't exist in directory\n"),
1435 /* print extract list into file. If NULL ptr passed print to screen */
1437 display_extract_list(
1441 EXTRACT_LIST_ITEM *that;
1444 char *pager_command;
1449 if ((pager = getenv("PAGER")) == NULL)
1454 * Set up the pager command so if the pager is terminated, we do
1455 * not get a SIGPIPE back.
1457 pager_command = stralloc2(pager, " ; /bin/cat > /dev/null");
1458 if ((fp = popen(pager_command, "w")) == NULL)
1460 g_printf(_("Warning - can't pipe through %s\n"), pager);
1463 amfree(pager_command);
1467 uqfile = unquote_string(file);
1468 if ((fp = fopen(uqfile, "w")) == NULL)
1470 g_printf(_("Can't open file %s to print extract list into\n"), file);
1477 for (this = extract_list; this != NULL; this = this->next)
1479 g_fprintf(fp, _("TAPE %s LEVEL %d DATE %s\n"),
1480 this->tape, this->level, this->date);
1481 for (that = this->files; that != NULL; that = that->next)
1482 g_fprintf(fp, "\t%s\n", that->path);
1488 g_printf(_("Extract list written to file %s\n"), file);
1499 struct dirent *entry;
1502 if((dir = opendir(fname)) == NULL)
1506 while(!gotentry && (entry = readdir(dir)) != NULL) {
1507 gotentry = !is_dot_or_dotdot(entry->d_name);
1515 /* returns 0 if extract list empty and 1 if it isn't */
1517 is_extract_list_nonempty(void)
1519 return (extract_list != NULL);
1523 /* prints continue prompt and waits for response,
1524 returns 0 if don't, non-0 if do */
1541 prompt = _("New device name [?]: ");
1542 } else if (allow_tape && allow_skip) {
1543 prompt = _("Continue [?/Y/n/s/d]? ");
1544 } else if (allow_tape && !allow_skip) {
1545 prompt = _("Continue [?/Y/n/d]? ");
1546 } else if (allow_retry) {
1547 prompt = _("Continue [?/Y/n/r]? ");
1549 prompt = _("Continue [?/Y/n]? ");
1551 fputs(prompt, stdout);
1552 fflush(stdout); fflush(stderr);
1554 if ((line = agets(stdin)) == NULL) {
1564 dbprintf("User prompt: '%s'; response: '%s'\n", prompt, line);
1567 while ((ch = *s++) != '\0' && g_ascii_isspace(ch)) {
1568 (void)ch; /* Quiet empty loop compiler warning */
1572 g_printf(_("Enter a new device name or \"default\"\n"));
1574 g_printf(_("Enter \"y\"es to continue, \"n\"o to stop"));
1576 g_printf(_(", \"s\"kip this tape"));
1579 g_printf(_(" or \"r\"etry this tape"));
1582 g_printf(_(" or \"d\" to change to a new device"));
1586 } else if (get_device) {
1587 char *tmp = stralloc(tape_server_name);
1589 if (strncmp_const(s - 1, "default") == 0) {
1590 set_device(tmp, NULL); /* default device, existing host */
1591 } else if (s[-1] != '\0') {
1592 set_device(tmp, s - 1); /* specified device, existing host */
1594 g_printf(_("No change.\n"));
1600 } else if (ch == '\0' || ch == 'Y' || ch == 'y') {
1602 } else if (allow_tape && (ch == 'D' || ch == 'd' || ch == 'T' || ch == 't')) {
1603 get_device = 1; /* ('T' and 't' are for backward-compatibility) */
1604 } else if (ch == 'N' || ch == 'n') {
1606 } else if (allow_retry && (ch == 'R' || ch == 'r')) {
1608 } else if (allow_skip && (ch == 'S' || ch == 's')) {
1619 send_to_tape_server(
1620 security_stream_t * stream,
1623 char *msg = stralloc2(cmd, "\r\n");
1625 g_debug("send_to_tape_server: %s\n", cmd);
1626 if (security_stream_write(stream, msg, strlen(msg)) < 0)
1628 error(_("Error writing to tape server"));
1636 /* start up connection to tape server and set commands to initiate
1637 transfer of dump image.
1638 Return tape server socket on success, -1 on error. */
1640 extract_files_setup(
1644 char *disk_regex = NULL;
1645 char *host_regex = NULL;
1646 char *clean_datestamp, *ch, *ch1;
1651 amidxtaped_secdrv = security_getdriver(authopt);
1652 if (amidxtaped_secdrv == NULL) {
1653 error(_("no '%s' security driver available for host '%s'"),
1654 authopt, tape_server_name);
1657 /* We assume that amidxtaped support fe_amidxtaped_options_features */
1658 /* and fe_amidxtaped_options_auth */
1659 /* We should send a noop to really know */
1660 req = vstralloc("SERVICE amidxtaped\n",
1661 "OPTIONS ", "features=", our_features_string, ";",
1662 "auth=", authopt, ";",
1664 protocol_sendreq(tape_server_name, amidxtaped_secdrv,
1665 generic_client_get_security_conf, req, STARTUP_TIMEOUT,
1666 amidxtaped_response, &response_error);
1669 if(response_error != 0) {
1673 disk_regex = make_exact_disk_expression(disk_name);
1674 host_regex = make_exact_host_expression(dump_hostname);
1676 clean_datestamp = stralloc(dump_datestamp);
1677 for(ch=ch1=clean_datestamp;*ch1 != '\0';ch1++) {
1684 /* push our feature list off to the tape server */
1685 /* XXX assumes that index server and tape server are equivalent, ew */
1687 if(am_has_feature(indexsrv_features, fe_amidxtaped_exchange_features)){
1688 tt = newstralloc2(tt, "FEATURES=", our_features_string);
1689 send_to_tape_server(amidxtaped_streams[CTLFD].fd, tt);
1690 get_amidxtaped_line();
1691 if(strncmp_const(amidxtaped_line,"FEATURES=") == 0) {
1692 tapesrv_features = am_string_to_feature(amidxtaped_line+9);
1694 g_fprintf(stderr, _("amrecover - expecting FEATURES line from amidxtaped\n"));
1698 amfree(clean_datestamp);
1702 *tapesrv_features = *indexsrv_features;
1706 if(am_has_feature(indexsrv_features, fe_amidxtaped_header) &&
1707 am_has_feature(indexsrv_features, fe_amidxtaped_device) &&
1708 am_has_feature(indexsrv_features, fe_amidxtaped_host) &&
1709 am_has_feature(indexsrv_features, fe_amidxtaped_disk) &&
1710 am_has_feature(indexsrv_features, fe_amidxtaped_datestamp)) {
1712 if(am_has_feature(indexsrv_features, fe_amidxtaped_config)) {
1713 tt = newstralloc2(tt, "CONFIG=", get_config_name());
1714 send_to_tape_server(amidxtaped_streams[CTLFD].fd, tt);
1716 if(am_has_feature(indexsrv_features, fe_amidxtaped_label) &&
1717 label && label[0] != '/') {
1718 tt = newstralloc2(tt,"LABEL=",label);
1719 send_to_tape_server(amidxtaped_streams[CTLFD].fd, tt);
1721 if(am_has_feature(indexsrv_features, fe_amidxtaped_fsf)) {
1723 g_snprintf(v_fsf, 99, "%lld", (long long)fsf);
1724 tt = newstralloc2(tt, "FSF=",v_fsf);
1725 send_to_tape_server(amidxtaped_streams[CTLFD].fd, tt);
1727 send_to_tape_server(amidxtaped_streams[CTLFD].fd, "HEADER");
1728 tt = newstralloc2(tt, "DEVICE=", dump_device_name);
1729 send_to_tape_server(amidxtaped_streams[CTLFD].fd, tt);
1730 tt = newstralloc2(tt, "HOST=", host_regex);
1731 send_to_tape_server(amidxtaped_streams[CTLFD].fd, tt);
1732 tt = newstralloc2(tt, "DISK=", disk_regex);
1733 send_to_tape_server(amidxtaped_streams[CTLFD].fd, tt);
1734 tt = newstralloc2(tt, "DATESTAMP=", clean_datestamp);
1735 send_to_tape_server(amidxtaped_streams[CTLFD].fd, tt);
1736 send_to_tape_server(amidxtaped_streams[CTLFD].fd, "END");
1739 else if(am_has_feature(indexsrv_features, fe_amidxtaped_nargs)) {
1740 /* send to the tape server what tape file we want */
1749 send_to_tape_server(amidxtaped_streams[CTLFD].fd, "6");
1750 send_to_tape_server(amidxtaped_streams[CTLFD].fd, "-h");
1751 send_to_tape_server(amidxtaped_streams[CTLFD].fd, "-p");
1752 send_to_tape_server(amidxtaped_streams[CTLFD].fd, dump_device_name);
1753 send_to_tape_server(amidxtaped_streams[CTLFD].fd, host_regex);
1754 send_to_tape_server(amidxtaped_streams[CTLFD].fd, disk_regex);
1755 send_to_tape_server(amidxtaped_streams[CTLFD].fd, clean_datestamp);
1757 dbprintf(_("Started amidxtaped with arguments \"6 -h -p %s %s %s %s\"\n"),
1758 dump_device_name, host_regex, disk_regex, clean_datestamp);
1763 amfree(clean_datestamp);
1770 * Reads the first block of a tape file.
1781 bytes_read = read_buffer(tapedev, buffer, buflen, READ_TIMEOUT);
1782 if(bytes_read < 0) {
1783 error(_("error reading header (%s), check amidxtaped.*.debug on server"),
1788 if((size_t)bytes_read < buflen) {
1789 g_fprintf(stderr, plural(_("%s: short block %d byte\n"),
1790 _("%s: short block %d bytes\n"), bytes_read),
1791 get_pname(), (int)bytes_read);
1792 print_header(stdout, file);
1793 error(_("Can't read file header"));
1797 /* bytes_read == buflen */
1798 parse_file_header(buffer, file, (size_t)bytes_read);
1812 extract_files_child(
1813 ctl_data_t *ctl_data)
1818 GPtrArray *argv_ptr = g_ptr_array_new();
1820 EXTRACT_LIST_ITEM *fn;
1821 enum dumptypes dumptype = IS_UNKNOWN;
1824 guint passwd_field = 999999999;
1826 char *domain = NULL, *smbpass = NULL;
1829 /* code executed by child to do extraction */
1832 /* make in_fd be our stdin */
1833 if (dup2(ctl_data->child_pipe[0], STDIN_FILENO) == -1)
1835 error(_("dup2 failed in extract_files_child: %s"), strerror(errno));
1839 if(ctl_data->file.type != F_DUMPFILE) {
1840 dump_dumpfile_t(&ctl_data->file);
1841 error(_("bad header"));
1845 if (ctl_data->file.program != NULL) {
1846 if (strcmp(ctl_data->file.program, "APPLICATION") == 0)
1847 dumptype = IS_APPLICATION_API;
1849 if (strcmp(ctl_data->file.program, GNUTAR) == 0)
1850 dumptype = IS_GNUTAR;
1853 if (dumptype == IS_UNKNOWN) {
1854 len_program = strlen(ctl_data->file.program);
1855 if(len_program >= 3 &&
1856 strcmp(&ctl_data->file.program[len_program-3],"tar") == 0)
1861 if (dumptype == IS_UNKNOWN && strcmp(ctl_data->file.program, SAMBA_CLIENT) ==0) {
1862 if (samba_extract_method == SAMBA_TAR)
1863 dumptype = IS_SAMBA_TAR;
1865 dumptype = IS_SAMBA;
1870 /* form the arguments to restore */
1871 files_off_tape = length_of_tape_list(ctl_data->elist);
1875 g_ptr_array_add(argv_ptr, stralloc("smbclient"));
1876 smbpass = findpass(ctl_data->file.disk, &domain);
1878 g_ptr_array_add(argv_ptr, stralloc(ctl_data->file.disk));
1879 g_ptr_array_add(argv_ptr, stralloc("-U"));
1880 passwd_field = argv_ptr->len;
1881 g_ptr_array_add(argv_ptr, stralloc(smbpass));
1883 g_ptr_array_add(argv_ptr, stralloc("-W"));
1884 g_ptr_array_add(argv_ptr, stralloc(domain));
1887 g_ptr_array_add(argv_ptr, stralloc("-d0"));
1888 g_ptr_array_add(argv_ptr, stralloc("-Tx"));
1889 g_ptr_array_add(argv_ptr, stralloc("-")); /* data on stdin */
1894 g_ptr_array_add(argv_ptr, stralloc("tar"));
1895 /* ignore trailing zero blocks on input (this was the default until tar-1.21) */
1896 g_ptr_array_add(argv_ptr, stralloc("--ignore-zeros"));
1897 g_ptr_array_add(argv_ptr, stralloc("--numeric-owner"));
1898 g_ptr_array_add(argv_ptr, stralloc("-xpGvf"));
1899 g_ptr_array_add(argv_ptr, stralloc("-")); /* data on stdin */
1902 g_ptr_array_add(argv_ptr, stralloc("tar"));
1903 g_ptr_array_add(argv_ptr, stralloc("-xpvf"));
1904 g_ptr_array_add(argv_ptr, stralloc("-")); /* data on stdin */
1908 g_ptr_array_add(argv_ptr, stralloc("restore"));
1910 restore_args[j++] = stralloc("-xB");
1911 g_ptr_array_add(argv_ptr, stralloc("-xB"));
1913 #if defined(XFSDUMP)
1914 if (strcmp(ctl_data->file.program, XFSDUMP) == 0) {
1915 g_ptr_array_add(argv_ptr, stralloc("-v"));
1916 g_ptr_array_add(argv_ptr, stralloc("silent"));
1920 if (strcmp(ctl_data->file.program, VDUMP) == 0) {
1921 g_ptr_array_add(argv_ptr, stralloc("xf"));
1922 g_ptr_array_add(argv_ptr, stralloc("-")); /* data on stdin */
1926 g_ptr_array_add(argv_ptr, stralloc("xbf"));
1927 g_ptr_array_add(argv_ptr, stralloc("2")); /* read in units of 1K */
1928 g_ptr_array_add(argv_ptr, stralloc("-")); /* data on stdin */
1932 case IS_APPLICATION_API:
1933 g_ptr_array_add(argv_ptr, stralloc(ctl_data->file.application));
1934 g_ptr_array_add(argv_ptr, stralloc("restore"));
1935 g_ptr_array_add(argv_ptr, stralloc("--config"));
1936 g_ptr_array_add(argv_ptr, stralloc(get_config_name()));
1937 g_ptr_array_add(argv_ptr, stralloc("--disk"));
1938 g_ptr_array_add(argv_ptr, stralloc(ctl_data->file.disk));
1939 if (dump_dle && dump_dle->device) {
1940 g_ptr_array_add(argv_ptr, stralloc("--device"));
1941 g_ptr_array_add(argv_ptr, stralloc(dump_dle->device));
1943 if (ctl_data->data_path == DATA_PATH_DIRECTTCP) {
1944 g_ptr_array_add(argv_ptr, stralloc("--data-path"));
1945 g_ptr_array_add(argv_ptr, stralloc("DIRECTTCP"));
1946 g_ptr_array_add(argv_ptr, stralloc("--direct-tcp"));
1947 g_ptr_array_add(argv_ptr, stralloc(ctl_data->addrs));
1949 if (ctl_data->bsu && ctl_data->bsu->smb_recover_mode &&
1950 samba_extract_method == SAMBA_SMBCLIENT){
1951 g_ptr_array_add(argv_ptr, stralloc("--recover-mode"));
1952 g_ptr_array_add(argv_ptr, stralloc("smb"));
1954 g_ptr_array_add(argv_ptr, stralloc("--level"));
1955 g_ptr_array_add(argv_ptr, g_strdup_printf("%d", ctl_data->elist->level));
1960 merge_properties(dump_dle, NULL, dump_dle->application_property,
1962 application_property_add_to_argv(argv_ptr, dump_dle, NULL,
1964 for (scriptlist = dump_dle->scriptlist; scriptlist != NULL;
1965 scriptlist = scriptlist->next) {
1966 script = (script_t *)scriptlist->data;
1967 if (script->result && script->result->proplist) {
1968 property_add_to_argv(argv_ptr, script->result->proplist);
1972 } else if (proplist) {
1973 property_add_to_argv(argv_ptr, proplist);
1978 for (i = 0, fn = ctl_data->elist->files; i < files_off_tape;
1982 case IS_APPLICATION_API:
1987 if (strcmp(fn->path, "/") == 0)
1988 g_ptr_array_add(argv_ptr, stralloc("."));
1990 g_ptr_array_add(argv_ptr, stralloc2(".", fn->path));
1994 #if defined(XFSDUMP)
1995 if (strcmp(ctl_data->file.program, XFSDUMP) == 0) {
1997 * xfsrestore needs a -s option before each file to be
1998 * restored, and also wants them to be relative paths.
2000 g_ptr_array_add(argv_ptr, stralloc("-s"));
2001 g_ptr_array_add(argv_ptr, stralloc(fn->path + 1));
2005 g_ptr_array_add(argv_ptr, stralloc(fn->path));
2010 #if defined(XFSDUMP)
2011 if (strcmp(ctl_data->file.program, XFSDUMP) == 0) {
2012 g_ptr_array_add(argv_ptr, stralloc("-"));
2013 g_ptr_array_add(argv_ptr, stralloc("."));
2016 g_ptr_array_add(argv_ptr, NULL);
2021 cmd = stralloc(SAMBA_CLIENT);
2024 /* fall through to ... */
2030 g_fprintf(stderr, _("warning: GNUTAR program not available.\n"));
2031 cmd = stralloc("tar");
2033 cmd = stralloc(GNUTAR);
2040 if (strcmp(ctl_data->file.program, DUMP) == 0) {
2041 cmd = stralloc(RESTORE);
2045 if (strcmp(ctl_data->file.program, VDUMP) == 0) {
2046 cmd = stralloc(VRESTORE);
2050 if (strcmp(ctl_data->file.program, VXDUMP) == 0) {
2051 cmd = stralloc(VXRESTORE);
2054 #if defined(XFSDUMP)
2055 if (strcmp(ctl_data->file.program, XFSDUMP) == 0) {
2056 cmd = stralloc(XFSRESTORE);
2060 g_fprintf(stderr, _("warning: restore program for %s not available.\n"),
2061 ctl_data->file.program);
2062 cmd = stralloc("restore");
2065 case IS_APPLICATION_API:
2066 cmd = vstralloc(APPLICATION_DIR, "/", ctl_data->file.application, NULL);
2070 dbprintf(_("Exec'ing %s with arguments:\n"), cmd);
2071 for (j = 0; j < argv_ptr->len - 1; j++) {
2072 if (j == passwd_field)
2073 dbprintf("\tXXXXX\n");
2075 dbprintf(_("\t%s\n"), (char *)g_ptr_array_index(argv_ptr, j));
2078 (void)execv(cmd, (char **)argv_ptr->pdata);
2079 /* only get here if exec failed */
2081 g_ptr_array_free_full(argv_ptr);
2083 perror(_("amrecover couldn't exec"));
2084 g_fprintf(stderr, _(" problem executing %s\n"), cmd);
2092 * Interpose something between the process writing out the dump (writing it to
2093 * some extraction program, really) and the socket from which we're reading, so
2094 * that we can do things like prompt for human interaction for multiple tapes.
2097 writer_intermediary(
2098 EXTRACT_LIST * elist)
2100 ctl_data_t ctl_data;
2101 amwait_t extractor_status;
2103 ctl_data.header_done = 0;
2104 ctl_data.child_pipe[0] = -1;
2105 ctl_data.child_pipe[1] = -1;
2107 ctl_data.elist = elist;
2108 fh_init(&ctl_data.file);
2109 ctl_data.data_path = DATA_PATH_AMANDA;
2110 ctl_data.addrs = NULL;
2111 ctl_data.bsu = NULL;
2112 ctl_data.bytes_read = 0;
2115 security_stream_read(amidxtaped_streams[DATAFD].fd,
2116 read_amidxtaped_data, &ctl_data);
2118 while(get_amidxtaped_line() >= 0) {
2119 char desired_tape[MAX_TAPE_LABEL_BUF];
2120 g_debug("get amidxtaped line: %s", amidxtaped_line);
2122 /* if prompted for a tape, relay said prompt to the user */
2123 if(sscanf(amidxtaped_line, "FEEDME %132s\n", desired_tape) == 1) {
2125 g_printf(_("Load tape %s now\n"), desired_tape);
2126 dbprintf(_("Requesting tape %s from user\n"), desired_tape);
2127 done = okay_to_continue(am_has_feature(indexsrv_features,
2128 fe_amrecover_feedme_tape),
2131 if (am_has_feature(indexsrv_features,
2132 fe_amrecover_feedme_tape)) {
2133 char *reply = stralloc2("TAPE ", tape_device_name);
2134 send_to_tape_server(amidxtaped_streams[CTLFD].fd, reply);
2137 send_to_tape_server(amidxtaped_streams[CTLFD].fd, "OK");
2140 send_to_tape_server(amidxtaped_streams[CTLFD].fd, "ERROR");
2143 } else if (strncmp_const(amidxtaped_line, "USE-DATAPATH ") == 0) {
2144 if (strncmp_const(amidxtaped_line+13, "AMANDA") == 0) {
2145 ctl_data.data_path = DATA_PATH_AMANDA;
2146 g_debug("Using AMANDA data-path");
2147 } else if (strncmp_const(amidxtaped_line+13, "DIRECT-TCP") == 0) {
2148 ctl_data.data_path = DATA_PATH_DIRECTTCP;
2149 ctl_data.addrs = stralloc(amidxtaped_line+24);
2150 g_debug("Using DIRECT-TCP data-path with %s", ctl_data.addrs);
2152 start_processing_data(&ctl_data);
2153 } else if(strncmp_const(amidxtaped_line, "MESSAGE ") == 0) {
2154 g_printf("%s\n",&amidxtaped_line[8]);
2156 g_fprintf(stderr, _("Strange message from tape server: %s"),
2162 /* CTL might be close before DATA */
2164 dumpfile_free_data(&ctl_data.file);
2165 amfree(ctl_data.addrs);
2166 amfree(ctl_data.bsu);
2167 if (ctl_data.child_pipe[1] != -1)
2168 aclose(ctl_data.child_pipe[1]);
2170 if (ctl_data.header_done == 0) {
2171 g_printf(_("Got no header and data from server, check in amidxtaped.*.debug and amandad.*.debug files on server\n"));
2174 if (ctl_data.pid != -1) {
2175 waitpid(ctl_data.pid, &extractor_status, 0);
2176 if(WEXITSTATUS(extractor_status) != 0){
2177 int ret = WEXITSTATUS(extractor_status);
2178 if(ret == 255) ret = -1;
2179 g_printf(_("Extractor child exited with status %d\n"), ret);
2183 g_debug("bytes read: %jd", (intmax_t)ctl_data.bytes_read);
2187 /* exec restore to do the actual restoration */
2189 /* does the actual extraction of files */
2191 * The original design had the dump image being returned exactly as it
2192 * appears on the tape, and this routine getting from the index server
2193 * whether or not it is compressed, on the assumption that the tape
2194 * server may not know how to uncompress it. But
2195 * - Amrestore can't do that. It returns either compressed or uncompressed
2196 * (always). Amrestore assumes it can uncompress files. It is thus a good
2197 * idea to run the tape server on a machine with gzip.
2198 * - The information about compression in the disklist is really only
2199 * for future dumps. It is possible to change compression on a drive
2200 * so the information in the disklist may not necessarily relate to
2201 * the dump image on the tape.
2202 * Consequently the design was changed to assuming that amrestore can
2203 * uncompress any dump image and have it return an uncompressed file
2209 EXTRACT_LIST *elist;
2213 tapelist_t *tlist = NULL, *a_tlist;
2214 g_option_t g_options;
2215 levellist_t all_level = NULL;
2218 if (!is_extract_list_nonempty())
2220 g_printf(_("Extract list empty - No files to extract!\n"));
2224 clean_extract_list();
2226 /* get tape device name from index server if none specified */
2227 if (tape_server_name == NULL) {
2228 tape_server_name = newstralloc(tape_server_name, server_name);
2230 if (tape_device_name == NULL) {
2231 if (send_command("TAPE") == -1)
2233 if (get_reply_line() == -1)
2236 if (!server_happy())
2238 g_printf("%s\n", l);
2241 /* skip reply number */
2242 tape_device_name = newstralloc(tape_device_name, l+4);
2245 if (strcmp(tape_device_name, "/dev/null") == 0)
2247 g_printf(_("amrecover: warning: using %s as the tape device will not work\n"),
2252 for (elist = first_tape_list(); elist != NULL; elist = next_tape_list(elist)) {
2253 if(elist->tape[0]!='/') {
2255 g_printf(_("\nExtracting files using tape drive %s on host %s.\n"),
2256 tape_device_name, tape_server_name);
2257 g_printf(_("The following tapes are needed:"));
2262 tlist = unmarshal_tapelist_str(elist->tape);
2263 for(a_tlist = tlist ; a_tlist != NULL; a_tlist = a_tlist->next)
2264 g_printf(" %s", a_tlist->label);
2266 free_tapelist(tlist);
2270 for (elist = first_tape_list(); elist != NULL; elist = next_tape_list(elist)) {
2271 if(elist->tape[0]=='/') {
2273 g_printf(_("\nExtracting files from holding disk on host %s.\n"),
2275 g_printf(_("The following files are needed:"));
2280 tlist = unmarshal_tapelist_str(elist->tape);
2281 for(a_tlist = tlist; a_tlist != NULL; a_tlist = a_tlist->next)
2282 g_printf(" %s", a_tlist->label);
2284 free_tapelist(tlist);
2289 g_options.config = get_config_name();
2290 g_options.hostname = dump_hostname;
2291 for (elist = first_tape_list(); elist != NULL;
2292 elist = next_tape_list(elist)) {
2293 level_t *level = g_new0(level_t, 1);
2294 level->level = elist->level;
2295 all_level = g_slist_append(all_level, level);
2298 slist_free_full(dump_dle->levellist, g_free);
2299 dump_dle->levellist = all_level;
2300 run_client_scripts(EXECUTE_ON_PRE_RECOVER, &g_options, dump_dle,
2302 dump_dle->levellist = NULL;
2305 while ((elist = first_tape_list()) != NULL)
2307 if(elist->tape[0]=='/') {
2308 dump_device_name = newstralloc(dump_device_name, elist->tape);
2309 g_printf(_("Extracting from file "));
2310 tlist = unmarshal_tapelist_str(dump_device_name);
2311 for(a_tlist = tlist; a_tlist != NULL; a_tlist = a_tlist->next)
2312 g_printf(" %s", a_tlist->label);
2314 free_tapelist(tlist);
2317 g_printf(_("Extracting files using tape drive %s on host %s.\n"),
2318 tape_device_name, tape_server_name);
2319 tlist = unmarshal_tapelist_str(elist->tape);
2320 g_printf(_("Load tape %s now\n"), tlist->label);
2321 dbprintf(_("Requesting tape %s from user\n"), tlist->label);
2322 free_tapelist(tlist);
2323 otc = okay_to_continue(1,1,0);
2326 else if (otc == SKIP_TAPE) {
2327 delete_tape_list(elist); /* skip this tape */
2330 dump_device_name = newstralloc(dump_device_name, tape_device_name);
2332 dump_datestamp = newstralloc(dump_datestamp, elist->date);
2334 if (last_level != -1 && dump_dle) {
2337 level = g_new0(level_t, 1);
2338 level->level = last_level;
2339 dump_dle->levellist = g_slist_append(dump_dle->levellist, level);
2341 level = g_new0(level_t, 1);
2342 level->level = elist->level;
2343 dump_dle->levellist = g_slist_append(dump_dle->levellist, level);
2344 run_client_scripts(EXECUTE_ON_INTER_LEVEL_RECOVER, &g_options,
2346 slist_free_full(dump_dle->levellist, g_free);
2347 dump_dle->levellist = NULL;
2350 /* connect to the tape handler daemon on the tape drive server */
2351 if ((extract_files_setup(elist->tape, elist->fileno)) == -1)
2353 g_fprintf(stderr, _("amrecover - can't talk to tape server: %s\n"),
2360 level = g_new0(level_t, 1);
2361 level->level = elist->level;
2362 dump_dle->levellist = g_slist_append(dump_dle->levellist, level);
2363 run_client_scripts(EXECUTE_ON_PRE_LEVEL_RECOVER, &g_options,
2366 last_level = elist->level;
2368 /* if the server have fe_amrecover_feedme_tape, it has asked for
2369 * the tape itself, even if the restore didn't succeed, we should
2372 if(writer_intermediary(elist) == 0 ||
2373 am_has_feature(indexsrv_features, fe_amrecover_feedme_tape))
2374 delete_tape_list(elist); /* tape done so delete from list */
2376 am_release_feature_set(tapesrv_features);
2380 run_client_scripts(EXECUTE_ON_POST_LEVEL_RECOVER, &g_options,
2382 slist_free_full(dump_dle->levellist, g_free);
2383 dump_dle->levellist = NULL;
2387 dump_dle->levellist = all_level;
2388 run_client_scripts(EXECUTE_ON_POST_RECOVER, &g_options, dump_dle,
2390 slist_free_full(dump_dle->levellist, g_free);
2392 dump_dle->levellist = NULL;
2397 amidxtaped_response(
2400 security_handle_t * sech)
2402 int ports[NSTREAMS], *response_error = datap, i;
2407 assert(response_error != NULL);
2408 assert(sech != NULL);
2409 memset(ports, -1, SIZEOF(ports));
2412 errstr = newvstrallocf(errstr, _("[request failed: %s]"), security_geterror(sech));
2413 *response_error = 1;
2416 security_close_connection(sech, dump_hostname);
2418 if (pkt->type == P_NAK) {
2419 #if defined(PACKET_DEBUG)
2420 g_fprintf(stderr, _("got nak response:\n----\n%s\n----\n\n"), pkt->body);
2423 tok = strtok(pkt->body, " ");
2424 if (tok == NULL || strcmp(tok, "ERROR") != 0)
2427 tok = strtok(NULL, "\n");
2429 errstr = newvstralloc(errstr, "NAK: ", tok, NULL);
2430 *response_error = 1;
2433 errstr = newstralloc(errstr, "request NAK");
2434 *response_error = 2;
2439 if (pkt->type != P_REP) {
2440 errstr = newvstrallocf(errstr, _("received strange packet type %s: %s"),
2441 pkt_type2str(pkt->type), pkt->body);
2442 *response_error = 1;
2446 #if defined(PACKET_DEBUG)
2447 g_fprintf(stderr, _("got response:\n----\n%s\n----\n\n"), pkt->body);
2450 for(i = 0; i < NSTREAMS; i++) {
2452 amidxtaped_streams[i].fd = NULL;
2456 while((tok = strtok(p, " \n")) != NULL) {
2460 * Error response packets have "ERROR" followed by the error message
2461 * followed by a newline.
2463 if (strcmp(tok, "ERROR") == 0) {
2464 tok = strtok(NULL, "\n");
2466 tok = _("[bogus error packet]");
2467 errstr = newstralloc(errstr, tok);
2468 *response_error = 2;
2474 * Regular packets have CONNECT followed by three streams
2476 if (strcmp(tok, "CONNECT") == 0) {
2479 * Parse the three stream specifiers out of the packet.
2481 for (i = 0; i < NSTREAMS; i++) {
2482 tok = strtok(NULL, " ");
2483 if (tok == NULL || strcmp(tok, amidxtaped_streams[i].name) != 0) {
2484 extra = vstrallocf(_("CONNECT token is \"%s\": expected \"%s\""),
2485 tok ? tok : "(null)",
2486 amidxtaped_streams[i].name);
2489 tok = strtok(NULL, " \n");
2490 if (tok == NULL || sscanf(tok, "%d", &ports[i]) != 1) {
2491 extra = vstrallocf(_("CONNECT %s token is \"%s\": expected a port number"),
2492 amidxtaped_streams[i].name,
2493 tok ? tok : "(null)");
2501 * OPTIONS [options string] '\n'
2503 if (strcmp(tok, "OPTIONS") == 0) {
2504 tok = strtok(NULL, "\n");
2506 extra = stralloc(_("OPTIONS token is missing"));
2510 while((p = strchr(tok, ';')) != NULL) {
2512 if(strncmp_const(tok, "features=") == 0) {
2513 tok += sizeof("features=") - 1;
2514 am_release_feature_set(their_features);
2515 if((their_features = am_string_to_feature(tok)) == NULL) {
2516 errstr = newvstralloc(errstr,
2517 _("OPTIONS: bad features value: "),
2529 extra = vstrallocf("next token is \"%s\": expected \"CONNECT\", \"ERROR\" or \"OPTIONS\""),
2530 tok ? tok : _("(null)"));
2536 * Connect the streams to their remote ports
2538 for (i = 0; i < NSTREAMS; i++) {
2541 amidxtaped_streams[i].fd = security_stream_client(sech, ports[i]);
2542 dbprintf(_("amidxtaped_streams[%d].fd = %p\n"),i, amidxtaped_streams[i].fd);
2543 if (amidxtaped_streams[i].fd == NULL) {
2544 errstr = newvstrallocf(errstr,\
2545 _("[could not connect %s stream: %s]"),
2546 amidxtaped_streams[i].name,
2547 security_geterror(sech));
2552 * Authenticate the streams
2554 for (i = 0; i < NSTREAMS; i++) {
2555 if (amidxtaped_streams[i].fd == NULL)
2557 if (security_stream_auth(amidxtaped_streams[i].fd) < 0) {
2558 errstr = newvstrallocf(errstr,
2559 _("[could not authenticate %s stream: %s]"),
2560 amidxtaped_streams[i].name,
2561 security_stream_geterror(amidxtaped_streams[i].fd));
2567 * The CTLFD and DATAFD streams are mandatory. If we didn't get
2570 if (amidxtaped_streams[CTLFD].fd == NULL) {
2571 errstr = newvstrallocf(errstr, _("[couldn't open CTL streams]"));
2574 if (amidxtaped_streams[DATAFD].fd == NULL) {
2575 errstr = newvstrallocf(errstr, _("[couldn't open DATA streams]"));
2579 /* everything worked */
2580 *response_error = 0;
2585 errstr = newvstrallocf(errstr,
2586 _("[parse of reply message failed: %s]"), extra);
2588 errstr = newvstrallocf(errstr,
2589 _("[parse of reply message failed: (no additional information)"));
2592 *response_error = 2;
2597 *response_error = 1;
2601 * This is called when everything needs to shut down so event_loop()
2605 stop_amidxtaped(void)
2609 for (i = 0; i < NSTREAMS; i++) {
2610 if (amidxtaped_streams[i].fd != NULL) {
2611 security_stream_close(amidxtaped_streams[i].fd);
2612 amidxtaped_streams[i].fd = NULL;
2617 static char* ctl_buffer = NULL;
2618 /* gets a "line" from server and put in server_line */
2619 /* server_line is terminated with \0, \r\n is striped */
2620 /* returns -1 if error */
2623 get_amidxtaped_line(void)
2629 amfree(amidxtaped_line);
2631 ctl_buffer = stralloc("");
2633 while (!strstr(ctl_buffer,"\r\n")) {
2634 if (amidxtaped_streams[CTLFD].fd == NULL)
2637 size = security_stream_read_sync(amidxtaped_streams[CTLFD].fd, &buf);
2641 else if(size == 0) {
2644 newbuf = alloc(strlen(ctl_buffer)+size+1);
2645 strncpy(newbuf, ctl_buffer, (size_t)(strlen(ctl_buffer) + size + 1));
2646 memcpy(newbuf+strlen(ctl_buffer), buf, (size_t)size);
2647 newbuf[strlen(ctl_buffer)+size] = '\0';
2649 ctl_buffer = newbuf;
2653 s = strstr(ctl_buffer,"\r\n");
2655 newbuf = stralloc(s+2);
2656 amidxtaped_line = stralloc(ctl_buffer);
2658 ctl_buffer = newbuf;
2664 read_amidxtaped_data(
2669 ctl_data_t *ctl_data = (ctl_data_t *)cookie;
2670 assert(cookie != NULL);
2673 errstr = newstralloc2(errstr, _("amidxtaped read: "),
2674 security_stream_geterror(amidxtaped_streams[DATAFD].fd));
2679 * EOF. Stop and return.
2682 security_stream_close(amidxtaped_streams[DATAFD].fd);
2683 amidxtaped_streams[DATAFD].fd = NULL;
2685 * If the mesg fd has also shut down, then we're done.
2690 assert(buf != NULL);
2692 if (ctl_data->header_done == 0) {
2693 GPtrArray *errarray;
2694 g_option_t g_options;
2695 data_path_t data_path_set = DATA_PATH_AMANDA;
2698 to_move = MIN(32768-header_size, size);
2699 memcpy(header_buf+header_size, buf, to_move);
2700 header_size += to_move;
2702 g_debug("read header %zd => %d", size, header_size);
2703 if (header_size < 32768) {
2704 security_stream_read(amidxtaped_streams[DATAFD].fd,
2705 read_amidxtaped_data, cookie);
2707 } else if (header_size > 32768) {
2708 error("header_size is %d\n", header_size);
2710 assert (to_move == size);
2711 /* parse the file header */
2712 fh_init(&ctl_data->file);
2713 parse_file_header(header_buf, &ctl_data->file, (size_t)header_size);
2715 /* call backup_support_option */
2716 g_options.config = get_config_name();
2717 g_options.hostname = dump_hostname;
2718 if (strcmp(ctl_data->file.program, "APPLICATION") == 0) {
2720 ctl_data->bsu = backup_support_option(ctl_data->file.application,
2722 ctl_data->file.disk,
2726 ctl_data->bsu = backup_support_option(ctl_data->file.application,
2728 ctl_data->file.disk, NULL,
2731 if (!ctl_data->bsu) {
2733 for (i=0; i < errarray->len; i++) {
2735 line = g_ptr_array_index(errarray, i);
2736 g_fprintf(stderr, "%s\n", line);
2738 g_ptr_array_free_full(errarray);
2741 data_path_set = ctl_data->bsu->data_path_set;
2743 /* handle backup_support_option failure */
2745 ctl_data->header_done = 1;
2746 if (!ask_file_overwrite(ctl_data)) {
2747 if (am_has_feature(tapesrv_features, fe_amidxtaped_abort)) {
2748 send_to_tape_server(amidxtaped_streams[CTLFD].fd, "ABORT");
2754 if (am_has_feature(tapesrv_features, fe_amidxtaped_datapath)) {
2756 /* send DATA-PATH request */
2757 msg = stralloc("AVAIL-DATAPATH");
2758 if (data_path_set & DATA_PATH_AMANDA)
2759 vstrextend(&msg, " AMANDA", NULL);
2760 if (data_path_set & DATA_PATH_DIRECTTCP)
2761 vstrextend(&msg, " DIRECT-TCP", NULL);
2762 send_to_tape_server(amidxtaped_streams[CTLFD].fd, msg);
2765 start_processing_data(ctl_data);
2768 ctl_data->bytes_read += size;
2769 /* Only the data is sent to the child */
2771 * We ignore errors while writing to the index file.
2773 (void)full_write(ctl_data->child_pipe[1], buf, (size_t)size);
2774 security_stream_read(amidxtaped_streams[DATAFD].fd,
2775 read_amidxtaped_data, cookie);
2781 ctl_data_t *ctl_data)
2783 char *restore_dir = NULL;
2785 if (ctl_data->file.dumplevel == 0) {
2786 property_t *property = g_hash_table_lookup(proplist, "directory");
2787 if (property && property->values && property->values->data) {
2788 /* take first property value */
2789 restore_dir = strdup(property->values->data);
2791 if (samba_extract_method == SAMBA_SMBCLIENT ||
2793 ctl_data->bsu->recover_path == RECOVER_PATH_REMOTE)) {
2795 restore_dir = g_strdup(ctl_data->file.disk);
2797 g_printf(_("Restoring files into target host %s\n"), restore_dir);
2800 restore_dir = g_get_current_dir();
2802 g_printf(_("Restoring files into directory %s\n"), restore_dir);
2805 /* Collect files to delete befause of a bug in gnutar */
2806 if (strcmp(ctl_data->file.program, "GNUTAR") == 0 ||
2807 (strcmp(ctl_data->file.program, "APPLICATION") == 0 &&
2808 strcmp(ctl_data->file.application, "amgtar") == 0)) {
2809 check_file_overwrite(restore_dir);
2811 g_printf(_("All existing files in %s can be deleted\n"),
2815 if (!okay_to_continue(0,0,0)) {
2817 amfree(restore_dir);
2822 /* delete the files for gnutar */
2824 if (!do_unlink_list()) {
2825 g_fprintf(stderr, _("Can't recover because I can't cleanup the restore directory (%s)\n"),
2828 amfree(restore_dir);
2833 amfree(restore_dir);
2839 start_processing_data(
2840 ctl_data_t *ctl_data)
2842 if (pipe(ctl_data->child_pipe) == -1) {
2843 error(_("extract_list - error setting up pipe to extractor: %s\n"),
2849 if (ctl_data->file.encrypted) {
2852 int errfd = fileno(stderr);
2854 g_debug("image is encrypted %s %s", ctl_data->file.clnt_encrypt, ctl_data->file.clnt_decrypt_opt);
2855 argv[0] = ctl_data->file.clnt_encrypt;
2856 argv[1] = ctl_data->file.clnt_decrypt_opt;
2858 pipespawnv(ctl_data->file.clnt_encrypt, STDOUT_PIPE, 0, &ctl_data->child_pipe[0], &crypt_out, &errfd, argv);
2859 ctl_data->child_pipe[0] = crypt_out;
2863 if (ctl_data->file.compressed) {
2866 int errfd = fileno(stderr);
2870 g_debug("image is compressed %s", ctl_data->file.clntcompprog);
2871 if (strlen(ctl_data->file.clntcompprog) > 0) {
2872 comp_prog = ctl_data->file.clntcompprog;
2875 comp_prog = UNCOMPRESS_PATH;
2876 comp_arg = UNCOMPRESS_OPT;
2878 argv[0] = comp_prog;
2881 pipespawnv(comp_prog, STDOUT_PIPE, 0, &ctl_data->child_pipe[0], &comp_out, &errfd, argv);
2882 ctl_data->child_pipe[0] = comp_out;
2885 /* okay, ready to extract. fork a child to do the actual work */
2886 if ((ctl_data->pid = fork()) == 0) {
2887 /* this is the child process */
2888 /* never gets out of this clause */
2889 aclose(ctl_data->child_pipe[1]);
2890 extract_files_child(ctl_data);
2894 if (ctl_data->pid == -1) {
2895 errstr = newstralloc(errstr, _("writer_intermediary - error forking child"));
2896 g_printf(_("writer_intermediary - error forking child"));
2899 aclose(ctl_data->child_pipe[0]);
2900 security_stream_read(amidxtaped_streams[DATAFD].fd, read_amidxtaped_data,
2902 if (am_has_feature(tapesrv_features, fe_amidxtaped_datapath)) {
2903 send_to_tape_server(amidxtaped_streams[CTLFD].fd, "DATAPATH-OK");