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: 316 406 77.8 %
Date: 2016-01-03 Functions: 20 22 90.9 %

          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 "../../fsops.h"
      11             : #include "../../handy.h"
      12             : #include "../../iobuf.h"
      13             : #include "../../log.h"
      14             : #include "../../sbuf.h"
      15             : #include "../child.h"
      16             : #include "../compress.h"
      17             : #include "../resume.h"
      18             : #include "blocklen.h"
      19             : #include "dpth.h"
      20             : 
      21             : static size_t treepathlen=0;
      22             : 
      23           0 : static int path_length_warn(struct iobuf *path, struct conf **cconfs)
      24             : {
      25           0 :         if(get_int(cconfs[OPT_PATH_LENGTH_WARN]))
      26           0 :                 logw(NULL, get_cntr(cconfs), "Path too long for tree - will save in data structure instead: %s\n", path->buf);
      27           0 :         return 1;
      28             : }
      29             : 
      30           4 : static int path_too_long(struct iobuf *path, struct conf **cconfs)
      31             : {
      32             :         static const char *cp;
      33           4 :         if(treepathlen+path->len+1>fs_full_path_max)
      34             :         {
      35             :                 // FIX THIS:
      36             :                 // Cannot warn down the asfd to the client, because it can
      37             :                 // arrive after the client has disconnected, which causes
      38             :                 // an error on the server side.
      39             :                 // Would need to change the way that "backupphase2end" works
      40             :                 // to be able to fix it.
      41           0 :                 return path_length_warn(path, cconfs);
      42             :         }
      43           8 :         if((cp=strrchr(path->buf, '/'))) cp++;
      44           0 :         else cp=path->buf;
      45           4 :         if(strlen(cp)>fs_name_max) return path_length_warn(path, cconfs);
      46             :         return 0;
      47             : }
      48             : 
      49           4 : static int treedata(struct sbuf *sb, struct conf **cconfs)
      50             : {
      51             :         // Windows is sending directory data as if it is file data - this
      52             :         // cannot be saved in a tree structure.
      53           4 :         if(S_ISDIR(sb->statp.st_mode)) return 0;
      54             : 
      55           4 :         if(sb->path.cmd!=CMD_FILE
      56           4 :           && sb->path.cmd!=CMD_ENC_FILE
      57           4 :           && sb->path.cmd!=CMD_EFS_FILE)
      58             :                 return 0;
      59             : 
      60           4 :         return !path_too_long(&sb->path, cconfs);
      61             : }
      62             : 
      63           4 : static char *set_new_datapth(struct asfd *asfd,
      64             :         struct sdirs *sdirs, struct conf **cconfs,
      65             :         struct sbuf *sb, struct dpth *dpth, int *istreedata)
      66             : {
      67           4 :         char *tmp=NULL;
      68           4 :         char *rpath=NULL;
      69           4 :         if(get_int(cconfs[OPT_DIRECTORY_TREE]))
      70           4 :                 *istreedata=treedata(sb, cconfs);
      71             : 
      72           4 :         if(*istreedata)
      73             :         {
      74             :                 // We want to place this file in a directory structure like
      75             :                 // the directory structure on the original client.
      76           4 :                 if(!(tmp=prepend_s(TREE_DIR, sb->path.buf)))
      77             :                 {
      78           0 :                         log_and_send_oom(asfd, __func__);
      79             :                         return NULL;
      80             :                 }
      81             :         }
      82             :         else
      83             :         {
      84           0 :                 if(!(tmp=strdup_w(dpth_protocol1_mk(dpth, sb->compression,
      85           0 :                         sb->path.cmd), __func__))) return NULL;
      86             :         }
      87           4 :         iobuf_from_str(&sb->protocol1->datapth, CMD_DATAPTH, tmp);
      88           4 :         if(build_path(sdirs->datadirtmp,
      89           4 :                 sb->protocol1->datapth.buf, &rpath, sdirs->datadirtmp))
      90             :         {
      91           0 :                 log_and_send(asfd, "build path failed");
      92             :                 return NULL;
      93             :         }
      94           4 :         return rpath;
      95             : }
      96             : 
      97           4 : static int start_to_receive_new_file(struct asfd *asfd,
      98             :         struct sdirs *sdirs, struct conf **cconfs,
      99             :         struct sbuf *sb, struct dpth *dpth)
     100             : {
     101           4 :         int ret=-1;
     102           4 :         char *rpath=NULL;
     103           4 :         int istreedata=0;
     104             : 
     105             : //logp("start to receive: %s\n", sb->path.buf);
     106             : 
     107           4 :         if(!(rpath=set_new_datapth(asfd,
     108           4 :                 sdirs, cconfs, sb, dpth, &istreedata)))
     109             :                         goto end;
     110             :         
     111           4 :         if(!(sb->protocol1->fzp=fzp_open(rpath, "wb")))
     112             :         {
     113           0 :                 log_and_send(asfd, "make file failed");
     114           0 :                 goto end;
     115             :         }
     116           4 :         if(!istreedata) dpth_incr(dpth);
     117             :         ret=0;
     118             : end:
     119           4 :         free_w(&rpath);
     120           4 :         return ret;
     121             : }
     122             : 
     123           2 : static int process_changed_file(struct asfd *asfd,
     124             :         struct sdirs *sdirs, struct conf **cconfs,
     125             :         struct sbuf *cb, struct sbuf *p1b,
     126             :         const char *adir)
     127             : {
     128           2 :         int ret=-1;
     129           2 :         size_t blocklen=0;
     130           2 :         char *curpath=NULL;
     131             :         //logp("need to process changed file: %s (%s)\n",
     132             :         //      cb->path, cb->datapth);
     133             : 
     134             :         // Move datapth onto p1b.
     135           2 :         iobuf_move(&p1b->protocol1->datapth, &cb->protocol1->datapth);
     136             : 
     137           2 :         if(!(curpath=prepend_s(adir, p1b->protocol1->datapth.buf)))
     138             :         {
     139           0 :                 log_out_of_memory(__func__);
     140             :                 goto end;
     141             :         }
     142           2 :         if(dpth_protocol1_is_compressed(cb->compression, curpath))
     143           0 :                 p1b->protocol1->sigfzp=fzp_gzopen(curpath, "rb");
     144             :         else
     145           2 :                 p1b->protocol1->sigfzp=fzp_open(curpath, "rb");
     146           2 :         if(!p1b->protocol1->sigfzp)
     147             :         {
     148           0 :                 logp("could not open %s: %s\n", curpath, strerror(errno));
     149             :                 goto end;
     150             :         }
     151             : 
     152           2 :         blocklen=get_librsync_block_len(cb->endfile.buf);
     153           2 :         if(!(p1b->protocol1->sigjob=
     154             : #ifdef RS_DEFAULT_STRONG_LEN
     155           2 :                 rs_sig_begin(blocklen, RS_DEFAULT_STRONG_LEN)
     156             : #else
     157             :                 // This is for librsync-1.0.0. RS_DEFAULT_STRONG_LEN was 8 in
     158             :                 // librsync-0.9.7.
     159             :                 rs_sig_begin(blocklen, 8,
     160             :                   rshash_to_magic_number(get_e_rshash(cconfs[OPT_RSHASH])))
     161             : #endif
     162           2 :         ))
     163             :         {
     164           0 :                 logp("could not start signature job.\n");
     165             :                 goto end;
     166             :         }
     167             :         //logp("sig begin: %s\n", p1b->protocol1->datapth.buf);
     168           2 :         if(!(p1b->protocol1->infb=rs_filebuf_new(NULL,
     169             :                 p1b->protocol1->sigfzp,
     170           2 :                 NULL, blocklen, -1)))
     171             :         {
     172           0 :                 logp("could not rs_filebuf_new for infb.\n");
     173             :                 goto end;
     174             :         }
     175           2 :         if(!(p1b->protocol1->outfb=rs_filebuf_new(NULL, NULL,
     176           2 :                 asfd, ASYNC_BUF_LEN, -1)))
     177             :         {
     178           0 :                 logp("could not rs_filebuf_new for in_outfb.\n");
     179             :                 goto end;
     180             :         }
     181             : 
     182             :         // Flag the things that need to be sent (to the client)
     183           2 :         p1b->flags |= SBUF_SEND_DATAPTH;
     184           2 :         p1b->flags |= SBUF_SEND_STAT;
     185           2 :         p1b->flags |= SBUF_SEND_PATH;
     186             : 
     187             :         //logp("sending sig for %s\n", p1b->path);
     188             :         //logp("(%s)\n", p1b->datapth);
     189             : 
     190           2 :         ret=0;
     191             : end:
     192           2 :         free_w(&curpath);
     193           2 :         return ret;
     194             : }
     195             : 
     196          68 : static int new_non_file(struct sbuf *p1b,
     197             :         struct manio *ucmanio, struct conf **cconfs)
     198             : {
     199             :         // Is something that does not need more data backed up.
     200             :         // Like a directory or a link or something like that.
     201             :         // Goes into the unchanged file, so that it does not end up out of
     202             :         // order with normal files, which has to wait around for their data
     203             :         // to turn up.
     204          68 :         if(manio_write_sbuf(ucmanio, p1b))
     205             :                 return -1;
     206          68 :         cntr_add(get_cntr(cconfs), p1b->path.cmd, 0);
     207          68 :         sbuf_free_content(p1b);
     208          68 :         return 0;
     209             : }
     210             : 
     211           0 : static int changed_non_file(struct sbuf *p1b,
     212             :         struct manio *ucmanio, enum cmd cmd, struct conf **cconfs)
     213             : {
     214             :         // As new_non_file.
     215           0 :         if(manio_write_sbuf(ucmanio, p1b))
     216             :                 return -1;
     217           0 :         cntr_add_changed(get_cntr(cconfs), cmd);
     218           0 :         sbuf_free_content(p1b);
     219           0 :         return 0;
     220             : }
     221             : 
     222         127 : static int process_new(struct sdirs *sdirs, struct conf **cconfs,
     223             :         struct sbuf *p1b, struct manio *ucmanio)
     224             : {
     225         127 :         if(!p1b->path.buf) return 0;
     226         244 :         if(sbuf_is_filedata(p1b)
     227         122 :           || sbuf_is_vssdata(p1b))
     228             :         {
     229             :                 //logp("need to process new file: %s\n", p1b->path);
     230             :                 // Flag the things that need to be sent (to the client)
     231          54 :                 p1b->flags |= SBUF_SEND_STAT;
     232          54 :                 p1b->flags |= SBUF_SEND_PATH;
     233             :                 return 0;
     234             :         }
     235          68 :         return new_non_file(p1b, ucmanio, cconfs);
     236             : }
     237             : 
     238           6 : static int process_unchanged_file(struct sbuf *p1b, struct sbuf *cb,
     239             :         struct manio *ucmanio, struct conf **cconfs)
     240             : {
     241             :         // Need to re-encode the p1b attribs to include compression and
     242             :         // other bits and pieces that are recorded on cb.
     243           6 :         iobuf_move(&p1b->protocol1->datapth, &cb->protocol1->datapth);
     244           6 :         iobuf_move(&p1b->endfile, &cb->endfile);
     245           6 :         p1b->compression=cb->compression;
     246           6 :         p1b->winattr=cb->winattr;
     247           6 :         if(attribs_encode(p1b))
     248             :                 return -1;
     249           6 :         if(manio_write_sbuf(ucmanio, p1b))
     250             :                 return -1;
     251           6 :         cntr_add_same(get_cntr(cconfs), p1b->path.cmd);
     252           6 :         if(p1b->endfile.buf) cntr_add_bytes(get_cntr(cconfs),
     253           0 :                  strtoull(p1b->endfile.buf, NULL, 10));
     254           6 :         sbuf_free_content(cb);
     255           6 :         return 1;
     256             : }
     257             : 
     258             : static int process_new_file(struct sdirs *sdirs, struct conf **cconfs,
     259             :         struct sbuf *cb, struct sbuf *p1b, struct manio *ucmanio)
     260             : {
     261           2 :         if(process_new(sdirs, cconfs, p1b, ucmanio))
     262             :                 return -1;
     263           2 :         sbuf_free_content(cb);
     264             :         return 1;
     265             : }
     266             : 
     267          10 : static int maybe_do_delta_stuff(struct asfd *asfd,
     268             :         struct sdirs *sdirs, struct sbuf *cb, struct sbuf *p1b,
     269             :         struct manio *ucmanio, struct conf **cconfs)
     270             : {
     271          10 :         int oldcompressed=0;
     272          10 :         int compression=p1b->compression;
     273             : 
     274             :         // If the file type changed, I think it is time to back it up again
     275             :         // (for example, EFS changing to normal file, or back again).
     276          10 :         if(cb->path.cmd!=p1b->path.cmd)
     277           0 :                 return process_new_file(sdirs, cconfs, cb, p1b, ucmanio);
     278             : 
     279             :         // mtime is the actual file data.
     280             :         // ctime is the attributes or meta data.
     281          10 :         if(cb->statp.st_mtime==p1b->statp.st_mtime
     282           6 :           && cb->statp.st_ctime==p1b->statp.st_ctime)
     283             :         {
     284             :                 // got an unchanged file
     285             :                 //logp("got unchanged file: %s %c %c\n",
     286             :                 //      cb->path.buf, cb->path.cmd, p1b->path.cmd);
     287           6 :                 return process_unchanged_file(p1b, cb, ucmanio, cconfs);
     288             :         }
     289             : 
     290           4 :         if(cb->statp.st_mtime==p1b->statp.st_mtime
     291           0 :           && cb->statp.st_ctime!=p1b->statp.st_ctime)
     292             :         {
     293             :                 // File data stayed the same, but attributes or meta data
     294             :                 // changed. We already have the attributes, but may need to get
     295             :                 // extra meta data.
     296             :                 // FIX THIS horrible mess.
     297           0 :                 if(cb->path.cmd==CMD_ENC_METADATA
     298           0 :                   || p1b->path.cmd==CMD_ENC_METADATA
     299           0 :                   || cb->path.cmd==CMD_EFS_FILE
     300           0 :                   || p1b->path.cmd==CMD_EFS_FILE
     301             :                 // FIX THIS: make unencrypted metadata use the librsync
     302           0 :                   || cb->path.cmd==CMD_METADATA
     303           0 :                   || p1b->path.cmd==CMD_METADATA
     304           0 :                   || sbuf_is_vssdata(cb)
     305           0 :                   || sbuf_is_vssdata(p1b))
     306             :                         return process_new_file(sdirs,
     307           0 :                                 cconfs, cb, p1b, ucmanio);
     308             :                 // On Windows, we have to back up the whole file if ctime
     309             :                 // changed, otherwise things like permission changes do not get
     310             :                 // noticed. So, in that case, fall through to the changed stuff
     311             :                 // below.
     312             :                 // Non-Windows clients finish here.
     313           0 :                 else if(!get_int(cconfs[OPT_CLIENT_IS_WINDOWS]))
     314             :                         return process_unchanged_file(p1b,
     315           0 :                                 cb, ucmanio, cconfs);
     316             :         }
     317             : 
     318             :         // Got a changed file.
     319             :         //logp("got changed file: %s\n", p1b->path.buf);
     320             : 
     321             :         // If either old or new is encrypted, or librsync is off, we need to
     322             :         // get a new file.
     323             :         // FIX THIS horrible mess.
     324           8 :         if(!get_int(cconfs[OPT_LIBRSYNC])
     325             :         // FIX THIS: make unencrypted metadata use the librsync
     326           4 :           || cb->path.cmd==CMD_METADATA
     327           4 :           || p1b->path.cmd==CMD_METADATA
     328           4 :           || sbuf_is_encrypted(cb)
     329           2 :           || sbuf_is_encrypted(p1b)
     330           2 :           || sbuf_is_vssdata(cb)
     331           6 :           || sbuf_is_vssdata(p1b))
     332           2 :                 return process_new_file(sdirs, cconfs, cb, p1b, ucmanio);
     333             : 
     334             :         // Get new files if they have switched between compression on or off.
     335           2 :         if(cb->protocol1->datapth.buf
     336           4 :           && dpth_protocol1_is_compressed(cb->compression,
     337           2 :             cb->protocol1->datapth.buf))
     338           0 :                 oldcompressed=1;
     339           2 :         if( ( oldcompressed && !compression)
     340           2 :          || (!oldcompressed &&  compression))
     341           0 :                 return process_new_file(sdirs, cconfs, cb, p1b, ucmanio);
     342             : 
     343             :         // Otherwise, do the delta stuff (if possible).
     344           4 :         if(sbuf_is_filedata(p1b)
     345           2 :           || sbuf_is_vssdata(p1b))
     346             :         {
     347           2 :                 if(process_changed_file(asfd, sdirs, cconfs, cb, p1b,
     348           2 :                         sdirs->currentdata)) return -1;
     349             :         }
     350             :         else
     351             :         {
     352           0 :                 if(changed_non_file(p1b, ucmanio, p1b->path.cmd, cconfs))
     353             :                         return -1;
     354             :         }
     355           2 :         sbuf_free_content(cb);
     356             :         return 1;
     357             : }
     358             : 
     359             : // return 1 to say that a file was processed
     360          10 : static int maybe_process_file(struct asfd *asfd,
     361             :         struct sdirs *sdirs, struct sbuf *cb, struct sbuf *p1b,
     362             :         struct manio *ucmanio, struct conf **cconfs)
     363             : {
     364          10 :         switch(sbuf_pathcmp(cb, p1b))
     365             :         {
     366             :                 case 0:
     367             :                         return maybe_do_delta_stuff(asfd, sdirs, cb, p1b,
     368          10 :                                 ucmanio, cconfs);
     369             :                 case 1:
     370             :                         //logp("ahead: %s\n", p1b->path);
     371             :                         // ahead - need to get the whole file
     372           0 :                         if(process_new(sdirs, cconfs, p1b, ucmanio))
     373             :                                 return -1;
     374             :                         // Do not free.
     375           0 :                         return 1;
     376             :                 case -1:
     377             :                 default:
     378             :                         //logp("behind: %s\n", p1b->path);
     379             :                         // Behind - need to read more from the old manifest.
     380             :                         // Count a deleted file - it was in the old manifest
     381             :                         // but not the new.
     382             :                         cntr_add_deleted(get_cntr(cconfs),
     383           0 :                                 cb->path.cmd);
     384           0 :                         return 0;
     385             :         }
     386             : }
     387             : 
     388             : // Return 1 if there is still stuff needing to be sent.
     389             : // FIX THIS: lots of repeated code.
     390         916 : static int do_stuff_to_send(struct asfd *asfd,
     391             :         struct sbuf *p1b, char **last_requested)
     392             : {
     393             :         static struct iobuf wbuf;
     394         916 :         if(p1b->flags & SBUF_SEND_DATAPTH)
     395             :         {
     396           2 :                 iobuf_copy(&wbuf, &p1b->protocol1->datapth);
     397           2 :                 switch(asfd->append_all_to_write_buffer(asfd, &wbuf))
     398             :                 {
     399             :                         case APPEND_OK: break;
     400             :                         case APPEND_BLOCKED: return 1;
     401           0 :                         default: return -1;
     402             :                 }
     403           2 :                 p1b->flags &= ~SBUF_SEND_DATAPTH;
     404             :         }
     405         916 :         if(p1b->flags & SBUF_SEND_STAT)
     406             :         {
     407          56 :                 iobuf_copy(&wbuf, &p1b->attr);
     408          56 :                 switch(asfd->append_all_to_write_buffer(asfd, &wbuf))
     409             :                 {
     410             :                         case APPEND_OK: break;
     411             :                         case APPEND_BLOCKED: return 1;
     412           0 :                         default: return -1;
     413             :                 }
     414          56 :                 p1b->flags &= ~SBUF_SEND_STAT;
     415             :         }
     416         916 :         if(p1b->flags & SBUF_SEND_PATH)
     417             :         {
     418          56 :                 iobuf_copy(&wbuf, &p1b->path);
     419          56 :                 switch(asfd->append_all_to_write_buffer(asfd, &wbuf))
     420             :                 {
     421             :                         case APPEND_OK: break;
     422             :                         case APPEND_BLOCKED: return 1;
     423           0 :                         default: return -1;
     424             :                 }
     425          56 :                 p1b->flags &= ~SBUF_SEND_PATH;
     426          56 :                 free_w(last_requested);
     427          56 :                 if(!(*last_requested=strdup_w(p1b->path.buf, __func__)))
     428             :                         return -1;
     429             :         }
     430         916 :         if(p1b->protocol1->sigjob && !(p1b->flags & SBUF_SEND_ENDOFSIG))
     431             :         {
     432             :                 rs_result sigresult;
     433             : 
     434           4 :                 switch((sigresult=rs_async(p1b->protocol1->sigjob,
     435             :                         &(p1b->protocol1->rsbuf),
     436           4 :                         p1b->protocol1->infb, p1b->protocol1->outfb)))
     437             :                 {
     438             :                         case RS_DONE:
     439           2 :                                 p1b->flags |= SBUF_SEND_ENDOFSIG;
     440             :                                 break;
     441             :                         case RS_BLOCKED:
     442             :                         case RS_RUNNING:
     443             :                                 // keep going round the loop.
     444             :                                 return 1;
     445             :                         default:
     446           0 :                                 logp("error in rs_async: %d\n", sigresult);
     447           0 :                                 return -1;
     448             :                 }
     449             :         }
     450         914 :         if(p1b->flags & SBUF_SEND_ENDOFSIG)
     451             :         {
     452           2 :                 iobuf_from_str(&wbuf, CMD_END_FILE, (char *)"endfile");
     453           2 :                 switch(asfd->append_all_to_write_buffer(asfd, &wbuf))
     454             :                 {
     455             :                         case APPEND_OK: break;
     456             :                         case APPEND_BLOCKED: return 1;
     457           0 :                         default: return -1;
     458             :                 }
     459           2 :                 p1b->flags &= ~SBUF_SEND_ENDOFSIG;
     460             :         }
     461             :         return 0;
     462             : }
     463             : 
     464           4 : static int start_to_receive_delta(struct sdirs *sdirs, struct conf **cconfs,
     465             :         struct sbuf *rb)
     466             : {
     467           4 :         if(rb->compression)
     468             :         {
     469           0 :                 if(!(rb->protocol1->fzp=fzp_gzopen(sdirs->deltmppath,
     470           0 :                         comp_level(rb->compression))))
     471             :                                 return -1;
     472             :         }
     473             :         else
     474             :         {
     475           4 :                 if(!(rb->protocol1->fzp=fzp_open(sdirs->deltmppath, "wb")))
     476             :                         return -1;
     477             :         }
     478           4 :         rb->flags |= SBUF_RECV_DELTA;
     479             : 
     480             :         return 0;
     481             : }
     482             : 
     483           4 : static int finish_delta(struct sdirs *sdirs, struct sbuf *rb)
     484             : {
     485           4 :         int ret=0;
     486           4 :         char *deltmp=NULL;
     487           4 :         char *delpath=NULL;
     488           8 :         if(!(deltmp=prepend_s("deltas.forward", rb->protocol1->datapth.buf))
     489           4 :           || !(delpath=prepend_s(sdirs->working, deltmp))
     490           4 :           || mkpath(&delpath, sdirs->working)
     491             :         // Rename race condition is of no consequence here, as delpath will
     492             :         // just get recreated.
     493           8 :           || do_rename(sdirs->deltmppath, delpath))
     494           0 :                 ret=-1;
     495           4 :         free_w(&delpath);
     496           4 :         free_w(&deltmp);
     497           4 :         return ret;
     498             : }
     499             : 
     500           8 : static int deal_with_receive_end_file(struct asfd *asfd, struct sdirs *sdirs,
     501           4 :         struct sbuf *rb, struct manio *chmanio, struct conf **cconfs,
     502             :         char **last_requested)
     503             : {
     504           8 :         int ret=-1;
     505             :         static char *cp=NULL;
     506             :         static struct iobuf *rbuf;
     507           8 :         struct cntr *cntr=get_cntr(cconfs);
     508           8 :         rbuf=asfd->rbuf;
     509             :         // Finished the file.
     510             :         // Write it to the phase2 file, and free the buffers.
     511             : 
     512           8 :         if(fzp_close(&(rb->protocol1->fzp)))
     513             :         {
     514           0 :                 logp("error closing delta for %s in receive\n", rb->path);
     515             :                 goto end;
     516             :         }
     517           8 :         iobuf_move(&rb->endfile, rbuf);
     518          12 :         if(rb->flags & SBUF_RECV_DELTA && finish_delta(sdirs, rb))
     519             :                 goto end;
     520             : 
     521           8 :         if(manio_write_sbuf(chmanio, rb))
     522             :                 goto end;
     523             : 
     524           8 :         if(rb->flags & SBUF_RECV_DELTA)
     525           4 :                 cntr_add_changed(cntr, rb->path.cmd);
     526             :         else
     527           4 :                 cntr_add(cntr, rb->path.cmd, 0);
     528             : 
     529           8 :         if(*last_requested && !strcmp(rb->path.buf, *last_requested))
     530           2 :                 free_w(last_requested);
     531             : 
     532          16 :         cp=strchr(rb->endfile.buf, ':');
     533           8 :         if(rb->endfile.buf)
     534           8 :                 cntr_add_bytes(cntr, strtoull(rb->endfile.buf, NULL, 10));
     535             :         if(cp)
     536             :         {
     537             :                 // checksum stuff goes here
     538             :         }
     539             : 
     540             :         ret=0;
     541             : end:
     542           8 :         sbuf_free_content(rb);
     543           8 :         return ret;
     544             : }
     545             : 
     546           8 : static int deal_with_receive_append(struct asfd *asfd, struct sbuf *rb,
     547             :         struct conf **cconfs)
     548             : {
     549           8 :         int app=0;
     550             :         static struct iobuf *rbuf;
     551           8 :         rbuf=asfd->rbuf;
     552             :         //logp("rbuf->len: %d\n", rbuf->len);
     553             : 
     554           8 :         cntr_add_recvbytes(get_cntr(cconfs), rbuf->len);
     555           8 :         if(rb->protocol1->fzp)
     556           8 :                 app=fzp_write(rb->protocol1->fzp, rbuf->buf, rbuf->len);
     557             : 
     558           8 :         if(app>0) return 0;
     559           0 :         logp("error when appending: %d\n", app);
     560           0 :         asfd->write_str(asfd, CMD_ERROR, "write failed");
     561             :         return -1;
     562             : }
     563             : 
     564           8 : static int deal_with_filedata(struct asfd *asfd,
     565             :         struct sdirs *sdirs, struct sbuf *rb,
     566             :         struct iobuf *rbuf, struct dpth *dpth, struct conf **cconfs)
     567             : {
     568           8 :         iobuf_move(&rb->path, rbuf);
     569             : 
     570           8 :         if(rb->protocol1->datapth.buf)
     571             :         {
     572             :                 // Receiving a delta.
     573           4 :                 if(start_to_receive_delta(sdirs, cconfs, rb))
     574             :                 {
     575           0 :                         logp("error in start_to_receive_delta\n");
     576           0 :                         return -1;
     577             :                 }
     578             :                 return 0;
     579             :         }
     580             : 
     581             :         // Receiving a whole new file.
     582           4 :         if(start_to_receive_new_file(asfd, sdirs, cconfs, rb, dpth))
     583             :         {
     584           0 :                 logp("error in start_to_receive_new_file\n");
     585           0 :                 return -1;
     586             :         }
     587             :         return 0;
     588             : }
     589             : 
     590             : // returns 1 for finished ok.
     591         905 : static int do_stuff_to_receive(struct asfd *asfd,
     592             :         struct sdirs *sdirs, struct conf **cconfs,
     593             :         struct sbuf *rb, struct manio *chmanio,
     594             :         struct dpth *dpth, char **last_requested)
     595             : {
     596         905 :         struct iobuf *rbuf=asfd->rbuf;
     597             : 
     598         905 :         iobuf_free_content(rbuf);
     599             :         // This also attempts to write anything in the write buffer.
     600         905 :         if(asfd->as->read_write(asfd->as))
     601             :         {
     602           0 :                 logp("error in %s\n", __func__);
     603           0 :                 return -1;
     604             :         }
     605             : 
     606         905 :         if(!rbuf->buf) return 0;
     607             : 
     608          85 :         if(rbuf->cmd==CMD_MESSAGE
     609          85 :           || rbuf->cmd==CMD_WARNING)
     610             :         {
     611           0 :                 struct cntr *cntr=NULL;
     612           0 :                 if(cconfs) cntr=get_cntr(cconfs);
     613           0 :                 log_recvd(rbuf, cntr, 0);
     614           0 :                 return 0;
     615             :         }
     616             : 
     617          85 :         if(rb->protocol1->fzp)
     618             :         {
     619             :                 // Currently writing a file (or meta data)
     620          16 :                 switch(rbuf->cmd)
     621             :                 {
     622             :                         case CMD_APPEND:
     623           8 :                                 if(deal_with_receive_append(asfd, rb, cconfs))
     624             :                                         goto error;
     625             :                                 return 0;
     626             :                         case CMD_END_FILE:
     627           8 :                                 if(deal_with_receive_end_file(asfd, sdirs, rb,
     628           8 :                                         chmanio, cconfs, last_requested))
     629             :                                                 goto error;
     630             :                                 return 0;
     631             :                         default:
     632           0 :                                 iobuf_log_unexpected(rbuf, __func__);
     633           0 :                                 goto error;
     634             :                 }
     635             :         }
     636             : 
     637             :         // Otherwise, expecting to be told of a file to save.
     638          69 :         switch(rbuf->cmd)
     639             :         {
     640             :                 case CMD_DATAPTH:
     641           4 :                         iobuf_move(&rb->protocol1->datapth, rbuf);
     642           4 :                         return 0;
     643             :                 case CMD_ATTRIBS:
     644           8 :                         iobuf_move(&rb->attr, rbuf);
     645           8 :                         attribs_decode(rb);
     646           8 :                         return 0;
     647             :                 case CMD_GEN:
     648           5 :                         if(!strcmp(rbuf->buf, "okbackupphase2end"))
     649             :                                 goto end_phase2;
     650           0 :                         iobuf_log_unexpected(rbuf, __func__);
     651           0 :                         goto error;
     652             :                 case CMD_INTERRUPT:
     653             :                         // Interrupt - forget about the last requested
     654             :                         // file if it matches. Otherwise, we can get
     655             :                         // stuck on the select in the async stuff,
     656             :                         // waiting for something that will never arrive.
     657          44 :                         if(*last_requested
     658          44 :                           && !strcmp(rbuf->buf, *last_requested))
     659           1 :                                 free_w(last_requested);
     660             :                         return 0;
     661             :                 default:
     662             :                         break;
     663             :         }
     664          16 :         if(iobuf_is_filedata(rbuf)
     665           8 :           || iobuf_is_vssdata(rbuf))
     666             :         {
     667           8 :                 if(deal_with_filedata(asfd, sdirs, rb, rbuf, dpth, cconfs))
     668             :                         goto error;
     669             :                 return 0;
     670             :         }
     671           0 :         iobuf_log_unexpected(rbuf, __func__);
     672             : 
     673             : error:
     674             :         return -1;
     675             : end_phase2:
     676             :         return 1;
     677             : }
     678             : 
     679           1 : static int vss_opts_changed(struct sdirs *sdirs, struct conf **cconfs,
     680             :         const char *incexc)
     681             : {
     682           1 :         int ret=-1;
     683             :         struct conf **oldconfs;
     684             :         struct conf **newconfs;
     685           2 :         if(!(oldconfs=confs_alloc())
     686           1 :           || !(newconfs=confs_alloc()))
     687             :                 goto end;
     688           1 :         confs_init(oldconfs);
     689           1 :         confs_init(newconfs);
     690             : 
     691             :         // Figure out the old config, which is in the incexc file left
     692             :         // in the current backup directory on the server.
     693           1 :         if(conf_parse_incexcs_path(oldconfs, sdirs->cincexc))
     694             :         {
     695             :                 // Assume that the file did not exist, and therefore
     696             :                 // the old split_vss setting is 0.
     697           0 :                 set_int(oldconfs[OPT_SPLIT_VSS], 0);
     698           0 :                 set_int(oldconfs[OPT_STRIP_VSS], 0);
     699             :         }
     700             : 
     701             :         // Figure out the new config, which is either in the incexc file from
     702             :         // the client, or in the cconf on the server.
     703           1 :         if(incexc)
     704             :         {
     705           0 :                 if(conf_parse_incexcs_buf(newconfs, incexc))
     706             :                 {
     707             :                         // Should probably not got here.
     708           0 :                         set_int(newconfs[OPT_SPLIT_VSS], 0);
     709           0 :                         set_int(newconfs[OPT_STRIP_VSS], 0);
     710             :                 }
     711             :         }
     712             :         else
     713             :         {
     714           1 :                 set_int(newconfs[OPT_SPLIT_VSS],
     715           2 :                         get_int(cconfs[OPT_SPLIT_VSS]));
     716           1 :                 set_int(newconfs[OPT_STRIP_VSS],
     717           2 :                         get_int(cconfs[OPT_STRIP_VSS]));
     718             :         }
     719             : 
     720           1 :         if(get_int(newconfs[OPT_SPLIT_VSS])!=get_int(oldconfs[OPT_SPLIT_VSS]))
     721             :         {
     722             :                 logp("split_vss=%d (changed since last backup)\n",
     723           0 :                         get_int(newconfs[OPT_SPLIT_VSS]));
     724           0 :                 ret=1; goto end;
     725             :         }
     726           1 :         if(get_int(newconfs[OPT_STRIP_VSS])!=get_int(oldconfs[OPT_STRIP_VSS]))
     727             :         {
     728             :                 logp("strip_vss=%d (changed since last backup)\n",
     729           0 :                         get_int(newconfs[OPT_STRIP_VSS]));
     730           0 :                 ret=1; goto end;
     731             :         }
     732             :         ret=0;
     733             : end:
     734           1 :         if(ret==1) logp("All files will be treated as new\n");
     735           1 :         confs_free(&oldconfs);
     736           1 :         confs_free(&newconfs);
     737           1 :         return ret;
     738             : }
     739             : 
     740             : // Open the previous (current) manifest.
     741             : // If the split_vss setting changed between the previous backup and the new
     742             : // backup, do not open the previous manifest. This will have the effect of
     743             : // making the client back up everything fresh. Need to do this, otherwise
     744             : // toggling split_vss on and off will result in backups that do not work.
     745           5 : static int open_previous_manifest(struct manio **cmanio,
     746             :         struct sdirs *sdirs, const char *incexc, struct conf **cconfs)
     747             : {
     748             :         struct stat statp;
     749          10 :         if(!lstat(sdirs->cmanifest, &statp)
     750           1 :           && !vss_opts_changed(sdirs, cconfs, incexc)
     751           6 :           && !(*cmanio=manio_open(sdirs->cmanifest, "rb", PROTO_1)))
     752             :         {
     753           0 :                 logp("could not open old manifest %s\n", sdirs->cmanifest);
     754           0 :                 return -1;
     755             :         }
     756             :         return 0;
     757             : }
     758             : 
     759          10 : int backup_phase2_server_protocol1(struct async *as, struct sdirs *sdirs,
     760             :         const char *incexc, int resume, struct conf **cconfs)
     761             : {
     762          10 :         int ret=0;
     763          10 :         man_off_t *p1pos=NULL;
     764          10 :         struct manio *p1manio=NULL;
     765          10 :         struct dpth *dpth=NULL;
     766          10 :         char *deltmppath=NULL;
     767          10 :         char *last_requested=NULL;
     768          10 :         struct manio *chmanio=NULL; // changed data
     769          10 :         struct manio *ucmanio=NULL; // unchanged data
     770          10 :         struct manio *cmanio=NULL; // previous (current) manifest.
     771          10 :         struct sbuf *cb=NULL; // file list in current manifest
     772          10 :         struct sbuf *p1b=NULL; // file list from client
     773          10 :         struct sbuf *rb=NULL; // receiving file from client
     774          10 :         struct asfd *asfd=NULL;
     775          10 :         int breaking=0;
     776          10 :         int breakcount=0;
     777          10 :         struct cntr *cntr=NULL;
     778             : 
     779          10 :         if(!as)
     780             :         {
     781           1 :                 logp("async not provided to %s()\n", __func__);
     782           1 :                 goto error;
     783             :         }
     784           9 :         if(!sdirs)
     785             :         {
     786           2 :                 logp("sdirs not provided to %s()\n", __func__);
     787           2 :                 goto error;
     788             :         }
     789           7 :         if(!cconfs)
     790             :         {
     791           1 :                 logp("cconfs not provided to %s()\n", __func__);
     792           1 :                 goto error;
     793             :         }
     794           6 :         asfd=as->asfd;
     795           6 :         if(!asfd)
     796             :         {
     797           1 :                 logp("asfd not provided to %s()\n", __func__);
     798           1 :                 goto error;
     799             :         }
     800           5 :         cntr=get_cntr(cconfs);
     801             : 
     802          10 :         if(get_int(cconfs[OPT_BREAKPOINT])>=2000
     803           5 :           && get_int(cconfs[OPT_BREAKPOINT])<3000)
     804             :         {
     805           0 :                 breaking=get_int(cconfs[OPT_BREAKPOINT]);
     806           0 :                 breakcount=breaking-2000;
     807             :         }
     808             : 
     809           5 :         logp("Begin phase2 (receive file data)\n");
     810             : 
     811          10 :         if(!(dpth=dpth_alloc())
     812          10 :           || dpth_protocol1_init(dpth, sdirs->currentdata,
     813           5 :                 get_int(cconfs[OPT_MAX_STORAGE_SUBDIRS])))
     814             :                         goto error;
     815             : 
     816           5 :         if(open_previous_manifest(&cmanio, sdirs, incexc, cconfs))
     817             :                 goto error;
     818             : 
     819           5 :         if(get_int(cconfs[OPT_DIRECTORY_TREE]))
     820             :         {
     821             :                 // Need to make sure we do not try to create a path that is
     822             :                 // too long.
     823           5 :                 if(build_path_w(sdirs->treepath)) goto error;
     824           5 :                 mkdir(sdirs->treepath, 0777);
     825           5 :                 treepathlen=strlen(sdirs->treepath);
     826           5 :                 if(init_fs_max(sdirs->treepath))
     827             :                         goto error;
     828             :         }
     829             : 
     830           5 :         if(resume && !(p1pos=do_resume(sdirs, dpth, cconfs)))
     831             :                 goto error;
     832             : 
     833          10 :         if(!(p1manio=manio_open_phase1(sdirs->phase1data, "rb", PROTO_1))
     834           5 :           || (resume && manio_seek(p1manio, p1pos)))
     835             :                 goto error;
     836          10 :         if(!(cb=sbuf_alloc(PROTO_1))
     837           5 :           || !(p1b=sbuf_alloc(PROTO_1))
     838          10 :           || !(rb=sbuf_alloc(PROTO_1)))
     839             :                 goto error;
     840             : 
     841             :         // Unchanged and changed should now be truncated correctly, we just
     842             :         // have to open them for appending.
     843             :         // Data is not getting written to a compressed file.
     844             :         // This is important for recovery if the power goes.
     845          10 :         if(!(ucmanio=manio_open_phase2(sdirs->unchanged, "ab", PROTO_1))
     846           5 :           || !(chmanio=manio_open_phase2(sdirs->changed, "ab", PROTO_1)))
     847             :                 goto error;
     848             : 
     849             :         while(1)
     850             :         {
     851         921 :                 if(breaking && breakcount--==0)
     852           0 :                         return breakpoint(breaking, __func__);
     853             : 
     854         921 :                 if(write_status(CNTR_STATUS_BACKUP,
     855         921 :                         rb->path.buf?rb->path.buf:p1b->path.buf, cntr))
     856             :                                 goto error;
     857         921 :                 if(last_requested
     858         921 :                   || !p1manio
     859          16 :                   || asfd->writebuflen)
     860             :                 {
     861         905 :                         switch(do_stuff_to_receive(asfd, sdirs,
     862         905 :                                 cconfs, rb, chmanio, dpth, &last_requested))
     863             :                         {
     864             :                                 case 0: break;
     865             :                                 case 1: goto end; // Finished ok.
     866             :                                 case -1: goto error;
     867             :                         }
     868             :                 }
     869             : 
     870         916 :                 switch(do_stuff_to_send(asfd, p1b, &last_requested))
     871             :                 {
     872             :                         case 0: break;
     873             :                         case 1: continue;
     874             :                         case -1: goto error;
     875             :                 }
     876             : 
     877         914 :                 if(!p1manio) continue;
     878             : 
     879         135 :                 sbuf_free_content(p1b);
     880             : 
     881         135 :                 switch(manio_read(p1manio, p1b))
     882             :                 {
     883             :                         case 0: break;
     884           5 :                         case 1: manio_close(&p1manio);
     885           5 :                                 if(asfd->write_str(asfd,
     886           5 :                                   CMD_GEN, "backupphase2end")) goto error;
     887             :                                 break;
     888             :                         case -1: goto error;
     889             :                 }
     890             : 
     891         135 :                 if(!cmanio)
     892             :                 {
     893             :                         // No old manifest, need to ask for a new file.
     894         124 :                         if(process_new(sdirs, cconfs, p1b, ucmanio))
     895             :                                 goto error;
     896             :                         continue;
     897             :                 }
     898             : 
     899             :                 // Have an old manifest, look for it there.
     900             : 
     901             :                 // Might already have it, or be ahead in the old
     902             :                 // manifest.
     903          11 :                 if(cb->path.buf) switch(maybe_process_file(asfd,
     904           0 :                         sdirs, cb, p1b, ucmanio, cconfs))
     905             :                 {
     906             :                         case 0: break;
     907             :                         case 1: continue;
     908             :                         case -1: goto error;
     909             :                 }
     910             : 
     911          12 :                 while(cmanio)
     912             :                 {
     913          11 :                         sbuf_free_content(cb);
     914          11 :                         switch(manio_read(cmanio, cb))
     915             :                         {
     916             :                                 case 0: break;
     917           1 :                                 case 1: manio_close(&cmanio);
     918           1 :                                         if(process_new(sdirs, cconfs, p1b,
     919           1 :                                                 ucmanio)) goto error;
     920             :                                         continue;
     921             :                                 case -1: goto error;
     922             :                         }
     923          10 :                         switch(maybe_process_file(asfd, sdirs,
     924          10 :                                 cb, p1b, ucmanio, cconfs))
     925             :                         {
     926             :                                 case 0: continue;
     927             :                                 case 1: break;
     928             :                                 case -1: goto error;
     929             :                         }
     930             :                         break;
     931             :                 }
     932             :         }
     933             : 
     934             : error:
     935             :         ret=-1;
     936             : end:
     937          10 :         if(manio_close(&chmanio))
     938             :         {
     939           0 :                 logp("error closing %s in %s\n", sdirs->changed, __func__);
     940           0 :                 ret=-1;
     941             :         }
     942          10 :         if(manio_close(&ucmanio))
     943             :         {
     944           0 :                 logp("error closing %s in %s\n", sdirs->unchanged, __func__);
     945           0 :                 ret=-1;
     946             :         }
     947          10 :         free_w(&deltmppath);
     948          10 :         free_w(&last_requested);
     949          10 :         sbuf_free(&cb);
     950          10 :         sbuf_free(&p1b);
     951          10 :         sbuf_free(&rb);
     952          10 :         manio_close(&p1manio);
     953          10 :         manio_close(&cmanio);
     954          10 :         dpth_free(&dpth);
     955          10 :         man_off_t_free(&p1pos);
     956          10 :         if(!ret && sdirs)
     957           5 :                 unlink(sdirs->phase1data);
     958             : 
     959          10 :         logp("End phase2 (receive file data)\n");
     960             : 
     961          10 :         return ret;
     962             : }

Generated by: LCOV version 1.10