LCOV - code coverage report
Current view: top level - src/server - restore.c (source / functions) Hit Total Coverage
Test: burp-coverage-clean.info Lines: 184 289 63.7 %
Date: 2016-05-30 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          84 :         if(S_ISDIR((*sb)->statp.st_mode)
     312             :           // Hack for metadata for now - just do it straight away.
     313          42 :           && !sbuf_is_metadata(*sb))
     314             :         {
     315             :                 // Add to the head of the list instead of the tail.
     316           0 :                 (*sb)->next=slist->head;
     317           0 :                 slist->head=*sb;
     318             : 
     319           0 :                 *last_ent_was_dir=1;
     320             : 
     321             :                 // Allocate a new sb.
     322           0 :                 if(!(*sb=sbuf_alloc(get_protocol(cconfs)))) goto end;
     323             :         }
     324             :         else
     325             :         {
     326          42 :                 *last_ent_was_dir=0;
     327          42 :                 if(restore_sbuf(asfd, *sb, bu,
     328             :                   act, sdirs, cntr_status, cconfs, need_data, manifest,
     329          42 :                   slist))
     330             :                         goto end;
     331             :         }
     332             :         ret=0;
     333             : end:
     334          42 :         return ret;
     335             : }
     336             : 
     337           5 : static int restore_remaining_dirs(struct asfd *asfd, struct bu *bu,
     338             :         struct slist *slist, enum action act, struct sdirs *sdirs,
     339             :         enum cntr_status cntr_status, struct conf **cconfs)
     340             : {
     341           5 :         int ret=-1;
     342             :         struct sbuf *sb;
     343           5 :         struct sbuf *need_data=NULL;
     344           5 :         if(!(need_data=sbuf_alloc(get_protocol(cconfs)))) goto end;
     345             :         // Restore any directories that are left in the list.
     346           5 :         for(sb=slist->head; sb; sb=sb->next)
     347             :         {
     348           0 :                 if(get_protocol(cconfs)==PROTO_1)
     349             :                 {
     350           0 :                         if(restore_sbuf_protocol1(asfd, sb, bu, act,
     351           0 :                                 sdirs, cntr_status, cconfs))
     352             :                                         goto end;
     353             :                 }
     354             :                 else
     355             :                 {
     356           0 :                         if(restore_sbuf_protocol2(asfd, sb, act,
     357           0 :                                 cntr_status, get_cntr(cconfs), need_data))
     358             :                                         goto end;
     359             :                 }
     360             :         }
     361             :         ret=0;
     362             : end:
     363           5 :         sbuf_free(&need_data);
     364           5 :         return ret;
     365             : }
     366             : 
     367           5 : static int restore_stream(struct asfd *asfd, struct sdirs *sdirs,
     368             :         struct slist *slist, struct bu *bu, const char *manifest,
     369             :         regex_t *regex, int srestore, struct conf **cconfs, enum action act,
     370             :         enum cntr_status cntr_status)
     371             : {
     372           5 :         int ret=-1;
     373           5 :         int last_ent_was_dir=0;
     374           5 :         struct sbuf *sb=NULL;
     375           5 :         struct iobuf *rbuf=asfd->rbuf;
     376           5 :         struct manio *manio=NULL;
     377           5 :         struct blk *blk=NULL;
     378           5 :         struct sbuf *need_data=NULL;
     379           5 :         enum protocol protocol=get_protocol(cconfs);
     380           5 :         struct cntr *cntr=get_cntr(cconfs);
     381             :         struct iobuf interrupt;
     382           5 :         iobuf_init(&interrupt);
     383             : 
     384           5 :         if(protocol==PROTO_2)
     385             :         {
     386             :                 static int rs_sent=0;
     387           4 :                 if(!(blk=blk_alloc()))
     388             :                         goto end;
     389           4 :                 if(!rs_sent)
     390             :                 {
     391           4 :                         rs_sent=1;
     392           4 :                         if(asfd->write_str(asfd,
     393           4 :                                 CMD_GEN, "restore_stream")
     394           8 :                           || asfd_read_expect(asfd,
     395           4 :                                 CMD_GEN, "restore_stream_ok"))
     396             :                                         goto end;
     397             :                 }
     398             :         }
     399             : 
     400          10 :         if(!(manio=manio_open(manifest, "rb", protocol))
     401           5 :           || !(need_data=sbuf_alloc(protocol))
     402          10 :           || !(sb=sbuf_alloc(protocol)))
     403             :                 goto end;
     404             : 
     405             :         while(1)
     406             :         {
     407         421 :                 iobuf_free_content(rbuf);
     408         421 :                 if(asfd->as->read_quick(asfd->as))
     409             :                 {
     410           0 :                         logp("read quick error\n");
     411           0 :                         goto end;
     412             :                 }
     413         421 :                 if(rbuf->buf) switch(rbuf->cmd)
     414             :                 {
     415             :                         case CMD_MESSAGE:
     416             :                         case CMD_WARNING:
     417             :                         {
     418           0 :                                 log_recvd(rbuf, cntr, 0);
     419           0 :                                 continue;
     420             :                         }
     421             :                         case CMD_INTERRUPT:
     422           5 :                                 if(protocol==PROTO_2)
     423             :                                 {
     424           5 :                                         iobuf_free_content(&interrupt);
     425           5 :                                         iobuf_move(&interrupt, rbuf);
     426             :                                 }
     427             :                                 // PROTO_1:
     428             :                                 // Client wanted to interrupt the
     429             :                                 // sending of a file. But if we are
     430             :                                 // here, we have already moved on.
     431             :                                 // Ignore.
     432             :                                 continue;
     433             :                         default:
     434           0 :                                 iobuf_log_unexpected(rbuf, __func__);
     435           0 :                                 goto end;
     436             :                 }
     437             : 
     438         416 :                 switch(manio_read_with_blk(manio,
     439         416 :                         sb, need_data->path.buf?blk:NULL, sdirs))
     440             :                 {
     441             :                         case 0: break; // Keep going.
     442           5 :                         case 1: ret=0; goto end; // Finished OK.
     443             :                         default: goto end; // Error;
     444             :                 }
     445             : 
     446         411 :                 if(protocol==PROTO_2)
     447             :                 {
     448         401 :                         if(sb->endfile.buf)
     449             :                         {
     450          21 :                                 if(act==ACTION_RESTORE
     451          21 :                                   && asfd->write(asfd, &sb->endfile))
     452             :                                         goto end;
     453          21 :                                 sbuf_free_content(sb);
     454          21 :                                 iobuf_free_content(&interrupt);
     455          21 :                                 continue;
     456             :                         }
     457         380 :                         if(interrupt.buf)
     458             :                         {
     459          61 :                                 if(!need_data->path.buf)
     460             :                                 {
     461           1 :                                         iobuf_free_content(&interrupt);
     462             :                                 }
     463          60 :                                 else if(!iobuf_pathcmp(&need_data->path,
     464          60 :                                         &interrupt))
     465             :                                 {
     466          32 :                                         if(blk->data)
     467          32 :                                                 blk->data=NULL;
     468             :                                         continue;
     469             :                                 }
     470             :                         }
     471         348 :                         if(blk->data)
     472             :                         {
     473         316 :                                 if(protocol2_extra_restore_stream_bits(asfd,
     474             :                                         blk, slist, act, need_data,
     475         316 :                                         last_ent_was_dir, cntr)) goto end;
     476             :                                 continue;
     477             :                         }
     478          32 :                         sbuf_free_content(need_data);
     479             :                 }
     480             : 
     481          42 :                 if(want_to_restore(srestore, sb, regex, cconfs))
     482             :                 {
     483          42 :                         if(restore_ent(asfd, &sb, slist,
     484             :                                 bu, act, sdirs, cntr_status, cconfs,
     485          42 :                                 need_data, &last_ent_was_dir, manifest))
     486             :                                         goto end;
     487             :                 }
     488           0 :                 else if(sbuf_is_filedata(sb) || sbuf_is_vssdata(sb))
     489             :                 {
     490             :                         // Add it to the list of filedata that was not
     491             :                         // restored.
     492           0 :                         struct f_link **bucket=NULL;
     493           0 :                         if(!linkhash_search(&sb->statp, &bucket)
     494           0 :                           && linkhash_add(sb->path.buf, &sb->statp, bucket))
     495             :                                 goto end;
     496             :                 }
     497             : 
     498          42 :                 sbuf_free_content(sb);
     499             :         }
     500             : end:
     501           5 :         blk_free(&blk);
     502           5 :         sbuf_free(&sb);
     503           5 :         sbuf_free(&need_data);
     504           5 :         iobuf_free_content(rbuf);
     505           5 :         iobuf_free_content(&interrupt);
     506           5 :         manio_close(&manio);
     507           5 :         return ret;
     508             : }
     509             : 
     510           5 : static int actual_restore(struct asfd *asfd, struct bu *bu,
     511             :         const char *manifest, regex_t *regex, int srestore, enum action act,
     512             :         struct sdirs *sdirs, enum cntr_status cntr_status, struct conf **cconfs)
     513             : {
     514           5 :         int ret=-1;
     515           5 :         int do_restore_stream=1;
     516             :         // For out-of-sequence directory restoring so that the
     517             :         // timestamps come out right:
     518           5 :         struct slist *slist=NULL;
     519           5 :         struct cntr *cntr=NULL;
     520             : 
     521          10 :         if(linkhash_init()
     522           5 :           || !(slist=slist_alloc()))
     523             :                 goto end;
     524             : 
     525           5 :         if(get_protocol(cconfs)==PROTO_2)
     526             :         {
     527           4 :                 if(rblk_init())
     528             :                         goto end;
     529           4 :                 switch(maybe_restore_spool(asfd, manifest, sdirs, bu,
     530           4 :                         srestore, regex, cconfs, slist, act, cntr_status))
     531             :                 {
     532             :                         case 1: do_restore_stream=0; break;
     533           4 :                         case 0: do_restore_stream=1; break;
     534             :                         default: goto end; // Error;
     535             :                 }
     536             :         }
     537          10 :         if(do_restore_stream && restore_stream(asfd, sdirs, slist,
     538             :                 bu, manifest, regex,
     539           5 :                 srestore, cconfs, act, cntr_status))
     540             :                         goto end;
     541             : 
     542           5 :         if(restore_remaining_dirs(asfd, bu, slist,
     543           5 :                 act, sdirs, cntr_status, cconfs)) goto end;
     544             : 
     545           5 :         if(cconfs) cntr=get_cntr(cconfs);
     546           5 :         cntr_print(cntr, act);
     547           5 :         cntr_stats_to_file(cntr, bu->path, act, cconfs);
     548             : end:
     549           5 :         slist_free(&slist);
     550           5 :         linkhash_free();
     551           5 :         rblk_free();
     552           5 :         return ret;
     553             : }
     554             : 
     555           5 : static int get_logpaths(struct bu *bu, const char *file,
     556             :         char **logpath, char **logpathz)
     557             : {
     558          10 :         if(!(*logpath=prepend_s(bu->path, file))
     559           5 :           || !(*logpathz=prepend(*logpath, ".gz")))
     560             :                 return -1;
     561             :         return 0;
     562             : }
     563             : 
     564          10 : static int restore_manifest(struct asfd *asfd, struct bu *bu,
     565             :         regex_t *regex, int srestore, enum action act, struct sdirs *sdirs,
     566             :         char **dir_for_notify, struct conf **cconfs)
     567             : {
     568           5 :         int ret=-1;
     569           5 :         char *manifest=NULL;
     570           5 :         char *logpath=NULL;
     571           5 :         char *logpathz=NULL;
     572             :         // For sending status information up to the server.
     573           5 :         enum cntr_status cntr_status=CNTR_STATUS_RESTORING;
     574             : 
     575           5 :         if(act==ACTION_RESTORE) cntr_status=CNTR_STATUS_RESTORING;
     576           0 :         else if(act==ACTION_VERIFY) cntr_status=CNTR_STATUS_VERIFYING;
     577             : 
     578          10 :         if((act==ACTION_RESTORE && get_logpaths(bu, "restorelog",
     579           5 :                 &logpath, &logpathz))
     580           5 :           || (act==ACTION_VERIFY && get_logpaths(bu, "verifylog",
     581           0 :                 &logpath, &logpathz))
     582          10 :           || !(manifest=prepend_s(bu->path,
     583           5 :                 get_protocol(cconfs)==PROTO_1?
     584           5 :                         "manifest.gz":"manifest")))
     585             :         {
     586           0 :                 log_and_send_oom(asfd, __func__);
     587           0 :                 goto end;
     588             :         }
     589             : 
     590           5 :         if(log_fzp_set(logpath, cconfs))
     591             :         {
     592           0 :                 char msg[256]="";
     593             :                 snprintf(msg, sizeof(msg),
     594           0 :                                 "could not open log file: %s", logpath);
     595           0 :                 log_and_send(asfd, msg);
     596             :                 goto end;
     597             :         }
     598             : 
     599           5 :         *dir_for_notify=strdup_w(bu->path, __func__);
     600             : 
     601           5 :         log_restore_settings(cconfs, srestore);
     602             : 
     603             :         // First, do a pass through the manifest to set up cntr.
     604             :         // This is the equivalent of a phase1 scan during backup.
     605             : 
     606           5 :         if(setup_cntr(asfd, manifest,
     607           5 :                 regex, srestore, act, cntr_status, cconfs))
     608             :                         goto end;
     609             : 
     610          10 :         if(get_int(cconfs[OPT_SEND_CLIENT_CNTR])
     611           5 :           && cntr_send(get_cntr(cconfs)))
     612             :                 goto end;
     613             : 
     614             :         // Now, do the actual restore.
     615             :         ret=actual_restore(asfd, bu, manifest,
     616           5 :                   regex, srestore, act, sdirs, cntr_status, cconfs);
     617             : end:
     618           5 :         log_fzp_set(NULL, cconfs);
     619           5 :         compress_file(logpath, logpathz, get_int(cconfs[OPT_COMPRESSION]));
     620           5 :         free_w(&manifest);
     621           5 :         free_w(&logpath);
     622           5 :         free_w(&logpathz);
     623           5 :         return ret;
     624             : }
     625             : 
     626           6 : int do_restore_server(struct asfd *asfd, struct sdirs *sdirs,
     627             :         enum action act, int srestore,
     628             :         char **dir_for_notify, struct conf **confs)
     629             : {
     630           6 :         int ret=-1;
     631           6 :         uint8_t found=0;
     632           6 :         struct bu *bu=NULL;
     633           6 :         struct bu *bu_list=NULL;
     634           6 :         unsigned long bno=0;
     635           6 :         regex_t *regex=NULL;
     636           6 :         const char *regexstr=get_string(confs[OPT_REGEX]);
     637           6 :         const char *backup=get_string(confs[OPT_BACKUP]);
     638             : 
     639           6 :         logp("in do_restore\n");
     640             : 
     641           6 :         if(regexstr
     642           1 :           && *regexstr
     643           7 :           && !(regex=regex_compile(regexstr)))
     644             :         {
     645           1 :                 char msg[256]="";
     646             :                 snprintf(msg, sizeof(msg), "unable to compile regex: %s\n",
     647             :                         regexstr);
     648           1 :                 log_and_send(asfd, msg);
     649             :                 goto end;
     650             :         }
     651             : 
     652           5 :         if(bu_get_list(sdirs, &bu_list))
     653             :                 goto end;
     654             : 
     655          10 :         if(bu_list &&
     656             :            (!backup
     657           5 :          || !*backup
     658           5 :          || (!(bno=strtoul(backup, NULL, 10)) && *backup!='a')))
     659             :         {
     660           0 :                 found=1;
     661             :                 // No backup specified, do the most recent.
     662           0 :                 for(bu=bu_list; bu && bu->next; bu=bu->next) { }
     663             :                 ret=restore_manifest(asfd, bu, regex, srestore,
     664           0 :                                 act, sdirs, dir_for_notify, confs);
     665             :         }
     666             : 
     667           5 :         if(!found) for(bu=bu_list; bu; bu=bu->next)
     668             :         {
     669           5 :                 if(!strcmp(bu->timestamp, backup)
     670           5 :                   || bu->bno==bno || (backup && *backup=='a'))
     671             :                 {
     672           5 :                         found=1;
     673             :                         //logp("got: %s\n", bu->path);
     674             :                         ret|=restore_manifest(asfd, bu, regex, srestore,
     675           5 :                                 act, sdirs, dir_for_notify, confs);
     676           5 :                         if(backup && *backup=='a')
     677             :                                 continue;
     678             :                         break;
     679             :                 }
     680             :         }
     681             : 
     682           5 :         bu_list_free(&bu_list);
     683             : 
     684             : 
     685           5 :         if(found)
     686             :         {
     687             :                 // Restore has nearly completed OK.
     688           5 :                 ret=restore_end(asfd, confs);
     689             :         }
     690             :         else
     691             :         {
     692           0 :                 logp("backup not found\n");
     693           0 :                 asfd->write_str(asfd, CMD_ERROR, "backup not found");
     694           0 :                 ret=-1;
     695             :         }
     696             : end:
     697           6 :         regex_free(&regex);
     698           6 :         return ret;
     699             : }

Generated by: LCOV version 1.10