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

Generated by: LCOV version 1.10