]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/filed/restore.c
BFILE I/O, new console @ commands, regression, file mode restore fixes
[bacula/bacula] / bacula / src / filed / restore.c
index a0acc19d5127be7063524b7e6ce1dd48cf38410e..da0f6c136fe97997476b9e064403701faf800de4 100644 (file)
@@ -7,7 +7,7 @@
  *
  */
 /*
-   Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
+   Copyright (C) 2000-2003 Kern Sibbald and John Walker
 
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
@@ -55,7 +55,7 @@ void do_restore(JCR *jcr)
    uint32_t record_file_index;
    struct stat statp;
    int extract = FALSE;
-   int ofd = -1;
+   BFILE bfd;
    int type, stat;
    uint32_t total = 0;               /* Job total but only 32 bits for debug */
    char *wbuf;                       /* write buffer */
@@ -64,10 +64,12 @@ void do_restore(JCR *jcr)
    
    wherelen = strlen(jcr->where);
 
+   binit(&bfd);
    sd = jcr->store_bsock;
-   jcr->JobStatus = JS_Running;
+   set_jcr_job_status(jcr, JS_Running);
 
    if (!bnet_set_buffer_size(sd, MAX_NETWORK_BUFFER_SIZE, BNET_SETBUF_READ)) {
+      set_jcr_job_status(jcr, JS_ErrorTerminated);
       return;
    }
    jcr->buf_size = sd->msglen;
@@ -89,10 +91,10 @@ void do_restore(JCR *jcr)
     *  2. Stream data
     *       a. Attributes (Unix or Win32)
     *   or  b. File data for the file
-    *   or  c. Possibly MD5 record
+    *   or  c. Possibly MD5 or SHA1 record
     *  3. Repeat step 1
     */
-   while (bnet_recv(sd) >= 0 && !job_cancelled(jcr)) {
+   while (bnet_recv(sd) >= 0 && !job_canceled(jcr)) {
       /*
        * First we expect a Stream Record Header 
        */
@@ -106,8 +108,9 @@ void do_restore(JCR *jcr)
       /* 
        * Now we expect the Stream Data
        */
-      if (bnet_recv(sd) < 0 && !job_cancelled(jcr)) {
+      if (bnet_recv(sd) < 0) {
          Jmsg1(jcr, M_FATAL, 0, _("Data record error. ERR=%s\n"), bnet_strerror(sd));
+        goto bail_out;
       }
       if (size != (uint32_t)sd->msglen) {
          Jmsg2(jcr, M_FATAL, 0, _("Actual data size %d not same as header %d\n"), sd->msglen, size);
@@ -118,17 +121,18 @@ void do_restore(JCR *jcr)
       /* File Attributes stream */
       if (stream == STREAM_UNIX_ATTRIBUTES || stream == STREAM_WIN32_ATTRIBUTES) {
         char *ap, *lp, *fp, *apex;
+        uint32_t LinkFI;
 
          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) {
-               Emsg0(M_ERROR, 0, _("Logic error output file should be open\n"));
+           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);
+                          &statp, attribsEx, &bfd);
            extract = FALSE;
             Dmsg0(30, "Stop extracting.\n");
         }
@@ -205,7 +209,7 @@ void do_restore(JCR *jcr)
 
          Dmsg3(200, "File %s\nattrib=%s\nattribsEx=%s\n", fname, ap, attribsEx);
 
-        decode_stat(ap, &statp);
+        decode_stat(ap, &statp, &LinkFI);
         /*
          * Prepend the where directory so that the
          * files are put where the user wants.
@@ -229,18 +233,34 @@ void do_restore(JCR *jcr)
            /* 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 */
-           if (type == FT_LNK || type == FT_LNKSAVED) {
-               if (lp[0] == '/') {      /* if absolute path */
+           /*
+            * Fixup link name -- if it is an absolute path
+            */
+           if (type == FT_LNKSAVED || type == FT_LNK) {
+              int add_link;
+              /* Always add prefix to hard links (FT_LNKSAVED) and
+               *  on user request to soft links
+               */
+               if (lp[0] == '/' &&
+                  (type == FT_LNKSAVED || jcr->prefix_links)) {
                  strcpy(lname, jcr->where);
-              }       
+                 add_link = 1;
+              } else {
+                 lname[0] = 0;
+                 add_link = 0;
+              }
                if (win32_client && lp[1] == ':') {
-                 strcat(lname, lp+2); /* copy rest of name */
+                 fn = lp+2;             /* skip over drive: */
               } else {
-                 strcat(lname, lp);   /* On Unix systems we take everything */
+                 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 */
            }
         }
 
@@ -248,19 +268,28 @@ void do_restore(JCR *jcr)
 
         extract = FALSE;
         stat = create_file(jcr, fname, ofile, lname, type, 
-                           stream, &statp, attribsEx, &ofd, jcr->replace);
+                           stream, &statp, attribsEx, &bfd, jcr->replace);
         switch (stat) {
         case CF_ERROR:
         case CF_SKIP:
            break;
         case CF_EXTRACT:
            extract = TRUE;
+           P(jcr->mutex);
            pm_strcpy(&jcr->last_fname, ofile);
-           /* Fall-through wanted */
+           V(jcr->mutex);
+           jcr->JobFiles++;
+           fileAddr = 0;
+           print_ls_output(jcr, ofile, lname, type, &statp);
+           /* Set attributes after file extracted */
+           break;
         case CF_CREATED:
            jcr->JobFiles++;
            fileAddr = 0;
            print_ls_output(jcr, ofile, lname, type, &statp);
+           /* set attributes now because file will not be extracted */
+           set_attributes(jcr, fname, ofile, lname, type, stream, 
+                          &statp, attribsEx, &bfd);
            break;
         }  
 
@@ -280,7 +309,7 @@ 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;
@@ -291,7 +320,7 @@ 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;
@@ -318,7 +347,7 @@ 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;
@@ -337,7 +366,7 @@ void do_restore(JCR *jcr)
            }
 
             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;
@@ -355,13 +384,13 @@ void do_restore(JCR *jcr)
       /* 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) {
-            Emsg0(M_ERROR, 0, _("Logic error output file should be open\n"));
+        if (!is_bopen(&bfd)) {
+            Jmsg0(jcr, M_ERROR, 0, _("Logic error output file should be open but is not.\n"));
         }
         set_attributes(jcr, fname, ofile, lname, type, stream, 
-                       &statp, attribsEx, &ofd);
+                       &statp, attribsEx, &bfd);
         extract = FALSE;
-      } else if (stream != STREAM_MD5_SIGNATURE) {
+      } else if (!(stream == STREAM_MD5_SIGNATURE || stream == STREAM_SHA1_SIGNATURE)) {
          Dmsg2(0, "None of above!!! stream=%d data=%s\n", stream,sd->msg);
       }
    }
@@ -369,15 +398,15 @@ void do_restore(JCR *jcr)
    /* 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) {
+   if (is_bopen(&bfd)) {
       set_attributes(jcr, fname, ofile, lname, type, stream, 
-                    &statp, attribsEx, &ofd);
+                    &statp, attribsEx, &bfd);
    }
-   jcr->JobStatus = JS_Terminated;
+   set_jcr_job_status(jcr, JS_Terminated);
    goto ok_out;
 
 bail_out:
-   jcr->JobStatus = JS_ErrorTerminated;
+   set_jcr_job_status(jcr, JS_ErrorTerminated);
 ok_out:
    if (jcr->compress_buf) {
       free(jcr->compress_buf);
@@ -414,19 +443,21 @@ static void print_ls_output(JCR *jcr, char *fname, char *lname, int type, struct
    p = encode_time(statp->st_ctime, p);
    *p++ = ' ';
    *p++ = ' ';
-   for (f=fname; *f && (p-buf) < (int)sizeof(buf); )
+   for (f=fname; *f && (p-buf) < (int)sizeof(buf)-10; ) {
       *p++ = *f++;
+   }
    if (type == FT_LNK) {
       *p++ = ' ';
       *p++ = '-';
       *p++ = '>';
       *p++ = ' ';
       /* Copy link name */
-      for (f=lname; *f && (p-buf) < (int)sizeof(buf); )
+      for (f=lname; *f && (p-buf) < (int)sizeof(buf)-10; ) {
         *p++ = *f++;
+      }
    }
    *p++ = '\n';
    *p = 0;
    Dmsg0(20, buf);
-   Jmsg(jcr, M_INFO, 0, buf);
+   Jmsg(jcr, M_RESTORED, 0, "%s", buf);
 }