LCOV - code coverage report
Current view: top level - src/server/protocol1 - backup_phase2.c (source / functions) Hit Total Coverage
Test: burp-coverage-clean.info Lines: 342 502 68.1 %
Date: 2018-08-31 08:21:15 Functions: 23 27 85.2 %

          Line data    Source code
       1             : #include "../../burp.h"
       2             : #include "../../alloc.h"
       3             : #include "../../asfd.h"
       4             : #include "../../async.h"
       5             : #include "../../attribs.h"
       6             : #include "../../cmd.h"
       7             : #include "../../cntr.h"
       8             : #include "../../conf.h"
       9             : #include "../../conffile.h"
      10             : #include "../../cstat.h"
      11             : #include "../../fsops.h"
      12             : #include "../../handy.h"
      13             : #include "../../iobuf.h"
      14             : #include "../../log.h"
      15             : #include "../../sbuf.h"
      16             : #include "../child.h"
      17             : #include "../compress.h"
      18             : #include "../resume.h"
      19             : #include "blocklen.h"
      20             : #include "dpth.h"
      21             : #include "backup_phase2.h"
      22             : #include "link.h"
      23             : 
      24             : static size_t treepathlen=0;
      25             : 
      26           0 : static int path_length_warn(struct iobuf *path, struct conf **cconfs)
      27             : {
      28           0 :         if(get_int(cconfs[OPT_PATH_LENGTH_WARN]))
      29           0 :                 logw(NULL, get_cntr(cconfs), "Path too long for tree - will save in data structure instead: %s\n", path->buf);
      30           0 :         return 1;
      31             : }
      32             : 
      33           4 : static int path_too_long(struct iobuf *path, struct conf **cconfs)
      34             : {
      35             :         const char *cp;
      36             :         const char *cp1;
      37             :         size_t len;
      38             : 
      39           4 :         if(treepathlen+path->len+1>fs_full_path_max)
      40             :         {
      41             :                 // FIX THIS:
      42             :                 // Cannot warn down the asfd to the client, because it can
      43             :                 // arrive after the client has disconnected, which causes
      44             :                 // an error on the server side.
      45             :                 // Would need to change the way that "backupphase2end" works
      46             :                 // to be able to fix it.
      47           0 :                 return path_length_warn(path, cconfs);
      48             :         }
      49             :         // We have to check every part in the path to ensure it less then fs_name_max
      50             :         // minimum windows case is c:/a, nix case is /
      51             :         // usual: c:/users, /home
      52           4 :         cp = strchr(path->buf, '/');
      53           4 :         if( !cp ) // very strange
      54           0 :             return strlen(path->buf) > fs_name_max ?  path_length_warn(path, cconfs):0;
      55          14 :         while(cp && *cp)
      56             :         {
      57          10 :             cp++;
      58          10 :             cp1 = strchr(cp, '/');
      59          10 :             len = cp1? cp1-cp : strlen(cp);
      60          10 :             if( len > fs_name_max )
      61           0 :                 return path_length_warn(path, cconfs);
      62             :             cp = cp1;
      63             :         }
      64             :         return 0;
      65             : }
      66             : 
      67           4 : static int treedata(struct sbuf *sb, struct conf **cconfs)
      68             : {
      69             :         // Windows is sending directory data as if it is file data - this
      70             :         // cannot be saved in a tree structure.
      71           4 :         if(S_ISDIR(sb->statp.st_mode)) return 0;
      72             : 
      73           8 :         if(sb->path.cmd!=CMD_FILE
      74           4 :           && sb->path.cmd!=CMD_ENC_FILE
      75           0 :           && sb->path.cmd!=CMD_EFS_FILE)
      76             :                 return 0;
      77             : 
      78           4 :         return !path_too_long(&sb->path, cconfs);
      79             : }
      80             : 
      81           4 : static char *set_new_datapth(struct sdirs *sdirs, struct conf **cconfs,
      82             :         struct sbuf *sb, struct dpth *dpth, int *istreedata)
      83             : {
      84           4 :         char *tmp=NULL;
      85           4 :         char *rpath=NULL;
      86           4 :         if(get_int(cconfs[OPT_DIRECTORY_TREE]))
      87           4 :                 *istreedata=treedata(sb, cconfs);
      88             : 
      89           4 :         if(*istreedata)
      90             :         {
      91             :                 // We want to place this file in a directory structure like
      92             :                 // the directory structure on the original client.
      93           4 :                 if(!(tmp=prepend_s(TREE_DIR, sb->path.buf)))
      94             :                 {
      95           0 :                         log_out_of_memory(__func__);
      96             :                         return NULL;
      97             :                 }
      98             :         }
      99             :         else
     100             :         {
     101           0 :                 if(!(tmp=strdup_w(dpth_protocol1_mk(dpth, sb->compression,
     102             :                         sb->path.cmd), __func__))) return NULL;
     103             :         }
     104           4 :         iobuf_from_str(&sb->protocol1->datapth, CMD_DATAPTH, tmp);
     105           4 :         if(build_path(sdirs->datadirtmp,
     106           4 :                 sb->protocol1->datapth.buf, &rpath, sdirs->datadirtmp))
     107             :         {
     108           0 :                 logp("build path failed");
     109             :                 return NULL;
     110             :         }
     111           4 :         return rpath;
     112             : }
     113             : 
     114           4 : static int start_to_receive_new_file(struct asfd *asfd,
     115             :         struct sdirs *sdirs, struct conf **cconfs,
     116             :         struct sbuf *sb, struct dpth *dpth)
     117             : {
     118           4 :         int ret=-1;
     119           4 :         char *rpath=NULL;
     120           4 :         int istreedata=0;
     121             : 
     122             : //logp("start to receive: %s\n", iobuf_to_printable(&sb->path));
     123             : 
     124           4 :         if(!(rpath=set_new_datapth(sdirs, cconfs, sb, dpth, &istreedata)))
     125             :                 goto end;
     126             : 
     127           4 :         if(!(sb->protocol1->fzp=fzp_open(rpath, "wb")))
     128             :         {
     129           0 :                 log_and_send(asfd, "make file failed");
     130           0 :                 goto end;
     131             :         }
     132           4 :         if(!istreedata) dpth_incr(dpth);
     133             :         ret=0;
     134             : end:
     135           4 :         free_w(&rpath);
     136           4 :         return ret;
     137             : }
     138             : 
     139             : enum processed_e
     140             : {
     141             :         P_ERROR=-1,
     142             :         P_DONE_NEW=0,
     143             :         P_DONE_CHANGED=1,
     144             :         P_DONE_UNCHANGED=2,
     145             :         P_DONE_DELETED=3,
     146             :         P_NEW=4,
     147             :         P_CHANGED=5
     148             : };
     149             : 
     150             : #include <librsync.h>
     151             : 
     152           2 : static enum processed_e process_changed_file(struct asfd *asfd,
     153             :         struct conf **cconfs,
     154             :         struct sbuf *cb, struct sbuf *p1b,
     155             :         const char *adir)
     156             : {
     157           2 :         int ret=P_ERROR;
     158           2 :         size_t blocklen=0;
     159           2 :         char *curpath=NULL;
     160             :         //logp("need to process changed file: %s (%s)\n",
     161             :         //      cb->path, cb->datapth);
     162             : 
     163             :         // Move datapth onto p1b.
     164           2 :         iobuf_move(&p1b->protocol1->datapth, &cb->protocol1->datapth);
     165             : 
     166           2 :         if(!(curpath=prepend_s(adir, p1b->protocol1->datapth.buf)))
     167             :         {
     168           0 :                 log_out_of_memory(__func__);
     169             :                 goto end;
     170             :         }
     171           2 :         if(dpth_protocol1_is_compressed(cb->compression, curpath))
     172           0 :                 p1b->protocol1->sigfzp=fzp_gzopen(curpath, "rb");
     173             :         else
     174           2 :                 p1b->protocol1->sigfzp=fzp_open(curpath, "rb");
     175           2 :         if(!p1b->protocol1->sigfzp)
     176             :         {
     177           0 :                 logp("could not open %s: %s\n", curpath, strerror(errno));
     178             :                 goto end;
     179             :         }
     180             : 
     181           2 :         blocklen=get_librsync_block_len(cb->endfile.buf);
     182           4 :         if(!(p1b->protocol1->sigjob=
     183           2 :                 rs_sig_begin(blocklen, PROTO1_RS_STRONG_LEN
     184             : #ifndef RS_DEFAULT_STRONG_LEN
     185             :                   , rshash_to_magic_number(get_e_rshash(cconfs[OPT_RSHASH]))
     186             : #endif
     187             :                 )
     188             :         ))
     189             :         {
     190           0 :                 logp("could not start signature job.\n");
     191             :                 goto end;
     192             :         }
     193             :         //logp("sig begin: %s\n", iobuf_to_printable(7p1b->protocol1->datapth));
     194           2 :         if(!(p1b->protocol1->infb=rs_filebuf_new(NULL,
     195             :                 p1b->protocol1->sigfzp,
     196             :                 NULL, blocklen, -1)))
     197             :         {
     198           0 :                 logp("could not rs_filebuf_new for infb.\n");
     199             :                 goto end;
     200             :         }
     201           2 :         if(!(p1b->protocol1->outfb=rs_filebuf_new(NULL, NULL,
     202             :                 asfd, ASYNC_BUF_LEN, -1)))
     203             :         {
     204           0 :                 logp("could not rs_filebuf_new for in_outfb.\n");
     205             :                 goto end;
     206             :         }
     207             : 
     208             :         // Flag the things that need to be sent (to the client)
     209           2 :         p1b->flags |= SBUF_SEND_DATAPTH;
     210           2 :         p1b->flags |= SBUF_SEND_STAT;
     211           2 :         p1b->flags |= SBUF_SEND_PATH;
     212             : 
     213             :         //logp("sending sig for %s\n", p1b->path);
     214             :         //logp("(%s)\n", p1b->datapth);
     215             : 
     216           2 :         ret=P_CHANGED;
     217             : end:
     218           2 :         free_w(&curpath);
     219           2 :         return ret;
     220             : }
     221             : 
     222          68 : static enum processed_e new_non_file(struct sbuf *p1b,
     223             :         struct manio *ucmanio, struct conf **cconfs)
     224             : {
     225             :         // Is something that does not need more data backed up.
     226             :         // Like a directory or a link or something like that.
     227             :         // Goes into the unchanged file, so that it does not end up out of
     228             :         // order with normal files, which has to wait around for their data
     229             :         // to turn up.
     230          68 :         if(manio_write_sbuf(ucmanio, p1b))
     231             :                 return P_ERROR;
     232          68 :         cntr_add(get_cntr(cconfs), p1b->path.cmd, 0);
     233          68 :         return P_DONE_NEW;
     234             : }
     235             : 
     236           0 : static enum processed_e changed_non_file(struct sbuf *p1b,
     237             :         struct manio *ucmanio, enum cmd cmd, struct conf **cconfs)
     238             : {
     239             :         // As new_non_file.
     240           0 :         if(manio_write_sbuf(ucmanio, p1b))
     241             :                 return P_ERROR;
     242           0 :         cntr_add_changed(get_cntr(cconfs), cmd);
     243           0 :         return P_DONE_CHANGED;
     244             : }
     245             : 
     246         122 : static enum processed_e process_new(struct conf **cconfs,
     247             :         struct sbuf *p1b, struct manio *ucmanio)
     248             : {
     249         122 :         if(sbuf_is_filedata(p1b)
     250          68 :           || sbuf_is_vssdata(p1b))
     251             :         {
     252             :                 //logp("need to process new file: %s\n", p1b->path);
     253             :                 // Flag the things that need to be sent (to the client)
     254          54 :                 p1b->flags |= SBUF_SEND_STAT;
     255          54 :                 p1b->flags |= SBUF_SEND_PATH;
     256          54 :                 return P_NEW;
     257             :         }
     258          68 :         return new_non_file(p1b, ucmanio, cconfs);
     259             : }
     260             : 
     261           6 : static enum processed_e process_unchanged_file(struct sbuf *p1b, struct sbuf *cb,
     262             :         struct manio *ucmanio, struct conf **cconfs)
     263             : {
     264             :         // Need to re-encode the p1b attribs to include compression and
     265             :         // other bits and pieces that are recorded on cb.
     266           6 :         iobuf_move(&p1b->protocol1->datapth, &cb->protocol1->datapth);
     267           6 :         iobuf_move(&p1b->endfile, &cb->endfile);
     268           6 :         p1b->protocol1->salt=cb->protocol1->salt;
     269           6 :         p1b->compression=cb->compression;
     270           6 :         p1b->encryption=cb->encryption;
     271             :         // Why is winattr not getting sent in phase1?
     272           6 :         p1b->winattr=cb->winattr;
     273             :         // Need to free attr so that it is reallocated, because it may get
     274             :         // longer than what the client told us in phase1.
     275           6 :         iobuf_free_content(&p1b->attr);
     276           6 :         if(attribs_encode(p1b))
     277             :                 return P_ERROR;
     278           6 :         if(manio_write_sbuf(ucmanio, p1b))
     279             :                 return P_ERROR;
     280           6 :         cntr_add_same(get_cntr(cconfs), p1b->path.cmd);
     281           6 :         if(p1b->endfile.buf) cntr_add_bytes(get_cntr(cconfs),
     282           0 :                 strtoull(p1b->endfile.buf, NULL, 10));
     283             :         return P_DONE_UNCHANGED;
     284             : }
     285             : 
     286          10 : static int open_previous_manifest(struct manio **manio, struct sdirs *sdirs)
     287             : {
     288             :         struct stat statp;
     289          20 :         if(!lstat(sdirs->cmanifest, &statp)
     290           2 :           && !(*manio=manio_open(sdirs->cmanifest, "rb", PROTO_1)))
     291             :         {
     292           0 :                 logp("could not open old manifest %s\n", sdirs->cmanifest);
     293             :                 return -1;
     294             :         }
     295             :         return 0;
     296             : }
     297             : 
     298           0 : static int get_hardlink_master_from_hmanio(
     299             :         struct manio **hmanio,
     300             :         struct sdirs *sdirs,
     301             :         struct sbuf *cb,
     302             :         struct sbuf *hb
     303             : ) {
     304             :         int passes=0;
     305             :         while(1)
     306             :         {
     307           0 :                 sbuf_free_content(hb);
     308           0 :                 switch(manio_read(*hmanio, hb))
     309             :                 {
     310             :                         case 0: // Keep going.
     311             :                                 break;
     312             :                         case 1: // Finished OK.
     313           0 :                                 manio_close(hmanio);
     314             :                                 // Might have already been part way down the
     315             :                                 // manifest when we started. If we get to the
     316             :                                 // end, go back to the beginning once.
     317           0 :                                 if(!passes)
     318             :                                 {
     319           0 :                                         if(open_previous_manifest(hmanio,
     320             :                                                 sdirs)) return -1;
     321           0 :                                         passes++;
     322           0 :                                         continue;
     323             :                                 }
     324             :                                 return 0;
     325             :                         default: // Error;
     326             :                                 return -1;
     327             :                 }
     328           0 :                 if(hb->path.cmd!=CMD_FILE
     329           0 :                   && hb->path.cmd!=CMD_ENC_FILE)
     330           0 :                         continue;
     331           0 :                 if(!strcmp(hb->path.buf, cb->link.buf)
     332           0 :                   && hb->protocol1->datapth.buf)
     333             :                         return 1; // Found it.
     334             :         }
     335             :         return -1;
     336             : }
     337             : 
     338           0 : static int relink_deleted_hardlink_master(
     339             :         struct dpth *dpth,
     340             :         struct manio **hmanio,
     341             :         struct sdirs *sdirs,
     342             :         struct sbuf *p1b,
     343             :         struct sbuf *cb,
     344             :         struct conf **cconfs
     345             : ) {
     346           0 :         int ret=-1;
     347           0 :         char *newdatapth_full=NULL;
     348           0 :         char *olddatapth_full=NULL;
     349           0 :         char *newdatapth=NULL;
     350           0 :         char *relinkpath_full=NULL;
     351             :         struct stat statp;
     352           0 :         struct sbuf *hb=NULL;
     353           0 :         int istreedata=0;
     354             : 
     355           0 :         if(!(hb=sbuf_alloc(PROTO_1)))
     356             :                 goto end;
     357             : 
     358           0 :         switch(get_hardlink_master_from_hmanio(hmanio, sdirs, cb, hb))
     359             :         {
     360             :                 case 0: // Did not find it.
     361             :                         return 0;
     362             :                 case 1: // Found it.
     363             :                         break;
     364             :                 default: // Error.
     365             :                         goto end;
     366             :         }
     367             : 
     368             :         // Protect against the old file being encrypted, and the new file not
     369             :         // being encrypted - and vice versa.
     370           0 :         if(p1b->path.cmd!=hb->path.cmd)
     371             :                 return 0;
     372             : 
     373           0 :         if(!(newdatapth_full=set_new_datapth(sdirs,
     374             :                 cconfs, p1b, dpth, &istreedata)))
     375             :                         goto end;
     376           0 :         if(!istreedata) dpth_incr(dpth);
     377           0 :         if(!(relinkpath_full=prepend_s(sdirs->relink,
     378           0 :                 p1b->protocol1->datapth.buf)))
     379             :                         goto end;
     380           0 :         if(!(newdatapth=strdup_w(p1b->protocol1->datapth.buf, __func__)))
     381             :                 goto end;
     382             : /*
     383             : printf("newdatapth_full: %s\n", newdatapth_full);
     384             : printf("relinkpath_full: %s\n", relinkpath_full);
     385             : printf("newdatapth: %s\n", newdatapth);
     386             : printf("hbdatapth: %s\n", iobuf_to_printable(&hb->protocol1->datapth));
     387             : printf("sdirs->currentdata: %s\n", sdirs->currentdata);
     388             : */
     389             : 
     390           0 :         if(!(olddatapth_full=prepend_s(sdirs->currentdata,
     391           0 :                 hb->protocol1->datapth.buf)))
     392             :                         goto end;
     393           0 :         if(lstat(olddatapth_full, &statp) || !S_ISREG(statp.st_mode))
     394             :         {
     395           0 :                 logw(NULL, get_cntr(cconfs),
     396             :                         "Did not find path for relink: %s\n", olddatapth_full);
     397           0 :                 ret=0;
     398           0 :                 goto end;
     399             :         }
     400           0 :         if(build_path_w(relinkpath_full))
     401             :                 goto end;
     402           0 :         if(do_link(olddatapth_full, relinkpath_full,
     403             :                 &statp, cconfs, /*overwrite*/0))
     404             :                         goto end;
     405             : 
     406           0 :         iobuf_free_content(&cb->protocol1->datapth);
     407             : 
     408           0 :         iobuf_from_str(&hb->protocol1->datapth, CMD_DATAPTH,
     409           0 :                 p1b->protocol1->datapth.buf);
     410           0 :         p1b->protocol1->datapth.buf=NULL;
     411           0 :         iobuf_from_str(&cb->protocol1->datapth, CMD_DATAPTH, newdatapth);
     412           0 :         newdatapth=NULL;
     413           0 :         iobuf_move(&cb->endfile, &hb->endfile);
     414             : 
     415           0 :         ret=1;
     416             : end:
     417           0 :         free_w(&newdatapth_full);
     418           0 :         free_w(&olddatapth_full);
     419           0 :         free_w(&newdatapth);
     420           0 :         free_w(&relinkpath_full);
     421           0 :         sbuf_free(&hb);
     422           0 :         return ret;
     423             : }
     424             : 
     425           4 : static int librsync_enabled(struct sbuf *p1b,
     426             :         struct sbuf *cb,
     427             :         struct conf **cconfs)
     428             : {
     429             :         off_t max_size;
     430           4 :         if(!get_int(cconfs[OPT_LIBRSYNC]))
     431             :                 return 0;
     432           4 :         max_size=(off_t)get_uint64_t(cconfs[OPT_LIBRSYNC_MAX_SIZE]);
     433           4 :         if(!max_size)
     434             :                 return 1;
     435           0 :         return max_size >= cb->statp.st_size && max_size >= p1b->statp.st_size;
     436             : }
     437             : 
     438          10 : static enum processed_e maybe_do_delta_stuff(struct asfd *asfd,
     439             :         struct dpth *dpth,
     440             :         struct sdirs *sdirs, struct sbuf *cb, struct sbuf *p1b,
     441             :         struct manio *ucmanio, struct manio **hmanio, struct conf **cconfs)
     442             : {
     443          10 :         int oldcompressed=0;
     444          10 :         int compression=p1b->compression;
     445             : 
     446             :         // If the file type changed, I think it is time to back it up again
     447             :         // (for example, EFS changing to normal file, or back again).
     448          10 :         if(cb->path.cmd!=p1b->path.cmd)
     449             :         {
     450           0 :                 if(hmanio && *hmanio
     451           0 :                   && cb->path.cmd==CMD_HARD_LINK
     452           0 :                   && (p1b->path.cmd==CMD_FILE
     453           0 :                         || p1b->path.cmd==CMD_ENC_FILE))
     454             :                 {
     455           0 :                         struct stat *p1statp=&p1b->statp;
     456           0 :                         struct stat *cstatp=&cb->statp;
     457             :                         // A hardlink changed into a file. It is possible that
     458             :                         // The file that the hardlink was pointing to got
     459             :                         // deleted. Maybe we can reuse the previous file.
     460           0 :                         if(p1statp->st_dev==cstatp->st_dev
     461           0 :                           && p1statp->st_ino==cstatp->st_ino
     462           0 :                           && p1statp->st_mtime==cstatp->st_mtime)
     463             :                         {
     464           0 :                                 switch(relink_deleted_hardlink_master(
     465             :                                         dpth, hmanio, sdirs,
     466             :                                         p1b, cb, cconfs))
     467             :                                 {
     468             :                                         case 0:
     469             :                                                 break;
     470             :                                         case 1:
     471           0 :                                                 return process_unchanged_file(
     472             :                                                         p1b, cb,
     473             :                                                         ucmanio, cconfs);
     474             :                                         default:
     475             :                                                 return P_ERROR;
     476             :                                 }
     477             :                         }
     478             :                 }
     479           0 :                 return process_new(cconfs, p1b, ucmanio);
     480             :         }
     481             : 
     482             :         // mtime is the actual file data.
     483             :         // ctime is the attributes or meta data.
     484          10 :         if(cb->statp.st_mtime==p1b->statp.st_mtime
     485           6 :           && cb->statp.st_ctime==p1b->statp.st_ctime)
     486             :         {
     487             :                 // got an unchanged file
     488             :                 //logp("got unchanged file: %s %c\n",
     489             :                 //      iobuf_to_printable(&cb->path), p1b->path.cmd);
     490           6 :                 return process_unchanged_file(p1b, cb, ucmanio, cconfs);
     491             :         }
     492             : 
     493           4 :         if(cb->statp.st_mtime==p1b->statp.st_mtime
     494           0 :           && cb->statp.st_ctime!=p1b->statp.st_ctime)
     495             :         {
     496             :                 // File data stayed the same, but attributes or meta data
     497             :                 // changed. We already have the attributes, but may need to get
     498             :                 // extra meta data.
     499             :                 // FIX THIS horrible mess.
     500           0 :                 if(cb->path.cmd==CMD_ENC_METADATA
     501           0 :                   || p1b->path.cmd==CMD_ENC_METADATA
     502           0 :                   || cb->path.cmd==CMD_EFS_FILE
     503           0 :                   || p1b->path.cmd==CMD_EFS_FILE
     504             :                 // FIX THIS: make unencrypted metadata use the librsync
     505           0 :                   || cb->path.cmd==CMD_METADATA
     506           0 :                   || p1b->path.cmd==CMD_METADATA
     507           0 :                   || sbuf_is_vssdata(cb)
     508           0 :                   || sbuf_is_vssdata(p1b))
     509           0 :                         return process_new(cconfs, p1b, ucmanio);
     510             :                 // On Windows, we have to back up the whole file if ctime
     511             :                 // changed, otherwise things like permission changes do not get
     512             :                 // noticed. So, in that case, fall through to the changed stuff
     513             :                 // below.
     514             :                 // Non-Windows clients finish here.
     515           0 :                 else if(!get_int(cconfs[OPT_CLIENT_IS_WINDOWS]))
     516           0 :                         return process_unchanged_file(p1b,
     517             :                                 cb, ucmanio, cconfs);
     518             :         }
     519             : 
     520             :         // Got a changed file.
     521             :         //logp("got changed file: %s\n", iobuf_to_printable(&p1b->path));
     522             : 
     523             :         // If either old or new is encrypted, or librsync is off, we need to
     524             :         // get a new file.
     525             :         // FIX THIS horrible mess.
     526           4 :         if(!librsync_enabled(p1b, cb, cconfs)
     527             :         // FIX THIS: make unencrypted metadata use the librsync
     528           4 :           || cb->path.cmd==CMD_METADATA
     529           4 :           || p1b->path.cmd==CMD_METADATA
     530           4 :           || sbuf_is_encrypted(cb)
     531           2 :           || sbuf_is_encrypted(p1b)
     532           2 :           || sbuf_is_vssdata(cb)
     533           2 :           || sbuf_is_vssdata(p1b))
     534           2 :                 return process_new(cconfs, p1b, ucmanio);
     535             : 
     536             :         // Get new files if they have switched between compression on or off.
     537           2 :         if(cb->protocol1->datapth.buf
     538           2 :           && dpth_protocol1_is_compressed(cb->compression,
     539             :             cb->protocol1->datapth.buf))
     540           0 :                 oldcompressed=1;
     541           4 :         if( ( oldcompressed && !compression)
     542           2 :          || (!oldcompressed &&  compression))
     543           0 :                 return process_new(cconfs, p1b, ucmanio);
     544             : 
     545             :         // Otherwise, do the delta stuff (if possible).
     546           2 :         if(sbuf_is_filedata(p1b)
     547           0 :           || sbuf_is_vssdata(p1b))
     548           2 :                 return process_changed_file(asfd, cconfs, cb, p1b,
     549           2 :                         sdirs->currentdata);
     550           0 :         return changed_non_file(p1b, ucmanio, p1b->path.cmd, cconfs);
     551             : }
     552             : 
     553             : static enum processed_e process_deleted_file(struct sbuf *cb,
     554             :         struct conf **cconfs)
     555             : {
     556           2 :         cntr_add_deleted(get_cntr(cconfs), cb->path.cmd);
     557             :         return P_DONE_DELETED;
     558             : }
     559             : 
     560             : // return 1 to say that a file was processed
     561          12 : static enum processed_e maybe_process_file(struct asfd *asfd,
     562             :         struct dpth *dpth,
     563             :         struct sdirs *sdirs, struct sbuf *cb, struct sbuf *p1b,
     564             :         struct manio *ucmanio, struct manio **hmanio, struct conf **cconfs)
     565             : {
     566             :         int pcmp;
     567          12 :         if(p1b)
     568             :         {
     569          11 :                 if(!(pcmp=sbuf_pathcmp(cb, p1b)))
     570          10 :                         return maybe_do_delta_stuff(asfd, dpth,sdirs, cb, p1b,
     571             :                                 ucmanio, hmanio, cconfs);
     572           1 :                 else if(pcmp>0)
     573             :                 {
     574             :                         //logp("ahead: %s\n", iobuf_to_printable(&p1b->path));
     575             :                         // ahead - need to get the whole file
     576           0 :                         return process_new(cconfs, p1b, ucmanio);
     577             :                 }
     578             :         }
     579             :         //logp("behind: %s\n", iobuf_to_printable(&p1b->path));
     580             :         // Behind - need to read more from the old manifest.
     581             :         // Count a deleted file - it was in the old manifest
     582             :         // but not the new.
     583           4 :         return process_deleted_file(cb, cconfs);
     584             : }
     585             : 
     586             : enum sts_e
     587             : {
     588             :         STS_ERROR=-1,
     589             :         STS_OK=0,
     590             :         STS_BLOCKED=1
     591             : };
     592             : 
     593             : // Return 1 if there is still stuff needing to be sent.
     594             : // FIX THIS: lots of repeated code.
     595          63 : static enum sts_e do_stuff_to_send(struct asfd *asfd,
     596             :         struct sbuf *p1b, char **last_requested)
     597             : {
     598             :         static struct iobuf wbuf;
     599          63 :         if(p1b->flags & SBUF_SEND_DATAPTH)
     600             :         {
     601           2 :                 iobuf_copy(&wbuf, &p1b->protocol1->datapth);
     602           2 :                 switch(asfd->append_all_to_write_buffer(asfd, &wbuf))
     603             :                 {
     604             :                         case APPEND_OK: break;
     605             :                         case APPEND_BLOCKED: return STS_BLOCKED;
     606           0 :                         default: return STS_ERROR;
     607             :                 }
     608           2 :                 p1b->flags &= ~SBUF_SEND_DATAPTH;
     609             :         }
     610          63 :         if(p1b->flags & SBUF_SEND_STAT)
     611             :         {
     612          56 :                 iobuf_copy(&wbuf, &p1b->attr);
     613          56 :                 switch(asfd->append_all_to_write_buffer(asfd, &wbuf))
     614             :                 {
     615             :                         case APPEND_OK: break;
     616             :                         case APPEND_BLOCKED: return STS_BLOCKED;
     617           0 :                         default: return STS_ERROR;
     618             :                 }
     619          56 :                 p1b->flags &= ~SBUF_SEND_STAT;
     620             :         }
     621          63 :         if(p1b->flags & SBUF_SEND_PATH)
     622             :         {
     623          56 :                 iobuf_copy(&wbuf, &p1b->path);
     624          56 :                 switch(asfd->append_all_to_write_buffer(asfd, &wbuf))
     625             :                 {
     626             :                         case APPEND_OK: break;
     627             :                         case APPEND_BLOCKED: return STS_BLOCKED;
     628           0 :                         default: return STS_ERROR;
     629             :                 }
     630          56 :                 p1b->flags &= ~SBUF_SEND_PATH;
     631          56 :                 free_w(last_requested);
     632          56 :                 if(!(*last_requested=strdup_w(p1b->path.buf, __func__)))
     633             :                         return STS_ERROR;
     634             :         }
     635          63 :         if(p1b->protocol1->sigjob && !(p1b->flags & SBUF_SEND_ENDOFSIG))
     636             :         {
     637             :                 rs_result sigresult;
     638             : 
     639           4 :                 switch((sigresult=rs_async(p1b->protocol1->sigjob,
     640             :                         &(p1b->protocol1->rsbuf),
     641             :                         p1b->protocol1->infb, p1b->protocol1->outfb)))
     642             :                 {
     643             :                         case RS_DONE:
     644           2 :                                 p1b->flags |= SBUF_SEND_ENDOFSIG;
     645           2 :                                 break;
     646             :                         case RS_BLOCKED:
     647             :                         case RS_RUNNING:
     648             :                                 // keep going round the loop.
     649             :                                 return STS_BLOCKED;
     650             :                         default:
     651           0 :                                 logp("error in rs_async: %d\n", sigresult);
     652           0 :                                 return STS_ERROR;
     653             :                 }
     654             :         }
     655          61 :         if(p1b->flags & SBUF_SEND_ENDOFSIG)
     656             :         {
     657           2 :                 iobuf_from_str(&wbuf, CMD_END_FILE, (char *)"endfile");
     658           2 :                 switch(asfd->append_all_to_write_buffer(asfd, &wbuf))
     659             :                 {
     660             :                         case APPEND_OK: break;
     661             :                         case APPEND_BLOCKED: return STS_BLOCKED;
     662           0 :                         default: return STS_ERROR;
     663             :                 }
     664           2 :                 p1b->flags &= ~SBUF_SEND_ENDOFSIG;
     665             :         }
     666             :         return STS_OK;
     667             : }
     668             : 
     669           4 : static int start_to_receive_delta(struct sdirs *sdirs, struct sbuf *rb)
     670             : {
     671           4 :         if(rb->compression)
     672             :         {
     673           0 :                 if(!(rb->protocol1->fzp=fzp_gzopen(sdirs->deltmppath,
     674           0 :                         comp_level(rb->compression))))
     675             :                                 return -1;
     676             :         }
     677             :         else
     678             :         {
     679           4 :                 if(!(rb->protocol1->fzp=fzp_open(sdirs->deltmppath, "wb")))
     680             :                         return -1;
     681             :         }
     682           4 :         rb->flags |= SBUF_RECV_DELTA;
     683             : 
     684             :         return 0;
     685             : }
     686             : 
     687           4 : static int finish_delta(struct sdirs *sdirs, struct sbuf *rb)
     688             : {
     689           4 :         int ret=0;
     690           4 :         char *deltmp=NULL;
     691           4 :         char *delpath=NULL;
     692           4 :         if(!(deltmp=prepend_s("deltas.forward", rb->protocol1->datapth.buf))
     693           4 :           || !(delpath=prepend_s(sdirs->working, deltmp))
     694           4 :           || mkpath(&delpath, sdirs->working)
     695             :         // Rename race condition is of no consequence here, as delpath will
     696             :         // just get recreated.
     697           4 :           || do_rename(sdirs->deltmppath, delpath))
     698             :                 ret=-1;
     699           4 :         free_w(&delpath);
     700           4 :         free_w(&deltmp);
     701           4 :         return ret;
     702             : }
     703             : 
     704           8 : static int deal_with_receive_end_file(struct asfd *asfd, struct sdirs *sdirs,
     705             :         struct sbuf *rb, struct manio *chmanio, struct conf **cconfs,
     706             :         char **last_requested)
     707             : {
     708           8 :         int ret=-1;
     709             :         static char *cp=NULL;
     710             :         static struct iobuf *rbuf;
     711           8 :         struct cntr *cntr=get_cntr(cconfs);
     712           8 :         rbuf=asfd->rbuf;
     713             :         // Finished the file.
     714             :         // Write it to the phase2 file, and free the buffers.
     715             : 
     716           8 :         if(fzp_close(&(rb->protocol1->fzp)))
     717             :         {
     718           0 :                 logp("error closing delta for %s in receive\n",
     719             :                         iobuf_to_printable(&rb->path));
     720             :                 goto end;
     721             :         }
     722           8 :         iobuf_move(&rb->endfile, rbuf);
     723           8 :         if(rb->flags & SBUF_RECV_DELTA && finish_delta(sdirs, rb))
     724             :                 goto end;
     725             : 
     726           8 :         if(manio_write_sbuf(chmanio, rb))
     727             :                 goto end;
     728             : 
     729           8 :         if(rb->flags & SBUF_RECV_DELTA)
     730           4 :                 cntr_add_changed(cntr, rb->path.cmd);
     731             :         else
     732           4 :                 cntr_add(cntr, rb->path.cmd, 0);
     733             : 
     734           8 :         if(*last_requested && !strcmp(rb->path.buf, *last_requested))
     735           2 :                 free_w(last_requested);
     736             : 
     737           8 :         cp=strchr(rb->endfile.buf, ':');
     738           8 :         if(rb->endfile.buf)
     739           8 :                 cntr_add_bytes(cntr, strtoull(rb->endfile.buf, NULL, 10));
     740             :         if(cp)
     741             :         {
     742             :                 // checksum stuff goes here
     743             :         }
     744             : 
     745           8 :         ret=0;
     746             : end:
     747           8 :         sbuf_free_content(rb);
     748           8 :         return ret;
     749             : }
     750             : 
     751           8 : static int deal_with_receive_append(struct asfd *asfd, struct sbuf *rb)
     752             : {
     753           8 :         int app=0;
     754             :         static struct iobuf *rbuf;
     755           8 :         rbuf=asfd->rbuf;
     756             :         //logp("rbuf->len: %d\n", rbuf->len);
     757             : 
     758           8 :         if(rb->protocol1->fzp)
     759           8 :                 app=fzp_write(rb->protocol1->fzp, rbuf->buf, rbuf->len);
     760             : 
     761           8 :         if(app>0) return 0;
     762           0 :         logp("error when appending: %d\n", app);
     763           0 :         asfd->write_str(asfd, CMD_ERROR, "write failed");
     764             :         return -1;
     765             : }
     766             : 
     767           8 : static int deal_with_filedata(struct asfd *asfd,
     768             :         struct sdirs *sdirs, struct sbuf *rb,
     769             :         struct iobuf *rbuf, struct dpth *dpth, struct conf **cconfs)
     770             : {
     771           8 :         iobuf_move(&rb->path, rbuf);
     772             : 
     773           8 :         if(rb->protocol1->datapth.buf)
     774             :         {
     775             :                 // Receiving a delta.
     776           4 :                 if(start_to_receive_delta(sdirs, rb))
     777             :                 {
     778           0 :                         logp("error in start_to_receive_delta\n");
     779           0 :                         return -1;
     780             :                 }
     781             :                 return 0;
     782             :         }
     783             : 
     784             :         // Receiving a whole new file.
     785           4 :         if(start_to_receive_new_file(asfd, sdirs, cconfs, rb, dpth))
     786             :         {
     787           0 :                 logp("error in start_to_receive_new_file\n");
     788           0 :                 return -1;
     789             :         }
     790             :         return 0;
     791             : }
     792             : 
     793             : enum str_e
     794             : {
     795             :         STR_ERROR=-1,
     796             :         STR_OK=0,
     797             :         STR_FINISHED=1
     798             : };
     799             : 
     800             : // returns 1 for finished ok.
     801          85 : static enum str_e do_stuff_to_receive(struct asfd *asfd,
     802             :         struct sdirs *sdirs, struct conf **cconfs,
     803             :         struct sbuf *rb, struct manio *chmanio,
     804             :         struct dpth *dpth, char **last_requested)
     805             : {
     806          85 :         struct iobuf *rbuf=asfd->rbuf;
     807             : 
     808         170 :         if(rbuf->cmd==CMD_MESSAGE
     809          85 :           || rbuf->cmd==CMD_WARNING)
     810             :         {
     811           0 :                 struct cntr *cntr=NULL;
     812           0 :                 if(cconfs) cntr=get_cntr(cconfs);
     813           0 :                 log_recvd(rbuf, cntr, 0);
     814           0 :                 return STR_OK;
     815             :         }
     816             : 
     817          85 :         if(rb->protocol1->fzp)
     818             :         {
     819             :                 // Currently writing a file (or meta data)
     820          16 :                 switch(rbuf->cmd)
     821             :                 {
     822             :                         case CMD_APPEND:
     823           8 :                                 if(deal_with_receive_append(asfd, rb))
     824             :                                         goto error;
     825             :                                 return STR_OK;
     826             :                         case CMD_END_FILE:
     827           8 :                                 if(deal_with_receive_end_file(asfd, sdirs, rb,
     828             :                                         chmanio, cconfs, last_requested))
     829             :                                                 goto error;
     830             :                                 return STR_OK;
     831             :                         default:
     832           0 :                                 iobuf_log_unexpected(rbuf, __func__);
     833           0 :                                 goto error;
     834             :                 }
     835             :         }
     836             : 
     837             :         // Otherwise, expecting to be told of a file to save.
     838          69 :         switch(rbuf->cmd)
     839             :         {
     840             :                 case CMD_DATAPTH:
     841           4 :                         iobuf_move(&rb->protocol1->datapth, rbuf);
     842           4 :                         return STR_OK;
     843             :                 case CMD_ATTRIBS:
     844           8 :                         iobuf_move(&rb->attr, rbuf);
     845           8 :                         attribs_decode(rb);
     846           8 :                         return STR_OK;
     847             :                 case CMD_GEN:
     848           5 :                         if(!strcmp(rbuf->buf, "okbackupphase2end"))
     849             :                                 goto end_phase2;
     850           0 :                         iobuf_log_unexpected(rbuf, __func__);
     851           0 :                         goto error;
     852             :                 case CMD_INTERRUPT:
     853             :                         // Interrupt - forget about the last requested
     854             :                         // file if it matches. Otherwise, we can get
     855             :                         // stuck on the select in the async stuff,
     856             :                         // waiting for something that will never arrive.
     857          44 :                         if(*last_requested
     858          44 :                           && !strcmp(rbuf->buf, *last_requested))
     859           1 :                                 free_w(last_requested);
     860             :                         return STR_OK;
     861             :                 default:
     862             :                         break;
     863             :         }
     864           8 :         if(iobuf_is_filedata(rbuf)
     865           0 :           || iobuf_is_vssdata(rbuf))
     866             :         {
     867           8 :                 if(deal_with_filedata(asfd, sdirs, rb, rbuf, dpth, cconfs))
     868             :                         goto error;
     869             :                 return STR_OK;
     870             :         }
     871           0 :         iobuf_log_unexpected(rbuf, __func__);
     872             : 
     873             : error:
     874             :         return STR_ERROR;
     875             : end_phase2:
     876             :         return STR_FINISHED;
     877             : }
     878             : 
     879           5 : static int vss_opts_changed(struct sdirs *sdirs, struct conf **cconfs,
     880             :         const char *incexc)
     881             : {
     882           5 :         int ret=-1;
     883             :         struct conf **oldconfs;
     884             :         struct conf **newconfs;
     885           5 :         if(!(oldconfs=confs_alloc())
     886           5 :           || !(newconfs=confs_alloc()))
     887             :                 goto end;
     888           5 :         confs_init(oldconfs);
     889           5 :         confs_init(newconfs);
     890             : 
     891             :         // Figure out the old config, which is in the incexc file left
     892             :         // in the current backup directory on the server.
     893           5 :         if(is_reg_lstat(sdirs->cincexc)<=0
     894           1 :           || conf_parse_incexcs_path(oldconfs, sdirs->cincexc))
     895             :         {
     896             :                 // Assume that the file did not exist, and therefore
     897             :                 // the old split_vss setting is 0.
     898           4 :                 set_int(oldconfs[OPT_SPLIT_VSS], 0);
     899           4 :                 set_int(oldconfs[OPT_STRIP_VSS], 0);
     900             :         }
     901             : 
     902             :         // Figure out the new config, which is either in the incexc file from
     903             :         // the client, or in the cconf on the server.
     904           5 :         if(incexc)
     905             :         {
     906           0 :                 if(conf_parse_incexcs_buf(newconfs, incexc))
     907             :                 {
     908             :                         // Should probably not got here.
     909           0 :                         set_int(newconfs[OPT_SPLIT_VSS], 0);
     910           0 :                         set_int(newconfs[OPT_STRIP_VSS], 0);
     911             :                 }
     912             :         }
     913             :         else
     914             :         {
     915           5 :                 set_int(newconfs[OPT_SPLIT_VSS],
     916           5 :                         get_int(cconfs[OPT_SPLIT_VSS]));
     917           5 :                 set_int(newconfs[OPT_STRIP_VSS],
     918           5 :                         get_int(cconfs[OPT_STRIP_VSS]));
     919             :         }
     920             : 
     921           5 :         if(get_int(newconfs[OPT_SPLIT_VSS])!=get_int(oldconfs[OPT_SPLIT_VSS]))
     922             :         {
     923           0 :                 logp("split_vss=%d (changed since last backup)\n",
     924           0 :                         get_int(newconfs[OPT_SPLIT_VSS]));
     925           0 :                 ret=1; goto end;
     926             :         }
     927           5 :         if(get_int(newconfs[OPT_STRIP_VSS])!=get_int(oldconfs[OPT_STRIP_VSS]))
     928             :         {
     929           0 :                 logp("strip_vss=%d (changed since last backup)\n",
     930           0 :                         get_int(newconfs[OPT_STRIP_VSS]));
     931           0 :                 ret=1; goto end;
     932             :         }
     933             :         ret=0;
     934             : end:
     935           5 :         if(ret==1) logp("All files will be treated as new\n");
     936           5 :         confs_free(&oldconfs);
     937           5 :         confs_free(&newconfs);
     938           5 :         return ret;
     939             : }
     940             : 
     941             : // Maybe open the previous (current) manifest.
     942             : // If the split_vss setting changed between the previous backup and the new
     943             : // backup, do not open the previous manifest. This will have the effect of
     944             : // making the client back up everything fresh. Need to do this, otherwise
     945             : // toggling split_vss on and off will result in backups that do not work.
     946           5 : static int maybe_open_previous_manifest(struct manio **manio,
     947             :         struct sdirs *sdirs, const char *incexc, struct conf **cconfs)
     948             : {
     949           5 :         if(vss_opts_changed(sdirs, cconfs, incexc))
     950             :                 return 0;
     951           5 :         return open_previous_manifest(manio, sdirs);
     952             : }
     953             : 
     954         135 : static int process_next_file_from_manios(struct asfd *asfd,
     955             :         struct dpth *dpth,
     956             :         struct sdirs *sdirs,
     957             :         struct manio **p1manio,
     958             :         struct manio **cmanio,
     959             :         struct manio *ucmanio,
     960             :         struct manio **hmanio,
     961             :         struct sbuf **p1b,
     962             :         struct sbuf *cb,
     963             :         struct conf **cconfs)
     964             : {
     965         135 :         if(!(*p1b=sbuf_alloc(PROTO_1)))
     966             :                 goto error;
     967         135 :         switch(manio_read(*p1manio, *p1b))
     968             :         {
     969             :                 case 0:
     970             :                         break;
     971             :                 case 1:
     972           5 :                         manio_close(p1manio);
     973           5 :                         if(asfd->write_str(asfd, CMD_GEN, "backupphase2end"))
     974             :                                 goto error;
     975           5 :                         sbuf_free(p1b);
     976           5 :                         break;
     977             :                 case -1:
     978             :                         goto error;
     979             :         }
     980             : 
     981         135 :         if(!*cmanio)
     982             :         {
     983         124 :                 if(!*p1b) return 0;
     984             :                 // No old manifest, need to ask for a new file.
     985         120 :                 switch(process_new(cconfs, *p1b, ucmanio))
     986             :                 {
     987             :                         case P_NEW:
     988             :                                 return 0;
     989             :                         case P_DONE_NEW:
     990             :                                 goto p1b_done;
     991             :                         default:
     992             :                                 goto error;
     993             :                 }
     994             :         }
     995             : 
     996             :         // Have an old manifest, look for it there.
     997             : 
     998             :         // Might already have it, or be ahead in the old
     999             :         // manifest.
    1000          11 :         if(cb->path.buf)
    1001             :         {
    1002           2 :                 switch(maybe_process_file(asfd, dpth,
    1003             :                         sdirs, cb, *p1b, ucmanio, hmanio, cconfs))
    1004             :                 {
    1005             :                         case P_NEW:
    1006             :                                 return 0;
    1007             :                         case P_CHANGED:
    1008             :                                 // Free cb content for things like encrypted
    1009             :                                 // files, which we only pretend are new.
    1010           0 :                                 sbuf_free_content(cb);
    1011           0 :                                 return 0;
    1012             :                         case P_DONE_NEW:
    1013             :                                 goto p1b_done;
    1014             :                         case P_DONE_CHANGED:
    1015             :                         case P_DONE_UNCHANGED:
    1016           0 :                                 sbuf_free_content(cb);
    1017           0 :                                 goto p1b_done;
    1018             :                         case P_DONE_DELETED:
    1019           2 :                                 sbuf_free_content(cb);
    1020           2 :                                 break;
    1021             :                         case P_ERROR:
    1022             :                                 goto error;
    1023             :                 }
    1024             :         }
    1025             : 
    1026             :         while(1)
    1027             :         {
    1028          11 :                 sbuf_free_content(cb);
    1029          11 :                 switch(manio_read(*cmanio, cb))
    1030             :                 {
    1031             :                         case 0: break;
    1032           1 :                         case 1: manio_close(cmanio);
    1033           1 :                                 if(*p1b) switch(process_new(cconfs,
    1034             :                                         *p1b, ucmanio))
    1035             :                                 {
    1036             :                                         case P_NEW:
    1037             :                                                 return 0;
    1038             :                                         case P_DONE_NEW:
    1039             :                                                 goto p1b_done;
    1040             :                                         default:
    1041             :                                                 goto error;
    1042             :                                 }
    1043             :                                 return 0;
    1044             :                         case -1: goto error;
    1045             :                 }
    1046          10 :                 switch(maybe_process_file(asfd, dpth, sdirs,
    1047             :                         cb, *p1b, ucmanio, hmanio, cconfs))
    1048             :                 {
    1049             :                         case P_NEW:
    1050             :                                 return 0;
    1051             :                         case P_CHANGED:
    1052             :                                 // Free cb content for things like encrypted
    1053             :                                 // files, which we only pretend are new.
    1054           2 :                                 sbuf_free_content(cb);
    1055           2 :                                 return 0;
    1056             :                         case P_DONE_NEW:
    1057             :                                 goto p1b_done;
    1058             :                         case P_DONE_CHANGED:
    1059             :                         case P_DONE_UNCHANGED:
    1060           6 :                                 sbuf_free_content(cb);
    1061           6 :                                 goto p1b_done;
    1062             :                         case P_DONE_DELETED:
    1063           0 :                                 sbuf_free_content(cb);
    1064           0 :                                 continue;
    1065             :                         case P_ERROR:
    1066             :                                 goto error;
    1067             :                 }
    1068             :         }
    1069             : error:
    1070             :         return -1;
    1071             : p1b_done:
    1072          74 :         sbuf_free(p1b);
    1073          74 :         return 0;
    1074             : }
    1075             : 
    1076          10 : int backup_phase2_server_protocol1(struct async *as, struct sdirs *sdirs,
    1077             :         const char *incexc, int resume, struct conf **cconfs)
    1078             : {
    1079          10 :         int ret=0;
    1080          10 :         man_off_t *p1pos=NULL;
    1081          10 :         struct manio *p1manio=NULL;
    1082          10 :         struct dpth *dpth=NULL;
    1083          10 :         char *deltmppath=NULL;
    1084          10 :         char *last_requested=NULL;
    1085          10 :         struct manio *chmanio=NULL; // changed data
    1086          10 :         struct manio *ucmanio=NULL; // unchanged data
    1087          10 :         struct manio *cmanio=NULL; // previous (current) manifest
    1088          10 :         struct manio *hmanio=NULL; // to look up deleted hardlinks
    1089          10 :         struct sbuf *cb=NULL; // file list in current manifest
    1090          10 :         struct sbuf *p1b=NULL; // file list from client
    1091          10 :         struct sbuf *rb=NULL; // receiving file from client
    1092          10 :         struct asfd *asfd=NULL;
    1093          10 :         int breaking=0;
    1094          10 :         int breakcount=0;
    1095          10 :         struct cntr *cntr=NULL;
    1096          10 :         struct cntr_ent *warn_ent=NULL;
    1097          10 :         int fail_on_warning=0;
    1098             : 
    1099          10 :         if(!as)
    1100             :         {
    1101           1 :                 logp("async not provided to %s()\n", __func__);
    1102           1 :                 goto error;
    1103             :         }
    1104           9 :         if(!sdirs)
    1105             :         {
    1106           2 :                 logp("sdirs not provided to %s()\n", __func__);
    1107           2 :                 goto error;
    1108             :         }
    1109           7 :         if(!cconfs)
    1110             :         {
    1111           1 :                 logp("cconfs not provided to %s()\n", __func__);
    1112           1 :                 goto error;
    1113             :         }
    1114           6 :         asfd=as->asfd;
    1115           6 :         if(!asfd)
    1116             :         {
    1117           1 :                 logp("asfd not provided to %s()\n", __func__);
    1118           1 :                 goto error;
    1119             :         }
    1120           5 :         cntr=get_cntr(cconfs);
    1121           5 :         fail_on_warning=get_int(cconfs[OPT_FAIL_ON_WARNING]);
    1122           5 :         if(cntr)
    1123           0 :                 warn_ent=cntr->ent[CMD_WARNING];
    1124             : 
    1125           5 :         if(get_int(cconfs[OPT_BREAKPOINT])>=2000
    1126           0 :           && get_int(cconfs[OPT_BREAKPOINT])<3000)
    1127             :         {
    1128           0 :                 breaking=get_int(cconfs[OPT_BREAKPOINT]);
    1129           0 :                 breakcount=breaking-2000;
    1130             :         }
    1131             : 
    1132           5 :         logp("Begin phase2 (receive file data)\n");
    1133             : 
    1134           5 :         if(!(dpth=dpth_alloc())
    1135           5 :           || dpth_protocol1_init(dpth, sdirs->currentdata,
    1136             :                 get_int(cconfs[OPT_MAX_STORAGE_SUBDIRS])))
    1137             :                         goto error;
    1138             : 
    1139           5 :         if(maybe_open_previous_manifest(&cmanio, sdirs, incexc, cconfs))
    1140             :                 goto error;
    1141             :         // If the first file that others hardlink to has been deleted, we will
    1142             :         // need to look through the previous manifest to find the information
    1143             :         // for that original file, in order to not have to copy all the data
    1144             :         // across again.
    1145           5 :         if(open_previous_manifest(&hmanio, sdirs))
    1146             :                 goto error;
    1147             : 
    1148           5 :         if(get_int(cconfs[OPT_DIRECTORY_TREE]))
    1149             :         {
    1150             :                 // Need to make sure we do not try to create a path that is
    1151             :                 // too long.
    1152           5 :                 if(build_path_w(sdirs->treepath)) goto error;
    1153           5 :                 mkdir(sdirs->treepath, 0777);
    1154           5 :                 treepathlen=strlen(sdirs->treepath);
    1155           5 :                 if(init_fs_max(sdirs->treepath))
    1156             :                         goto error;
    1157             :         }
    1158             : 
    1159           5 :         if(resume)
    1160             :         {
    1161           0 :                 if(!(p1pos=do_resume(sdirs, dpth, cconfs)))
    1162             :                         goto error;
    1163           0 :                 if(cntr_send_sdirs(asfd, sdirs, cconfs, CNTR_STATUS_BACKUP))
    1164             :                         goto error;
    1165             :         }
    1166             : 
    1167           5 :         if(!(p1manio=manio_open_phase1(sdirs->phase1data, "rb", PROTO_1))
    1168           5 :           || (resume && manio_seek(p1manio, p1pos)))
    1169             :                 goto error;
    1170           5 :         if(!(cb=sbuf_alloc(PROTO_1))
    1171           5 :           || !(p1b=sbuf_alloc(PROTO_1))
    1172           5 :           || !(rb=sbuf_alloc(PROTO_1)))
    1173             :                 goto error;
    1174             : 
    1175             :         // Unchanged and changed should now be truncated correctly, we just
    1176             :         // have to open them for appending.
    1177             :         // Data is not getting written to a compressed file.
    1178             :         // This is important for recovery if the power goes.
    1179           5 :         if(!(ucmanio=manio_open_phase2(sdirs->unchanged, "ab", PROTO_1))
    1180           5 :           || !(chmanio=manio_open_phase2(sdirs->changed, "ab", PROTO_1)))
    1181             :                 goto error;
    1182             : 
    1183             :         while(1)
    1184             :         {
    1185         921 :                 if(check_fail_on_warning(fail_on_warning, warn_ent))
    1186             :                         goto error;
    1187             : 
    1188         921 :                 if(breaking && breakcount--==0)
    1189           0 :                         return breakpoint(breaking, __func__);
    1190             : 
    1191         921 :                 if(write_status(CNTR_STATUS_BACKUP,
    1192         921 :                         rb->path.buf?rb->path.buf:"", cntr))
    1193             :                                 goto error;
    1194        1842 :                 if(last_requested
    1195         921 :                   || !p1manio
    1196          16 :                   || asfd->writebuflen)
    1197             :                 {
    1198         905 :                         iobuf_free_content(asfd->rbuf);
    1199         905 :                         if(asfd->as->read_write(asfd->as))
    1200             :                         {
    1201           0 :                                 logp("error in %s\n", __func__);
    1202           0 :                                 goto error;
    1203             :                         }
    1204             : 
    1205         905 :                         if(asfd->rbuf->buf)
    1206          85 :                           switch(do_stuff_to_receive(asfd, sdirs,
    1207             :                                 cconfs, rb, chmanio, dpth, &last_requested))
    1208             :                         {
    1209             :                                 case STR_OK:
    1210             :                                         break;
    1211             :                                 case STR_FINISHED:
    1212           5 :                                         if(check_fail_on_warning(
    1213             :                                                 fail_on_warning, warn_ent))
    1214             :                                                         goto error;
    1215             :                                         goto end;
    1216             :                                 case STR_ERROR:
    1217             :                                         goto error;
    1218             :                         }
    1219             :                 }
    1220             : 
    1221         916 :                 if(p1b) switch(do_stuff_to_send(asfd, p1b, &last_requested))
    1222             :                 {
    1223             :                         case STS_OK:
    1224          61 :                                 sbuf_free(&p1b);
    1225          61 :                                 break;
    1226             :                         case STS_BLOCKED:
    1227           2 :                                 continue;
    1228             :                         case STS_ERROR:
    1229             :                                 goto error;
    1230             :                 }
    1231             : 
    1232         914 :                 if(p1manio
    1233         135 :                  && process_next_file_from_manios(asfd, dpth, sdirs,
    1234             :                         &p1manio, &cmanio, ucmanio, &hmanio, &p1b, cb, cconfs))
    1235             :                                         goto error;
    1236             :         }
    1237             : 
    1238             : error:
    1239           5 :         ret=-1;
    1240           5 :         if(p1b)
    1241           0 :                 logp("  last tried file:    %s\n",
    1242             :                         iobuf_to_printable(&p1b->path));
    1243             : end:
    1244          10 :         if(manio_close(&chmanio))
    1245             :         {
    1246           0 :                 logp("error closing %s in %s\n", sdirs->changed, __func__);
    1247           0 :                 ret=-1;
    1248             :         }
    1249          10 :         if(manio_close(&ucmanio))
    1250             :         {
    1251           0 :                 logp("error closing %s in %s\n", sdirs->unchanged, __func__);
    1252           0 :                 ret=-1;
    1253             :         }
    1254          10 :         free_w(&deltmppath);
    1255          10 :         free_w(&last_requested);
    1256          10 :         sbuf_free(&cb);
    1257          10 :         sbuf_free(&p1b);
    1258          10 :         sbuf_free(&rb);
    1259          10 :         manio_close(&p1manio);
    1260          10 :         manio_close(&cmanio);
    1261          10 :         manio_close(&hmanio);
    1262          10 :         dpth_free(&dpth);
    1263          10 :         man_off_t_free(&p1pos);
    1264          10 :         if(!ret && sdirs)
    1265           5 :                 unlink(sdirs->phase1data);
    1266             : 
    1267          10 :         logp("End phase2 (receive file data)\n");
    1268             : 
    1269          10 :         return ret;
    1270             : }

Generated by: LCOV version 1.13