LCOV - code coverage report
Current view: top level - src/server/protocol2 - dpth.c (source / functions) Hit Total Coverage
Test: burp-coverage-clean.info Lines: 141 153 92.2 %
Date: 2016-07-02 Functions: 15 15 100.0 %

          Line data    Source code
       1             : #include "../../burp.h"
       2             : #include "../../alloc.h"
       3             : #include "../../cmd.h"
       4             : #include "../../fsops.h"
       5             : #include "../../hexmap.h"
       6             : #include "../../iobuf.h"
       7             : #include "../../lock.h"
       8             : #include "../../log.h"
       9             : #include "../../prepend.h"
      10             : #include "../../protocol2/blk.h"
      11             : #include "dpth.h"
      12             : 
      13          15 : static int get_data_lock(struct lock *lock, const char *path)
      14             : {
      15          15 :         int ret=-1;
      16          15 :         char *lockfile=NULL;
      17             :         // Use just the first three components, excluding sig number.
      18          15 :         if(!(lockfile=prepend(path, ".lock")))
      19             :                 goto end;
      20          30 :         if(lock_init(lock, lockfile)
      21          15 :           || build_path_w(lock->path))
      22             :                 goto end;
      23          15 :         lock_get_quick(lock);
      24          15 :         ret=0;
      25             : end:
      26          15 :         free_w(&lockfile);
      27          15 :         return ret;
      28             : }
      29             : 
      30             : static char *dpth_mk_prim(struct dpth *dpth)
      31             : {
      32             :         static char path[8];
      33          39 :         snprintf(path, sizeof(path), "%04X", dpth->comp[0]);
      34             :         return path;
      35             : }
      36             : 
      37             : static char *dpth_mk_seco(struct dpth *dpth)
      38             : {
      39             :         static char path[16];
      40          39 :         snprintf(path, sizeof(path), "%04X/%04X", dpth->comp[0], dpth->comp[1]);
      41             :         return path;
      42             : }
      43             : 
      44          11 : static struct dpth_lock *dpth_lock_alloc(const char *save_path)
      45             : {
      46             :         struct dpth_lock *dpth_lock;
      47          11 :         if(!(dpth_lock=(struct dpth_lock *)
      48             :                 calloc_w(1, sizeof(struct dpth_lock), __func__)))
      49             :                         return NULL;
      50             :         snprintf(dpth_lock->save_path, sizeof(dpth_lock->save_path),
      51          11 :                 "%s", save_path);
      52          11 :         return dpth_lock;
      53             : }
      54             : 
      55          11 : static int add_lock_to_list(struct dpth *dpth,
      56             :         struct lock *lock, const char *save_path)
      57             : {
      58             :         struct dpth_lock *dlnew;
      59          11 :         if(!(dlnew=dpth_lock_alloc(save_path))) return -1;
      60          11 :         dlnew->lock=lock;
      61             : 
      62             :         // Add to the end of the list.
      63          11 :         if(dpth->tail) dpth->tail->next=dlnew;
      64           8 :         else if(!dpth->head) dpth->head=dlnew;
      65          11 :         dpth->tail=dlnew;
      66             :         return 0;
      67             : }
      68             : 
      69        8538 : char *dpth_protocol2_get_save_path(struct dpth *dpth)
      70             : {
      71             :         static char save_path[32];
      72             :         snprintf(save_path, sizeof(save_path), "%04X/%04X/%04X/%04X",
      73        8538 :                 dpth->comp[0], dpth->comp[1], dpth->comp[2], dpth->comp[3]);
      74        8538 :         return save_path;
      75             : }
      76             : 
      77        8527 : char *dpth_protocol2_mk(struct dpth *dpth)
      78             : {
      79        8527 :         char *p=NULL;
      80             :         static char *save_path=NULL;
      81             :         static struct lock *lock=NULL;
      82             :         while(1)
      83             :         {
      84        8531 :                 free_w(&p);
      85        8531 :                 save_path=dpth_protocol2_get_save_path(dpth);
      86        8531 :                 if(!dpth->need_data_lock)
      87             :                         return save_path;
      88             : 
      89          15 :                 if(!lock && !(lock=lock_alloc()))
      90             :                         goto error;
      91             : 
      92             :                 // Use just the first three components, excluding sig number.
      93          15 :                 if(!(p=prepend_slash(dpth->base_path, save_path, 14)))
      94             :                         goto error;
      95             : 
      96          15 :                 if(get_data_lock(lock, p))
      97             :                         goto error;
      98             : 
      99          15 :                 switch(lock->status)
     100             :                 {
     101             :                         case GET_LOCK_GOT:
     102             :                                 struct stat statp;
     103          28 :                                 if(lstat(p, &statp))
     104             :                                 {
     105             :                                         // File does not exist yet, and we
     106             :                                         // have the lock. All good.
     107             :                                         break;
     108             :                                 }
     109             :                                 // The file that we want to write already
     110             :                                 // exists.
     111           3 :                                 if(lock_release(lock))
     112             :                                         goto error;
     113           3 :                                 lock_free(&lock);
     114             :                                 // Fall through and try again.
     115             :                         case GET_LOCK_NOT_GOT:
     116             :                                 // Increment and try again.
     117           4 :                                 if(dpth_incr(dpth))
     118             :                                         goto error;
     119           4 :                                 continue;
     120             :                         case GET_LOCK_ERROR:
     121             :                         default:
     122             :                                 goto error;
     123             :                 }
     124             : 
     125          11 :                 dpth->need_data_lock=0; // Got it.
     126          11 :                 if(add_lock_to_list(dpth, lock, save_path))
     127             :                         goto error;
     128          11 :                 lock=NULL;
     129          11 :                 free_w(&p);
     130          11 :                 return save_path;
     131             :         }
     132             : error:
     133           0 :         free_w(&p);
     134           0 :         lock_free(&lock);
     135           4 :         return NULL;
     136             : }
     137             : 
     138             : // Returns 0 on OK, -1 on error. *max gets set to the next entry.
     139         153 : int get_highest_entry(const char *path, int *max, size_t len)
     140             : {
     141         153 :         int ent=0;
     142         153 :         int ret=0;
     143         153 :         DIR *d=NULL;
     144         153 :         struct dirent *dp=NULL;
     145             : 
     146         153 :         *max=-1;
     147         153 :         if(!(d=opendir(path))) goto end;
     148         229 :         while((dp=readdir(d)))
     149             :         {
     150         174 :                 if(!dp->d_ino
     151         174 :                   || strlen(dp->d_name)!=len)
     152             :                         continue;
     153          24 :                 ent=strtol(dp->d_name, NULL, 16);
     154          24 :                 if(ent>*max) *max=ent;
     155             :         }
     156             : 
     157             : end:
     158         153 :         if(d) closedir(d);
     159         153 :         return ret;
     160             : }
     161             : 
     162       16725 : int dpth_protocol2_incr_sig(struct dpth *dpth)
     163             : {
     164       16725 :         if(++dpth->comp[3]<DATA_FILE_SIG_MAX) return 0;
     165           9 :         dpth->comp[3]=0;
     166           9 :         dpth->need_data_lock=1;
     167           9 :         return dpth_incr(dpth);
     168             : }
     169             : 
     170          39 : static int open_cfile_fzp(struct dpth *dpth,
     171             :         const char *cname, const char *cfiles)
     172             : {
     173             :         int fd;
     174          39 :         int ret=-1;
     175          39 :         char *fname=NULL;
     176          39 :         char *fullpath=NULL;
     177             : 
     178          39 :         if(!(fname=prepend(cname, "XXXXXX")))
     179             :                 goto end;
     180          39 :         if(!(fullpath=prepend_s(cfiles, fname)))
     181             :                 goto end;
     182          39 :         if(build_path_w(fullpath))
     183             :                 goto end;
     184          39 :         if((fd=mkstemp(fullpath))<0)
     185             :         {
     186             :                 logp("Could not mkstemp from template %s: %s\n",
     187           0 :                         fullpath, strerror(errno));
     188             :                 goto end;
     189             :         }
     190          39 :         if(!(dpth->cfile_fzp=fzp_dopen(fd, "wb")))
     191             :                 goto end;
     192             : 
     193          39 :         ret=0;
     194             : end:
     195          39 :         free_w(&fname);
     196          39 :         free_w(&fullpath);
     197          39 :         return ret;
     198             : }
     199             : 
     200         117 : int dpth_protocol2_init(struct dpth *dpth, const char *base_path,
     201             :         const char *cname, const char *cfiles, int max_storage_subdirs)
     202             : {
     203             :         int max;
     204          39 :         int ret=0;
     205          39 :         char *tmp=NULL;
     206             : 
     207          39 :         if(!base_path)
     208             :         {
     209           0 :                 logp("No base_path supplied in %s()\n", __func__);
     210           0 :                 goto error;
     211             :         }
     212             : 
     213          39 :         if(open_cfile_fzp(dpth, cname, cfiles)) goto error;
     214             : 
     215          39 :         dpth->max_storage_subdirs=max_storage_subdirs;
     216             : 
     217          39 :         free_w(&dpth->base_path);
     218          39 :         if(!(dpth->base_path=strdup_w(base_path, __func__)))
     219             :                 goto error;
     220             : 
     221          39 :         dpth->savepath=0;
     222          39 :         dpth->need_data_lock=1;
     223             : 
     224          39 :         if(get_highest_entry(dpth->base_path, &max, 4))
     225             :                 goto error;
     226          39 :         if(max<0) max=0;
     227          39 :         dpth->comp[0]=max;
     228          78 :         tmp=dpth_mk_prim(dpth);
     229          39 :         if(!(tmp=prepend_s(dpth->base_path, tmp)))
     230             :                 goto error;
     231             : 
     232          39 :         if(get_highest_entry(tmp, &max, 4))
     233             :                 goto error;
     234          39 :         if(max<0) max=0;
     235          39 :         dpth->comp[1]=max;
     236          39 :         free_w(&tmp);
     237         117 :         tmp=dpth_mk_seco(dpth);
     238          39 :         if(!(tmp=prepend_s(dpth->base_path, tmp)))
     239             :                 goto error;
     240             : 
     241          39 :         if(get_highest_entry(tmp, &max, 4))
     242             :                 goto error;
     243          39 :         if(max<0)
     244             :         {
     245          31 :                 dpth->comp[2]=0;
     246             :         }
     247             :         else
     248             :         {
     249           8 :                 dpth->comp[2]=max;
     250           8 :                 if(dpth_incr(dpth)) goto error;
     251             :         }
     252             : 
     253             :         goto end;
     254             : error:
     255             :         ret=-1;
     256             : end:
     257          39 :         free_w(&tmp);
     258          39 :         return ret;
     259             : }
     260             : 
     261       16713 : static int fprint_tag(struct fzp *fzp, enum cmd cmd, unsigned int s)
     262             : {
     263       16713 :         if(fzp_printf(fzp, "%c%04X", cmd, s)!=5)
     264             :         {
     265           0 :                 logp("Short fprintf\n");
     266           0 :                 return -1;
     267             :         }
     268             :         return 0;
     269             : }
     270             : 
     271       16713 : static int fwrite_buf(enum cmd cmd,
     272             :         const char *buf, unsigned int s, struct fzp *fzp)
     273             : {
     274             :         static size_t bytes;
     275       16713 :         if(fprint_tag(fzp, cmd, s)) return -1;
     276       16713 :         if((bytes=fzp_write(fzp, buf, s))!=s)
     277             :         {
     278           0 :                 logp("Short write: %d\n", (int)bytes);
     279           0 :                 return -1;
     280             :         }
     281             :         return 0;
     282             : }
     283             : 
     284           9 : static struct fzp *file_open_w(const char *path, const char *mode)
     285             : {
     286           9 :         if(build_path_w(path)) return NULL;
     287           9 :         return fzp_open(path, "wb");
     288             : }
     289             : 
     290           9 : static int write_to_cfile(struct dpth *dpth, struct blk *blk)
     291             : {
     292             :         struct iobuf wbuf;
     293           9 :         blk_to_iobuf_savepath(blk, &wbuf);
     294           9 :         if(iobuf_send_msg_fzp(&wbuf, dpth->cfile_fzp))
     295             :                 return -1;
     296           9 :         if(fzp_flush(dpth->cfile_fzp))
     297             :                 return -1;
     298           9 :         if(fsync(fzp_fileno(dpth->cfile_fzp)))
     299             :         {
     300           0 :                 logp("fsync on cfile_fzp failed: %s\n", strerror(errno));
     301             :                 return -1;
     302             :         }
     303             :         return 0;
     304             : }
     305             : 
     306           9 : static struct fzp *open_data_file_for_write(struct dpth *dpth, struct blk *blk)
     307             : {
     308           9 :         char *path=NULL;
     309           9 :         struct fzp *fzp=NULL;
     310           9 :         char *savepathstr=NULL;
     311           9 :         struct dpth_lock *head=dpth->head;
     312             : 
     313           9 :         savepathstr=uint64_to_savepathstr(blk->savepath);
     314             : 
     315             :         // Sanity check. They should be coming through from the client
     316             :         // in the same order in which we locked them.
     317             :         // Remember that the save_path on the lock list is shorter than the
     318             :         // full save_path on the blk.
     319           9 :         if(!head
     320           9 :           || strncmp(head->save_path,
     321             :                 //FIX THIS
     322           9 :                 savepathstr, sizeof(head->save_path)-1))
     323             :         {
     324             :                 logp("lock and block save_path mismatch: %s %s\n",
     325           0 :                         head?head->save_path:"(null)", savepathstr);
     326           0 :                 goto end;
     327             :         }
     328             : 
     329           9 :         if(!(path=prepend_slash(dpth->base_path, savepathstr, 14)))
     330             :                 goto end;
     331           9 :         if(write_to_cfile(dpth, blk))
     332             :                 goto end;
     333           9 :         fzp=file_open_w(path, "wb");
     334             : end:
     335           9 :         free_w(&path);
     336           9 :         return fzp;
     337             : }
     338             : 
     339       16713 : int dpth_protocol2_fwrite(struct dpth *dpth,
     340             :         struct iobuf *iobuf, struct blk *blk)
     341             : {
     342             :         // Remember that the save_path on the lock list is shorter than the
     343             :         // full save_path on the blk.
     344       16713 :         if(dpth->fzp
     345       16705 :           && strncmp(dpth->head->save_path,
     346       16705 :                 uint64_to_savepathstr(blk->savepath),
     347       16705 :                 sizeof(dpth->head->save_path)-1)
     348       16714 :           && dpth_release_and_move_to_next_in_list(dpth))
     349             :                 return -1;
     350             : 
     351             :         // Open the current list head if we have no fzp.
     352       33426 :         if(!dpth->fzp
     353       16713 :           && !(dpth->fzp=open_data_file_for_write(dpth, blk))) return -1;
     354             : 
     355       16713 :         return fwrite_buf(CMD_DATA, iobuf->buf, iobuf->len, dpth->fzp);
     356             : }

Generated by: LCOV version 1.10