LCOV - code coverage report
Current view: top level - src/server - timestamp.c (source / functions) Hit Total Coverage
Test: burp-coverage-clean.info Lines: 39 42 92.9 %
Date: 2022-12-03 01:09:05 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         658 : int timestamp_read(const char *path, char buf[], size_t len)
      12             : {
      13         658 :         char *cp=NULL;
      14         658 :         char *fgetret=NULL;
      15         658 :         struct fzp *fzp=NULL;
      16             : 
      17         658 :         if(!(fzp=fzp_open(path, "rb")))
      18             :         {
      19           0 :                 *buf=0;
      20           0 :                 return -1;
      21             :         }
      22         658 :         fgetret=fzp_gets(fzp, buf, len);
      23         658 :         fzp_close(&fzp);
      24         658 :         if(!fgetret) return -1;
      25         658 :         if((cp=strrchr(buf, '\n'))) *cp='\0';
      26             :         return 0;
      27             : }
      28             : 
      29         482 : int timestamp_write(const char *path, const char *tstmp)
      30             : {
      31         482 :         struct fzp *fzp=NULL;
      32         482 :         if(!(fzp=fzp_open(path, "ab"))) return -1;
      33         482 :         fzp_printf(fzp, "%s\n", tstmp);
      34         482 :         fzp_close(&fzp);
      35         482 :         return 0;
      36             : }
      37             : 
      38             : #ifndef UTEST
      39             : static
      40             : #endif
      41          13 : void timestamp_write_to_buf(char *buf, size_t s,
      42             :         uint64_t index, const char *format, time_t *t)
      43             : {
      44          13 :         char tmpbuf[38]="";
      45          13 :         const char *fmt=DEFAULT_TIMESTAMP_FORMAT;
      46          13 :         if(format) fmt=format;
      47          13 :         strftime(tmpbuf, sizeof(tmpbuf), fmt, localtime(t));
      48          13 :         if(index)
      49           4 :                 snprintf(buf, s, "%07" PRIu64 " %s", index, tmpbuf);
      50             :         else
      51           9 :                 snprintf(buf, s, "%s", tmpbuf);
      52          13 : }
      53             : 
      54          11 : int timestamp_get_new(uint64_t index,
      55             :         char *buf, size_t s, char *bufforfile, size_t bs, const char *format)
      56             : {
      57          11 :         time_t t=0;
      58             : 
      59          11 :         time(&t);
      60             :         // Windows does not like the %T strftime format option - you get
      61             :         // complaints under gdb.
      62             : 
      63          11 :         if(buf)
      64          11 :                 timestamp_write_to_buf(buf, s, index, NULL, &t);
      65          11 :         if(bufforfile)
      66           2 :                 timestamp_write_to_buf(bufforfile, bs, index, format, &t);
      67             : 
      68          11 :         return 0;
      69             : }
      70             : 
      71          37 : long timestamp_to_long(const char *buf)
      72             : {
      73             :         struct tm tm;
      74          37 :         const char *b=NULL;
      75          37 :         if(!(b=strchr(buf, ' '))) return 0;
      76          37 :         memset(&tm, 0, sizeof(struct tm));
      77          37 :         if(!strptime(b+1, DEFAULT_TIMESTAMP_FORMAT, &tm)
      78           0 :           && !strptime(b+1, DEFAULT_TIMESTAMP_FORMAT_OLD, &tm))
      79             :                 return 0;
      80             :         // Unset dst so that mktime has to figure it out.
      81          37 :         tm.tm_isdst=-1;
      82          37 :         return (long)mktime(&tm);
      83             : }

Generated by: LCOV version 1.13