LCOV - code coverage report
Current view: top level - src/server - restore.c (source / functions) Hit Total Coverage
Test: burp-coverage-clean.info Lines: 183 288 63.5 %
Date: 2016-02-29 Functions: 12 15 80.0 %

          Line data    Source code
       1             : #include "../burp.h"
       2             : #include "../alloc.h"
       3             : #include "../asfd.h"
       4             : #include "../async.h"
       5             : #include "../bu.h"
       6             : #include "../cmd.h"
       7             : #include "../cntr.h"
       8             : #include "../handy.h"
       9             : #include "../linkhash.h"
      10             : #include "../log.h"
      11             : #include "../pathcmp.h"
      12             : #include "../prepend.h"
      13             : #include "../protocol2/blk.h"
      14             : #include "../regexp.h"
      15             : #include "../slist.h"
      16             : #include "../strlist.h"
      17             : #include "bu_get.h"
      18             : #include "child.h"
      19             : #include "compress.h"
      20             : #include "manio.h"
      21             : #include "protocol1/restore.h"
      22             : #include "protocol2/dpth.h"
      23             : #include "protocol2/rblk.h"
      24             : #include "protocol2/restore.h"
      25             : #include "protocol2/restore_spool.h"
      26             : #include "sdirs.h"
      27             : 
      28           5 : static enum asl_ret restore_end_func(struct asfd *asfd,
      29             :         struct conf **confs, void *param)
      30             : {
      31           5 :         if(!strcmp(asfd->rbuf->buf, "restoreend ok"))
      32             :                 return ASL_END_OK;
      33             :         // Old v2 clients send something slightly different.
      34           0 :         if(!strcmp(asfd->rbuf->buf, "restoreend_ok"))
      35             :                 return ASL_END_OK;
      36           0 :         iobuf_log_unexpected(asfd->rbuf, __func__);
      37           0 :         return ASL_END_ERROR;
      38             : }
      39             : 
      40           5 : static int restore_end(struct asfd *asfd, struct conf **confs)
      41             : {
      42           5 :         if(asfd->write_str(asfd, CMD_GEN, "restoreend")) return -1;
      43           5 :         return asfd->simple_loop(asfd, confs, NULL, __func__, restore_end_func);
      44             : }
      45             : 
      46           0 : static int srestore_matches(struct strlist *s, const char *path)
      47             : {
      48           0 :         int r=0;
      49           0 :         if(!s->flag) return 0; // Do not know how to do excludes yet.
      50           0 :         if((r=strncmp_w(path, s->path))) return 0; // no match
      51           0 :         if(!r) return 1; // exact match
      52           0 :         if(*(path+strlen(s->path)+1)=='/')
      53             :                 return 1; // matched directory contents
      54             :         return 0; // no match
      55             : }
      56             : 
      57             : // Used when restore is initiated from the server.
      58           0 : static int check_srestore(struct conf **confs, const char *path)
      59             : {
      60           0 :         struct strlist *l=get_strlist(confs[OPT_INCEXCDIR]);
      61             : 
      62             :         // If no includes specified, restore everything.
      63           0 :         if(!l) return 1;
      64             : 
      65           0 :         for(; l; l=l->next)
      66           0 :                 if(srestore_matches(l, path))
      67             :                         return 1;
      68             :         return 0;
      69             : }
      70             : 
      71          42 : int want_to_restore(int srestore, struct sbuf *sb,
      72             :         regex_t *regex, struct conf **cconfs)
      73             : {
      74           0 :         return (!srestore || check_srestore(cconfs, sb->path.buf))
      75          84 :           && (!regex || regex_check(regex, sb->path.buf));
      76             : }
      77             : 
      78           5 : static int setup_cntr(struct asfd *asfd, const char *manifest,
      79             :         regex_t *regex, int srestore,
      80             :         enum action act, char status, struct conf **cconfs)
      81             : {
      82           5 :         int ars=0;
      83           5 :         int ret=-1;
      84           5 :         struct fzp *fzp=NULL;
      85           5 :         struct sbuf *sb=NULL;
      86           5 :         struct cntr *cntr=NULL;
      87             : 
      88           5 :         cntr=get_cntr(cconfs);
      89           5 :         if(!cntr) return 0;
      90             : 
      91             : // FIX THIS: this is only trying to work for protocol1.
      92           0 :         if(get_protocol(cconfs)!=PROTO_1) return 0;
      93             : 
      94           0 :         if(!(sb=sbuf_alloc(PROTO_1))) goto end;
      95           0 :         if(!(fzp=fzp_gzopen(manifest, "rb")))
      96             :         {
      97           0 :                 log_and_send(asfd, "could not open manifest");
      98             :                 goto end;
      99             :         }
     100             :         while(1)
     101             :         {
     102           0 :                 if((ars=sbuf_fill_from_file(sb, fzp, NULL, NULL)))
     103             :                 {
     104           0 :                         if(ars<0) goto end;
     105             :                         // ars==1 means end ok
     106             :                         break;
     107             :                 }
     108             :                 else
     109             :                 {
     110           0 :                         if(want_to_restore(srestore, sb, regex, cconfs))
     111             :                         {
     112           0 :                                 cntr_add_phase1(cntr, sb->path.cmd, 0);
     113           0 :                                 if(sb->endfile.buf)
     114             :                                   cntr_add_val(cntr,
     115             :                                         CMD_BYTES_ESTIMATED,
     116             :                                         strtoull(sb->endfile.buf,
     117           0 :                                                 NULL, 10), 0);
     118             :                         }
     119             :                 }
     120           0 :                 sbuf_free_content(sb);
     121             :         }
     122           0 :         ret=0;
     123             : end:
     124           0 :         sbuf_free(&sb);
     125           0 :         fzp_close(&fzp);
     126             :         return ret;
     127             : }
     128             : 
     129             : static int restore_sbuf(struct asfd *asfd, struct sbuf *sb, struct bu *bu,
     130             :         enum action act, struct sdirs *sdirs, enum cntr_status cntr_status,
     131             :         struct conf **cconfs, struct sbuf *need_data, const char *manifest,
     132             :         struct slist *slist);
     133             : 
     134             : // Used when restoring a hard link that we have not restored the destination
     135             : // for. Read through the manifest from the beginning and substitute the path
     136             : // and data to the new location.
     137           0 : static int hard_link_substitution(struct asfd *asfd,
     138             :         struct sbuf *sb, struct f_link *lp,
     139             :         struct bu *bu, enum action act, struct sdirs *sdirs,
     140             :         enum cntr_status cntr_status, struct conf **cconfs,
     141             :         const char *manifest, struct slist *slist)
     142             : {
     143           0 :         int ret=-1;
     144           0 :         struct sbuf *need_data=NULL;
     145           0 :         int last_ent_was_dir=0;
     146           0 :         struct sbuf *hb=NULL;
     147           0 :         struct manio *manio=NULL;
     148           0 :         struct blk *blk=NULL;
     149             :         int pcmp;
     150           0 :         enum protocol protocol=get_protocol(cconfs);
     151           0 :         struct cntr *cntr=get_cntr(cconfs);
     152             : 
     153           0 :         if(!(manio=manio_open(manifest, "rb", protocol))
     154           0 :           || !(need_data=sbuf_alloc(protocol))
     155           0 :           || !(hb=sbuf_alloc(protocol)))
     156             :                 goto end;
     157             : 
     158           0 :         if(protocol==PROTO_2)
     159             :         {
     160           0 :                   if(!(blk=blk_alloc()))
     161             :                         goto end;
     162             :         }
     163             : 
     164             :         while(1)
     165             :         {
     166           0 :                 switch(manio_read_with_blk(manio,
     167           0 :                         hb, need_data->path.buf?blk:NULL, sdirs))
     168             :                 {
     169             :                         case 0: break; // Keep going.
     170           0 :                         case 1: ret=0; goto end; // Finished OK.
     171             :                         default: goto end; // Error;
     172             :                 }
     173             : 
     174           0 :                 if(protocol==PROTO_2)
     175             :                 {
     176           0 :                         if(hb->endfile.buf)
     177             :                         {
     178           0 :                                 sbuf_free_content(hb);
     179             :                                 continue;
     180             :                         }
     181           0 :                         if(blk->data)
     182             :                         {
     183           0 :                                 if(protocol2_extra_restore_stream_bits(asfd,
     184             :                                         blk, slist, act, need_data,
     185           0 :                                         last_ent_was_dir, cntr)) goto end;
     186             :                                 continue;
     187             :                         }
     188           0 :                         sbuf_free_content(need_data);
     189             :                 }
     190             : 
     191           0 :                 pcmp=pathcmp(lp->name, hb->path.buf);
     192             : 
     193           0 :                 if(!pcmp && (sbuf_is_filedata(hb) || sbuf_is_vssdata(hb)))
     194             :                 {
     195             :                         // Copy the path from sb to hb.
     196           0 :                         free_w(&hb->path.buf);
     197           0 :                         if(!(hb->path.buf=strdup_w(sb->path.buf, __func__)))
     198             :                                 goto end;
     199             :                         // Should now be able to restore the original data
     200             :                         // to the new location.
     201             :                         ret=restore_sbuf(asfd, hb, bu, act, sdirs,
     202           0 :                           cntr_status, cconfs, need_data, manifest, slist);
     203             :                         // May still need to get protocol2 data.
     204           0 :                         if(!ret && need_data->path.buf) continue;
     205             :                         break;
     206             :                 }
     207             : 
     208           0 :                 sbuf_free_content(hb);
     209             :                 // Break out once we have gone past the entry that we are
     210             :                 // interested in.
     211           0 :                 if(pcmp<0) break;
     212             :         }
     213             : end:
     214           0 :         blk_free(&blk);
     215           0 :         sbuf_free(&hb);
     216           0 :         manio_close(&manio);
     217           0 :         return ret;
     218             : }
     219             : 
     220          42 : static int restore_sbuf(struct asfd *asfd, struct sbuf *sb, struct bu *bu,
     221             :         enum action act, struct sdirs *sdirs, enum cntr_status cntr_status,
     222             :         struct conf **cconfs, struct sbuf *need_data, const char *manifest,
     223             :         struct slist *slist)
     224             : {
     225             :         //printf("%s: %s\n", act==ACTION_RESTORE?"restore":"verify", sb->path.buf);
     226          42 :         if(write_status(cntr_status, sb->path.buf, get_cntr(cconfs)))
     227             :                 return -1;
     228             : 
     229          42 :         if(sb->path.cmd==CMD_HARD_LINK)
     230             :         {
     231           1 :                 struct f_link *lp=NULL;
     232           1 :                 struct f_link **bucket=NULL;
     233           1 :                 if((lp=linkhash_search(&sb->statp, &bucket)))
     234             :                 {
     235             :                         // It is in the list of stuff that is in the manifest,
     236             :                         // but was skipped on this restore.
     237             :                         // Need to go through the manifest from the beginning,
     238             :                         // and substitute in the data to restore to this
     239             :                         // location.
     240             :                         return hard_link_substitution(asfd, sb, lp,
     241             :                                 bu, act, sdirs,
     242           0 :                                 cntr_status, cconfs, manifest, slist);
     243             :                         // FIX THIS: Would be nice to remember the new link
     244             :                         // location so that further hard links would link to
     245             :                         // it instead of doing the hard_link_substitution
     246             :                         // business over again.
     247             :                 }
     248             :         }
     249             : 
     250          42 :         if(get_protocol(cconfs)==PROTO_1)
     251             :         {
     252             :                 return restore_sbuf_protocol1(asfd, sb, bu,
     253          10 :                   act, sdirs, cntr_status, cconfs);
     254             :         }
     255             :         else
     256             :         {
     257             :                 return restore_sbuf_protocol2(asfd, sb,
     258          32 :                   act, cntr_status, get_cntr(cconfs), need_data);
     259             :         }
     260             : }
     261             : 
     262          42 : int restore_ent(struct asfd *asfd,
     263             :         struct sbuf **sb,
     264             :         struct slist *slist,
     265             :         struct bu *bu,
     266             :         enum action act,
     267             :         struct sdirs *sdirs,
     268             :         enum cntr_status cntr_status,
     269             :         struct conf **cconfs,
     270             :         struct sbuf *need_data,
     271             :         int *last_ent_was_dir,
     272             :         const char *manifest)
     273             : {
     274          42 :         int ret=-1;
     275             :         struct sbuf *xb;
     276             : 
     277          42 :         if(!(*sb)->path.buf)
     278             :         {
     279           0 :                 logp("Got NULL path!\n");
     280           0 :                 return -1;
     281             :         }
     282             : 
     283             :         // Check if we have any directories waiting to be restored.
     284          42 :         while((xb=slist->head))
     285             :         {
     286           0 :                 if(is_subdir(xb->path.buf, (*sb)->path.buf))
     287             :                 {
     288             :                         // We are still in a subdir.
     289             :                         break;
     290             :                 }
     291             :                 else
     292             :                 {
     293             :                         // Can now restore xb because nothing else is fiddling
     294             :                         // in a subdirectory.
     295           0 :                         if(restore_sbuf(asfd, xb, bu,
     296             :                           act, sdirs, cntr_status, cconfs, need_data, manifest,
     297           0 :                           slist))
     298             :                                 goto end;
     299           0 :                         slist->head=xb->next;
     300           0 :                         sbuf_free(&xb);
     301             :                 }
     302             :         }
     303             : 
     304             :         /* If it is a directory, need to remember it and restore it later, so
     305             :            that the permissions come out right. */
     306             :         /* Meta data of directories will also have the stat stuff set to be a
     307             :            directory, so will also come out at the end. */
     308             :         /* FIX THIS: for Windows, need to read and remember the blocks that
     309             :            go with the directories. Probably have to do the same for metadata
     310             :            that goes with directories. */
     311          42 :         if(S_ISDIR((*sb)->statp.st_mode))
     312             :         {
     313             :                 // Add to the head of the list instead of the tail.
     314           0 :                 (*sb)->next=slist->head;
     315           0 :                 slist->head=*sb;
     316             : 
     317           0 :                 *last_ent_was_dir=1;
     318             : 
     319             :                 // Allocate a new sb.
     320           0 :                 if(!(*sb=sbuf_alloc(get_protocol(cconfs)))) goto end;
     321             :         }
     322             :         else
     323             :         {
     324          42 :                 *last_ent_was_dir=0;
     325          42 :                 if(restore_sbuf(asfd, *sb, bu,
     326             :                   act, sdirs, cntr_status, cconfs, need_data, manifest,
     327          42 :                   slist))
     328             :                         goto end;
     329             :         }
     330             :         ret=0;
     331             : end:
     332          42 :         return ret;
     333             : }
     334             : 
     335           5 : static int restore_remaining_dirs(struct asfd *asfd, struct bu *bu,
     336             :         struct slist *slist, enum action act, struct sdirs *sdirs,
     337             :         enum cntr_status cntr_status, struct conf **cconfs)
     338             : {
     339           5 :         int ret=-1;
     340             :         struct sbuf *sb;
     341           5 :         struct sbuf *need_data=NULL;
     342           5 :         if(!(need_data=sbuf_alloc(get_protocol(cconfs)))) goto end;
     343             :         // Restore any directories that are left in the list.
     344           5 :         for(sb=slist->head; sb; sb=sb->next)
     345             :         {
     346           0 :                 if(get_protocol(cconfs)==PROTO_1)
     347             :                 {
     348           0 :                         if(restore_sbuf_protocol1(asfd, sb, bu, act,
     349           0 :                                 sdirs, cntr_status, cconfs))
     350             :                                         goto end;
     351             :                 }
     352             :                 else
     353             :                 {
     354           0 :                         if(restore_sbuf_protocol2(asfd, sb, act,
     355           0 :                                 cntr_status, get_cntr(cconfs), need_data))
     356             :                                         goto end;
     357             :                 }
     358             :         }
     359             :         ret=0;
     360             : end:
     361           5 :         sbuf_free(&need_data);
     362           5 :         return ret;
     363             : }
     364             : 
     365           5 : static int restore_stream(struct asfd *asfd, struct sdirs *sdirs,
     366             :         struct slist *slist, struct bu *bu, const char *manifest,
     367             :         regex_t *regex, int srestore, struct conf **cconfs, enum action act,
     368             :         enum cntr_status cntr_status)
     369             : {
     370           5 :         int ret=-1;
     371           5 :         int last_ent_was_dir=0;
     372           5 :         struct sbuf *sb=NULL;
     373           5 :         struct iobuf *rbuf=asfd->rbuf;
     374           5 :         struct manio *manio=NULL;
     375           5 :         struct blk *blk=NULL;
     376           5 :         struct sbuf *need_data=NULL;
     377           5 :         enum protocol protocol=get_protocol(cconfs);
     378           5 :         struct cntr *cntr=get_cntr(cconfs);
     379             :         struct iobuf interrupt;
     380           5 :         iobuf_init(&interrupt);
     381             : 
     382           5 :         if(protocol==PROTO_2)
     383             :         {
     384             :                 static int rs_sent=0;
     385           4 :                 if(!(blk=blk_alloc()))
     386             :                         goto end;
     387           4 :                 if(!rs_sent)
     388             :                 {
     389           4 :                         rs_sent=1;
     390           4 :                         if(asfd->write_str(asfd,
     391           4 :                                 CMD_GEN, "restore_stream")
     392           8 :                           || asfd_read_expect(asfd,
     393           4 :                                 CMD_GEN, "restore_stream_ok"))
     394             :                                         goto end;
     395             :                 }
     396             :         }
     397             : 
     398          10 :         if(!(manio=manio_open(manifest, "rb", protocol))
     399           5 :           || !(need_data=sbuf_alloc(protocol))
     400          10 :           || !(sb=sbuf_alloc(protocol)))
     401             :                 goto end;
     402             : 
     403             :         while(1)
     404             :         {
     405         421 :                 iobuf_free_content(rbuf);
     406         421 :                 if(asfd->as->read_quick(asfd->as))
     407             :                 {
     408           0 :                         logp("read quick error\n");
     409           0 :                         goto end;
     410             :                 }
     411         421 :                 if(rbuf->buf) switch(rbuf->cmd)
     412             :                 {
     413             :                         case CMD_MESSAGE:
     414             :                         case CMD_WARNING:
     415             :                         {
     416           0 :                                 log_recvd(rbuf, cntr, 0);
     417           0 :                                 continue;
     418             :                         }
     419             :                         case CMD_INTERRUPT:
     420           5 :                                 if(protocol==PROTO_2)
     421             :                                 {
     422           5 :                                         iobuf_free_content(&interrupt);
     423           5 :                                         iobuf_move(&interrupt, rbuf);
     424             :                                 }
     425             :                                 // PROTO_1:
     426             :                                 // Client wanted to interrupt the
     427             :                                 // sending of a file. But if we are
     428             :                                 // here, we have already moved on.
     429             :                                 // Ignore.
     430             :                                 continue;
     431             :                         default:
     432           0 :                                 iobuf_log_unexpected(rbuf, __func__);
     433           0 :                                 goto end;
     434             :                 }
     435             : 
     436         416 :                 switch(manio_read_with_blk(manio,
     437         416 :                         sb, need_data->path.buf?blk:NULL, sdirs))
     438             :                 {
     439             :                         case 0: break; // Keep going.
     440           5 :                         case 1: ret=0; goto end; // Finished OK.
     441             :                         default: goto end; // Error;
     442             :                 }
     443             : 
     444         411 :                 if(protocol==PROTO_2)
     445             :                 {
     446         401 :                         if(sb->endfile.buf)
     447             :                         {
     448          21 :                                 if(act==ACTION_RESTORE
     449          21 :                                   && asfd->write(asfd, &sb->endfile))
     450             :                                         goto end;
     451          21 :                                 sbuf_free_content(sb);
     452          21 :                                 iobuf_free_content(&interrupt);
     453          21 :                                 continue;
     454             :                         }
     455         380 :                         if(interrupt.buf)
     456             :                         {
     457          61 :                                 if(!need_data->path.buf)
     458             :                                 {
     459           1 :                                         iobuf_free_content(&interrupt);
     460             :                                 }
     461          60 :                                 else if(!iobuf_pathcmp(&need_data->path,
     462          60 :                                         &interrupt))
     463             :                                 {
     464          32 :                                         if(blk->data)
     465          32 :                                                 blk->data=NULL;
     466             :                                         continue;
     467             :                                 }
     468             :                         }
     469         348 :                         if(blk->data)
     470             :                         {
     471         316 :                                 if(protocol2_extra_restore_stream_bits(asfd,
     472             :                                         blk, slist, act, need_data,
     473         316 :                                         last_ent_was_dir, cntr)) goto end;
     474             :                                 continue;
     475             :                         }
     476          32 :                         sbuf_free_content(need_data);
     477             :                 }
     478             : 
     479          42 :                 if(want_to_restore(srestore, sb, regex, cconfs))
     480             :                 {
     481          42 :                         if(restore_ent(asfd, &sb, slist,
     482             :                                 bu, act, sdirs, cntr_status, cconfs,
     483          42 :                                 need_data, &last_ent_was_dir, manifest))
     484             :                                         goto end;
     485             :                 }
     486           0 :                 else if(sbuf_is_filedata(sb) || sbuf_is_vssdata(sb))
     487             :                 {
     488             :                         // Add it to the list of filedata that was not
     489             :                         // restored.
     490           0 :                         struct f_link **bucket=NULL;
     491           0 :                         if(!linkhash_search(&sb->statp, &bucket)
     492           0 :                           && linkhash_add(sb->path.buf, &sb->statp, bucket))
     493             :                                 goto end;
     494             :                 }
     495             : 
     496          42 :                 sbuf_free_content(sb);
     497             :         }
     498             : end:
     499           5 :         blk_free(&blk);
     500           5 :         sbuf_free(&sb);
     501           5 :         sbuf_free(&need_data);
     502           5 :         iobuf_free_content(rbuf);
     503           5 :         iobuf_free_content(&interrupt);
     504           5 :         manio_close(&manio);
     505           5 :         return ret;
     506             : }
     507             : 
     508           5 : static int actual_restore(struct asfd *asfd, struct bu *bu,
     509             :         const char *manifest, regex_t *regex, int srestore, enum action act,
     510             :         struct sdirs *sdirs, enum cntr_status cntr_status, struct conf **cconfs)
     511             : {
     512           5 :         int ret=-1;
     513           5 :         int do_restore_stream=1;
     514             :         // For out-of-sequence directory restoring so that the
     515             :         // timestamps come out right:
     516           5 :         struct slist *slist=NULL;
     517           5 :         struct cntr *cntr=NULL;
     518             : 
     519          10 :         if(linkhash_init()
     520           5 :           || !(slist=slist_alloc()))
     521             :                 goto end;
     522             : 
     523           5 :         if(get_protocol(cconfs)==PROTO_2)
     524             :         {
     525           4 :                 if(rblk_init())
     526             :                         goto end;
     527           4 :                 switch(maybe_restore_spool(asfd, manifest, sdirs, bu,
     528           4 :                         srestore, regex, cconfs, slist, act, cntr_status))
     529             :                 {
     530             :                         case 1: do_restore_stream=0; break;
     531           4 :                         case 0: do_restore_stream=1; break;
     532             :                         default: goto end; // Error;
     533             :                 }
     534             :         }
     535          10 :         if(do_restore_stream && restore_stream(asfd, sdirs, slist,
     536             :                 bu, manifest, regex,
     537           5 :                 srestore, cconfs, act, cntr_status))
     538             :                         goto end;
     539             : 
     540           5 :         if(restore_remaining_dirs(asfd, bu, slist,
     541           5 :                 act, sdirs, cntr_status, cconfs)) goto end;
     542             : 
     543           5 :         if(cconfs) cntr=get_cntr(cconfs);
     544           5 :         cntr_print(cntr, act);
     545           5 :         cntr_stats_to_file(cntr, bu->path, act, cconfs);
     546             : end:
     547           5 :         slist_free(&slist);
     548           5 :         linkhash_free();
     549           5 :         rblk_free();
     550           5 :         return ret;
     551             : }
     552             : 
     553           5 : static int get_logpaths(struct bu *bu, const char *file,
     554             :         char **logpath, char **logpathz)
     555             : {
     556          10 :         if(!(*logpath=prepend_s(bu->path, file))
     557           5 :           || !(*logpathz=prepend(*logpath, ".gz")))
     558             :                 return -1;
     559             :         return 0;
     560             : }
     561             : 
     562          10 : static int restore_manifest(struct asfd *asfd, struct bu *bu,
     563             :         regex_t *regex, int srestore, enum action act, struct sdirs *sdirs,
     564             :         char **dir_for_notify, struct conf **cconfs)
     565             : {
     566           5 :         int ret=-1;
     567           5 :         char *manifest=NULL;
     568           5 :         char *logpath=NULL;
     569           5 :         char *logpathz=NULL;
     570             :         // For sending status information up to the server.
     571           5 :         enum cntr_status cntr_status=CNTR_STATUS_RESTORING;
     572             : 
     573           5 :         if(act==ACTION_RESTORE) cntr_status=CNTR_STATUS_RESTORING;
     574           0 :         else if(act==ACTION_VERIFY) cntr_status=CNTR_STATUS_VERIFYING;
     575             : 
     576          10 :         if((act==ACTION_RESTORE && get_logpaths(bu, "restorelog",
     577           5 :                 &logpath, &logpathz))
     578           5 :           || (act==ACTION_VERIFY && get_logpaths(bu, "verifylog",
     579           0 :                 &logpath, &logpathz))
     580          10 :           || !(manifest=prepend_s(bu->path,
     581           5 :                 get_protocol(cconfs)==PROTO_1?
     582           5 :                         "manifest.gz":"manifest")))
     583             :         {
     584           0 :                 log_and_send_oom(asfd, __func__);
     585           0 :                 goto end;
     586             :         }
     587             : 
     588           5 :         if(log_fzp_set(logpath, cconfs))
     589             :         {
     590           0 :                 char msg[256]="";
     591             :                 snprintf(msg, sizeof(msg),
     592           0 :                                 "could not open log file: %s", logpath);
     593           0 :                 log_and_send(asfd, msg);
     594             :                 goto end;
     595             :         }
     596             : 
     597           5 :         *dir_for_notify=strdup_w(bu->path, __func__);
     598             : 
     599           5 :         log_restore_settings(cconfs, srestore);
     600             : 
     601             :         // First, do a pass through the manifest to set up cntr.
     602             :         // This is the equivalent of a phase1 scan during backup.
     603             : 
     604           5 :         if(setup_cntr(asfd, manifest,
     605           5 :                 regex, srestore, act, cntr_status, cconfs))
     606             :                         goto end;
     607             : 
     608          10 :         if(get_int(cconfs[OPT_SEND_CLIENT_CNTR])
     609           5 :           && cntr_send(get_cntr(cconfs)))
     610             :                 goto end;
     611             : 
     612             :         // Now, do the actual restore.
     613             :         ret=actual_restore(asfd, bu, manifest,
     614           5 :                   regex, srestore, act, sdirs, cntr_status, cconfs);
     615             : end:
     616           5 :         log_fzp_set(NULL, cconfs);
     617           5 :         compress_file(logpath, logpathz, get_int(cconfs[OPT_COMPRESSION]));
     618           5 :         free_w(&manifest);
     619           5 :         free_w(&logpath);
     620           5 :         free_w(&logpathz);
     621           5 :         return ret;
     622             : }
     623             : 
     624           6 : int do_restore_server(struct asfd *asfd, struct sdirs *sdirs,
     625             :         enum action act, int srestore,
     626             :         char **dir_for_notify, struct conf **confs)
     627             : {
     628           6 :         int ret=-1;
     629           6 :         uint8_t found=0;
     630           6 :         struct bu *bu=NULL;
     631           6 :         struct bu *bu_list=NULL;
     632           6 :         unsigned long bno=0;
     633           6 :         regex_t *regex=NULL;
     634           6 :         const char *regexstr=get_string(confs[OPT_REGEX]);
     635           6 :         const char *backup=get_string(confs[OPT_BACKUP]);
     636             : 
     637           6 :         logp("in do_restore\n");
     638             : 
     639           6 :         if(regexstr
     640           1 :           && *regexstr
     641           7 :           && !(regex=regex_compile(regexstr)))
     642             :         {
     643           1 :                 char msg[256]="";
     644             :                 snprintf(msg, sizeof(msg), "unable to compile regex: %s\n",
     645             :                         regexstr);
     646           1 :                 log_and_send(asfd, msg);
     647             :                 goto end;
     648             :         }
     649             : 
     650           5 :         if(bu_get_list(sdirs, &bu_list))
     651             :                 goto end;
     652             : 
     653          10 :         if(bu_list &&
     654             :            (!backup
     655           5 :          || !*backup
     656           5 :          || (!(bno=strtoul(backup, NULL, 10)) && *backup!='a')))
     657             :         {
     658           0 :                 found=1;
     659             :                 // No backup specified, do the most recent.
     660           0 :                 for(bu=bu_list; bu && bu->next; bu=bu->next) { }
     661             :                 ret=restore_manifest(asfd, bu, regex, srestore,
     662           0 :                                 act, sdirs, dir_for_notify, confs);
     663             :         }
     664             : 
     665           5 :         if(!found) for(bu=bu_list; bu; bu=bu->next)
     666             :         {
     667           5 :                 if(!strcmp(bu->timestamp, backup)
     668           5 :                   || bu->bno==bno || (backup && *backup=='a'))
     669             :                 {
     670           5 :                         found=1;
     671             :                         //logp("got: %s\n", bu->path);
     672             :                         ret|=restore_manifest(asfd, bu, regex, srestore,
     673           5 :                                 act, sdirs, dir_for_notify, confs);
     674           5 :                         if(backup && *backup=='a')
     675             :                                 continue;
     676             :                         break;
     677             :                 }
     678             :         }
     679             : 
     680           5 :         bu_list_free(&bu_list);
     681             : 
     682             : 
     683           5 :         if(found)
     684             :         {
     685             :                 // Restore has nearly completed OK.
     686           5 :                 ret=restore_end(asfd, confs);
     687             :         }
     688             :         else
     689             :         {
     690           0 :                 logp("backup not found\n");
     691           0 :                 asfd->write_str(asfd, CMD_ERROR, "backup not found");
     692           0 :                 ret=-1;
     693             :         }
     694             : end:
     695           6 :         regex_free(&regex);
     696           6 :         return ret;
     697             : }

Generated by: LCOV version 1.10