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

Generated by: LCOV version 1.10