1 /*
2    Bacula(R) - The Network Backup Solution
3 
4    Copyright (C) 2000-2020 Kern Sibbald
5 
6    The original author of Bacula is Kern Sibbald, with contributions
7    from many others, a complete list can be found in the file AUTHORS.
8 
9    You may use this file and others of this release according to the
10    license defined in the LICENSE file, which includes the Affero General
11    Public License, v3.0 ("AGPLv3") and some additional permissions and
12    terms pursuant to its AGPLv3 Section 7.
13 
14    This notice must be preserved when any source code is
15    conveyed and/or propagated.
16 
17    Bacula(R) is a registered trademark of Kern Sibbald.
18  */
19 
20 #ifndef journalfilerecord_H
21 #define journalfilerecord_H
22 
23 #include "bacula.h"
24 #include <string.h>
25 
26 /**
27  * Implementation is part of Bacula's findlib module, and is on:
28  * "/src/findlib/attribs.c"
29  */
30 extern int decode_stat(char *buf, struct stat *statp, int stat_size, int32_t *LinkFI);
31 extern void encode_stat(char *buf, struct stat *statp, int stat_size, int32_t LinkFI, int data_stream);
32 
33 /**
34  * @brief Data that is saved and retrieved by using the @class Journal
35  */
36 class FileRecord
37 {
38    public:
39       char *name;
40       char *sname;
41       char *fattrs;
42       int64_t mtime;
43 
FileRecord()44       FileRecord():
45          name(NULL), sname(NULL), fattrs(NULL), mtime(0)
46       {}
47 
encode_attrs()48       bool encode_attrs() {
49          struct stat statbuf;
50 #ifndef HAVE_WIN32
51          if(lstat(this->name, &statbuf) != 0) {
52             return false;
53          }
54 
55          this->mtime = (int64_t) statbuf.st_mtime;
56          this->fattrs = (char *) malloc(500 * sizeof(char));
57          encode_stat(this->fattrs, &statbuf, sizeof(statbuf), 0, 0);
58 #else
59          FILE *fp = fopen(this->name, "r");
60 
61          if (!fp) {
62             Dmsg1(0, "Could not open file %s\n", this->name);
63             return false;
64          }
65 
66          int fd = _fileno(fp);
67 
68          if(fstat(fd, &statbuf) != 0) {
69             fclose(fp);
70             Dmsg1(0, "Could not encode attributes of file %s\n", this->name);
71             return false;
72          }
73 
74          this->mtime = (int64_t) statbuf.st_mtime;
75          this->fattrs = (char *) malloc(500 * sizeof(char));
76          encode_stat(this->fattrs, &statbuf, sizeof(statbuf), 0, 0);
77          fclose(fp);
78 #endif
79          return true;
80       }
81 
decode_attrs(struct stat & sbuf)82       void decode_attrs(struct stat &sbuf) {
83          int32_t lfi;
84          decode_stat(this->fattrs, &sbuf, sizeof(sbuf), &lfi);
85       }
86 
equals(const FileRecord * rec)87       bool equals(const FileRecord *rec) {
88          return strcmp(this->name, rec->name) == 0
89             && strcmp(this->fattrs, rec->fattrs) == 0
90             && this->mtime == rec->mtime;
91       }
92 
getBaculaName(POOLMEM * target)93       void getBaculaName(POOLMEM *target) {
94          char mtime_date[200];
95          time_t t = (time_t) mtime;
96          struct tm *timeinfo = localtime(&t);
97          strftime(mtime_date, 200, "%Y%m%d_%H%M%S", timeinfo);
98          Mmsg(target, "%s.%s", name, mtime_date);
99       }
100 
~FileRecord()101       ~FileRecord() {
102          if (name != NULL) {
103             free(name);
104          }
105 
106          if (sname != NULL) {
107             free(sname);
108          }
109 
110          if (fattrs != NULL) {
111             free(fattrs);
112          }
113       }
114 };
115 
116 #endif
117