LCOV - code coverage report
Current view: top level - src/server - timestamp.c (source / functions) Hit Total Coverage
Test: burp-coverage-clean.info Lines: 34 36 94.4 %
Date: 2016-07-31 Functions: 4 4 100.0 %

          Line data    Source code
       1             : #include "../burp.h"
       2             : #include "../bu.h"
       3             : #include "../conf.h"
       4             : #include "../fsops.h"
       5             : #include "../fzp.h"
       6             : #include "bu_get.h"
       7             : 
       8             : #include "timestamp.h"
       9             : 
      10        1198 : int timestamp_read(const char *path, char buf[], size_t len)
      11             : {
      12        1198 :         char *cp=NULL;
      13        1198 :         char *fgetret=NULL;
      14        1198 :         struct fzp *fzp=NULL;
      15             : 
      16        1198 :         if(!(fzp=fzp_open(path, "rb")))
      17             :         {
      18           0 :                 *buf=0;
      19           0 :                 return -1;
      20             :         }
      21        1198 :         fgetret=fzp_gets(fzp, buf, len);
      22        1198 :         fzp_close(&fzp);
      23        1198 :         if(!fgetret) return -1;
      24        1198 :         if((cp=strrchr(buf, '\n'))) *cp='\0';
      25             :         return 0;
      26             : }
      27             : 
      28         849 : int timestamp_write(const char *path, const char *tstmp)
      29             : {
      30         849 :         struct fzp *fzp=NULL;
      31         849 :         if(!(fzp=fzp_open(path, "wb"))) return -1;
      32         849 :         fzp_printf(fzp, "%s\n", tstmp);
      33         849 :         fzp_close(&fzp);
      34         849 :         return 0;
      35             : }
      36             : 
      37           4 : static void timestamp_write_to_buf(char *buf, size_t s,
      38             :         uint64_t index, const char *format, time_t *t)
      39             : {
      40           4 :         char tmpbuf[32]="";
      41           4 :         const char *fmt=DEFAULT_TIMESTAMP_FORMAT;
      42           4 :         if(format) fmt=format;
      43           4 :         strftime(tmpbuf, sizeof(tmpbuf), fmt, localtime(t));
      44             :         snprintf(buf, s, "%07" PRIu64 " %s", index, tmpbuf);
      45           4 : }
      46             : 
      47           2 : int timestamp_get_new(struct sdirs *sdirs,
      48             :         char *buf, size_t s, char *bufforfile, size_t bs, const char *format)
      49             : {
      50           2 :         time_t t=0;
      51           2 :         uint64_t index=0;
      52           2 :         struct bu *bu=NULL;
      53           2 :         struct bu *bu_list=NULL;
      54             : 
      55             :         // Want to prefix the timestamp with an index that increases by
      56             :         // one each time. This makes it far more obvious which backup depends
      57             :         // on which - even if the system clock moved around. Take that,
      58             :         // bacula!
      59             : 
      60             :         // This function orders the array with the highest index number last.
      61           2 :         if(bu_get_list(sdirs, &bu_list)) return -1;
      62           2 :         for(bu=bu_list; bu; bu=bu->next) if(!bu->next) index=bu->bno;
      63             : 
      64           2 :         bu_list_free(&bu_list);
      65             : 
      66           2 :         time(&t);
      67             :         // Windows does not like the %T strftime format option - you get
      68             :         // complaints under gdb.
      69           2 :         index++;
      70             : 
      71           2 :         timestamp_write_to_buf(buf, s, index, NULL, &t);
      72           2 :         timestamp_write_to_buf(bufforfile, bs, index, format, &t);
      73             : 
      74           2 :         return 0;
      75             : }

Generated by: LCOV version 1.10