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;
82 security_stream_t *fd;
83 } amidxtaped_streams[] = {
89 #define NSTREAMS (int)(sizeof(amidxtaped_streams) / sizeof(amidxtaped_streams[0]))
92 static void amidxtaped_response(void *, pkt_t *, security_handle_t *);
93 static void stop_amidxtaped(void);
94 static char *dump_device_name = NULL;
96 static char *amidxtaped_line = NULL;
97 extern char *localhost;
99 /* global pid storage for interrupt handler */
100 pid_t extract_restore_child_pid = -1;
102 static EXTRACT_LIST *extract_list = NULL;
103 static const security_driver_t *amidxtaped_secdrv;
105 unsigned short samba_extract_method = SAMBA_TAR;
107 #define READ_TIMEOUT 240*60
109 EXTRACT_LIST *first_tape_list(void);
110 EXTRACT_LIST *next_tape_list(EXTRACT_LIST *list);
111 static int is_empty_dir(char *fname);
112 int is_extract_list_nonempty(void);
113 int length_of_tape_list(EXTRACT_LIST *tape_list);
114 void add_file(char *path, char *regex);
115 void add_glob(char *glob);
116 void add_regex(char *regex);
117 void clear_extract_list(void);
118 void clean_tape_list(EXTRACT_LIST *tape_list);
119 void clean_extract_list(void);
120 void check_file_overwrite(char *filename);
121 void delete_file(char *path, char *regex);
122 void delete_glob(char *glob);
123 void delete_regex(char *regex);
124 void delete_tape_list(EXTRACT_LIST *tape_list);
125 void display_extract_list(char *file);
126 void extract_files(void);
127 void read_file_header(char *buffer,
131 static int add_extract_item(DIR_ITEM *ditem);
132 static int delete_extract_item(DIR_ITEM *ditem);
133 static int extract_files_setup(char *label, off_t fsf);
134 static int okay_to_continue(int allow_tape,
137 static ssize_t read_buffer(int datafd,
141 static void clear_tape_list(EXTRACT_LIST *tape_list);
142 static void extract_files_child(ctl_data_t *ctl_data);
143 static void send_to_tape_server(security_stream_t *stream, char *cmd);
144 int writer_intermediary(EXTRACT_LIST *elist);
145 int get_amidxtaped_line(void);
146 static void read_amidxtaped_data(void *, void *, ssize_t);
147 static char *merge_path(char *path1, char *path2);
148 static gboolean ask_file_overwrite(ctl_data_t *ctl_data);
149 static void start_processing_data(ctl_data_t *ctl_data);
152 * Function: ssize_t read_buffer(datafd, buffer, buflen, timeout_s)
155 * read data from input file desciptor waiting up to timeout_s
156 * seconds before returning data.
159 * datafd - File descriptor to read from.
160 * buffer - Buffer to read into.
161 * buflen - Maximum number of bytes to read into buffer.
162 * timeout_s - Seconds to wait before returning what was already read.
165 * >0 - Number of data bytes in buffer.
167 * -1 - errno == ETIMEDOUT if no data available in specified time.
168 * errno == ENFILE if datafd is invalid.
169 * otherwise errno is set by select or read..
180 SELECT_ARG_TYPE readset;
181 struct timeval timeout;
186 if(datafd < 0 || datafd >= (int)FD_SETSIZE) {
187 errno = EMFILE; /* out of range */
192 spaceleft = (ssize_t)buflen;
196 FD_SET(datafd, &readset);
197 timeout.tv_sec = timeout_s;
199 nfound = select(datafd+1, &readset, NULL, NULL, &timeout);
201 /* Select returned an error. */
202 g_fprintf(stderr,_("select error: %s\n"), strerror(errno));
208 /* Select timed out. */
209 if (timeout_s != 0) {
210 /* Not polling: a real read timeout */
211 g_fprintf(stderr,_("timeout waiting for restore\n"));
212 g_fprintf(stderr,_("increase READ_TIMEOUT in recover-src/extract_list.c if your tape is slow\n"));
219 if(!FD_ISSET(datafd, &readset))
222 /* Select says data is available, so read it. */
223 size = read(datafd, dataptr, (size_t)spaceleft);
225 if ((errno == EINTR) || (errno == EAGAIN)) {
228 if (errno != EPIPE) {
229 g_fprintf(stderr, _("read_buffer: read error - %s"),
237 } while ((size > 0) && (spaceleft > 0));
239 return ((((ssize_t)buflen-spaceleft) > 0) ? ((ssize_t)buflen-spaceleft) : size);
244 first_tape_list(void)
251 /*@keep@*/EXTRACT_LIST *list)
260 EXTRACT_LIST * tape_list)
262 EXTRACT_LIST_ITEM *this, *next;
265 this = tape_list->files;
273 tape_list->files = NULL;
277 /* remove a tape list from the extract list, clearing the tape list
278 beforehand if necessary */
281 EXTRACT_LIST *tape_list)
283 EXTRACT_LIST *this, *prev;
285 if (tape_list == NULL)
288 /* is it first on the list? */
289 if (tape_list == extract_list)
291 extract_list = tape_list->next;
292 clear_tape_list(tape_list);
293 amfree(tape_list->date);
294 amfree(tape_list->tape);
299 /* so not first on list - find it and delete */
301 this = extract_list->next;
304 if (this == tape_list)
306 prev->next = tape_list->next;
307 clear_tape_list(tape_list);
308 amfree(tape_list->date);
309 amfree(tape_list->tape);
320 /* return the number of files on a tape's list */
323 EXTRACT_LIST *tape_list)
325 EXTRACT_LIST_ITEM *fn;
329 for (fn = tape_list->files; fn != NULL; fn = fn->next)
337 clear_extract_list(void)
339 while (extract_list != NULL)
340 delete_tape_list(extract_list);
346 EXTRACT_LIST *tape_list)
348 EXTRACT_LIST_ITEM *fn1, *pfn1, *ofn1;
349 EXTRACT_LIST_ITEM *fn2, *pfn2, *ofn2;
354 fn1 = tape_list->files;
355 while (fn1 != NULL) {
360 while (fn2 != NULL && remove_fn1 == 0) {
362 if(strcmp(fn1->path, fn2->path) == 0) {
364 } else if (strncmp(fn1->path, fn2->path, strlen(fn1->path)) == 0 &&
365 ((strlen(fn2->path) > strlen(fn1->path) &&
366 fn2->path[strlen(fn1->path)] == '/') ||
367 (fn1->path[strlen(fn1->path)-1] == '/'))) {
369 } else if (strncmp(fn2->path, fn1->path, strlen(fn2->path)) == 0 &&
370 ((strlen(fn1->path) > strlen(fn2->path) &&
371 fn1->path[strlen(fn2->path)] == '/') ||
372 (fn2->path[strlen(fn2->path)-1] == '/'))) {
378 dbprintf(_("removing path %s, it is included in %s\n"),
379 fn2->path, fn1->path);
385 } else if (remove_fn1 == 0) {
391 if(remove_fn1 != 0) {
392 /* fn2->path is always valid */
393 /*@i@*/ dbprintf(_("removing path %s, it is included in %s\n"),
394 /*@i@*/ fn1->path, fn2->path);
399 amfree(tape_list->files);
400 tape_list->files = fn1;
418 char *npath = g_path_get_basename(path);
419 *dir = g_path_get_dirname(path);
420 if (strcmp(*dir, ".") == 0) {
427 clean_extract_list(void)
431 for (this = extract_list; this != NULL; this = this->next)
432 clean_tape_list(this);
436 int add_to_unlink_list(char *path);
437 int do_unlink_list(void);
438 void free_unlink_list(void);
440 typedef struct s_unlink_list {
442 struct s_unlink_list *next;
444 t_unlink_list *unlink_list = NULL;
453 unlink_list = alloc(SIZEOF(*unlink_list));
454 unlink_list->path = stralloc(path);
455 unlink_list->next = NULL;
457 for (ul = unlink_list; ul != NULL; ul = ul->next) {
458 if (strcmp(ul->path, path) == 0)
461 ul = alloc(SIZEOF(*ul));
462 ul->path = stralloc(path);
463 ul->next = unlink_list;
475 for (ul = unlink_list; ul != NULL; ul = ul->next) {
476 if (unlink(ul->path) < 0) {
477 g_fprintf(stderr,_("Can't unlink %s: %s\n"), ul->path, strerror(errno));
486 free_unlink_list(void)
488 t_unlink_list *ul, *ul1;
490 for (ul = unlink_list; ul != NULL; ul = ul1) {
502 check_file_overwrite(
506 EXTRACT_LIST_ITEM *fn;
507 struct stat stat_buf;
511 for (this = extract_list; this != NULL; this = this->next) {
512 for (fn = this->files; fn != NULL ; fn = fn->next) {
514 /* Check path component of fn->path */
516 path = stralloc2(dir, fn->path);
517 if (path[strlen(path)-1] == '/') {
518 path[strlen(path)-1] = '\0';
521 s = path + strlen(dir) + 1;
522 while((s = strchr(s, '/'))) {
524 if (lstat(path, &stat_buf) == 0) {
525 if(!S_ISDIR(stat_buf.st_mode)) {
526 if (add_to_unlink_list(path)) {
527 g_printf(_("WARNING: %s is not a directory, "
528 "it will be deleted.\n"),
533 else if (errno != ENOENT) {
534 g_printf(_("Can't stat %s: %s\n"), path, strerror(errno));
543 filename = stralloc2(dir, fn->path);
544 if (filename[strlen(filename)-1] == '/') {
545 filename[strlen(filename)-1] = '\0';
548 if (lstat(filename, &stat_buf) == 0) {
549 if(S_ISDIR(stat_buf.st_mode)) {
550 if(!is_empty_dir(filename)) {
551 g_printf(_("WARNING: All existing files in %s "
552 "will be deleted.\n"), filename);
554 } else if(S_ISREG(stat_buf.st_mode)) {
555 g_printf(_("WARNING: Existing file %s will be overwritten\n"),
558 if (add_to_unlink_list(filename)) {
559 g_printf(_("WARNING: Existing entry %s will be deleted\n"),
563 } else if (errno != ENOENT) {
564 g_printf(_("Can't stat %s: %s\n"), filename, strerror(errno));
572 /* returns -1 if error */
573 /* returns 0 on succes */
574 /* returns 1 if already added */
579 EXTRACT_LIST *this, *this1;
580 EXTRACT_LIST_ITEM *that, *curr;
581 char *ditem_path = NULL;
583 ditem_path = stralloc(ditem->path);
584 clean_pathname(ditem_path);
586 for (this = extract_list; this != NULL; this = this->next)
588 /* see if this is the list for the tape */
589 if (this->level == ditem->level && strcmp(this->tape, ditem->tape) == 0)
591 /* yes, so add to list */
595 if (strcmp(curr->path,ditem_path) == 0) {
601 that = (EXTRACT_LIST_ITEM *)alloc(sizeof(EXTRACT_LIST_ITEM));
602 that->path = stralloc(ditem_path);
603 that->next = this->files;
604 this->files = that; /* add at front since easiest */
610 /* so this is the first time we have seen this tape */
611 this = (EXTRACT_LIST *)alloc(sizeof(EXTRACT_LIST));
612 this->tape = stralloc(ditem->tape);
613 this->level = ditem->level;
614 this->fileno = ditem->fileno;
615 this->date = stralloc(ditem->date);
616 that = (EXTRACT_LIST_ITEM *)alloc(sizeof(EXTRACT_LIST_ITEM));
617 that->path = stralloc(ditem_path);
621 /* add this in date increasing order */
622 /* because restore must be done in this order */
623 /* add at begining */
624 if(extract_list==NULL || strcmp(this->date,extract_list->date) < 0)
626 this->next = extract_list;
631 for (this1 = extract_list; this1->next != NULL; this1 = this1->next)
633 /* add in the middle */
634 if(strcmp(this->date,this1->next->date) < 0)
636 this->next = this1->next;
650 /* returns -1 if error */
651 /* returns 0 on deletion */
652 /* returns 1 if not there */
658 EXTRACT_LIST_ITEM *that, *prev;
659 char *ditem_path = NULL;
661 ditem_path = stralloc(ditem->path);
662 clean_pathname(ditem_path);
664 for (this = extract_list; this != NULL; this = this->next)
666 /* see if this is the list for the tape */
667 if (this->level == ditem->level && strcmp(this->tape, ditem->tape) == 0)
669 /* yes, so find file on list */
671 if (strcmp(that->path, ditem_path) == 0)
674 this->files = that->next;
677 /* if list empty delete it */
678 if (this->files == NULL)
679 delete_tape_list(this);
687 if (strcmp(that->path, ditem_path) == 0)
689 prev->next = that->next;
713 int len = strlen(path1);
714 if (path1[len-1] == '/' && path2[0] == '/') {
715 result = stralloc2(path1, path2+1);
716 } else if (path1[len-1] != '/' && path2[0] != '/') {
717 result = vstralloc(path1, "/", path2, NULL);
719 result = stralloc2(path1, path2);
736 if (disk_path == NULL) {
737 g_printf(_("Must select directory before adding files\n"));
741 uqglob = unquote_string(glob);
742 glob = file_of_path(uqglob, &dir);
744 sdir = merge_path(mount_point, disk_path);
745 result = cd_glob(dir, 0);
749 regex = glob_to_regex(glob);
750 dbprintf(_("add_glob (%s) -> %s\n"), uqglob, regex);
751 if ((s = validate_regexp(regex)) != NULL) {
752 g_printf(_("%s is not a valid shell wildcard pattern: "), glob);
756 * glob_to_regex() anchors the beginning of the pattern with ^,
757 * but we will be tacking it onto the end of the current directory
758 * in add_file, so strip that off. Also, it anchors the end with
759 * $, but we need to match an optional trailing /, so tack that on
762 regex_path = stralloc(regex + 1);
763 regex_path[strlen(regex_path) - 1] = '\0';
764 strappend(regex_path, "[/]*$");
765 add_file(uqglob, regex_path);
769 set_directory(sdir, 0);
789 if (disk_path == NULL) {
790 g_printf(_("Must select directory before adding files\n"));
794 uqregex = unquote_string(regex);
795 newregex = file_of_path(uqregex, &dir);
797 sdir = merge_path(mount_point, disk_path);
798 result = cd_regex(dir, 0);
803 if ((s = validate_regexp(newregex)) != NULL) {
804 g_printf(_("\"%s\" is not a valid regular expression: "), newregex);
807 add_file(uqregex, newregex);
810 set_directory(sdir, 0);
823 DIR_ITEM *ditem, lditem;
824 char *path_on_disk = NULL;
829 char *dir, *dir_undo, dir_undo_ch = '\0';
830 char *ditem_path = NULL;
831 char *qditem_path = NULL;
834 char *s, *fp, *quoted;
839 if (disk_path == NULL) {
840 g_printf(_("Must select directory before adding files\n"));
843 memset(&lditem, 0, sizeof(lditem)); /* Prevent use of bogus data... */
845 dbprintf(_("add_file: Looking for \"%s\"\n"), regex);
847 if(strcmp(regex, "/[/]*$") == 0) { /* "/" behave like "." */
850 else if(strcmp(regex, "[^/]*[/]*$") == 0) { /* "*" */
851 regex = "([^/.]|\\.[^/]+|[^/.][^/]*)[/]*$";
853 /* remove "/" at end of path */
854 j = (ssize_t)(strlen(regex) - 1);
855 while(j >= 0 && regex[j] == '/')
859 /* convert path (assumed in cwd) to one on disk */
860 if (strcmp(disk_path, "/") == 0) {
862 /* No mods needed if already starts with '/' */
863 path_on_disk = stralloc(regex);
866 path_on_disk = stralloc2("/", regex);
869 char *clean_disk_path = clean_regex(disk_path, 0);
870 path_on_disk = vstralloc(clean_disk_path, "/", regex, NULL);
871 amfree(clean_disk_path);
874 dbprintf(_("add_file: Converted path=\"%s\" to path_on_disk=\"%s\"\n"),
875 regex, path_on_disk);
879 for (ditem=get_dir_list(); ditem!=NULL; ditem=get_next_dir_item(ditem))
882 quoted = quote_string(ditem->path);
883 dbprintf(_("add_file: Pondering ditem->path=%s\n"), quoted);
885 if (match(path_on_disk, ditem->path))
888 j = (ssize_t)strlen(ditem->path);
889 if((j > 0 && ditem->path[j-1] == '/')
890 || (j > 1 && ditem->path[j-2] == '/' && ditem->path[j-1] == '.'))
891 { /* It is a directory */
892 ditem_path = newstralloc(ditem_path, ditem->path);
893 clean_pathname(ditem_path);
895 qditem_path = quote_string(ditem_path);
896 cmd = newstralloc2(cmd, "ORLD ", qditem_path);
898 if(send_command(cmd) == -1) {
901 amfree(path_on_disk);
907 if ((i = get_reply_line()) == -1) {
909 amfree(path_on_disk);
912 if(i==0) { /* assume something wrong */
914 amfree(path_on_disk);
921 lditem.path = newstralloc(lditem.path, ditem->path);
922 /* skip the last line -- duplicate of the preamble */
924 while ((i = get_reply_line()) != 0) {
927 amfree(path_on_disk);
932 if(dir_undo) *dir_undo = dir_undo_ch;
934 cmd = stralloc(l); /* save for error report */
936 continue; /* throw the rest of the lines away */
939 if (!server_happy()) {
945 if(strncmp_const_skip(l, "201-", s, ch) != 0) {
946 err = _("bad reply: not 201-");
951 skip_whitespace(s, ch);
953 err = _("bad reply: missing date field");
957 skip_non_whitespace(s, ch);
959 lditem.date = newstralloc(lditem.date, fp);
962 skip_whitespace(s, ch);
963 if(ch == '\0' || sscanf(s - 1, "%d", &lditem.level) != 1) {
964 err = _("bad reply: cannot parse level field");
969 skip_whitespace(s, ch);
971 err = _("bad reply: missing tape field");
975 skip_non_whitespace(s, ch);
977 lditem.tape = newstralloc(lditem.tape, fp);
980 if(am_has_feature(indexsrv_features, fe_amindexd_fileno_in_ORLD)) {
981 long long fileno_ = (long long)0;
982 skip_whitespace(s, ch);
984 sscanf(s - 1, "%lld", &fileno_) != 1) {
985 err = _("bad reply: cannot parse fileno field");
988 lditem.fileno = (off_t)fileno_;
992 skip_whitespace(s, ch);
994 err = _("bad reply: missing directory field");
998 skip_quoted_string(s, ch);
1000 dir_undo_ch = *dir_undo;
1003 switch(add_extract_item(&lditem)) {
1005 g_printf(_("System error\n"));
1006 dbprintf(_("add_file: (Failed) System error\n"));
1010 quoted = quote_string(lditem.path);
1011 g_printf(_("Added dir %s at date %s\n"),
1012 quoted, lditem.date);
1013 dbprintf(_("add_file: (Successful) Added dir %s at date %s\n"),
1014 quoted, lditem.date);
1023 if(!server_happy()) {
1030 } else if(added == 0) {
1031 quoted = quote_string(ditem_path);
1032 g_printf(_("dir %s already added\n"), quoted);
1033 dbprintf(_("add_file: dir %s already added\n"), quoted);
1037 else /* It is a file */
1039 switch(add_extract_item(ditem)) {
1041 g_printf(_("System error\n"));
1042 dbprintf(_("add_file: (Failed) System error\n"));
1046 quoted = quote_string(ditem->path);
1047 g_printf(_("Added file %s\n"), quoted);
1048 dbprintf(_("add_file: (Successful) Added %s\n"), quoted);
1053 quoted = quote_string(ditem->path);
1054 g_printf(_("File %s already added\n"), quoted);
1055 dbprintf(_("add_file: file %s already added\n"), quoted);
1064 amfree(path_on_disk);
1066 amfree(lditem.path);
1067 amfree(lditem.date);
1068 amfree(lditem.tape);
1071 quoted = quote_string(path);
1072 g_printf(_("File %s doesn't exist in directory\n"), quoted);
1073 dbprintf(_("add_file: (Failed) File %s doesn't exist in directory\n"),
1093 if (disk_path == NULL) {
1094 g_printf(_("Must select directory before adding files\n"));
1098 uqglob = unquote_string(glob);
1099 newglob = file_of_path(uqglob, &dir);
1101 sdir = merge_path(mount_point, disk_path);
1102 result = cd_glob(dir, 0);
1106 regex = glob_to_regex(newglob);
1107 dbprintf(_("delete_glob (%s) -> %s\n"), newglob, regex);
1108 if ((s = validate_regexp(regex)) != NULL) {
1109 g_printf(_("\"%s\" is not a valid shell wildcard pattern: "),
1114 * glob_to_regex() anchors the beginning of the pattern with ^,
1115 * but we will be tacking it onto the end of the current directory
1116 * in add_file, so strip that off. Also, it anchors the end with
1117 * $, but we need to match an optional trailing /, so tack that on
1120 regex_path = stralloc(regex + 1);
1121 regex_path[strlen(regex_path) - 1] = '\0';
1122 strappend(regex_path, "[/]*$");
1123 delete_file(uqglob, regex_path);
1127 set_directory(sdir, 0);
1147 if (disk_path == NULL) {
1148 g_printf(_("Must select directory before adding files\n"));
1152 uqregex = unquote_string(regex);
1153 newregex = file_of_path(uqregex, &dir);
1155 sdir = merge_path(mount_point, disk_path);
1156 result = cd_regex(dir, 0);
1161 if ((s = validate_regexp(newregex)) != NULL) {
1162 g_printf(_("\"%s\" is not a valid regular expression: "), newregex);
1165 delete_file(newregex, regex);
1168 set_directory(sdir, 0);
1181 DIR_ITEM *ditem, lditem;
1182 char *path_on_disk = NULL;
1188 char *tape, *tape_undo, tape_undo_ch = '\0';
1189 char *dir_undo, dir_undo_ch = '\0';
1192 char *ditem_path = NULL;
1201 if (disk_path == NULL) {
1202 g_printf(_("Must select directory before deleting files\n"));
1205 memset(&lditem, 0, sizeof(lditem)); /* Prevent use of bogus data... */
1207 dbprintf(_("delete_file: Looking for \"%s\"\n"), path);
1209 if (strcmp(regex, "[^/]*[/]*$") == 0) {
1210 /* Looking for * find everything but single . */
1211 regex = "([^/.]|\\.[^/]+|[^/.][^/]*)[/]*$";
1213 /* remove "/" at end of path */
1214 j = (ssize_t)(strlen(regex) - 1);
1215 while(j >= 0 && regex[j] == '/') regex[j--] = '\0';
1218 /* convert path (assumed in cwd) to one on disk */
1219 if (strcmp(disk_path, "/") == 0) {
1220 if (*regex == '/') {
1221 if (strcmp(regex, "/[/]*$") == 0) {
1222 /* We want "/" to match the directory itself: "/." */
1223 path_on_disk = stralloc("/\\.[/]*$");
1225 /* No mods needed if already starts with '/' */
1226 path_on_disk = stralloc(regex);
1230 path_on_disk = stralloc2("/", regex);
1233 char *clean_disk_path = clean_regex(disk_path, 0);
1234 path_on_disk = vstralloc(clean_disk_path, "/", regex, NULL);
1235 amfree(clean_disk_path);
1238 dbprintf(_("delete_file: Converted path=\"%s\" to path_on_disk=\"%s\"\n"),
1239 regex, path_on_disk);
1241 for (ditem=get_dir_list(); ditem!=NULL; ditem=get_next_dir_item(ditem))
1243 quoted = quote_string(ditem->path);
1244 dbprintf(_("delete_file: Pondering ditem->path=%s\n"), quoted);
1246 if (match(path_on_disk, ditem->path))
1249 j = (ssize_t)strlen(ditem->path);
1250 if((j > 0 && ditem->path[j-1] == '/')
1251 || (j > 1 && ditem->path[j-2] == '/' && ditem->path[j-1] == '.'))
1252 { /* It is a directory */
1253 ditem_path = newstralloc(ditem_path, ditem->path);
1254 clean_pathname(ditem_path);
1256 qditem_path = quote_string(ditem_path);
1257 cmd = newstralloc2(cmd, "ORLD ", qditem_path);
1258 amfree(qditem_path);
1259 if(send_command(cmd) == -1) {
1262 amfree(path_on_disk);
1267 if ((i = get_reply_line()) == -1) {
1269 amfree(path_on_disk);
1272 if(i==0) /* assume something wrong */
1275 amfree(path_on_disk);
1277 g_printf("%s\n", l);
1281 lditem.path = newstralloc(lditem.path, ditem->path);
1283 tape_undo = dir_undo = NULL;
1284 /* skip the last line -- duplicate of the preamble */
1285 while ((i = get_reply_line()) != 0)
1289 amfree(path_on_disk);
1294 if(tape_undo) *tape_undo = tape_undo_ch;
1295 if(dir_undo) *dir_undo = dir_undo_ch;
1296 tape_undo = dir_undo = NULL;
1297 cmd = stralloc(l); /* save for the error report */
1299 continue; /* throw the rest of the lines away */
1302 if (!server_happy()) {
1308 if(strncmp_const_skip(l, "201-", s, ch) != 0) {
1309 err = _("bad reply: not 201-");
1314 skip_whitespace(s, ch);
1316 err = _("bad reply: missing date field");
1320 skip_non_whitespace(s, ch);
1323 skip_whitespace(s, ch);
1324 if(ch == '\0' || sscanf(s - 1, "%d", &level) != 1) {
1325 err = _("bad reply: cannot parse level field");
1328 skip_integer(s, ch);
1330 skip_whitespace(s, ch);
1332 err = _("bad reply: missing tape field");
1336 skip_non_whitespace(s, ch);
1338 tape_undo_ch = *tape_undo;
1341 if(am_has_feature(indexsrv_features, fe_amindexd_fileno_in_ORLD)) {
1342 long long fileno_ = (long long)0;
1343 skip_whitespace(s, ch);
1345 sscanf(s - 1, "%lld", &fileno_) != 1) {
1346 err = _("bad reply: cannot parse fileno field");
1349 fileno = (off_t)fileno_;
1350 skip_integer(s, ch);
1353 skip_whitespace(s, ch);
1355 err = _("bad reply: missing directory field");
1358 skip_non_whitespace(s, ch);
1360 dir_undo_ch = *dir_undo;
1363 lditem.date = newstralloc(lditem.date, date);
1365 lditem.tape = newstralloc(lditem.tape, tape);
1366 switch(delete_extract_item(&lditem)) {
1368 g_printf(_("System error\n"));
1369 dbprintf(_("delete_file: (Failed) System error\n"));
1372 g_printf(_("Deleted dir %s at date %s\n"), ditem_path, date);
1373 dbprintf(_("delete_file: (Successful) Deleted dir %s at date %s\n"),
1381 if(!server_happy()) {
1388 } else if(deleted == 0) {
1389 g_printf(_("Warning - dir '%s' not on tape list\n"),
1391 dbprintf(_("delete_file: dir '%s' not on tape list\n"),
1397 switch(delete_extract_item(ditem)) {
1399 g_printf(_("System error\n"));
1400 dbprintf(_("delete_file: (Failed) System error\n"));
1403 g_printf(_("Deleted %s\n"), ditem->path);
1404 dbprintf(_("delete_file: (Successful) Deleted %s\n"),
1408 g_printf(_("Warning - file '%s' not on tape list\n"),
1410 dbprintf(_("delete_file: file '%s' not on tape list\n"),
1419 amfree(path_on_disk);
1422 g_printf(_("File %s doesn't exist in directory\n"), path);
1423 dbprintf(_("delete_file: (Failed) File %s doesn't exist in directory\n"),
1429 /* print extract list into file. If NULL ptr passed print to screen */
1431 display_extract_list(
1435 EXTRACT_LIST_ITEM *that;
1438 char *pager_command;
1443 if ((pager = getenv("PAGER")) == NULL)
1448 * Set up the pager command so if the pager is terminated, we do
1449 * not get a SIGPIPE back.
1451 pager_command = stralloc2(pager, " ; /bin/cat > /dev/null");
1452 if ((fp = popen(pager_command, "w")) == NULL)
1454 g_printf(_("Warning - can't pipe through %s\n"), pager);
1457 amfree(pager_command);
1461 uqfile = unquote_string(file);
1462 if ((fp = fopen(uqfile, "w")) == NULL)
1464 g_printf(_("Can't open file %s to print extract list into\n"), file);
1471 for (this = extract_list; this != NULL; this = this->next)
1473 g_fprintf(fp, _("TAPE %s LEVEL %d DATE %s\n"),
1474 this->tape, this->level, this->date);
1475 for (that = this->files; that != NULL; that = that->next)
1476 g_fprintf(fp, "\t%s\n", that->path);
1482 g_printf(_("Extract list written to file %s\n"), file);
1493 struct dirent *entry;
1496 if((dir = opendir(fname)) == NULL)
1500 while(!gotentry && (entry = readdir(dir)) != NULL) {
1501 gotentry = !is_dot_or_dotdot(entry->d_name);
1509 /* returns 0 if extract list empty and 1 if it isn't */
1511 is_extract_list_nonempty(void)
1513 return (extract_list != NULL);
1517 /* prints continue prompt and waits for response,
1518 returns 0 if don't, non-0 if do */
1535 prompt = _("New device name [?]: ");
1536 } else if (allow_tape && allow_skip) {
1537 prompt = _("Continue [?/Y/n/s/d]? ");
1538 } else if (allow_tape && !allow_skip) {
1539 prompt = _("Continue [?/Y/n/d]? ");
1540 } else if (allow_retry) {
1541 prompt = _("Continue [?/Y/n/r]? ");
1543 prompt = _("Continue [?/Y/n]? ");
1545 fputs(prompt, stdout);
1546 fflush(stdout); fflush(stderr);
1548 if ((line = agets(stdin)) == NULL) {
1558 dbprintf("User prompt: '%s'; response: '%s'\n", prompt, line);
1561 while ((ch = *s++) != '\0' && g_ascii_isspace(ch)) {
1562 (void)ch; /* Quiet empty loop compiler warning */
1566 g_printf(_("Enter a new device name or \"default\"\n"));
1568 g_printf(_("Enter \"y\"es to continue, \"n\"o to stop"));
1570 g_printf(_(", \"s\"kip this tape"));
1573 g_printf(_(" or \"r\"etry this tape"));
1576 g_printf(_(" or \"d\" to change to a new device"));
1580 } else if (get_device) {
1581 char *tmp = stralloc(tape_server_name);
1583 if (strncmp_const(s - 1, "default") == 0) {
1584 set_device(tmp, NULL); /* default device, existing host */
1585 } else if (s[-1] != '\0') {
1586 set_device(tmp, s - 1); /* specified device, existing host */
1588 g_printf(_("No change.\n"));
1594 } else if (ch == '\0' || ch == 'Y' || ch == 'y') {
1596 } else if (allow_tape && (ch == 'D' || ch == 'd' || ch == 'T' || ch == 't')) {
1597 get_device = 1; /* ('T' and 't' are for backward-compatibility) */
1598 } else if (ch == 'N' || ch == 'n') {
1600 } else if (allow_retry && (ch == 'R' || ch == 'r')) {
1602 } else if (allow_skip && (ch == 'S' || ch == 's')) {
1613 send_to_tape_server(
1614 security_stream_t * stream,
1617 char *msg = stralloc2(cmd, "\r\n");
1619 g_debug("send_to_tape_server: %s\n", cmd);
1620 if (security_stream_write(stream, msg, strlen(msg)) < 0)
1622 error(_("Error writing to tape server"));
1630 /* start up connection to tape server and set commands to initiate
1631 transfer of dump image.
1632 Return tape server socket on success, -1 on error. */
1634 extract_files_setup(
1638 char *disk_regex = NULL;
1639 char *host_regex = NULL;
1640 char *clean_datestamp, *ch, *ch1;
1645 amidxtaped_secdrv = security_getdriver(authopt);
1646 if (amidxtaped_secdrv == NULL) {
1647 error(_("no '%s' security driver available for host '%s'"),
1648 authopt, tape_server_name);
1651 /* We assume that amidxtaped support fe_amidxtaped_options_features */
1652 /* and fe_amidxtaped_options_auth */
1653 /* We should send a noop to really know */
1654 req = vstralloc("SERVICE amidxtaped\n",
1655 "OPTIONS ", "features=", our_features_string, ";",
1656 "auth=", authopt, ";",
1658 protocol_sendreq(tape_server_name, amidxtaped_secdrv,
1659 generic_client_get_security_conf, req, STARTUP_TIMEOUT,
1660 amidxtaped_response, &response_error);
1663 if(response_error != 0) {
1667 disk_regex = make_exact_disk_expression(disk_name);
1668 host_regex = make_exact_host_expression(dump_hostname);
1670 clean_datestamp = stralloc(dump_datestamp);
1671 for(ch=ch1=clean_datestamp;*ch1 != '\0';ch1++) {
1678 /* push our feature list off to the tape server */
1679 /* XXX assumes that index server and tape server are equivalent, ew */
1681 if(am_has_feature(indexsrv_features, fe_amidxtaped_exchange_features)){
1682 tt = newstralloc2(tt, "FEATURES=", our_features_string);
1683 send_to_tape_server(amidxtaped_streams[CTLFD].fd, tt);
1684 get_amidxtaped_line();
1685 if(strncmp_const(amidxtaped_line,"FEATURES=") == 0) {
1686 tapesrv_features = am_string_to_feature(amidxtaped_line+9);
1688 g_fprintf(stderr, _("amrecover - expecting FEATURES line from amidxtaped\n"));
1692 amfree(clean_datestamp);
1696 *tapesrv_features = *indexsrv_features;
1700 if(am_has_feature(indexsrv_features, fe_amidxtaped_header) &&
1701 am_has_feature(indexsrv_features, fe_amidxtaped_device) &&
1702 am_has_feature(indexsrv_features, fe_amidxtaped_host) &&
1703 am_has_feature(indexsrv_features, fe_amidxtaped_disk) &&
1704 am_has_feature(indexsrv_features, fe_amidxtaped_datestamp)) {
1706 if(am_has_feature(indexsrv_features, fe_amidxtaped_config)) {
1707 tt = newstralloc2(tt, "CONFIG=", get_config_name());
1708 send_to_tape_server(amidxtaped_streams[CTLFD].fd, tt);
1710 if(am_has_feature(indexsrv_features, fe_amidxtaped_label) &&
1711 label && label[0] != '/') {
1712 tt = newstralloc2(tt,"LABEL=",label);
1713 send_to_tape_server(amidxtaped_streams[CTLFD].fd, tt);
1715 if(am_has_feature(indexsrv_features, fe_amidxtaped_fsf)) {
1717 g_snprintf(v_fsf, 99, "%lld", (long long)fsf);
1718 tt = newstralloc2(tt, "FSF=",v_fsf);
1719 send_to_tape_server(amidxtaped_streams[CTLFD].fd, tt);
1721 send_to_tape_server(amidxtaped_streams[CTLFD].fd, "HEADER");
1722 tt = newstralloc2(tt, "DEVICE=", dump_device_name);
1723 send_to_tape_server(amidxtaped_streams[CTLFD].fd, tt);
1724 tt = newstralloc2(tt, "HOST=", host_regex);
1725 send_to_tape_server(amidxtaped_streams[CTLFD].fd, tt);
1726 tt = newstralloc2(tt, "DISK=", disk_regex);
1727 send_to_tape_server(amidxtaped_streams[CTLFD].fd, tt);
1728 tt = newstralloc2(tt, "DATESTAMP=", clean_datestamp);
1729 send_to_tape_server(amidxtaped_streams[CTLFD].fd, tt);
1730 send_to_tape_server(amidxtaped_streams[CTLFD].fd, "END");
1733 else if(am_has_feature(indexsrv_features, fe_amidxtaped_nargs)) {
1734 /* send to the tape server what tape file we want */
1743 send_to_tape_server(amidxtaped_streams[CTLFD].fd, "6");
1744 send_to_tape_server(amidxtaped_streams[CTLFD].fd, "-h");
1745 send_to_tape_server(amidxtaped_streams[CTLFD].fd, "-p");
1746 send_to_tape_server(amidxtaped_streams[CTLFD].fd, dump_device_name);
1747 send_to_tape_server(amidxtaped_streams[CTLFD].fd, host_regex);
1748 send_to_tape_server(amidxtaped_streams[CTLFD].fd, disk_regex);
1749 send_to_tape_server(amidxtaped_streams[CTLFD].fd, clean_datestamp);
1751 dbprintf(_("Started amidxtaped with arguments \"6 -h -p %s %s %s %s\"\n"),
1752 dump_device_name, host_regex, disk_regex, clean_datestamp);
1757 amfree(clean_datestamp);
1764 * Reads the first block of a tape file.
1775 bytes_read = read_buffer(tapedev, buffer, buflen, READ_TIMEOUT);
1776 if(bytes_read < 0) {
1777 error(_("error reading header (%s), check amidxtaped.*.debug on server"),
1782 if((size_t)bytes_read < buflen) {
1783 g_fprintf(stderr, plural(_("%s: short block %d byte\n"),
1784 _("%s: short block %d bytes\n"), bytes_read),
1785 get_pname(), (int)bytes_read);
1786 print_header(stdout, file);
1787 error(_("Can't read file header"));
1791 /* bytes_read == buflen */
1792 parse_file_header(buffer, file, (size_t)bytes_read);
1806 extract_files_child(
1807 ctl_data_t *ctl_data)
1812 GPtrArray *argv_ptr = g_ptr_array_new();
1814 EXTRACT_LIST_ITEM *fn;
1815 enum dumptypes dumptype = IS_UNKNOWN;
1818 guint passwd_field = 999999999;
1820 char *domain = NULL, *smbpass = NULL;
1823 /* code executed by child to do extraction */
1826 /* make in_fd be our stdin */
1827 if (dup2(ctl_data->child_pipe[0], STDIN_FILENO) == -1)
1829 error(_("dup2 failed in extract_files_child: %s"), strerror(errno));
1833 if(ctl_data->file.type != F_DUMPFILE) {
1834 dump_dumpfile_t(&ctl_data->file);
1835 error(_("bad header"));
1839 if (ctl_data->file.program != NULL) {
1840 if (strcmp(ctl_data->file.program, "APPLICATION") == 0)
1841 dumptype = IS_APPLICATION_API;
1843 if (strcmp(ctl_data->file.program, GNUTAR) == 0)
1844 dumptype = IS_GNUTAR;
1847 if (dumptype == IS_UNKNOWN) {
1848 len_program = strlen(ctl_data->file.program);
1849 if(len_program >= 3 &&
1850 strcmp(&ctl_data->file.program[len_program-3],"tar") == 0)
1855 if (dumptype == IS_UNKNOWN && strcmp(ctl_data->file.program, SAMBA_CLIENT) ==0) {
1856 if (samba_extract_method == SAMBA_TAR)
1857 dumptype = IS_SAMBA_TAR;
1859 dumptype = IS_SAMBA;
1864 /* form the arguments to restore */
1865 files_off_tape = length_of_tape_list(ctl_data->elist);
1869 g_ptr_array_add(argv_ptr, stralloc("smbclient"));
1870 smbpass = findpass(ctl_data->file.disk, &domain);
1872 g_ptr_array_add(argv_ptr, stralloc(ctl_data->file.disk));
1873 g_ptr_array_add(argv_ptr, stralloc("-U"));
1874 passwd_field = argv_ptr->len;
1875 g_ptr_array_add(argv_ptr, stralloc(smbpass));
1877 g_ptr_array_add(argv_ptr, stralloc("-W"));
1878 g_ptr_array_add(argv_ptr, stralloc(domain));
1881 g_ptr_array_add(argv_ptr, stralloc("-d0"));
1882 g_ptr_array_add(argv_ptr, stralloc("-Tx"));
1883 g_ptr_array_add(argv_ptr, stralloc("-")); /* data on stdin */
1888 g_ptr_array_add(argv_ptr, stralloc("tar"));
1889 g_ptr_array_add(argv_ptr, stralloc("--numeric-owner"));
1890 g_ptr_array_add(argv_ptr, stralloc("-xpGvf"));
1891 g_ptr_array_add(argv_ptr, stralloc("-")); /* data on stdin */
1894 g_ptr_array_add(argv_ptr, stralloc("tar"));
1895 g_ptr_array_add(argv_ptr, stralloc("-xpvf"));
1896 g_ptr_array_add(argv_ptr, stralloc("-")); /* data on stdin */
1900 g_ptr_array_add(argv_ptr, stralloc("restore"));
1902 restore_args[j++] = stralloc("-xB");
1903 g_ptr_array_add(argv_ptr, stralloc("-xB"));
1905 #if defined(XFSDUMP)
1906 if (strcmp(ctl_data->file.program, XFSDUMP) == 0) {
1907 g_ptr_array_add(argv_ptr, stralloc("-v"));
1908 g_ptr_array_add(argv_ptr, stralloc("silent"));
1912 if (strcmp(ctl_data->file.program, VDUMP) == 0) {
1913 g_ptr_array_add(argv_ptr, stralloc("xf"));
1914 g_ptr_array_add(argv_ptr, stralloc("-")); /* data on stdin */
1918 g_ptr_array_add(argv_ptr, stralloc("xbf"));
1919 g_ptr_array_add(argv_ptr, stralloc("2")); /* read in units of 1K */
1920 g_ptr_array_add(argv_ptr, stralloc("-")); /* data on stdin */
1924 case IS_APPLICATION_API:
1925 g_ptr_array_add(argv_ptr, stralloc(ctl_data->file.application));
1926 g_ptr_array_add(argv_ptr, stralloc("restore"));
1927 g_ptr_array_add(argv_ptr, stralloc("--config"));
1928 g_ptr_array_add(argv_ptr, stralloc(get_config_name()));
1929 g_ptr_array_add(argv_ptr, stralloc("--disk"));
1930 g_ptr_array_add(argv_ptr, stralloc(ctl_data->file.disk));
1931 if (dump_dle && dump_dle->device) {
1932 g_ptr_array_add(argv_ptr, stralloc("--device"));
1933 g_ptr_array_add(argv_ptr, stralloc(dump_dle->device));
1935 if (ctl_data->data_path == DATA_PATH_DIRECTTCP) {
1936 g_ptr_array_add(argv_ptr, stralloc("--data-path"));
1937 g_ptr_array_add(argv_ptr, stralloc("DIRECTTCP"));
1938 g_ptr_array_add(argv_ptr, stralloc("--direct-tcp"));
1939 g_ptr_array_add(argv_ptr, stralloc(ctl_data->addrs));
1941 if (ctl_data->bsu && ctl_data->bsu->smb_recover_mode &&
1942 samba_extract_method == SAMBA_SMBCLIENT){
1943 g_ptr_array_add(argv_ptr, stralloc("--recover-mode"));
1944 g_ptr_array_add(argv_ptr, stralloc("smb"));
1946 g_ptr_array_add(argv_ptr, stralloc("--level"));
1947 g_ptr_array_add(argv_ptr, g_strdup_printf("%d", ctl_data->elist->level));
1952 merge_properties(dump_dle->application_property, proplist);
1953 application_property_add_to_argv(argv_ptr, dump_dle, NULL,
1955 for (scriptlist = dump_dle->scriptlist; scriptlist != NULL;
1956 scriptlist = scriptlist->next) {
1957 script = (script_t *)scriptlist->data;
1958 if (script->result && script->result->proplist) {
1959 property_add_to_argv(argv_ptr, script->result->proplist);
1963 } else if (proplist) {
1964 g_hash_table_foreach(proplist, &proplist_add_to_argv, argv_ptr);
1969 for (i = 0, fn = ctl_data->elist->files; i < files_off_tape;
1973 case IS_APPLICATION_API:
1978 if (strcmp(fn->path, "/") == 0)
1979 g_ptr_array_add(argv_ptr, stralloc("."));
1981 g_ptr_array_add(argv_ptr, stralloc2(".", fn->path));
1985 #if defined(XFSDUMP)
1986 if (strcmp(ctl_data->file.program, XFSDUMP) == 0) {
1988 * xfsrestore needs a -s option before each file to be
1989 * restored, and also wants them to be relative paths.
1991 g_ptr_array_add(argv_ptr, stralloc("-s"));
1992 g_ptr_array_add(argv_ptr, stralloc(fn->path + 1));
1996 g_ptr_array_add(argv_ptr, stralloc(fn->path));
2001 #if defined(XFSDUMP)
2002 if (strcmp(ctl_data->file.program, XFSDUMP) == 0) {
2003 g_ptr_array_add(argv_ptr, stralloc("-"));
2004 g_ptr_array_add(argv_ptr, stralloc("."));
2007 g_ptr_array_add(argv_ptr, NULL);
2012 cmd = stralloc(SAMBA_CLIENT);
2015 /* fall through to ... */
2021 g_fprintf(stderr, _("warning: GNUTAR program not available.\n"));
2022 cmd = stralloc("tar");
2024 cmd = stralloc(GNUTAR);
2031 if (strcmp(ctl_data->file.program, DUMP) == 0) {
2032 cmd = stralloc(RESTORE);
2036 if (strcmp(ctl_data->file.program, VDUMP) == 0) {
2037 cmd = stralloc(VRESTORE);
2041 if (strcmp(ctl_data->file.program, VXDUMP) == 0) {
2042 cmd = stralloc(VXRESTORE);
2045 #if defined(XFSDUMP)
2046 if (strcmp(ctl_data->file.program, XFSDUMP) == 0) {
2047 cmd = stralloc(XFSRESTORE);
2051 g_fprintf(stderr, _("warning: restore program for %s not available.\n"),
2052 ctl_data->file.program);
2053 cmd = stralloc("restore");
2056 case IS_APPLICATION_API:
2057 cmd = vstralloc(APPLICATION_DIR, "/", ctl_data->file.application, NULL);
2061 dbprintf(_("Exec'ing %s with arguments:\n"), cmd);
2062 for (j = 0; j < argv_ptr->len - 1; j++) {
2063 if (j == passwd_field)
2064 dbprintf("\tXXXXX\n");
2066 dbprintf(_("\t%s\n"), (char *)g_ptr_array_index(argv_ptr, j));
2069 (void)execv(cmd, (char **)argv_ptr->pdata);
2070 /* only get here if exec failed */
2072 g_ptr_array_free_full(argv_ptr);
2074 perror(_("amrecover couldn't exec"));
2075 g_fprintf(stderr, _(" problem executing %s\n"), cmd);
2083 * Interpose something between the process writing out the dump (writing it to
2084 * some extraction program, really) and the socket from which we're reading, so
2085 * that we can do things like prompt for human interaction for multiple tapes.
2088 writer_intermediary(
2089 EXTRACT_LIST * elist)
2091 ctl_data_t ctl_data;
2092 amwait_t extractor_status;
2094 ctl_data.header_done = 0;
2095 ctl_data.child_pipe[0] = -1;
2096 ctl_data.child_pipe[1] = -1;
2098 ctl_data.elist = elist;
2099 fh_init(&ctl_data.file);
2100 ctl_data.data_path = DATA_PATH_AMANDA;
2101 ctl_data.addrs = NULL;
2102 ctl_data.bsu = NULL;
2104 security_stream_read(amidxtaped_streams[DATAFD].fd,
2105 read_amidxtaped_data, &ctl_data);
2107 while(get_amidxtaped_line() >= 0) {
2108 char desired_tape[MAX_TAPE_LABEL_BUF];
2109 g_debug("get amidxtaped line: %s", amidxtaped_line);
2111 /* if prompted for a tape, relay said prompt to the user */
2112 if(sscanf(amidxtaped_line, "FEEDME %132s\n", desired_tape) == 1) {
2114 g_printf(_("Load tape %s now\n"), desired_tape);
2115 dbprintf(_("Requesting tape %s from user\n"), desired_tape);
2116 done = okay_to_continue(am_has_feature(indexsrv_features,
2117 fe_amrecover_feedme_tape),
2120 if (am_has_feature(indexsrv_features,
2121 fe_amrecover_feedme_tape)) {
2122 char *reply = stralloc2("TAPE ", tape_device_name);
2123 send_to_tape_server(amidxtaped_streams[CTLFD].fd, reply);
2126 send_to_tape_server(amidxtaped_streams[CTLFD].fd, "OK");
2129 send_to_tape_server(amidxtaped_streams[CTLFD].fd, "ERROR");
2132 } else if (strncmp_const(amidxtaped_line, "USE-DATAPATH ") == 0) {
2133 if (strncmp_const(amidxtaped_line+13, "AMANDA") == 0) {
2134 ctl_data.data_path = DATA_PATH_AMANDA;
2135 g_debug("Using AMANDA data-path");
2136 } else if (strncmp_const(amidxtaped_line+13, "DIRECT-TCP") == 0) {
2137 ctl_data.data_path = DATA_PATH_DIRECTTCP;
2138 ctl_data.addrs = stralloc(amidxtaped_line+24);
2139 g_debug("Using DIRECT-TCP data-path with %s", ctl_data.addrs);
2141 start_processing_data(&ctl_data);
2142 } else if(strncmp_const(amidxtaped_line, "MESSAGE ") == 0) {
2143 g_printf("%s\n",&amidxtaped_line[8]);
2145 g_fprintf(stderr, _("Strange message from tape server: %s"),
2151 /* CTL might be close before DATA */
2153 dumpfile_free_data(&ctl_data.file);
2154 amfree(ctl_data.addrs);
2155 amfree(ctl_data.bsu);
2156 if (ctl_data.child_pipe[1] != -1)
2157 aclose(ctl_data.child_pipe[1]);
2159 if (ctl_data.header_done == 0) {
2160 g_printf(_("Got no header and data from server, check in amidxtaped.*.debug and amandad.*.debug files on server\n"));
2163 if (ctl_data.pid != -1) {
2164 waitpid(ctl_data.pid, &extractor_status, 0);
2165 if(WEXITSTATUS(extractor_status) != 0){
2166 int ret = WEXITSTATUS(extractor_status);
2167 if(ret == 255) ret = -1;
2168 g_printf(_("Extractor child exited with status %d\n"), ret);
2175 /* exec restore to do the actual restoration */
2177 /* does the actual extraction of files */
2179 * The original design had the dump image being returned exactly as it
2180 * appears on the tape, and this routine getting from the index server
2181 * whether or not it is compressed, on the assumption that the tape
2182 * server may not know how to uncompress it. But
2183 * - Amrestore can't do that. It returns either compressed or uncompressed
2184 * (always). Amrestore assumes it can uncompress files. It is thus a good
2185 * idea to run the tape server on a machine with gzip.
2186 * - The information about compression in the disklist is really only
2187 * for future dumps. It is possible to change compression on a drive
2188 * so the information in the disklist may not necessarily relate to
2189 * the dump image on the tape.
2190 * Consequently the design was changed to assuming that amrestore can
2191 * uncompress any dump image and have it return an uncompressed file
2197 EXTRACT_LIST *elist;
2201 tapelist_t *tlist = NULL, *a_tlist;
2202 g_option_t g_options;
2203 levellist_t all_level = NULL;
2206 if (!is_extract_list_nonempty())
2208 g_printf(_("Extract list empty - No files to extract!\n"));
2212 clean_extract_list();
2214 /* get tape device name from index server if none specified */
2215 if (tape_server_name == NULL) {
2216 tape_server_name = newstralloc(tape_server_name, server_name);
2218 if (tape_device_name == NULL) {
2219 if (send_command("TAPE") == -1)
2221 if (get_reply_line() == -1)
2224 if (!server_happy())
2226 g_printf("%s\n", l);
2229 /* skip reply number */
2230 tape_device_name = newstralloc(tape_device_name, l+4);
2233 if (strcmp(tape_device_name, "/dev/null") == 0)
2235 g_printf(_("amrecover: warning: using %s as the tape device will not work\n"),
2240 for (elist = first_tape_list(); elist != NULL; elist = next_tape_list(elist)) {
2241 if(elist->tape[0]!='/') {
2243 g_printf(_("\nExtracting files using tape drive %s on host %s.\n"),
2244 tape_device_name, tape_server_name);
2245 g_printf(_("The following tapes are needed:"));
2250 tlist = unmarshal_tapelist_str(elist->tape);
2251 for(a_tlist = tlist ; a_tlist != NULL; a_tlist = a_tlist->next)
2252 g_printf(" %s", a_tlist->label);
2254 free_tapelist(tlist);
2258 for (elist = first_tape_list(); elist != NULL; elist = next_tape_list(elist)) {
2259 if(elist->tape[0]=='/') {
2261 g_printf(_("\nExtracting files from holding disk on host %s.\n"),
2263 g_printf(_("The following files are needed:"));
2268 tlist = unmarshal_tapelist_str(elist->tape);
2269 for(a_tlist = tlist; a_tlist != NULL; a_tlist = a_tlist->next)
2270 g_printf(" %s", a_tlist->label);
2272 free_tapelist(tlist);
2277 g_options.config = get_config_name();
2278 g_options.hostname = dump_hostname;
2279 for (elist = first_tape_list(); elist != NULL;
2280 elist = next_tape_list(elist)) {
2281 level_t *level = g_new0(level_t, 1);
2282 level->level = elist->level;
2283 all_level = g_slist_append(all_level, level);
2286 g_slist_free_full(dump_dle->levellist);
2287 dump_dle->levellist = all_level;
2288 run_client_scripts(EXECUTE_ON_PRE_RECOVER, &g_options, dump_dle,
2290 dump_dle->levellist = NULL;
2293 while ((elist = first_tape_list()) != NULL)
2295 if(elist->tape[0]=='/') {
2296 dump_device_name = newstralloc(dump_device_name, elist->tape);
2297 g_printf(_("Extracting from file "));
2298 tlist = unmarshal_tapelist_str(dump_device_name);
2299 for(a_tlist = tlist; a_tlist != NULL; a_tlist = a_tlist->next)
2300 g_printf(" %s", a_tlist->label);
2302 free_tapelist(tlist);
2305 g_printf(_("Extracting files using tape drive %s on host %s.\n"),
2306 tape_device_name, tape_server_name);
2307 tlist = unmarshal_tapelist_str(elist->tape);
2308 g_printf(_("Load tape %s now\n"), tlist->label);
2309 dbprintf(_("Requesting tape %s from user\n"), tlist->label);
2310 free_tapelist(tlist);
2311 otc = okay_to_continue(1,1,0);
2314 else if (otc == SKIP_TAPE) {
2315 delete_tape_list(elist); /* skip this tape */
2318 dump_device_name = newstralloc(dump_device_name, tape_device_name);
2320 dump_datestamp = newstralloc(dump_datestamp, elist->date);
2322 if (last_level != -1 && dump_dle) {
2325 level = g_new0(level_t, 1);
2326 level->level = last_level;
2327 dump_dle->levellist = g_slist_append(dump_dle->levellist, level);
2329 level = g_new0(level_t, 1);
2330 level->level = elist->level;
2331 dump_dle->levellist = g_slist_append(dump_dle->levellist, level);
2332 run_client_scripts(EXECUTE_ON_INTER_LEVEL_RECOVER, &g_options,
2334 g_slist_free_full(dump_dle->levellist);
2335 dump_dle->levellist = NULL;
2338 /* connect to the tape handler daemon on the tape drive server */
2339 if ((extract_files_setup(elist->tape, elist->fileno)) == -1)
2341 g_fprintf(stderr, _("amrecover - can't talk to tape server: %s\n"),
2348 level = g_new0(level_t, 1);
2349 level->level = elist->level;
2350 dump_dle->levellist = g_slist_append(dump_dle->levellist, level);
2351 run_client_scripts(EXECUTE_ON_PRE_LEVEL_RECOVER, &g_options,
2354 last_level = elist->level;
2356 /* if the server have fe_amrecover_feedme_tape, it has asked for
2357 * the tape itself, even if the restore didn't succeed, we should
2360 if(writer_intermediary(elist) == 0 ||
2361 am_has_feature(indexsrv_features, fe_amrecover_feedme_tape))
2362 delete_tape_list(elist); /* tape done so delete from list */
2364 am_release_feature_set(tapesrv_features);
2368 run_client_scripts(EXECUTE_ON_POST_LEVEL_RECOVER, &g_options,
2370 g_slist_free_full(dump_dle->levellist);
2371 dump_dle->levellist = NULL;
2375 dump_dle->levellist = all_level;
2376 run_client_scripts(EXECUTE_ON_POST_RECOVER, &g_options, dump_dle,
2378 g_slist_free_full(dump_dle->levellist);
2380 dump_dle->levellist = NULL;
2385 amidxtaped_response(
2388 security_handle_t * sech)
2390 int ports[NSTREAMS], *response_error = datap, i;
2395 assert(response_error != NULL);
2396 assert(sech != NULL);
2397 memset(ports, -1, SIZEOF(ports));
2400 errstr = newvstrallocf(errstr, _("[request failed: %s]"), security_geterror(sech));
2401 *response_error = 1;
2404 security_close_connection(sech, dump_hostname);
2406 if (pkt->type == P_NAK) {
2407 #if defined(PACKET_DEBUG)
2408 g_fprintf(stderr, _("got nak response:\n----\n%s\n----\n\n"), pkt->body);
2411 tok = strtok(pkt->body, " ");
2412 if (tok == NULL || strcmp(tok, "ERROR") != 0)
2415 tok = strtok(NULL, "\n");
2417 errstr = newvstralloc(errstr, "NAK: ", tok, NULL);
2418 *response_error = 1;
2421 errstr = newstralloc(errstr, "request NAK");
2422 *response_error = 2;
2427 if (pkt->type != P_REP) {
2428 errstr = newvstrallocf(errstr, _("received strange packet type %s: %s"),
2429 pkt_type2str(pkt->type), pkt->body);
2430 *response_error = 1;
2434 #if defined(PACKET_DEBUG)
2435 g_fprintf(stderr, _("got response:\n----\n%s\n----\n\n"), pkt->body);
2438 for(i = 0; i < NSTREAMS; i++) {
2440 amidxtaped_streams[i].fd = NULL;
2444 while((tok = strtok(p, " \n")) != NULL) {
2448 * Error response packets have "ERROR" followed by the error message
2449 * followed by a newline.
2451 if (strcmp(tok, "ERROR") == 0) {
2452 tok = strtok(NULL, "\n");
2454 tok = _("[bogus error packet]");
2455 errstr = newstralloc(errstr, tok);
2456 *response_error = 2;
2462 * Regular packets have CONNECT followed by three streams
2464 if (strcmp(tok, "CONNECT") == 0) {
2467 * Parse the three stream specifiers out of the packet.
2469 for (i = 0; i < NSTREAMS; i++) {
2470 tok = strtok(NULL, " ");
2471 if (tok == NULL || strcmp(tok, amidxtaped_streams[i].name) != 0) {
2472 extra = vstrallocf(_("CONNECT token is \"%s\": expected \"%s\""),
2473 tok ? tok : "(null)",
2474 amidxtaped_streams[i].name);
2477 tok = strtok(NULL, " \n");
2478 if (tok == NULL || sscanf(tok, "%d", &ports[i]) != 1) {
2479 extra = vstrallocf(_("CONNECT %s token is \"%s\": expected a port number"),
2480 amidxtaped_streams[i].name,
2481 tok ? tok : "(null)");
2489 * OPTIONS [options string] '\n'
2491 if (strcmp(tok, "OPTIONS") == 0) {
2492 tok = strtok(NULL, "\n");
2494 extra = stralloc(_("OPTIONS token is missing"));
2498 while((p = strchr(tok, ';')) != NULL) {
2500 if(strncmp_const(tok, "features=") == 0) {
2501 tok += sizeof("features=") - 1;
2502 am_release_feature_set(their_features);
2503 if((their_features = am_string_to_feature(tok)) == NULL) {
2504 errstr = newvstralloc(errstr,
2505 _("OPTIONS: bad features value: "),
2517 extra = vstrallocf("next token is \"%s\": expected \"CONNECT\", \"ERROR\" or \"OPTIONS\""),
2518 tok ? tok : _("(null)"));
2524 * Connect the streams to their remote ports
2526 for (i = 0; i < NSTREAMS; i++) {
2529 amidxtaped_streams[i].fd = security_stream_client(sech, ports[i]);
2530 dbprintf(_("amidxtaped_streams[%d].fd = %p\n"),i, amidxtaped_streams[i].fd);
2531 if (amidxtaped_streams[i].fd == NULL) {
2532 errstr = newvstrallocf(errstr,\
2533 _("[could not connect %s stream: %s]"),
2534 amidxtaped_streams[i].name,
2535 security_geterror(sech));
2540 * Authenticate the streams
2542 for (i = 0; i < NSTREAMS; i++) {
2543 if (amidxtaped_streams[i].fd == NULL)
2545 if (security_stream_auth(amidxtaped_streams[i].fd) < 0) {
2546 errstr = newvstrallocf(errstr,
2547 _("[could not authenticate %s stream: %s]"),
2548 amidxtaped_streams[i].name,
2549 security_stream_geterror(amidxtaped_streams[i].fd));
2555 * The CTLFD and DATAFD streams are mandatory. If we didn't get
2558 if (amidxtaped_streams[CTLFD].fd == NULL) {
2559 errstr = newvstrallocf(errstr, _("[couldn't open CTL streams]"));
2562 if (amidxtaped_streams[DATAFD].fd == NULL) {
2563 errstr = newvstrallocf(errstr, _("[couldn't open DATA streams]"));
2567 /* everything worked */
2568 *response_error = 0;
2573 errstr = newvstrallocf(errstr,
2574 _("[parse of reply message failed: %s]"), extra);
2576 errstr = newvstrallocf(errstr,
2577 _("[parse of reply message failed: (no additional information)"));
2580 *response_error = 2;
2585 *response_error = 1;
2589 * This is called when everything needs to shut down so event_loop()
2593 stop_amidxtaped(void)
2597 for (i = 0; i < NSTREAMS; i++) {
2598 if (amidxtaped_streams[i].fd != NULL) {
2599 security_stream_close(amidxtaped_streams[i].fd);
2600 amidxtaped_streams[i].fd = NULL;
2605 static char* ctl_buffer = NULL;
2606 /* gets a "line" from server and put in server_line */
2607 /* server_line is terminated with \0, \r\n is striped */
2608 /* returns -1 if error */
2611 get_amidxtaped_line(void)
2617 amfree(amidxtaped_line);
2619 ctl_buffer = stralloc("");
2621 while (!strstr(ctl_buffer,"\r\n")) {
2622 if (amidxtaped_streams[CTLFD].fd == NULL)
2625 size = security_stream_read_sync(amidxtaped_streams[CTLFD].fd, &buf);
2629 else if(size == 0) {
2632 newbuf = alloc(strlen(ctl_buffer)+size+1);
2633 strncpy(newbuf, ctl_buffer, (size_t)(strlen(ctl_buffer) + size + 1));
2634 memcpy(newbuf+strlen(ctl_buffer), buf, (size_t)size);
2635 newbuf[strlen(ctl_buffer)+size] = '\0';
2637 ctl_buffer = newbuf;
2641 s = strstr(ctl_buffer,"\r\n");
2643 newbuf = stralloc(s+2);
2644 amidxtaped_line = stralloc(ctl_buffer);
2646 ctl_buffer = newbuf;
2652 read_amidxtaped_data(
2657 ctl_data_t *ctl_data = (ctl_data_t *)cookie;
2658 assert(cookie != NULL);
2661 errstr = newstralloc2(errstr, _("amidxtaped read: "),
2662 security_stream_geterror(amidxtaped_streams[DATAFD].fd));
2667 * EOF. Stop and return.
2670 security_stream_close(amidxtaped_streams[DATAFD].fd);
2671 amidxtaped_streams[DATAFD].fd = NULL;
2673 * If the mesg fd has also shut down, then we're done.
2678 assert(buf != NULL);
2680 if (ctl_data->header_done == 0) {
2681 GPtrArray *errarray;
2682 g_option_t g_options;
2683 data_path_t data_path_set = DATA_PATH_AMANDA;
2685 /* parse the file header */
2686 fh_init(&ctl_data->file);
2687 parse_file_header(buf, &ctl_data->file, (size_t)size);
2689 /* call backup_support_option */
2690 g_options.config = get_config_name();
2691 g_options.hostname = dump_hostname;
2692 if (strcmp(ctl_data->file.program, "APPLICATION") == 0) {
2694 ctl_data->bsu = backup_support_option(ctl_data->file.application,
2696 ctl_data->file.disk,
2700 ctl_data->bsu = backup_support_option(ctl_data->file.application,
2702 ctl_data->file.disk, NULL,
2705 if (!ctl_data->bsu) {
2707 for (i=0; i < errarray->len; i++) {
2709 line = g_ptr_array_index(errarray, i);
2710 g_fprintf(stderr, "%s\n", line);
2712 g_ptr_array_free_full(errarray);
2715 data_path_set = ctl_data->bsu->data_path_set;
2717 /* handle backup_support_option failure */
2719 ctl_data->header_done = 1;
2720 if (!ask_file_overwrite(ctl_data)) {
2721 if (am_has_feature(tapesrv_features, fe_amidxtaped_abort)) {
2722 send_to_tape_server(amidxtaped_streams[CTLFD].fd, "ABORT");
2728 if (am_has_feature(tapesrv_features, fe_amidxtaped_datapath)) {
2730 /* send DATA-PATH request */
2731 msg = stralloc("AVAIL-DATAPATH");
2732 if (data_path_set & DATA_PATH_AMANDA)
2733 vstrextend(&msg, " AMANDA", NULL);
2734 if (data_path_set & DATA_PATH_DIRECTTCP)
2735 vstrextend(&msg, " DIRECT-TCP", NULL);
2736 send_to_tape_server(amidxtaped_streams[CTLFD].fd, msg);
2739 start_processing_data(ctl_data);
2742 /* Only the data is sent to the child */
2744 * We ignore errors while writing to the index file.
2746 (void)full_write(ctl_data->child_pipe[1], buf, (size_t)size);
2747 security_stream_read(amidxtaped_streams[DATAFD].fd,
2748 read_amidxtaped_data, cookie);
2754 ctl_data_t *ctl_data)
2756 char *restore_dir = NULL;
2758 if (ctl_data->file.dumplevel == 0) {
2759 property_t *property = g_hash_table_lookup(proplist, "directory");
2760 if (property && property->values && property->values->data) {
2761 /* take first property value */
2762 restore_dir = strdup(property->values->data);
2764 if (samba_extract_method == SAMBA_SMBCLIENT ||
2766 ctl_data->bsu->recover_path == RECOVER_PATH_REMOTE)) {
2768 restore_dir = g_strdup(ctl_data->file.disk);
2770 g_printf(_("Restoring files into target host %s\n"), restore_dir);
2773 restore_dir = g_get_current_dir();
2775 g_printf(_("Restoring files into directory %s\n"), restore_dir);
2778 /* Collect files to delete befause of a bug in gnutar */
2779 if (strcmp(ctl_data->file.program, "GNUTAR") == 0 ||
2780 (strcmp(ctl_data->file.program, "APPLICATION") == 0 &&
2781 strcmp(ctl_data->file.application, "amgtar") == 0)) {
2782 check_file_overwrite(restore_dir);
2784 g_printf(_("All existing files in %s can be deleted\n"),
2788 if (!okay_to_continue(0,0,0)) {
2790 amfree(restore_dir);
2795 /* delete the files for gnutar */
2797 if (!do_unlink_list()) {
2798 g_fprintf(stderr, _("Can't recover because I can't cleanup the restore directory (%s)\n"),
2801 amfree(restore_dir);
2806 amfree(restore_dir);
2812 start_processing_data(
2813 ctl_data_t *ctl_data)
2815 if (pipe(ctl_data->child_pipe) == -1) {
2816 error(_("extract_list - error setting up pipe to extractor: %s\n"),
2821 /* okay, ready to extract. fork a child to do the actual work */
2822 if ((ctl_data->pid = fork()) == 0) {
2823 /* this is the child process */
2824 /* never gets out of this clause */
2825 aclose(ctl_data->child_pipe[1]);
2826 extract_files_child(ctl_data);
2830 if (ctl_data->pid == -1) {
2831 errstr = newstralloc(errstr, _("writer_intermediary - error forking child"));
2832 g_printf(_("writer_intermediary - error forking child"));
2835 aclose(ctl_data->child_pipe[0]);
2836 security_stream_read(amidxtaped_streams[DATAFD].fd, read_amidxtaped_data,
2838 if (am_has_feature(tapesrv_features, fe_amidxtaped_datapath)) {
2839 send_to_tape_server(amidxtaped_streams[CTLFD].fd, "DATAPATH-OK");