2 * Bacula File Daemon restore.c Restorefiles.
4 * Kern Sibbald, November MM
8 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2 of
13 the License, or (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public
21 License along with this program; if not, write to the Free
22 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /* Data received from Storage Daemon */
31 static char rec_header[] = "rechdr %ld %ld %ld %ld %ld";
33 /* Forward referenced functions */
34 static void print_ls_output(JCR *jcr, char *fname, char *lname, int type, struct stat *statp);
36 #define RETRY 10 /* retry wait time */
39 * Restore the requested files.
42 void do_restore(JCR *jcr, char *addr, int port)
46 char *fname; /* original file name */
47 char *ofile; /* output name with possible prefix */
48 char *lname; /* link name */
51 uint32_t VolSessionId, VolSessionTime, file_index;
52 uint32_t record_file_index;
59 wherelen = strlen(jcr->where);
61 sd = jcr->store_bsock;
62 jcr->JobStatus = JS_Running;
64 if (!bnet_set_buffer_size(sd, MAX_NETWORK_BUFFER_SIZE, BNET_SETBUF_READ)) {
68 fname = (char *) get_pool_memory(PM_FNAME);
69 ofile = (char *) get_pool_memory(PM_FNAME);
70 lname = (char *) get_pool_memory(PM_FNAME);
73 * Get a record from the Storage daemon
75 while (bnet_recv(sd) > 0) {
77 * First we expect a Stream Record Header
79 if (sscanf(sd->msg, rec_header, &VolSessionId, &VolSessionTime, &file_index,
80 &stream, &size) != 5) {
81 Jmsg1(jcr, M_FATAL, 0, _("Record header scan error: %s\n"), sd->msg);
82 free_pool_memory(fname);
83 free_pool_memory(ofile);
84 free_pool_memory(lname);
87 Dmsg2(30, "Got hdr: FilInx=%d Stream=%d.\n", file_index, stream);
90 * Now we expect the Stream Data
92 if (bnet_recv(sd) < 0) {
93 Jmsg1(jcr, M_FATAL, 0, _("Data record error. ERR=%s\n"), bnet_strerror(sd));
95 if (size != ((uint32_t) sd->msglen)) {
96 Jmsg2(jcr, M_FATAL, 0, _("Actual data size %d not same as header %d\n"), sd->msglen, size);
97 free_pool_memory(fname);
98 free_pool_memory(ofile);
99 free_pool_memory(lname);
102 Dmsg1(30, "Got stream data, len=%d\n", sd->msglen);
104 /* File Attributes stream */
105 if (stream == STREAM_UNIX_ATTRIBUTES) {
108 Dmsg1(30, "Stream=Unix Attributes. extract=%d\n", extract);
109 /* If extracting, it was from previous stream, so
110 * close the output file.
114 Emsg0(M_ABORT, 0, _("Logic error output file should be open\n"));
119 set_statp(jcr, fname, ofile, lname, type, &statp);
120 Dmsg0(30, "Stop extracting.\n");
123 if ((int)sizeof_pool_memory(fname) < sd->msglen) {
124 fname = (char *) realloc_pool_memory(fname, sd->msglen + 1);
126 if (sizeof_pool_memory(ofile) < sizeof_pool_memory(fname) + wherelen + 1) {
127 ofile = (char *) realloc_pool_memory(ofile, sizeof_pool_memory(fname) + wherelen + 1);
129 if ((int)sizeof_pool_memory(lname) < sd->msglen) {
130 ofile = (char *) realloc_pool_memory(ofile, sd->msglen + 1);
136 * An Attributes record consists of:
141 * Link name (if file linked i.e. FT_LNK)
144 if (sscanf(sd->msg, "%d %d %s", &record_file_index, &type, fname) != 3) {
145 Emsg1(M_FATAL, 0, _("Error scanning record header: %s\n"), sd->msg);
146 /** ****FIXME**** need to cleanup */
147 Dmsg0(0, "\nError scanning header\n");
150 Dmsg3(30, "Got Attr: FilInx=%d type=%d fname=%s\n", record_file_index,
152 if (record_file_index != file_index) {
153 Emsg2(M_ABORT, 0, _("Record header file index %ld not equal record index %ld\n"),
154 file_index, record_file_index);
155 Dmsg0(0, "File index error\n");
158 /* Skip to attributes */
162 /* Skip to Link name */
163 if (type == FT_LNK) {
168 strcat(lname, lp); /* "save" link name */
173 decode_stat(ap, &statp);
175 * Prepend the where directory so that the
176 * files are put where the user wants.
178 * We do a little jig here to handle Win32 files with
180 * If where is null and we are running on a win32 client,
182 * Otherwise, if the second character of the filename is a
183 * colon (:), change it into a slash (/) -- this creates
184 * a reasonable pathname on most systems.
186 if (jcr->where[0] == 0 && win32_client) {
187 strcpy(ofile, fname);
189 strcpy(ofile, jcr->where);
190 if (fname[1] == ':') {
192 strcat(ofile, fname);
195 strcat(ofile, fname);
199 Dmsg1(30, "Outfile=%s\n", ofile);
200 print_ls_output(jcr, ofile, lname, type, &statp);
202 extract = create_file(jcr, fname, ofile, lname, type, &statp, &ofd);
203 Dmsg1(40, "Extract=%d\n", extract);
207 jcr->num_files_examined++;
210 } else if (stream == STREAM_FILE_DATA) {
212 Dmsg2(30, "Write %d bytes, total before write=%d\n", sd->msglen, total);
213 if (write(ofd, sd->msg, sd->msglen) != sd->msglen) {
214 Dmsg0(0, "===Write error===\n");
215 Jmsg2(jcr, M_ERROR, 0, "Write error on %s: %s\n", ofile, strerror(errno));
216 free_pool_memory(fname);
217 free_pool_memory(ofile);
218 free_pool_memory(lname);
222 jcr->JobBytes += sd->msglen;
225 /* If extracting, wierd stream (not 1 or 2), close output file anyway */
226 } else if (extract) {
227 Dmsg1(30, "Found wierd stream %d\n", stream);
229 Emsg0(M_ABORT, 0, _("Logic error output file should be open\n"));
234 set_statp(jcr, fname, ofile, lname, type, &statp);
235 } else if (stream != STREAM_MD5_SIGNATURE) {
236 Dmsg2(0, "None of above!!! stream=%d data=%s\n", stream,sd->msg);
240 /* If output file is still open, it was the last one in the
241 * archive since we just hit an end of file, so close the file.
245 set_statp(jcr, fname, ofile, lname, type, &statp);
248 free_pool_memory(fname);
249 free_pool_memory(ofile);
250 free_pool_memory(lname);
251 Dmsg2(10, "End Do Restore. Files=%d Bytes=%" lld "\n", jcr->JobFiles,
255 extern char *getuser(uid_t uid);
256 extern char *getgroup(gid_t gid);
259 * Print an ls style message, also send INFO
261 static void print_ls_output(JCR *jcr, char *fname, char *lname, int type, struct stat *statp)
263 /* ********FIXME******** make memory pool */
268 p = encode_mode(statp->st_mode, buf);
269 n = sprintf(p, " %2d ", (uint32_t)statp->st_nlink);
271 n = sprintf(p, "%-8.8s %-8.8s", getuser(statp->st_uid), getgroup(statp->st_gid));
273 n = sprintf(p, "%8" lld " ", (uint64_t)statp->st_size);
275 p = encode_time(statp->st_ctime, p);
280 if (type == FT_LNK) {
292 Jmsg(jcr, M_INFO, 0, buf);