LCOV - code coverage report
Current view: top level - src/server - timestamp.c (source / functions) Hit Total Coverage
Test: burp-coverage-clean.info Lines: 37 40 92.5 %
Date: 2018-12-30 04:45:13 Functions: 5 5 100.0 %

          Line data    Source code
       1             : #include "../burp.h"
       2             : #include "../alloc.h"
       3             : #include "../bu.h"
       4             : #include "../conf.h"
       5             : #include "../fsops.h"
       6             : #include "../fzp.h"
       7             : #include "../times.h"
       8             : 
       9             : #include "timestamp.h"
      10             : 
      11        1217 : int timestamp_read(const char *path, char buf[], size_t len)
      12             : {
      13        1217 :         char *cp=NULL;
      14        1217 :         char *fgetret=NULL;
      15        1217 :         struct fzp *fzp=NULL;
      16             : 
      17        1217 :         if(!(fzp=fzp_open(path, "rb")))
      18             :         {
      19           0 :                 *buf=0;
      20           0 :                 return -1;
      21             :         }
      22        1217 :         fgetret=fzp_gets(fzp, buf, len);
      23        1217 :         fzp_close(&fzp);
      24        1217 :         if(!fgetret) return -1;
      25        1217 :         if((cp=strrchr(buf, '\n'))) *cp='\0';
      26             :         return 0;
      27             : }
      28             : 
      29         883 : int timestamp_write(const char *path, const char *tstmp)
      30             : {
      31         883 :         struct fzp *fzp=NULL;
      32         883 :         if(!(fzp=fzp_open(path, "ab"))) return -1;
      33         883 :         fzp_printf(fzp, "%s\n", tstmp);
      34         883 :         fzp_close(&fzp);
      35         883 :         return 0;
      36             : }
      37             : 
      38             : #ifndef UTEST
      39             : static
      40             : #endif
      41          15 : void timestamp_write_to_buf(char *buf, size_t s,
      42             :         uint64_t index, const char *format, time_t *t)
      43             : {
      44          15 :         char tmpbuf[38]="";
      45          15 :         const char *fmt=DEFAULT_TIMESTAMP_FORMAT;
      46          15 :         if(format) fmt=format;
      47          15 :         strftime(tmpbuf, sizeof(tmpbuf), fmt, localtime(t));
      48          15 :         snprintf(buf, s, "%07" PRIu64 " %s", index, tmpbuf);
      49          15 : }
      50             : 
      51          12 : int timestamp_get_new(uint64_t index,
      52             :         char *buf, size_t s, char *bufforfile, size_t bs, const char *format)
      53             : {
      54          12 :         time_t t=0;
      55             : 
      56          12 :         time(&t);
      57             :         // Windows does not like the %T strftime format option - you get
      58             :         // complaints under gdb.
      59             : 
      60          12 :         if(buf)
      61          12 :                 timestamp_write_to_buf(buf, s, index, NULL, &t);
      62          12 :         if(bufforfile)
      63           3 :                 timestamp_write_to_buf(bufforfile, bs, index, format, &t);
      64             : 
      65          12 :         return 0;
      66             : }
      67             : 
      68          37 : long timestamp_to_long(const char *buf)
      69             : {
      70             :         struct tm tm;
      71          37 :         const char *b=NULL;
      72          37 :         if(!(b=strchr(buf, ' '))) return 0;
      73          37 :         memset(&tm, 0, sizeof(struct tm));
      74          37 :         if(!strptime(b+1, DEFAULT_TIMESTAMP_FORMAT, &tm)
      75           0 :           && !strptime(b+1, DEFAULT_TIMESTAMP_FORMAT_OLD, &tm))
      76             :                 return 0;
      77             :         // Unset dst so that mktime has to figure it out.
      78          37 :         tm.tm_isdst=-1;
      79          37 :         return (long)mktime(&tm);
      80             : }

Generated by: LCOV version 1.13