]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/filed/restore.c
This commit was manufactured by cvs2svn to create tag
[bacula/bacula] / bacula / src / filed / restore.c
index 864c3bd36f1d2211777c1ac212400a19500d732d..b7bb63320aa2f5c7229f3b6969f2d82146c0672f 100644 (file)
@@ -33,7 +33,7 @@
 static char rec_header[] = "rechdr %ld %ld %ld %ld %ld";
 
 /* Forward referenced functions */
-static void print_ls_output(JCR *jcr, char *fname, char *lname, int type, struct stat *statp);
+static char *zlib_strerror(int stat);
 
 #define RETRY 10                     /* retry wait time */
 
@@ -43,27 +43,25 @@ static void print_ls_output(JCR *jcr, char *fname, char *lname, int type, struct
  */
 void do_restore(JCR *jcr)
 {
-   int wherelen;
    BSOCK *sd;
-   POOLMEM *fname;                   /* original file name */
-   POOLMEM *ofile;                   /* output name with possible prefix */
-   POOLMEM *lname;                   /* link name with possible prefix */
-   POOLMEM *attribsEx;               /* Extended attributes (Win32) */
    int32_t stream;
    uint32_t size;
-   uint32_t VolSessionId, VolSessionTime, file_index;
-   uint32_t record_file_index;
-   struct stat statp;
-   int extract = FALSE;
-   int ofd = -1;
-   int type, stat;
+   uint32_t VolSessionId, VolSessionTime;
+   int32_t file_index;
+   bool extract = false;
+   BFILE bfd;
+   int stat;
    uint32_t total = 0;               /* Job total but only 32 bits for debug */
    char *wbuf;                       /* write buffer */
    uint32_t wsize;                   /* write size */
    uint64_t fileAddr = 0;            /* file write address */
+   int non_support_data = 0;
+   int non_support_attr = 0;
+   int prog_name_msg = 0;
+   ATTR *attr;
    
-   wherelen = strlen(jcr->where);
 
+   binit(&bfd);
    sd = jcr->store_bsock;
    set_jcr_job_status(jcr, JS_Running);
 
@@ -73,10 +71,7 @@ void do_restore(JCR *jcr)
    }
    jcr->buf_size = sd->msglen;
 
-   fname = get_pool_memory(PM_FNAME);
-   ofile = get_pool_memory(PM_FNAME);
-   lname = get_pool_memory(PM_FNAME);
-   attribsEx = get_pool_memory(PM_FNAME);
+   attr = new_attr();
 
 #ifdef HAVE_LIBZ
    uint32_t compress_buf_size = jcr->buf_size + 12 + ((jcr->buf_size+999) / 1000) + 100;
@@ -93,7 +88,7 @@ void do_restore(JCR *jcr)
     *   or  c. Possibly MD5 or SHA1 record
     *  3. Repeat step 1
     */
-   while (bnet_recv(sd) >= 0 && !job_canceled(jcr)) {
+   while (bget_msg(sd) >= 0 && !job_canceled(jcr)) {
       /*
        * First we expect a Stream Record Header 
        */
@@ -107,7 +102,7 @@ void do_restore(JCR *jcr)
       /* 
        * Now we expect the Stream Data
        */
-      if (bnet_recv(sd) < 0) {
+      if (bget_msg(sd) < 0) {
          Jmsg1(jcr, M_FATAL, 0, _("Data record error. ERR=%s\n"), bnet_strerror(sd));
         goto bail_out;
       }
@@ -118,174 +113,85 @@ void do_restore(JCR *jcr)
       Dmsg1(30, "Got stream data, len=%d\n", sd->msglen);
 
       /* File Attributes stream */
-      if (stream == STREAM_UNIX_ATTRIBUTES || stream == STREAM_WIN32_ATTRIBUTES) {
-        char *ap, *lp, *fp, *apex;
-        uint32_t LinkFI;
+      switch (stream) {
+      case STREAM_UNIX_ATTRIBUTES:
+      case STREAM_UNIX_ATTRIBUTES_EX:
 
          Dmsg1(30, "Stream=Unix Attributes. extract=%d\n", extract);
         /* If extracting, it was from previous stream, so
          * close the output file.
          */
         if (extract) {
-           if (ofd < 0) {
+           if (!is_bopen(&bfd)) {
                Jmsg0(jcr, M_ERROR, 0, _("Logic error output file should be open\n"));
            }
-           set_attributes(jcr, fname, ofile, lname, type, stream, 
-                          &statp, attribsEx, &ofd);
-           extract = FALSE;
+           set_attributes(jcr, attr, &bfd);
+           extract = false;
             Dmsg0(30, "Stop extracting.\n");
         }
 
-        if ((int)sizeof_pool_memory(fname) <  sd->msglen) {
-           fname = realloc_pool_memory(fname, sd->msglen + 1);
-        }
-        if ((int)sizeof_pool_memory(ofile) < sd->msglen + wherelen + 1) {
-           ofile = realloc_pool_memory(ofile, sd->msglen + wherelen + 1);
-        }
-        if ((int)sizeof_pool_memory(lname) < sd->msglen + wherelen + 1) {
-           lname = realloc_pool_memory(lname, sd->msglen + wherelen + 1);
-        }
-        *fname = 0;
-        *lname = 0;
-
-        /*              
-         * An Attributes record consists of:
-         *    File_index
-         *    Type   (FT_types)
-         *    Filename
-         *    Attributes
-         *    Link name (if file linked i.e. FT_LNK)
-         *    Extended attributes (Win32)
-         *
-         */
-         Dmsg1(100, "Attr: %s\n", sd->msg);
-         if (sscanf(sd->msg, "%d %d", &record_file_index, &type) != 2) {
-            Jmsg(jcr, M_FATAL, 0, _("Error scanning attributes: %s\n"), sd->msg);
-            Dmsg1(100, "\nError scanning attributes. %s\n", sd->msg);
+        if (!unpack_attributes_record(jcr, stream, sd->msg, attr)) {
            goto bail_out;
         }
-         Dmsg2(100, "Got Attr: FilInx=%d type=%d\n", record_file_index, type);
-        if (record_file_index != file_index) {
+        if (file_index != attr->file_index) {
             Jmsg(jcr, M_FATAL, 0, _("Record header file index %ld not equal record index %ld\n"),
-              file_index, record_file_index);
+                file_index, attr->file_index);
             Dmsg0(100, "File index error\n");
            goto bail_out;
         }
-        ap = sd->msg;
-         while (*ap++ != ' ')         /* skip record file index */
-           ;
-         while (*ap++ != ' ')         /* skip type */
-           ;
-        /* Save filename and position to attributes */
-        fp = fname;
-        while (*ap != 0) {
-           *fp++  = *ap++;           /* copy filename to fname */
-        }
-        *fp = *ap++;                 /* terminate filename & point to attribs */
+           
+         Dmsg3(200, "File %s\nattrib=%s\nattribsEx=%s\n", attr->fname, 
+              attr->attr, attr->attrEx);
 
-        /* Skip to Link name */
-        if (type == FT_LNK || type == FT_LNKSAVED) {
-           lp = ap;
-           while (*lp++ != 0) {
-              ;
-           }
-        } else {
-            lp = "";
-        }
+        attr->data_stream = decode_stat(attr->attr, &attr->statp, &attr->LinkFI);
 
-        if (stream == STREAM_WIN32_ATTRIBUTES) {
-           apex = ap;                   /* start at attributes */
-           while (*apex++ != 0) {       /* skip attributes */
-              ;
-           }
-           while (*apex++ != 0) {       /* skip link name */
-              ;
+        if (!is_stream_supported(attr->data_stream)) {
+           if (!non_support_data++) {
+               Jmsg(jcr, M_ERROR, 0, _("%s stream not supported on this Client.\n"),
+                 stream_to_ascii(attr->data_stream));
            }
-           pm_strcpy(&attribsEx, apex); /* make a copy */
-        } else {
-           *attribsEx = 0;              /* no extended attributes */
+           continue;
         }
 
-         Dmsg3(200, "File %s\nattrib=%s\nattribsEx=%s\n", fname, ap, attribsEx);
-
-        decode_stat(ap, &statp, &LinkFI);
-        /*
-         * Prepend the where directory so that the
-         * files are put where the user wants.
-         *
-         * We do a little jig here to handle Win32 files with
-         *   a drive letter -- we simply strip the drive: from
-         *   every filename if a prefix is supplied.
-         *     
-         */
-        if (jcr->where[0] == 0) {
-           strcpy(ofile, fname);
-           strcpy(lname, lp);
-        } else {
-           char *fn;
-           strcpy(ofile, jcr->where);  /* copy prefix */
-            if (win32_client && fname[1] == ':') {
-              fn = fname+2;          /* skip over drive: */
-           } else {
-              fn = fname;            /* take whole name */
-           }
-           /* Ensure where is terminated with a slash */
-            if (jcr->where[wherelen-1] != '/' && fn[0] != '/') {
-               strcat(ofile, "/");
-           }   
-           strcat(ofile, fn);        /* copy rest of name */
-           /*
-            * Fixup link name -- add where only if requested
-            *   and if it is an absolute path
-            */
-           if (type == FT_LNKSAVED || type == FT_LNK) {
-              int add_link;
-               if (jcr->prefix_links && lp[0] == '/') {      /* if absolute path */
-                 strcpy(lname, jcr->where);
-                 add_link = 1;
-              } else {
-                 lname[0] = 0;
-                 add_link = 0;
-              }
-               if (win32_client && lp[1] == ':') {
-                 fn = lp+2;             /* skip over drive: */
-              } else {
-                 fn = lp;               /* take whole name */
-              }
-              /* Ensure where is terminated with a slash */
-               if (add_link && jcr->where[wherelen-1] != '/' && fn[0] != '/') {
-                  strcat(lname, "/");
-              }   
-              strcat(lname, fn);     /* copy rest of link */
-           }
-        }
+        build_attr_output_fnames(jcr, attr);
 
-         Dmsg1(30, "Outfile=%s\n", ofile);
+        jcr->num_files_examined++;
 
-        extract = FALSE;
-        stat = create_file(jcr, fname, ofile, lname, type, 
-                           stream, &statp, attribsEx, &ofd, jcr->replace);
+         Dmsg1(30, "Outfile=%s\n", attr->ofname);
+        extract = false;
+        stat = create_file(jcr, attr, &bfd, jcr->replace);
         switch (stat) {
         case CF_ERROR:
         case CF_SKIP:
            break;
         case CF_EXTRACT:
-           extract = TRUE;
+           extract = true;
            P(jcr->mutex);
-           pm_strcpy(&jcr->last_fname, ofile);
+           pm_strcpy(&jcr->last_fname, attr->ofname);
            V(jcr->mutex);
-           /* Fall-through wanted */
+           jcr->JobFiles++;
+           fileAddr = 0;
+           print_ls_output(jcr, attr);
+           /* Set attributes after file extracted */
+           break;
         case CF_CREATED:
+           P(jcr->mutex);
+           pm_strcpy(&jcr->last_fname, attr->ofname);
+           V(jcr->mutex);
            jcr->JobFiles++;
            fileAddr = 0;
-           print_ls_output(jcr, ofile, lname, type, &statp);
+           print_ls_output(jcr, attr);
+           /* set attributes now because file will not be extracted */
+           set_attributes(jcr, attr, &bfd);
            break;
         }  
-
-        jcr->num_files_examined++;
+        break;
 
       /* Data stream */
-      } else if (stream == STREAM_FILE_DATA || stream == STREAM_SPARSE_DATA) {
+      case STREAM_FILE_DATA:
+      case STREAM_SPARSE_DATA: 
+      case STREAM_WIN32_DATA:  
+
         if (extract) {
            if (stream == STREAM_SPARSE_DATA) {
               ser_declare;
@@ -298,10 +204,12 @@ void do_restore(JCR *jcr)
               unser_uint64(faddr);
               if (fileAddr != faddr) {
                  fileAddr = faddr;
-                 if (lseek(ofd, (off_t)fileAddr, SEEK_SET) < 0) {
+                 if (blseek(&bfd, (off_t)fileAddr, SEEK_SET) < 0) {
                      Jmsg3(jcr, M_ERROR, 0, _("Seek to %s error on %s: ERR=%s\n"),
-                        edit_uint64(fileAddr, ec1), ofile, strerror(errno));
-                    goto bail_out;
+                        edit_uint64(fileAddr, ec1), attr->ofname, berror(&bfd));
+                    extract = false;
+                    bclose(&bfd);
+                    continue;
                  }
               }
            } else {
@@ -309,37 +217,45 @@ void do_restore(JCR *jcr)
               wsize = sd->msglen;
            }
             Dmsg2(30, "Write %u bytes, total before write=%u\n", wsize, total);
-           if ((uint32_t)write(ofd, wbuf, wsize) != wsize) {
+           if ((uint32_t)bwrite(&bfd, wbuf, wsize) != wsize) {
                Dmsg0(0, "===Write error===\n");
-               Jmsg2(jcr, M_ERROR, 0, _("Write error on %s: %s\n"), ofile, strerror(errno));
-              goto bail_out;
-           }
+               Jmsg2(jcr, M_ERROR, 0, _("Write error on %s: ERR=%s\n"), attr->ofname, berror(&bfd));
+              extract = false;
+              bclose(&bfd);
+              continue;
+           } 
            total += wsize;
            jcr->JobBytes += wsize;
+           jcr->ReadBytes += wsize;
            fileAddr += wsize;
         }
-       
+        break;
+
       /* GZIP data stream */
-      } else if (stream == STREAM_GZIP_DATA || stream == STREAM_SPARSE_GZIP_DATA) {
+      case STREAM_GZIP_DATA:
+      case STREAM_SPARSE_GZIP_DATA:  
+      case STREAM_WIN32_GZIP_DATA:  
 #ifdef HAVE_LIBZ
         if (extract) {
-           ser_declare;
            uLong compress_len;
-           uint64_t faddr;
-           char ec1[50];
            int stat;
 
            if (stream == STREAM_SPARSE_GZIP_DATA) {
+              ser_declare;
+              uint64_t faddr;
+              char ec1[50];
               wbuf = sd->msg + SPARSE_FADDR_SIZE;
               wsize = sd->msglen - SPARSE_FADDR_SIZE;
               ser_begin(sd->msg, SPARSE_FADDR_SIZE);
               unser_uint64(faddr);
               if (fileAddr != faddr) {
                  fileAddr = faddr;
-                 if (lseek(ofd, (off_t)fileAddr, SEEK_SET) < 0) {
+                 if (blseek(&bfd, (off_t)fileAddr, SEEK_SET) < 0) {
                      Jmsg3(jcr, M_ERROR, 0, _("Seek to %s error on %s: ERR=%s\n"),
-                        edit_uint64(fileAddr, ec1), ofile, strerror(errno));
-                    goto bail_out;
+                        edit_uint64(fileAddr, ec1), attr->ofname, berror(&bfd));
+                    extract = false;
+                    bclose(&bfd);
+                    continue;
                  }
               }
            } else {
@@ -350,46 +266,70 @@ void do_restore(JCR *jcr)
             Dmsg2(100, "Comp_len=%d msglen=%d\n", compress_len, wsize);
            if ((stat=uncompress((Byte *)jcr->compress_buf, &compress_len, 
                  (const Byte *)wbuf, (uLong)wsize)) != Z_OK) {
-               Jmsg(jcr, M_ERROR, 0, _("Uncompression error. ERR=%d\n"), stat);
-              goto bail_out;
+               Jmsg(jcr, M_ERROR, 0, _("Uncompression error on file %s. ERR=%s\n"), 
+                 attr->ofname, zlib_strerror(stat));
+              extract = false;
+              bclose(&bfd);
+              continue;
            }
 
             Dmsg2(100, "Write uncompressed %d bytes, total before write=%d\n", compress_len, total);
-           if ((uLong)write(ofd, jcr->compress_buf, compress_len) != compress_len) {
+           if ((uLong)bwrite(&bfd, jcr->compress_buf, compress_len) != compress_len) {
                Dmsg0(0, "===Write error===\n");
-               Jmsg2(jcr, M_ERROR, 0, "Write error on %s: %s\n", ofile, strerror(errno));
-              goto bail_out;
+               Jmsg2(jcr, M_ERROR, 0, _("Write error on %s: %s\n"), attr->ofname, berror(&bfd));
+              extract = false;
+              bclose(&bfd);
+              continue;
            }
            total += compress_len;
            jcr->JobBytes += compress_len;
+           jcr->ReadBytes += wsize;
            fileAddr += compress_len;
         }
 #else
         if (extract) {
-            Jmsg(jcr, M_ERROR, 0, "GZIP data stream found, but GZIP not configured!\n");
-           goto bail_out;
+            Jmsg(jcr, M_ERROR, 0, _("GZIP data stream found, but GZIP not configured!\n"));
+           extract = false;
+           bclose(&bfd);
+           continue;
         }
 #endif
-      /* If extracting, wierd stream (not 1 or 2), close output file anyway */
-      } else if (extract) {
-         Dmsg1(30, "Found wierd stream %d\n", stream);
-        if (ofd < 0) {
-            Jmsg0(jcr, M_ERROR, 0, _("Logic error output file should be open\n"));
+        break;
+
+      case STREAM_MD5_SIGNATURE:
+      case STREAM_SHA1_SIGNATURE:
+        break;
+
+      case STREAM_PROGRAM_NAMES:
+      case STREAM_PROGRAM_DATA:
+        if (!prog_name_msg) {
+            Pmsg0(000, "Got Program Name or Data Stream. Ignored.\n");
+           prog_name_msg++;
         }
-        set_attributes(jcr, fname, ofile, lname, type, stream, 
-                       &statp, attribsEx, &ofd);
-        extract = FALSE;
-      } else if (!(stream == STREAM_MD5_SIGNATURE || stream == STREAM_SHA1_SIGNATURE)) {
+        break;
+
+      default:
+        /* If extracting, wierd stream (not 1 or 2), close output file anyway */
+        if (extract) {
+            Dmsg1(30, "Found wierd stream %d\n", stream);
+           if (!is_bopen(&bfd)) {
+               Jmsg0(jcr, M_ERROR, 0, _("Logic error output file should be open but is not.\n"));
+           }
+           set_attributes(jcr, attr, &bfd);
+           extract = false;
+        }
+         Jmsg(jcr, M_ERROR, 0, _("Unknown stream=%d ignored. This shouldn't happen!\n"), stream);
          Dmsg2(0, "None of above!!! stream=%d data=%s\n", stream,sd->msg);
-      }
-   }
+        break;
+      } /* end switch(stream) */
+
+   } /* end while get_msg() */
 
    /* If output file is still open, it was the last one in the
     * archive since we just hit an end of file, so close the file. 
     */
-   if (ofd >= 0) {
-      set_attributes(jcr, fname, ofile, lname, type, stream, 
-                    &statp, attribsEx, &ofd);
+   if (is_bopen(&bfd)) {
+      set_attributes(jcr, attr, &bfd);
    }
    set_jcr_job_status(jcr, JS_Terminated);
    goto ok_out;
@@ -401,52 +341,38 @@ ok_out:
       free(jcr->compress_buf);
       jcr->compress_buf = NULL;
    }
-   free_pool_memory(fname);
-   free_pool_memory(ofile);
-   free_pool_memory(lname);
-   free_pool_memory(attribsEx);
+   bclose(&bfd);
+   free_attr(attr);
    Dmsg2(10, "End Do Restore. Files=%d Bytes=%" lld "\n", jcr->JobFiles,
       jcr->JobBytes);
+   if (non_support_data > 1 || non_support_attr > 1) {
+      Jmsg(jcr, M_ERROR, 0, _("%d non-supported data streams and %d non-supported attrib streams ignored.\n"),
+        non_support_data, non_support_attr);
+   }
 }         
 
-extern char *getuser(uid_t uid);
-extern char *getgroup(gid_t gid);
-
 /*
- * Print an ls style message, also send INFO
+ * Convert ZLIB error code into an ASCII message
  */
-static void print_ls_output(JCR *jcr, char *fname, char *lname, int type, struct stat *statp)
+static char *zlib_strerror(int stat)
 {
-   char buf[2000]; 
-   char ec1[30];
-   char *p, *f;
-   int n;
-
-   p = encode_mode(statp->st_mode, buf);
-   n = sprintf(p, "  %2d ", (uint32_t)statp->st_nlink);
-   p += n;
-   n = sprintf(p, "%-8.8s %-8.8s", getuser(statp->st_uid), getgroup(statp->st_gid));
-   p += n;
-   n = sprintf(p, "%8.8s ", edit_uint64(statp->st_size, ec1));
-   p += n;
-   p = encode_time(statp->st_ctime, p);
-   *p++ = ' ';
-   *p++ = ' ';
-   for (f=fname; *f && (p-buf) < (int)sizeof(buf)-10; ) {
-      *p++ = *f++;
+   if (stat >= 0) {
+      return "None";
    }
-   if (type == FT_LNK) {
-      *p++ = ' ';
-      *p++ = '-';
-      *p++ = '>';
-      *p++ = ' ';
-      /* Copy link name */
-      for (f=lname; *f && (p-buf) < (int)sizeof(buf)-10; ) {
-        *p++ = *f++;
-      }
+   switch (stat) {
+   case Z_ERRNO:
+      return "Zlib errno";
+   case Z_STREAM_ERROR:
+      return "Zlib stream error";
+   case Z_DATA_ERROR:
+      return "Zlib data error";
+   case Z_MEM_ERROR:
+      return "Zlib memory error";
+   case Z_BUF_ERROR:
+      return "Zlib buffer error";
+   case Z_VERSION_ERROR:
+      return "Zlib version error";
+   default:
+      return "*none*";
    }
-   *p++ = '\n';
-   *p = 0;
-   Dmsg0(20, buf);
-   Jmsg(jcr, M_RESTORED, 0, "%s", buf);
 }