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: 0 480 0.0 %
Date: 2015-11-30 Functions: 0 23 0.0 %

          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           0 : static int path_too_long(struct iobuf *path, struct conf **cconfs)
      31             : {
      32             :         static const char *cp;
      33           0 :         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           0 :         if((cp=strrchr(path->buf, '/'))) cp++;
      44           0 :         else cp=path->buf;
      45           0 :         if(strlen(cp)>fs_name_max) return path_length_warn(path, cconfs);
      46           0 :         return 0;
      47             : }
      48             : 
      49           0 : 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           0 :         if(S_ISDIR(sb->statp.st_mode)) return 0;
      54             : 
      55           0 :         if(sb->path.cmd!=CMD_FILE
      56           0 :           && sb->path.cmd!=CMD_ENC_FILE
      57           0 :           && sb->path.cmd!=CMD_EFS_FILE)
      58           0 :                 return 0;
      59             : 
      60           0 :         return !path_too_long(&sb->path, cconfs);
      61             : }
      62             : 
      63           0 : 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           0 :         char *tmp=NULL;
      68           0 :         char *rpath=NULL;
      69           0 :         if(get_int(cconfs[OPT_DIRECTORY_TREE]))
      70           0 :                 *istreedata=treedata(sb, cconfs);
      71             : 
      72           0 :         if(*istreedata)
      73             :         {
      74             :                 // We want to place this file in a directory structure like
      75             :                 // the directory structure on the original client.
      76           0 :                 if(!(tmp=prepend_s(TREE_DIR, sb->path.buf)))
      77             :                 {
      78           0 :                         log_and_send_oom(asfd, __func__);
      79           0 :                         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           0 :         iobuf_from_str(&sb->protocol1->datapth, CMD_DATAPTH, tmp);
      88           0 :         if(build_path(sdirs->datadirtmp,
      89           0 :                 sb->protocol1->datapth.buf, &rpath, sdirs->datadirtmp))
      90             :         {
      91           0 :                 log_and_send(asfd, "build path failed");
      92           0 :                 return NULL;
      93             :         }
      94           0 :         return rpath;
      95             : }
      96             : 
      97           0 : 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           0 :         int ret=-1;
     102           0 :         char *rpath=NULL;
     103           0 :         int istreedata=0;
     104             : 
     105             : //logp("start to receive: %s\n", sb->path.buf);
     106             : 
     107           0 :         if(!(rpath=set_new_datapth(asfd,
     108           0 :                 sdirs, cconfs, sb, dpth, &istreedata)))
     109           0 :                         goto end;
     110             :         
     111           0 :         if(!(sb->protocol1->fzp=fzp_open(rpath, "wb")))
     112             :         {
     113           0 :                 log_and_send(asfd, "make file failed");
     114           0 :                 goto end;
     115             :         }
     116           0 :         if(!istreedata) dpth_incr(dpth);
     117           0 :         ret=0;
     118             : end:
     119           0 :         free_w(&rpath);
     120           0 :         return ret;
     121             : }
     122             : 
     123           0 : 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           0 :         int ret=-1;
     129           0 :         size_t blocklen=0;
     130           0 :         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           0 :         iobuf_move(&p1b->protocol1->datapth, &cb->protocol1->datapth);
     136             : 
     137           0 :         if(!(curpath=prepend_s(adir, p1b->protocol1->datapth.buf)))
     138             :         {
     139           0 :                 log_out_of_memory(__func__);
     140           0 :                 goto end;
     141             :         }
     142           0 :         if(dpth_protocol1_is_compressed(cb->compression, curpath))
     143           0 :                 p1b->protocol1->sigfzp=fzp_gzopen(curpath, "rb");
     144             :         else
     145           0 :                 p1b->protocol1->sigfzp=fzp_open(curpath, "rb");
     146           0 :         if(!p1b->protocol1->sigfzp)
     147             :         {
     148           0 :                 logp("could not open %s: %s\n", curpath, strerror(errno));
     149           0 :                 goto end;
     150             :         }
     151             : 
     152           0 :         blocklen=get_librsync_block_len(cb->endfile.buf);
     153           0 :         if(!(p1b->protocol1->sigjob=
     154             : #ifdef RS_DEFAULT_STRONG_LEN
     155           0 :                 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           0 :         ))
     163             :         {
     164           0 :                 logp("could not start signature job.\n");
     165           0 :                 goto end;
     166             :         }
     167             :         //logp("sig begin: %s\n", p1b->protocol1->datapth.buf);
     168           0 :         if(!(p1b->protocol1->infb=rs_filebuf_new(NULL,
     169             :                 p1b->protocol1->sigfzp,
     170           0 :                 NULL, blocklen, -1)))
     171             :         {
     172           0 :                 logp("could not rs_filebuf_new for infb.\n");
     173           0 :                 goto end;
     174             :         }
     175           0 :         if(!(p1b->protocol1->outfb=rs_filebuf_new(NULL, NULL,
     176           0 :                 asfd, ASYNC_BUF_LEN, -1)))
     177             :         {
     178           0 :                 logp("could not rs_filebuf_new for in_outfb.\n");
     179           0 :                 goto end;
     180             :         }
     181             : 
     182             :         // Flag the things that need to be sent (to the client)
     183           0 :         p1b->flags |= SBUF_SEND_DATAPTH;
     184           0 :         p1b->flags |= SBUF_SEND_STAT;
     185           0 :         p1b->flags |= SBUF_SEND_PATH;
     186             : 
     187             :         //logp("sending sig for %s\n", p1b->path);
     188             :         //logp("(%s)\n", p1b->datapth);
     189             : 
     190           0 :         ret=0;
     191             : end:
     192           0 :         free_w(&curpath);
     193           0 :         return ret;
     194             : }
     195             : 
     196           0 : 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           0 :         if(manio_write_sbuf(ucmanio, p1b))
     205           0 :                 return -1;
     206           0 :         cntr_add(get_cntr(cconfs), p1b->path.cmd, 0);
     207           0 :         sbuf_free_content(p1b);
     208           0 :         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           0 :                 return -1;
     217           0 :         cntr_add_changed(get_cntr(cconfs), cmd);
     218           0 :         sbuf_free_content(p1b);
     219           0 :         return 0;
     220             : }
     221             : 
     222           0 : static int process_new(struct sdirs *sdirs, struct conf **cconfs,
     223             :         struct sbuf *p1b, struct manio *ucmanio)
     224             : {
     225           0 :         if(!p1b->path.buf) return 0;
     226           0 :         if(sbuf_is_filedata(p1b)
     227           0 :           || 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           0 :                 p1b->flags |= SBUF_SEND_STAT;
     232           0 :                 p1b->flags |= SBUF_SEND_PATH;
     233           0 :                 return 0;
     234             :         }
     235           0 :         return new_non_file(p1b, ucmanio, cconfs);
     236             : }
     237             : 
     238           0 : 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           0 :         iobuf_move(&p1b->protocol1->datapth, &cb->protocol1->datapth);
     244           0 :         iobuf_move(&p1b->endfile, &cb->endfile);
     245           0 :         p1b->compression=cb->compression;
     246           0 :         p1b->winattr=cb->winattr;
     247           0 :         if(attribs_encode(p1b))
     248           0 :                 return -1;
     249           0 :         if(manio_write_sbuf(ucmanio, p1b))
     250           0 :                 return -1;
     251           0 :         cntr_add_same(get_cntr(cconfs), p1b->path.cmd);
     252           0 :         if(p1b->endfile.buf) cntr_add_bytes(get_cntr(cconfs),
     253           0 :                  strtoull(p1b->endfile.buf, NULL, 10));
     254           0 :         sbuf_free_content(cb);
     255           0 :         return 1;
     256             : }
     257             : 
     258           0 : static int process_new_file(struct sdirs *sdirs, struct conf **cconfs,
     259             :         struct sbuf *cb, struct sbuf *p1b, struct manio *ucmanio)
     260             : {
     261           0 :         if(process_new(sdirs, cconfs, p1b, ucmanio))
     262           0 :                 return -1;
     263           0 :         sbuf_free_content(cb);
     264           0 :         return 1;
     265             : }
     266             : 
     267           0 : 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           0 :         int oldcompressed=0;
     272           0 :         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           0 :         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           0 :         if(cb->statp.st_mtime==p1b->statp.st_mtime
     282           0 :           && 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           0 :                 return process_unchanged_file(p1b, cb, ucmanio, cconfs);
     288             :         }
     289             : 
     290           0 :         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           0 :         if(!get_int(cconfs[OPT_LIBRSYNC])
     325             :         // FIX THIS: make unencrypted metadata use the librsync
     326           0 :           || cb->path.cmd==CMD_METADATA
     327           0 :           || p1b->path.cmd==CMD_METADATA
     328           0 :           || sbuf_is_encrypted(cb)
     329           0 :           || sbuf_is_encrypted(p1b)
     330           0 :           || sbuf_is_vssdata(cb)
     331           0 :           || sbuf_is_vssdata(p1b))
     332           0 :                 return process_new_file(sdirs, cconfs, cb, p1b, ucmanio);
     333             : 
     334             :         // Get new files if they have switched between compression on or off.
     335           0 :         if(cb->protocol1->datapth.buf
     336           0 :           && dpth_protocol1_is_compressed(cb->compression,
     337           0 :             cb->protocol1->datapth.buf))
     338           0 :                 oldcompressed=1;
     339           0 :         if( ( oldcompressed && !compression)
     340           0 :          || (!oldcompressed &&  compression))
     341           0 :                 return process_new_file(sdirs, cconfs, cb, p1b, ucmanio);
     342             : 
     343             :         // Otherwise, do the delta stuff (if possible).
     344           0 :         if(sbuf_is_filedata(p1b)
     345           0 :           || sbuf_is_vssdata(p1b))
     346             :         {
     347           0 :                 if(process_changed_file(asfd, sdirs, cconfs, cb, p1b,
     348           0 :                         sdirs->currentdata)) return -1;
     349             :         }
     350             :         else
     351             :         {
     352           0 :                 if(changed_non_file(p1b, ucmanio, p1b->path.cmd, cconfs))
     353           0 :                         return -1;
     354             :         }
     355           0 :         sbuf_free_content(cb);
     356           0 :         return 1;
     357             : }
     358             : 
     359             : // return 1 to say that a file was processed
     360           0 : 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           0 :         switch(sbuf_pathcmp(cb, p1b))
     365             :         {
     366             :                 case 0:
     367             :                         return maybe_do_delta_stuff(asfd, sdirs, cb, p1b,
     368           0 :                                 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           0 :                                 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           0 : static int do_stuff_to_send(struct asfd *asfd,
     391             :         struct sbuf *p1b, char **last_requested)
     392             : {
     393             :         static struct iobuf wbuf;
     394           0 :         if(p1b->flags & SBUF_SEND_DATAPTH)
     395             :         {
     396           0 :                 iobuf_copy(&wbuf, &p1b->protocol1->datapth);
     397           0 :                 switch(asfd->append_all_to_write_buffer(asfd, &wbuf))
     398             :                 {
     399           0 :                         case APPEND_OK: break;
     400           0 :                         case APPEND_BLOCKED: return 1;
     401           0 :                         default: return -1;
     402             :                 }
     403           0 :                 p1b->flags &= ~SBUF_SEND_DATAPTH;
     404             :         }
     405           0 :         if(p1b->flags & SBUF_SEND_STAT)
     406             :         {
     407           0 :                 iobuf_copy(&wbuf, &p1b->attr);
     408           0 :                 switch(asfd->append_all_to_write_buffer(asfd, &wbuf))
     409             :                 {
     410           0 :                         case APPEND_OK: break;
     411           0 :                         case APPEND_BLOCKED: return 1;
     412           0 :                         default: return -1;
     413             :                 }
     414           0 :                 p1b->flags &= ~SBUF_SEND_STAT;
     415             :         }
     416           0 :         if(p1b->flags & SBUF_SEND_PATH)
     417             :         {
     418           0 :                 iobuf_copy(&wbuf, &p1b->path);
     419           0 :                 switch(asfd->append_all_to_write_buffer(asfd, &wbuf))
     420             :                 {
     421           0 :                         case APPEND_OK: break;
     422           0 :                         case APPEND_BLOCKED: return 1;
     423           0 :                         default: return -1;
     424             :                 }
     425           0 :                 p1b->flags &= ~SBUF_SEND_PATH;
     426           0 :                 if(*last_requested) free(*last_requested);
     427           0 :                 if(!(*last_requested=strdup_w(p1b->path.buf, __func__)))
     428           0 :                         return -1;
     429             :         }
     430           0 :         if(p1b->protocol1->sigjob && !(p1b->flags & SBUF_SEND_ENDOFSIG))
     431             :         {
     432             :                 rs_result sigresult;
     433             : 
     434           0 :                 switch((sigresult=rs_async(p1b->protocol1->sigjob,
     435             :                         &(p1b->protocol1->rsbuf),
     436           0 :                         p1b->protocol1->infb, p1b->protocol1->outfb)))
     437             :                 {
     438             :                         case RS_DONE:
     439           0 :                                 p1b->flags |= SBUF_SEND_ENDOFSIG;
     440           0 :                                 break;
     441             :                         case RS_BLOCKED:
     442             :                         case RS_RUNNING:
     443             :                                 // keep going round the loop.
     444           0 :                                 return 1;
     445             :                         default:
     446           0 :                                 logp("error in rs_async: %d\n", sigresult);
     447           0 :                                 return -1;
     448             :                 }
     449             :         }
     450           0 :         if(p1b->flags & SBUF_SEND_ENDOFSIG)
     451             :         {
     452           0 :                 iobuf_from_str(&wbuf, CMD_END_FILE, (char *)"endfile");
     453           0 :                 switch(asfd->append_all_to_write_buffer(asfd, &wbuf))
     454             :                 {
     455           0 :                         case APPEND_OK: break;
     456           0 :                         case APPEND_BLOCKED: return 1;
     457           0 :                         default: return -1;
     458             :                 }
     459           0 :                 p1b->flags &= ~SBUF_SEND_ENDOFSIG;
     460             :         }
     461           0 :         return 0;
     462             : }
     463             : 
     464           0 : static int start_to_receive_delta(struct sdirs *sdirs, struct conf **cconfs,
     465             :         struct sbuf *rb)
     466             : {
     467           0 :         if(rb->compression)
     468             :         {
     469           0 :                 if(!(rb->protocol1->fzp=fzp_gzopen(sdirs->deltmppath,
     470           0 :                         comp_level(rb->compression))))
     471           0 :                                 return -1;
     472             :         }
     473             :         else
     474             :         {
     475           0 :                 if(!(rb->protocol1->fzp=fzp_open(sdirs->deltmppath, "wb")))
     476           0 :                         return -1;
     477             :         }
     478           0 :         rb->flags |= SBUF_RECV_DELTA;
     479             : 
     480           0 :         return 0;
     481             : }
     482             : 
     483           0 : static int finish_delta(struct sdirs *sdirs, struct sbuf *rb)
     484             : {
     485           0 :         int ret=0;
     486           0 :         char *deltmp=NULL;
     487           0 :         char *delpath=NULL;
     488           0 :         if(!(deltmp=prepend_s("deltas.forward", rb->protocol1->datapth.buf))
     489           0 :           || !(delpath=prepend_s(sdirs->working, deltmp))
     490           0 :           || mkpath(&delpath, sdirs->working)
     491             :         // Rename race condition is of no consequence here, as delpath will
     492             :         // just get recreated.
     493           0 :           || do_rename(sdirs->deltmppath, delpath))
     494           0 :                 ret=-1;
     495           0 :         if(delpath) free(delpath);
     496           0 :         if(deltmp) free(deltmp);
     497           0 :         return ret;
     498             : }
     499             : 
     500           0 : static int deal_with_receive_end_file(struct asfd *asfd, struct sdirs *sdirs,
     501             :         struct sbuf *rb, struct manio *chmanio, struct conf **cconfs,
     502             :         char **last_requested)
     503             : {
     504           0 :         int ret=-1;
     505             :         static char *cp=NULL;
     506             :         static struct iobuf *rbuf;
     507           0 :         struct cntr *cntr=get_cntr(cconfs);
     508           0 :         rbuf=asfd->rbuf;
     509             :         // Finished the file.
     510             :         // Write it to the phase2 file, and free the buffers.
     511             : 
     512           0 :         if(fzp_close(&(rb->protocol1->fzp)))
     513             :         {
     514           0 :                 logp("error closing delta for %s in receive\n", rb->path);
     515           0 :                 goto end;
     516             :         }
     517           0 :         iobuf_move(&rb->endfile, rbuf);
     518           0 :         if(rb->flags & SBUF_RECV_DELTA && finish_delta(sdirs, rb))
     519           0 :                 goto end;
     520             : 
     521           0 :         if(manio_write_sbuf(chmanio, rb))
     522           0 :                 goto end;
     523             : 
     524           0 :         if(rb->flags & SBUF_RECV_DELTA)
     525           0 :                 cntr_add_changed(cntr, rb->path.cmd);
     526             :         else
     527           0 :                 cntr_add(cntr, rb->path.cmd, 0);
     528             : 
     529           0 :         if(*last_requested && !strcmp(rb->path.buf, *last_requested))
     530             :         {
     531           0 :                 free(*last_requested);
     532           0 :                 *last_requested=NULL;
     533             :         }
     534             : 
     535           0 :         cp=strchr(rb->endfile.buf, ':');
     536           0 :         if(rb->endfile.buf)
     537           0 :                 cntr_add_bytes(cntr, strtoull(rb->endfile.buf, NULL, 10));
     538           0 :         if(cp)
     539             :         {
     540             :                 // checksum stuff goes here
     541             :         }
     542             : 
     543           0 :         ret=0;
     544             : end:
     545           0 :         sbuf_free_content(rb);
     546           0 :         return ret;
     547             : }
     548             : 
     549           0 : static int deal_with_receive_append(struct asfd *asfd, struct sbuf *rb,
     550             :         struct conf **cconfs)
     551             : {
     552           0 :         int app=0;
     553             :         static struct iobuf *rbuf;
     554           0 :         rbuf=asfd->rbuf;
     555             :         //logp("rbuf->len: %d\n", rbuf->len);
     556             : 
     557           0 :         cntr_add_recvbytes(get_cntr(cconfs), rbuf->len);
     558           0 :         if(rb->protocol1->fzp)
     559           0 :                 app=fzp_write(rb->protocol1->fzp, rbuf->buf, rbuf->len);
     560             : 
     561           0 :         if(app>0) return 0;
     562           0 :         logp("error when appending: %d\n", app);
     563           0 :         asfd->write_str(asfd, CMD_ERROR, "write failed");
     564           0 :         return -1;
     565             : }
     566             : 
     567           0 : static int deal_with_filedata(struct asfd *asfd,
     568             :         struct sdirs *sdirs, struct sbuf *rb,
     569             :         struct iobuf *rbuf, struct dpth *dpth, struct conf **cconfs)
     570             : {
     571           0 :         iobuf_move(&rb->path, rbuf);
     572             : 
     573           0 :         if(rb->protocol1->datapth.buf)
     574             :         {
     575             :                 // Receiving a delta.
     576           0 :                 if(start_to_receive_delta(sdirs, cconfs, rb))
     577             :                 {
     578           0 :                         logp("error in start_to_receive_delta\n");
     579           0 :                         return -1;
     580             :                 }
     581           0 :                 return 0;
     582             :         }
     583             : 
     584             :         // Receiving a whole new file.
     585           0 :         if(start_to_receive_new_file(asfd, sdirs, cconfs, rb, dpth))
     586             :         {
     587           0 :                 logp("error in start_to_receive_new_file\n");
     588           0 :                 return -1;
     589             :         }
     590           0 :         return 0;
     591             : }
     592             : 
     593             : // returns 1 for finished ok.
     594           0 : static int do_stuff_to_receive(struct asfd *asfd,
     595             :         struct sdirs *sdirs, struct conf **cconfs,
     596             :         struct sbuf *rb, struct manio *chmanio,
     597             :         struct dpth *dpth, char **last_requested)
     598             : {
     599           0 :         struct iobuf *rbuf=asfd->rbuf;
     600             : 
     601           0 :         iobuf_free_content(rbuf);
     602             :         // This also attempts to write anything in the write buffer.
     603           0 :         if(asfd->as->read_write(asfd->as))
     604             :         {
     605           0 :                 logp("error in %s\n", __func__);
     606           0 :                 return -1;
     607             :         }
     608             : 
     609           0 :         if(!rbuf->buf) return 0;
     610             : 
     611           0 :         if(rbuf->cmd==CMD_MESSAGE
     612           0 :           || rbuf->cmd==CMD_WARNING)
     613             :         {
     614           0 :                 struct cntr *cntr=NULL;
     615           0 :                 if(cconfs) cntr=get_cntr(cconfs);
     616           0 :                 log_recvd(rbuf, cntr, 0);
     617           0 :                 return 0;
     618             :         }
     619             : 
     620           0 :         if(rb->protocol1->fzp)
     621             :         {
     622             :                 // Currently writing a file (or meta data)
     623           0 :                 switch(rbuf->cmd)
     624             :                 {
     625             :                         case CMD_APPEND:
     626           0 :                                 if(deal_with_receive_append(asfd, rb, cconfs))
     627           0 :                                         goto error;
     628           0 :                                 return 0;
     629             :                         case CMD_END_FILE:
     630           0 :                                 if(deal_with_receive_end_file(asfd, sdirs, rb,
     631           0 :                                         chmanio, cconfs, last_requested))
     632           0 :                                                 goto error;
     633           0 :                                 return 0;
     634             :                         default:
     635           0 :                                 iobuf_log_unexpected(rbuf, __func__);
     636           0 :                                 goto error;
     637             :                 }
     638             :         }
     639             : 
     640             :         // Otherwise, expecting to be told of a file to save.
     641           0 :         switch(rbuf->cmd)
     642             :         {
     643             :                 case CMD_DATAPTH:
     644           0 :                         iobuf_move(&rb->protocol1->datapth, rbuf);
     645           0 :                         return 0;
     646             :                 case CMD_ATTRIBS:
     647           0 :                         iobuf_move(&rb->attr, rbuf);
     648           0 :                         attribs_decode(rb);
     649           0 :                         return 0;
     650             :                 case CMD_GEN:
     651           0 :                         if(!strcmp(rbuf->buf, "okbackupphase2end"))
     652           0 :                                 goto end_phase2;
     653           0 :                         iobuf_log_unexpected(rbuf, __func__);
     654           0 :                         goto error;
     655             :                 case CMD_INTERRUPT:
     656             :                         // Interrupt - forget about the last requested
     657             :                         // file if it matches. Otherwise, we can get
     658             :                         // stuck on the select in the async stuff,
     659             :                         // waiting for something that will never arrive.
     660           0 :                         if(*last_requested
     661           0 :                           && !strcmp(rbuf->buf, *last_requested))
     662           0 :                                 free_w(last_requested);
     663           0 :                         return 0;
     664             :                 default:
     665           0 :                         break;
     666             :         }
     667           0 :         if(iobuf_is_filedata(rbuf)
     668           0 :           || iobuf_is_vssdata(rbuf))
     669             :         {
     670           0 :                 if(deal_with_filedata(asfd, sdirs, rb, rbuf, dpth, cconfs))
     671           0 :                         goto error;
     672           0 :                 return 0;
     673             :         }
     674           0 :         iobuf_log_unexpected(rbuf, __func__);
     675             : 
     676             : error:
     677           0 :         return -1;
     678             : end_phase2:
     679           0 :         return 1;
     680             : }
     681             : 
     682           0 : static int vss_opts_changed(struct sdirs *sdirs, struct conf **cconfs,
     683             :         const char *incexc)
     684             : {
     685           0 :         int ret=-1;
     686             :         struct conf **oldconfs;
     687             :         struct conf **newconfs;
     688           0 :         if(!(oldconfs=confs_alloc())
     689           0 :           || !(newconfs=confs_alloc()))
     690           0 :                 goto end;
     691           0 :         confs_init(oldconfs);
     692           0 :         confs_init(newconfs);
     693             : 
     694             :         // Figure out the old config, which is in the incexc file left
     695             :         // in the current backup directory on the server.
     696           0 :         if(conf_parse_incexcs_path(oldconfs, sdirs->cincexc))
     697             :         {
     698             :                 // Assume that the file did not exist, and therefore
     699             :                 // the old split_vss setting is 0.
     700           0 :                 set_int(oldconfs[OPT_SPLIT_VSS], 0);
     701           0 :                 set_int(oldconfs[OPT_STRIP_VSS], 0);
     702             :         }
     703             : 
     704             :         // Figure out the new config, which is either in the incexc file from
     705             :         // the client, or in the cconf on the server.
     706           0 :         if(incexc)
     707             :         {
     708           0 :                 if(conf_parse_incexcs_buf(newconfs, incexc))
     709             :                 {
     710             :                         // Should probably not got here.
     711           0 :                         set_int(newconfs[OPT_SPLIT_VSS], 0);
     712           0 :                         set_int(newconfs[OPT_STRIP_VSS], 0);
     713             :                 }
     714             :         }
     715             :         else
     716             :         {
     717           0 :                 set_int(newconfs[OPT_SPLIT_VSS],
     718           0 :                         get_int(cconfs[OPT_SPLIT_VSS]));
     719           0 :                 set_int(newconfs[OPT_STRIP_VSS],
     720           0 :                         get_int(cconfs[OPT_STRIP_VSS]));
     721             :         }
     722             : 
     723           0 :         if(get_int(newconfs[OPT_SPLIT_VSS])!=get_int(oldconfs[OPT_SPLIT_VSS]))
     724             :         {
     725             :                 logp("split_vss=%d (changed since last backup)\n",
     726           0 :                         get_int(newconfs[OPT_SPLIT_VSS]));
     727           0 :                 ret=1; goto end;
     728             :         }
     729           0 :         if(get_int(newconfs[OPT_STRIP_VSS])!=get_int(oldconfs[OPT_STRIP_VSS]))
     730             :         {
     731             :                 logp("strip_vss=%d (changed since last backup)\n",
     732           0 :                         get_int(newconfs[OPT_STRIP_VSS]));
     733           0 :                 ret=1; goto end;
     734             :         }
     735           0 :         ret=0;
     736             : end:
     737           0 :         if(ret==1) logp("All files will be treated as new\n");
     738           0 :         confs_free(&oldconfs);
     739           0 :         confs_free(&newconfs);
     740           0 :         return ret;
     741             : }
     742             : 
     743             : // Open the previous (current) manifest.
     744             : // If the split_vss setting changed between the previous backup and the new
     745             : // backup, do not open the previous manifest. This will have the effect of
     746             : // making the client back up everything fresh. Need to do this, otherwise
     747             : // toggling split_vss on and off will result in backups that do not work.
     748           0 : static int open_previous_manifest(struct manio **cmanio,
     749             :         struct sdirs *sdirs, const char *incexc, struct conf **cconfs)
     750             : {
     751             :         struct stat statp;
     752           0 :         if(!lstat(sdirs->cmanifest, &statp)
     753           0 :           && !vss_opts_changed(sdirs, cconfs, incexc)
     754           0 :           && !(*cmanio=manio_open(sdirs->cmanifest,
     755           0 :                 "rb", get_protocol(cconfs))))
     756             :         {
     757           0 :                 logp("could not open old manifest %s\n", sdirs->cmanifest);
     758           0 :                 return -1;
     759             :         }
     760           0 :         return 0;
     761             : }
     762             : 
     763             : 
     764           0 : int backup_phase2_server_protocol1(struct async *as, struct sdirs *sdirs,
     765             :         const char *incexc, int resume, struct conf **cconfs)
     766             : {
     767           0 :         int ret=0;
     768           0 :         man_off_t *p1pos=NULL;
     769           0 :         struct manio *p1manio=NULL;
     770           0 :         struct dpth *dpth=NULL;
     771           0 :         char *deltmppath=NULL;
     772           0 :         char *last_requested=NULL;
     773           0 :         struct manio *chmanio=NULL; // changed data
     774           0 :         struct manio *ucmanio=NULL; // unchanged data
     775           0 :         struct manio *cmanio=NULL; // previous (current) manifest.
     776           0 :         struct sbuf *cb=NULL; // file list in current manifest
     777           0 :         struct sbuf *p1b=NULL; // file list from client
     778           0 :         struct sbuf *rb=NULL; // receiving file from client
     779           0 :         struct asfd *asfd=as->asfd;
     780           0 :         int breaking=0;
     781           0 :         int breakcount=0;
     782           0 :         struct cntr *cntr=get_cntr(cconfs);
     783           0 :         if(get_int(cconfs[OPT_BREAKPOINT])>=2000
     784           0 :           && get_int(cconfs[OPT_BREAKPOINT])<3000)
     785             :         {
     786           0 :                 breaking=get_int(cconfs[OPT_BREAKPOINT]);
     787           0 :                 breakcount=breaking-2000;
     788             :         }
     789             : 
     790           0 :         logp("Begin phase2 (receive file data)\n");
     791             : 
     792           0 :         if(!(dpth=dpth_alloc())
     793           0 :           || dpth_protocol1_init(dpth, sdirs->currentdata,
     794           0 :                 get_int(cconfs[OPT_MAX_STORAGE_SUBDIRS])))
     795           0 :                         goto error;
     796             : 
     797           0 :         if(open_previous_manifest(&cmanio, sdirs, incexc, cconfs))
     798           0 :                 goto error;
     799             : 
     800           0 :         if(get_int(cconfs[OPT_DIRECTORY_TREE]))
     801             :         {
     802             :                 // Need to make sure we do not try to create a path that is
     803             :                 // too long.
     804           0 :                 if(build_path_w(sdirs->treepath)) goto error;
     805           0 :                 mkdir(sdirs->treepath, 0777);
     806           0 :                 treepathlen=strlen(sdirs->treepath);
     807           0 :                 if(init_fs_max(sdirs->treepath))
     808           0 :                         goto error;
     809             :         }
     810             : 
     811           0 :         if(resume && !(p1pos=do_resume(sdirs, dpth, cconfs)))
     812           0 :                 goto error;
     813             : 
     814           0 :         if(!(p1manio=manio_open_phase1(sdirs->phase1data, "rb", PROTO_1))
     815           0 :           || (resume && manio_seek(p1manio, p1pos)))
     816           0 :                 goto error;
     817           0 :         if(!(cb=sbuf_alloc(PROTO_1))
     818           0 :           || !(p1b=sbuf_alloc(PROTO_1))
     819           0 :           || !(rb=sbuf_alloc(PROTO_1)))
     820           0 :                 goto error;
     821             : 
     822             :         // Unchanged and changed should now be truncated correctly, we just
     823             :         // have to open them for appending.
     824             :         // Data is not getting written to a compressed file.
     825             :         // This is important for recovery if the power goes.
     826           0 :         if(!(ucmanio=manio_open_phase2(sdirs->unchanged, "ab", PROTO_1))
     827           0 :           || !(chmanio=manio_open_phase2(sdirs->changed, "ab", PROTO_1)))
     828           0 :                 goto error;
     829             : 
     830             :         while(1)
     831             :         {
     832           0 :                 if(breaking && breakcount--==0)
     833           0 :                         return breakpoint(breaking, __func__);
     834             : 
     835           0 :                 if(write_status(CNTR_STATUS_BACKUP,
     836           0 :                         rb->path.buf?rb->path.buf:p1b->path.buf, cntr))
     837           0 :                                 goto error;
     838           0 :                 if(last_requested
     839           0 :                   || !p1manio
     840           0 :                   || asfd->writebuflen)
     841             :                 {
     842           0 :                         switch(do_stuff_to_receive(asfd, sdirs,
     843           0 :                                 cconfs, rb, chmanio, dpth, &last_requested))
     844             :                         {
     845           0 :                                 case 0: break;
     846           0 :                                 case 1: goto end; // Finished ok.
     847           0 :                                 case -1: goto error;
     848             :                         }
     849             :                 }
     850             : 
     851           0 :                 switch(do_stuff_to_send(asfd, p1b, &last_requested))
     852             :                 {
     853           0 :                         case 0: break;
     854           0 :                         case 1: continue;
     855           0 :                         case -1: goto error;
     856             :                 }
     857             : 
     858           0 :                 if(!p1manio) continue;
     859             : 
     860           0 :                 sbuf_free_content(p1b);
     861             : 
     862           0 :                 switch(manio_read(p1manio, p1b))
     863             :                 {
     864           0 :                         case 0: break;
     865           0 :                         case 1: manio_close(&p1manio);
     866           0 :                                 if(asfd->write_str(asfd,
     867           0 :                                   CMD_GEN, "backupphase2end")) goto error;
     868           0 :                                 break;
     869           0 :                         case -1: goto error;
     870             :                 }
     871             : 
     872           0 :                 if(!cmanio)
     873             :                 {
     874             :                         // No old manifest, need to ask for a new file.
     875           0 :                         if(process_new(sdirs, cconfs, p1b, ucmanio))
     876           0 :                                 goto error;
     877           0 :                         continue;
     878             :                 }
     879             : 
     880             :                 // Have an old manifest, look for it there.
     881             : 
     882             :                 // Might already have it, or be ahead in the old
     883             :                 // manifest.
     884           0 :                 if(cb->path.buf) switch(maybe_process_file(asfd,
     885           0 :                         sdirs, cb, p1b, ucmanio, cconfs))
     886             :                 {
     887           0 :                         case 0: break;
     888           0 :                         case 1: continue;
     889           0 :                         case -1: goto error;
     890             :                 }
     891             : 
     892           0 :                 while(cmanio)
     893             :                 {
     894           0 :                         sbuf_free_content(cb);
     895           0 :                         switch(manio_read(cmanio, cb))
     896             :                         {
     897           0 :                                 case 0: break;
     898           0 :                                 case 1: manio_close(&cmanio);
     899           0 :                                         if(process_new(sdirs, cconfs, p1b,
     900           0 :                                                 ucmanio)) goto error;
     901           0 :                                         continue;
     902           0 :                                 case -1: goto error;
     903             :                         }
     904           0 :                         switch(maybe_process_file(asfd, sdirs,
     905           0 :                                 cb, p1b, ucmanio, cconfs))
     906             :                         {
     907           0 :                                 case 0: continue;
     908           0 :                                 case 1: break;
     909           0 :                                 case -1: goto error;
     910             :                         }
     911           0 :                         break;
     912             :                 }
     913             :         }
     914             : 
     915             : error:
     916           0 :         ret=-1;
     917             : end:
     918           0 :         if(manio_close(&chmanio))
     919             :         {
     920           0 :                 logp("error closing %s in %s\n", sdirs->changed, __func__);
     921           0 :                 ret=-1;
     922             :         }
     923           0 :         if(manio_close(&ucmanio))
     924             :         {
     925           0 :                 logp("error closing %s in %s\n", sdirs->unchanged, __func__);
     926           0 :                 ret=-1;
     927             :         }
     928           0 :         free_w(&deltmppath);
     929           0 :         sbuf_free(&cb);
     930           0 :         sbuf_free(&p1b);
     931           0 :         sbuf_free(&rb);
     932           0 :         manio_close(&p1manio);
     933           0 :         manio_close(&cmanio);
     934           0 :         dpth_free(&dpth);
     935           0 :         man_off_t_free(&p1pos);
     936           0 :         if(!ret) unlink(sdirs->phase1data);
     937             : 
     938           0 :         logp("End phase2 (receive file data)\n");
     939             : 
     940           0 :         return ret;
     941             : }

Generated by: LCOV version 1.10