]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/record_util.c
7c3c54dad5d7db96441a1216b0029f893c635a8c
[bacula/bacula] / bacula / src / stored / record_util.c
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2000-2016 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  *
21  *   record-util.c -- Utilities for record handling
22  *
23  *            Kern Sibbald, October MMXII
24  *
25  */
26
27 #include "bacula.h"
28 #include "stored.h"
29
30 /*
31  * Convert a FileIndex into a printable
32  *   ASCII string.  Not reentrant.
33  * If the FileIndex is negative, it flags the
34  *   record as a Label, otherwise it is simply
35  *   the FileIndex of the current file.
36  */
37 const char *FI_to_ascii(char *buf, int fi)
38 {
39    if (fi >= 0) {
40       sprintf(buf, "%d", fi);
41       return buf;
42    }
43    switch (fi) {
44    case PRE_LABEL:
45       return "PRE_LABEL";
46    case VOL_LABEL:
47       return "VOL_LABEL";
48    case EOM_LABEL:
49       return "EOM_LABEL";
50    case SOS_LABEL:
51       return "SOS_LABEL";
52    case EOS_LABEL:
53       return "EOS_LABEL";
54    case EOT_LABEL:
55       return "EOT_LABEL";
56       break;
57    case SOB_LABEL:
58       return "SOB_LABEL";
59       break;
60    case EOB_LABEL:
61       return "EOB_LABEL";
62       break;
63    default:
64      sprintf(buf, _("unknown: %d"), fi);
65      return buf;
66    }
67 }
68
69 /*
70  * Convert a Stream ID into a printable
71  * ASCII string.  Not reentrant.
72
73  * A negative stream number represents
74  *   stream data that is continued from a
75  *   record in the previous block.
76  * If the FileIndex is negative, we are
77  *   dealing with a Label, hence the
78  *   stream is the JobId.
79  */
80 const char *stream_to_ascii(char *buf, int stream, int fi)
81 {
82
83    if (fi < 0) {
84       sprintf(buf, "%d", stream);
85       return buf;
86    }
87    if (stream < 0) {
88       stream = -stream;
89       stream &= STREAMMASK_TYPE;
90       /* Stream was negative => all are continuation items */
91       switch (stream) {
92       case STREAM_UNIX_ATTRIBUTES:
93          return "contUATTR";
94       case STREAM_FILE_DATA:
95          return "contDATA";
96       case STREAM_WIN32_DATA:
97          return "contWIN32-DATA";
98       case STREAM_WIN32_GZIP_DATA:
99          return "contWIN32-GZIP";
100       case STREAM_WIN32_COMPRESSED_DATA:
101          return "contWIN32-COMPRESSED";
102       case STREAM_MD5_DIGEST:
103          return "contMD5";
104       case STREAM_SHA1_DIGEST:
105          return "contSHA1";
106       case STREAM_GZIP_DATA:
107          return "contGZIP";
108       case STREAM_COMPRESSED_DATA:
109          return "contCOMPRESSED";
110       case STREAM_UNIX_ATTRIBUTES_EX:
111          return "contUNIX-ATTR-EX";
112       case STREAM_RESTORE_OBJECT:
113          return "contRESTORE-OBJECT";
114       case STREAM_SPARSE_DATA:
115          return "contSPARSE-DATA";
116       case STREAM_SPARSE_GZIP_DATA:
117          return "contSPARSE-GZIP";
118       case STREAM_SPARSE_COMPRESSED_DATA:
119          return "contSPARSE-COMPRESSED";
120       case STREAM_PROGRAM_NAMES:
121          return "contPROG-NAMES";
122       case STREAM_PROGRAM_DATA:
123          return "contPROG-DATA";
124       case STREAM_MACOS_FORK_DATA:
125          return "contMACOS-RSRC";
126       case STREAM_HFSPLUS_ATTRIBUTES:
127          return "contHFSPLUS-ATTR";
128       case STREAM_SHA256_DIGEST:
129          return "contSHA256";
130       case STREAM_SHA512_DIGEST:
131          return "contSHA512";
132       case STREAM_SIGNED_DIGEST:
133          return "contSIGNED-DIGEST";
134       case STREAM_ENCRYPTED_SESSION_DATA:
135          return "contENCRYPTED-SESSION-DATA";
136       case STREAM_ENCRYPTED_FILE_DATA:
137          return "contENCRYPTED-FILE";
138       case STREAM_ENCRYPTED_FILE_GZIP_DATA:
139          return "contENCRYPTED-GZIP";
140       case STREAM_ENCRYPTED_FILE_COMPRESSED_DATA:
141          return "contENCRYPTED-COMPRESSED";
142       case STREAM_ENCRYPTED_WIN32_DATA:
143          return "contENCRYPTED-WIN32-DATA";
144       case STREAM_ENCRYPTED_WIN32_GZIP_DATA:
145          return "contENCRYPTED-WIN32-GZIP";
146       case STREAM_ENCRYPTED_WIN32_COMPRESSED_DATA:
147          return "contENCRYPTED-WIN32-COMPRESSED";
148       case STREAM_ENCRYPTED_MACOS_FORK_DATA:
149          return "contENCRYPTED-MACOS-RSRC";
150       case STREAM_PLUGIN_NAME:
151          return "contPLUGIN-NAME";
152
153       default:
154          sprintf(buf, "%d", -stream);
155          return buf;
156       }
157    }
158
159    switch (stream & STREAMMASK_TYPE) {
160    case STREAM_UNIX_ATTRIBUTES:
161       return "UATTR";
162    case STREAM_FILE_DATA:
163       return "DATA";
164    case STREAM_WIN32_DATA:
165       return "WIN32-DATA";
166    case STREAM_WIN32_GZIP_DATA:
167       return "WIN32-GZIP";
168    case STREAM_WIN32_COMPRESSED_DATA:
169       return "WIN32-COMPRESSED";
170    case STREAM_MD5_DIGEST:
171       return "MD5";
172    case STREAM_SHA1_DIGEST:
173       return "SHA1";
174    case STREAM_GZIP_DATA:
175       return "GZIP";
176    case STREAM_COMPRESSED_DATA:
177       return "COMPRESSED";
178    case STREAM_UNIX_ATTRIBUTES_EX:
179       return "UNIX-ATTR-EX";
180    case STREAM_RESTORE_OBJECT:
181       return "RESTORE-OBJECT";
182    case STREAM_SPARSE_DATA:
183       return "SPARSE-DATA";
184    case STREAM_SPARSE_GZIP_DATA:
185       return "SPARSE-GZIP";
186    case STREAM_SPARSE_COMPRESSED_DATA:
187       return "SPARSE-COMPRESSED";
188    case STREAM_PROGRAM_NAMES:
189       return "PROG-NAMES";
190    case STREAM_PROGRAM_DATA:
191       return "PROG-DATA";
192    case STREAM_PLUGIN_NAME:
193       return "PLUGIN-NAME";
194    case STREAM_MACOS_FORK_DATA:
195       return "MACOS-RSRC";
196    case STREAM_HFSPLUS_ATTRIBUTES:
197       return "HFSPLUS-ATTR";
198    case STREAM_SHA256_DIGEST:
199       return "SHA256";
200    case STREAM_SHA512_DIGEST:
201       return "SHA512";
202    case STREAM_SIGNED_DIGEST:
203       return "SIGNED-DIGEST";
204    case STREAM_ENCRYPTED_SESSION_DATA:
205       return "ENCRYPTED-SESSION-DATA";
206    case STREAM_ENCRYPTED_FILE_DATA:
207       return "ENCRYPTED-FILE";
208    case STREAM_ENCRYPTED_FILE_GZIP_DATA:
209       return "ENCRYPTED-GZIP";
210    case STREAM_ENCRYPTED_FILE_COMPRESSED_DATA:
211       return "ENCRYPTED-COMPRESSED";
212    case STREAM_ENCRYPTED_WIN32_DATA:
213       return "ENCRYPTED-WIN32-DATA";
214    case STREAM_ENCRYPTED_WIN32_GZIP_DATA:
215       return "ENCRYPTED-WIN32-GZIP";
216    case STREAM_ENCRYPTED_WIN32_COMPRESSED_DATA:
217       return "ENCRYPTED-WIN32-COMPRESSED";
218    case STREAM_ENCRYPTED_MACOS_FORK_DATA:
219       return "ENCRYPTED-MACOS-RSRC";
220
221    default:
222       sprintf(buf, "%d", stream);
223       return buf;
224    }
225 }
226
227 /*
228  * Return a new record entity
229  */
230 DEV_RECORD *new_record(void)
231 {
232    DEV_RECORD *rec;
233
234    rec = (DEV_RECORD *)get_memory(sizeof(DEV_RECORD));
235    memset(rec, 0, sizeof(DEV_RECORD));
236    rec->data = get_pool_memory(PM_MESSAGE);
237    rec->wstate = st_none;
238    rec->rstate = st_none;
239    return rec;
240 }
241
242 void empty_record(DEV_RECORD *rec)
243 {
244    rec->File = rec->Block = 0;
245    rec->VolSessionId = rec->VolSessionTime = 0;
246    rec->FileIndex = rec->Stream = 0;
247    rec->data_len = rec->remainder = 0;
248    rec->state_bits &= ~(REC_PARTIAL_RECORD|REC_BLOCK_EMPTY|REC_NO_MATCH|REC_CONTINUATION);
249    rec->wstate = st_none;
250    rec->rstate = st_none;
251 }
252
253 /*
254  * Free the record entity
255  *
256  */
257 void free_record(DEV_RECORD *rec)
258 {
259    Dmsg0(950, "Enter free_record.\n");
260    if (rec->data) {
261       free_pool_memory(rec->data);
262    }
263    Dmsg0(950, "Data buf is freed.\n");
264    free_pool_memory((POOLMEM *)rec);
265    Dmsg0(950, "Leave free_record.\n");
266 }
267
268 /*
269  * Test if we can write whole record to the block
270  *
271  *  Returns: false on failure
272  *           true  on success (all bytes can be written)
273  */
274 bool can_write_record_to_block(DEV_BLOCK *block, DEV_RECORD *rec)
275 {
276    uint32_t remlen;
277
278    remlen = block->buf_len - block->binbuf;
279    if (rec->remainder == 0) {
280       if (remlen >= WRITE_RECHDR_LENGTH) {
281          remlen -= WRITE_RECHDR_LENGTH;
282          rec->remainder = rec->data_len;
283       } else {
284          return false;
285       }
286    } else {
287       return false;
288    }
289    if (rec->remainder > 0 && remlen < rec->remainder) {
290       return false;
291    }
292    return true;
293 }
294
295 uint64_t get_record_address(DEV_RECORD *rec)
296 {
297    return ((uint64_t)rec->File)<<32 | rec->Block;
298 }