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 1208 : int timestamp_read(const char *path, char buf[], size_t len)
11 : {
12 1208 : char *cp=NULL;
13 1208 : char *fgetret=NULL;
14 1208 : struct fzp *fzp=NULL;
15 :
16 1208 : if(!(fzp=fzp_open(path, "rb")))
17 : {
18 0 : *buf=0;
19 0 : return -1;
20 : }
21 1208 : fgetret=fzp_gets(fzp, buf, len);
22 1208 : fzp_close(&fzp);
23 1208 : if(!fgetret) return -1;
24 1208 : if((cp=strrchr(buf, '\n'))) *cp='\0';
25 : return 0;
26 : }
27 :
28 860 : int timestamp_write(const char *path, const char *tstmp)
29 : {
30 860 : struct fzp *fzp=NULL;
31 860 : if(!(fzp=fzp_open(path, "wb"))) return -1;
32 860 : fzp_printf(fzp, "%s\n", tstmp);
33 860 : fzp_close(&fzp);
34 860 : return 0;
35 : }
36 :
37 6 : static void timestamp_write_to_buf(char *buf, size_t s,
38 : uint64_t index, const char *format, time_t *t)
39 : {
40 6 : char tmpbuf[32]="";
41 6 : const char *fmt=DEFAULT_TIMESTAMP_FORMAT;
42 6 : if(format) fmt=format;
43 6 : strftime(tmpbuf, sizeof(tmpbuf), fmt, localtime(t));
44 : snprintf(buf, s, "%07" PRIu64 " %s", index, tmpbuf);
45 6 : }
46 :
47 3 : int timestamp_get_new(struct sdirs *sdirs,
48 : char *buf, size_t s, char *bufforfile, size_t bs, const char *format)
49 : {
50 3 : time_t t=0;
51 3 : uint64_t index=0;
52 3 : struct bu *bu=NULL;
53 3 : 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 3 : if(bu_get_list(sdirs, &bu_list)) return -1;
62 3 : for(bu=bu_list; bu; bu=bu->next) if(!bu->next) index=bu->bno;
63 :
64 3 : bu_list_free(&bu_list);
65 :
66 3 : time(&t);
67 : // Windows does not like the %T strftime format option - you get
68 : // complaints under gdb.
69 3 : index++;
70 :
71 3 : timestamp_write_to_buf(buf, s, index, NULL, &t);
72 3 : timestamp_write_to_buf(bufforfile, bs, index, format, &t);
73 :
74 3 : return 0;
75 : }
|