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: 343 509 67.4 %
Date: 2019-02-01 23:00:05 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          18 :         while(cp && *cp)
      56             :         {
      57          14 :             cp++;
      58          14 :             cp1 = strchr(cp, '/');
      59          14 :             len = cp1? cp1-cp : strlen(cp);
      60          14 :             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             :                         case CMD_INTERRUPT:
     832           0 :                                 if(*last_requested
     833           0 :                                   && !strcmp(rbuf->buf, *last_requested))
     834           0 :                                         free_w(last_requested);
     835           0 :                                 fzp_close(&(rb->protocol1->fzp));
     836           0 :                                 sbuf_free_content(rb);
     837           0 :                                 return STR_OK;
     838             :                         default:
     839           0 :                                 iobuf_log_unexpected(rbuf, __func__);
     840           0 :                                 goto error;
     841             :                 }
     842             :         }
     843             : 
     844             :         // Otherwise, expecting to be told of a file to save.
     845          69 :         switch(rbuf->cmd)
     846             :         {
     847             :                 case CMD_DATAPTH:
     848           4 :                         if(iobuf_relative_path_attack(rbuf))
     849             :                                 goto error;
     850           4 :                         iobuf_move(&rb->protocol1->datapth, rbuf);
     851           4 :                         return STR_OK;
     852             :                 case CMD_ATTRIBS:
     853           8 :                         iobuf_move(&rb->attr, rbuf);
     854           8 :                         attribs_decode(rb);
     855           8 :                         return STR_OK;
     856             :                 case CMD_GEN:
     857           5 :                         if(!strcmp(rbuf->buf, "okbackupphase2end"))
     858             :                                 goto end_phase2;
     859           0 :                         iobuf_log_unexpected(rbuf, __func__);
     860           0 :                         goto error;
     861             :                 case CMD_INTERRUPT:
     862             :                         // Interrupt - forget about the last requested
     863             :                         // file if it matches. Otherwise, we can get
     864             :                         // stuck on the select in the async stuff,
     865             :                         // waiting for something that will never arrive.
     866          44 :                         if(*last_requested
     867          44 :                           && !strcmp(rbuf->buf, *last_requested))
     868           1 :                                 free_w(last_requested);
     869             :                         return STR_OK;
     870             :                 default:
     871             :                         break;
     872             :         }
     873           8 :         if(iobuf_is_filedata(rbuf)
     874           0 :           || iobuf_is_vssdata(rbuf))
     875             :         {
     876           8 :                 if(deal_with_filedata(asfd, sdirs, rb, rbuf, dpth, cconfs))
     877             :                         goto error;
     878             :                 return STR_OK;
     879             :         }
     880           0 :         iobuf_log_unexpected(rbuf, __func__);
     881             : 
     882             : error:
     883             :         return STR_ERROR;
     884             : end_phase2:
     885             :         return STR_FINISHED;
     886             : }
     887             : 
     888           5 : static int vss_opts_changed(struct sdirs *sdirs, struct conf **cconfs,
     889             :         const char *incexc)
     890             : {
     891           5 :         int ret=-1;
     892             :         struct conf **oldconfs;
     893             :         struct conf **newconfs;
     894           5 :         if(!(oldconfs=confs_alloc())
     895           5 :           || !(newconfs=confs_alloc()))
     896             :                 goto end;
     897           5 :         confs_init(oldconfs);
     898           5 :         confs_init(newconfs);
     899             : 
     900             :         // Figure out the old config, which is in the incexc file left
     901             :         // in the current backup directory on the server.
     902           5 :         if(is_reg_lstat(sdirs->cincexc)<=0
     903           1 :           || conf_parse_incexcs_path(oldconfs, sdirs->cincexc))
     904             :         {
     905             :                 // Assume that the file did not exist, and therefore
     906             :                 // the old split_vss setting is 0.
     907           4 :                 set_int(oldconfs[OPT_SPLIT_VSS], 0);
     908           4 :                 set_int(oldconfs[OPT_STRIP_VSS], 0);
     909             :         }
     910             : 
     911             :         // Figure out the new config, which is either in the incexc file from
     912             :         // the client, or in the cconf on the server.
     913           5 :         if(incexc)
     914             :         {
     915           0 :                 if(conf_parse_incexcs_buf(newconfs, incexc))
     916             :                 {
     917             :                         // Should probably not got here.
     918           0 :                         set_int(newconfs[OPT_SPLIT_VSS], 0);
     919           0 :                         set_int(newconfs[OPT_STRIP_VSS], 0);
     920             :                 }
     921             :         }
     922             :         else
     923             :         {
     924           5 :                 set_int(newconfs[OPT_SPLIT_VSS],
     925           5 :                         get_int(cconfs[OPT_SPLIT_VSS]));
     926           5 :                 set_int(newconfs[OPT_STRIP_VSS],
     927           5 :                         get_int(cconfs[OPT_STRIP_VSS]));
     928             :         }
     929             : 
     930           5 :         if(get_int(newconfs[OPT_SPLIT_VSS])!=get_int(oldconfs[OPT_SPLIT_VSS]))
     931             :         {
     932           0 :                 logp("split_vss=%d (changed since last backup)\n",
     933           0 :                         get_int(newconfs[OPT_SPLIT_VSS]));
     934           0 :                 ret=1; goto end;
     935             :         }
     936           5 :         if(get_int(newconfs[OPT_STRIP_VSS])!=get_int(oldconfs[OPT_STRIP_VSS]))
     937             :         {
     938           0 :                 logp("strip_vss=%d (changed since last backup)\n",
     939           0 :                         get_int(newconfs[OPT_STRIP_VSS]));
     940           0 :                 ret=1; goto end;
     941             :         }
     942             :         ret=0;
     943             : end:
     944           5 :         if(ret==1) logp("All files will be treated as new\n");
     945           5 :         confs_free(&oldconfs);
     946           5 :         confs_free(&newconfs);
     947           5 :         return ret;
     948             : }
     949             : 
     950             : // Maybe open the previous (current) manifest.
     951             : // If the split_vss setting changed between the previous backup and the new
     952             : // backup, do not open the previous manifest. This will have the effect of
     953             : // making the client back up everything fresh. Need to do this, otherwise
     954             : // toggling split_vss on and off will result in backups that do not work.
     955           5 : static int maybe_open_previous_manifest(struct manio **manio,
     956             :         struct sdirs *sdirs, const char *incexc, struct conf **cconfs)
     957             : {
     958           5 :         if(vss_opts_changed(sdirs, cconfs, incexc))
     959             :                 return 0;
     960           5 :         return open_previous_manifest(manio, sdirs);
     961             : }
     962             : 
     963         135 : static int process_next_file_from_manios(struct asfd *asfd,
     964             :         struct dpth *dpth,
     965             :         struct sdirs *sdirs,
     966             :         struct manio **p1manio,
     967             :         struct manio **cmanio,
     968             :         struct manio *ucmanio,
     969             :         struct manio **hmanio,
     970             :         struct sbuf **p1b,
     971             :         struct sbuf *cb,
     972             :         struct conf **cconfs)
     973             : {
     974         135 :         if(!(*p1b=sbuf_alloc(PROTO_1)))
     975             :                 goto error;
     976         135 :         switch(manio_read(*p1manio, *p1b))
     977             :         {
     978             :                 case 0:
     979             :                         break;
     980             :                 case 1:
     981           5 :                         manio_close(p1manio);
     982           5 :                         if(asfd->write_str(asfd, CMD_GEN, "backupphase2end"))
     983             :                                 goto error;
     984           5 :                         sbuf_free(p1b);
     985           5 :                         break;
     986             :                 case -1:
     987             :                         goto error;
     988             :         }
     989             : 
     990         135 :         if(!*cmanio)
     991             :         {
     992         124 :                 if(!*p1b) return 0;
     993             :                 // No old manifest, need to ask for a new file.
     994         120 :                 switch(process_new(cconfs, *p1b, ucmanio))
     995             :                 {
     996             :                         case P_NEW:
     997             :                                 return 0;
     998             :                         case P_DONE_NEW:
     999             :                                 goto p1b_done;
    1000             :                         default:
    1001             :                                 goto error;
    1002             :                 }
    1003             :         }
    1004             : 
    1005             :         // Have an old manifest, look for it there.
    1006             : 
    1007             :         // Might already have it, or be ahead in the old
    1008             :         // manifest.
    1009          11 :         if(cb->path.buf)
    1010             :         {
    1011           2 :                 switch(maybe_process_file(asfd, dpth,
    1012             :                         sdirs, cb, *p1b, ucmanio, hmanio, cconfs))
    1013             :                 {
    1014             :                         case P_NEW:
    1015             :                                 return 0;
    1016             :                         case P_CHANGED:
    1017             :                                 // Free cb content for things like encrypted
    1018             :                                 // files, which we only pretend are new.
    1019           0 :                                 sbuf_free_content(cb);
    1020           0 :                                 return 0;
    1021             :                         case P_DONE_NEW:
    1022             :                                 goto p1b_done;
    1023             :                         case P_DONE_CHANGED:
    1024             :                         case P_DONE_UNCHANGED:
    1025           0 :                                 sbuf_free_content(cb);
    1026           0 :                                 goto p1b_done;
    1027             :                         case P_DONE_DELETED:
    1028           2 :                                 sbuf_free_content(cb);
    1029           2 :                                 break;
    1030             :                         case P_ERROR:
    1031             :                                 goto error;
    1032             :                 }
    1033             :         }
    1034             : 
    1035             :         while(1)
    1036             :         {
    1037          11 :                 sbuf_free_content(cb);
    1038          11 :                 switch(manio_read(*cmanio, cb))
    1039             :                 {
    1040             :                         case 0: break;
    1041           1 :                         case 1: manio_close(cmanio);
    1042           1 :                                 if(*p1b) switch(process_new(cconfs,
    1043             :                                         *p1b, ucmanio))
    1044             :                                 {
    1045             :                                         case P_NEW:
    1046             :                                                 return 0;
    1047             :                                         case P_DONE_NEW:
    1048             :                                                 goto p1b_done;
    1049             :                                         default:
    1050             :                                                 goto error;
    1051             :                                 }
    1052             :                                 return 0;
    1053             :                         case -1: goto error;
    1054             :                 }
    1055          10 :                 switch(maybe_process_file(asfd, dpth, sdirs,
    1056             :                         cb, *p1b, ucmanio, hmanio, cconfs))
    1057             :                 {
    1058             :                         case P_NEW:
    1059             :                                 return 0;
    1060             :                         case P_CHANGED:
    1061             :                                 // Free cb content for things like encrypted
    1062             :                                 // files, which we only pretend are new.
    1063           2 :                                 sbuf_free_content(cb);
    1064           2 :                                 return 0;
    1065             :                         case P_DONE_NEW:
    1066             :                                 goto p1b_done;
    1067             :                         case P_DONE_CHANGED:
    1068             :                         case P_DONE_UNCHANGED:
    1069           6 :                                 sbuf_free_content(cb);
    1070           6 :                                 goto p1b_done;
    1071             :                         case P_DONE_DELETED:
    1072           0 :                                 sbuf_free_content(cb);
    1073           0 :                                 continue;
    1074             :                         case P_ERROR:
    1075             :                                 goto error;
    1076             :                 }
    1077             :         }
    1078             : error:
    1079             :         return -1;
    1080             : p1b_done:
    1081          74 :         sbuf_free(p1b);
    1082          74 :         return 0;
    1083             : }
    1084             : 
    1085          10 : int backup_phase2_server_protocol1(struct async *as, struct sdirs *sdirs,
    1086             :         const char *incexc, int resume, struct conf **cconfs)
    1087             : {
    1088          10 :         int ret=0;
    1089          10 :         man_off_t *p1pos=NULL;
    1090          10 :         struct manio *p1manio=NULL;
    1091          10 :         struct dpth *dpth=NULL;
    1092          10 :         char *deltmppath=NULL;
    1093          10 :         char *last_requested=NULL;
    1094          10 :         struct manio *chmanio=NULL; // changed data
    1095          10 :         struct manio *ucmanio=NULL; // unchanged data
    1096          10 :         struct manio *cmanio=NULL; // previous (current) manifest
    1097          10 :         struct manio *hmanio=NULL; // to look up deleted hardlinks
    1098          10 :         struct sbuf *cb=NULL; // file list in current manifest
    1099          10 :         struct sbuf *p1b=NULL; // file list from client
    1100          10 :         struct sbuf *rb=NULL; // receiving file from client
    1101          10 :         struct asfd *asfd=NULL;
    1102          10 :         int breaking=0;
    1103          10 :         int breakcount=0;
    1104          10 :         struct cntr *cntr=NULL;
    1105          10 :         struct cntr_ent *warn_ent=NULL;
    1106          10 :         int fail_on_warning=0;
    1107             : 
    1108          10 :         if(!as)
    1109             :         {
    1110           1 :                 logp("async not provided to %s()\n", __func__);
    1111           1 :                 goto error;
    1112             :         }
    1113           9 :         if(!sdirs)
    1114             :         {
    1115           2 :                 logp("sdirs not provided to %s()\n", __func__);
    1116           2 :                 goto error;
    1117             :         }
    1118           7 :         if(!cconfs)
    1119             :         {
    1120           1 :                 logp("cconfs not provided to %s()\n", __func__);
    1121           1 :                 goto error;
    1122             :         }
    1123           6 :         asfd=as->asfd;
    1124           6 :         if(!asfd)
    1125             :         {
    1126           1 :                 logp("asfd not provided to %s()\n", __func__);
    1127           1 :                 goto error;
    1128             :         }
    1129           5 :         cntr=get_cntr(cconfs);
    1130           5 :         fail_on_warning=get_int(cconfs[OPT_FAIL_ON_WARNING]);
    1131           5 :         if(cntr)
    1132           0 :                 warn_ent=cntr->ent[CMD_WARNING];
    1133             : 
    1134           5 :         if(get_int(cconfs[OPT_BREAKPOINT])>=2000
    1135           0 :           && get_int(cconfs[OPT_BREAKPOINT])<3000)
    1136             :         {
    1137           0 :                 breaking=get_int(cconfs[OPT_BREAKPOINT]);
    1138           0 :                 breakcount=breaking-2000;
    1139             :         }
    1140             : 
    1141           5 :         logp("Begin phase2 (receive file data)\n");
    1142             : 
    1143           5 :         if(!(dpth=dpth_alloc())
    1144           5 :           || dpth_protocol1_init(dpth, sdirs->currentdata,
    1145             :                 get_int(cconfs[OPT_MAX_STORAGE_SUBDIRS])))
    1146             :                         goto error;
    1147             : 
    1148           5 :         if(maybe_open_previous_manifest(&cmanio, sdirs, incexc, cconfs))
    1149             :                 goto error;
    1150             :         // If the first file that others hardlink to has been deleted, we will
    1151             :         // need to look through the previous manifest to find the information
    1152             :         // for that original file, in order to not have to copy all the data
    1153             :         // across again.
    1154           5 :         if(open_previous_manifest(&hmanio, sdirs))
    1155             :                 goto error;
    1156             : 
    1157           5 :         if(get_int(cconfs[OPT_DIRECTORY_TREE]))
    1158             :         {
    1159             :                 // Need to make sure we do not try to create a path that is
    1160             :                 // too long.
    1161           5 :                 if(build_path_w(sdirs->treepath)) goto error;
    1162           5 :                 mkdir(sdirs->treepath, 0777);
    1163           5 :                 treepathlen=strlen(sdirs->treepath);
    1164           5 :                 if(init_fs_max(sdirs->treepath))
    1165             :                         goto error;
    1166             :         }
    1167             : 
    1168           5 :         if(resume)
    1169             :         {
    1170           0 :                 if(!(p1pos=do_resume(sdirs, dpth, cconfs)))
    1171             :                         goto error;
    1172           0 :                 if(cntr_send_sdirs(asfd, sdirs, cconfs, CNTR_STATUS_BACKUP))
    1173             :                         goto error;
    1174             :         }
    1175             : 
    1176           5 :         if(!(p1manio=manio_open_phase1(sdirs->phase1data, "rb", PROTO_1))
    1177           5 :           || (resume && manio_seek(p1manio, p1pos)))
    1178             :                 goto error;
    1179           5 :         if(!(cb=sbuf_alloc(PROTO_1))
    1180           5 :           || !(p1b=sbuf_alloc(PROTO_1))
    1181           5 :           || !(rb=sbuf_alloc(PROTO_1)))
    1182             :                 goto error;
    1183             : 
    1184             :         // Unchanged and changed should now be truncated correctly, we just
    1185             :         // have to open them for appending.
    1186             :         // Data is not getting written to a compressed file.
    1187             :         // This is important for recovery if the power goes.
    1188           5 :         if(!(ucmanio=manio_open_phase2(sdirs->unchanged, "ab", PROTO_1))
    1189           5 :           || !(chmanio=manio_open_phase2(sdirs->changed, "ab", PROTO_1)))
    1190             :                 goto error;
    1191             : 
    1192             :         while(1)
    1193             :         {
    1194         921 :                 if(check_fail_on_warning(fail_on_warning, warn_ent))
    1195             :                         goto error;
    1196             : 
    1197         921 :                 if(breaking && breakcount--==0)
    1198           0 :                         return breakpoint(breaking, __func__);
    1199             : 
    1200         921 :                 if(write_status(CNTR_STATUS_BACKUP,
    1201         921 :                         rb->path.buf?rb->path.buf:"", cntr))
    1202             :                                 goto error;
    1203        1842 :                 if(last_requested
    1204         921 :                   || !p1manio
    1205          16 :                   || asfd->writebuflen)
    1206             :                 {
    1207         905 :                         iobuf_free_content(asfd->rbuf);
    1208         905 :                         if(asfd->as->read_write(asfd->as))
    1209             :                         {
    1210           0 :                                 logp("error in %s\n", __func__);
    1211           0 :                                 goto error;
    1212             :                         }
    1213             : 
    1214         905 :                         if(asfd->rbuf->buf)
    1215          85 :                           switch(do_stuff_to_receive(asfd, sdirs,
    1216             :                                 cconfs, rb, chmanio, dpth, &last_requested))
    1217             :                         {
    1218             :                                 case STR_OK:
    1219             :                                         break;
    1220             :                                 case STR_FINISHED:
    1221           5 :                                         if(check_fail_on_warning(
    1222             :                                                 fail_on_warning, warn_ent))
    1223             :                                                         goto error;
    1224             :                                         goto end;
    1225             :                                 case STR_ERROR:
    1226             :                                         goto error;
    1227             :                         }
    1228             :                 }
    1229             : 
    1230         916 :                 if(p1b) switch(do_stuff_to_send(asfd, p1b, &last_requested))
    1231             :                 {
    1232             :                         case STS_OK:
    1233          61 :                                 sbuf_free(&p1b);
    1234          61 :                                 break;
    1235             :                         case STS_BLOCKED:
    1236           2 :                                 continue;
    1237             :                         case STS_ERROR:
    1238             :                                 goto error;
    1239             :                 }
    1240             : 
    1241         914 :                 if(p1manio
    1242         135 :                  && process_next_file_from_manios(asfd, dpth, sdirs,
    1243             :                         &p1manio, &cmanio, ucmanio, &hmanio, &p1b, cb, cconfs))
    1244             :                                         goto error;
    1245             :         }
    1246             : 
    1247             : error:
    1248           5 :         ret=-1;
    1249           5 :         if(p1b)
    1250           0 :                 logp("  last tried file:    %s\n",
    1251             :                         iobuf_to_printable(&p1b->path));
    1252             : end:
    1253          10 :         if(manio_close(&chmanio))
    1254             :         {
    1255           0 :                 logp("error closing %s in %s\n", sdirs->changed, __func__);
    1256           0 :                 ret=-1;
    1257             :         }
    1258          10 :         if(manio_close(&ucmanio))
    1259             :         {
    1260           0 :                 logp("error closing %s in %s\n", sdirs->unchanged, __func__);
    1261           0 :                 ret=-1;
    1262             :         }
    1263          10 :         free_w(&deltmppath);
    1264          10 :         free_w(&last_requested);
    1265          10 :         sbuf_free(&cb);
    1266          10 :         sbuf_free(&p1b);
    1267          10 :         sbuf_free(&rb);
    1268          10 :         manio_close(&p1manio);
    1269          10 :         manio_close(&cmanio);
    1270          10 :         manio_close(&hmanio);
    1271          10 :         dpth_free(&dpth);
    1272          10 :         man_off_t_free(&p1pos);
    1273          10 :         if(!ret && sdirs)
    1274           5 :                 unlink(sdirs->phase1data);
    1275             : 
    1276          10 :         logp("End phase2 (receive file data)\n");
    1277             : 
    1278          10 :         return ret;
    1279             : }

Generated by: LCOV version 1.13