2 Bacula(R) - The Network Backup Solution
4 Copyright (C) 2000-2017 Kern Sibbald
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.
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.
14 This notice must be preserved when any source code is
15 conveyed and/or propagated.
17 Bacula(R) is a registered trademark of Kern Sibbald.
21 * record-util.c -- Utilities for record handling
23 * Kern Sibbald, October MMXII
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.
37 const char *FI_to_ascii(char *buf, int fi)
40 sprintf(buf, "%d", fi);
64 sprintf(buf, _("unknown: %d"), fi);
70 * Convert a Stream ID into a printable
71 * ASCII string. Not reentrant.
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.
80 const char *stream_to_ascii(char *buf, int stream, int fi)
84 sprintf(buf, "%d", stream);
89 stream &= STREAMMASK_TYPE;
90 /* Stream was negative => all are continuation items */
92 case STREAM_UNIX_ATTRIBUTES:
94 case STREAM_FILE_DATA:
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:
104 case STREAM_SHA1_DIGEST:
106 case STREAM_GZIP_DATA:
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:
130 case STREAM_SHA512_DIGEST:
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 case STREAM_ADATA_BLOCK_HEADER:
153 return "contADATA-BLOCK-HEADER";
154 case STREAM_ADATA_RECORD_HEADER:
155 return "contADATA-RECORD-HEADER";
158 sprintf(buf, "%d", -stream);
163 switch (stream & STREAMMASK_TYPE) {
164 case STREAM_UNIX_ATTRIBUTES:
166 case STREAM_FILE_DATA:
168 case STREAM_WIN32_DATA:
170 case STREAM_WIN32_GZIP_DATA:
172 case STREAM_WIN32_COMPRESSED_DATA:
173 return "WIN32-COMPRESSED";
174 case STREAM_MD5_DIGEST:
176 case STREAM_SHA1_DIGEST:
178 case STREAM_GZIP_DATA:
180 case STREAM_COMPRESSED_DATA:
182 case STREAM_UNIX_ATTRIBUTES_EX:
183 return "UNIX-ATTR-EX";
184 case STREAM_RESTORE_OBJECT:
185 return "RESTORE-OBJECT";
186 case STREAM_SPARSE_DATA:
187 return "SPARSE-DATA";
188 case STREAM_SPARSE_GZIP_DATA:
189 return "SPARSE-GZIP";
190 case STREAM_SPARSE_COMPRESSED_DATA:
191 return "SPARSE-COMPRESSED";
192 case STREAM_PROGRAM_NAMES:
194 case STREAM_PROGRAM_DATA:
196 case STREAM_PLUGIN_NAME:
197 return "PLUGIN-NAME";
198 case STREAM_MACOS_FORK_DATA:
200 case STREAM_HFSPLUS_ATTRIBUTES:
201 return "HFSPLUS-ATTR";
202 case STREAM_SHA256_DIGEST:
204 case STREAM_SHA512_DIGEST:
206 case STREAM_SIGNED_DIGEST:
207 return "SIGNED-DIGEST";
208 case STREAM_ENCRYPTED_SESSION_DATA:
209 return "ENCRYPTED-SESSION-DATA";
210 case STREAM_ENCRYPTED_FILE_DATA:
211 return "ENCRYPTED-FILE";
212 case STREAM_ENCRYPTED_FILE_GZIP_DATA:
213 return "ENCRYPTED-GZIP";
214 case STREAM_ENCRYPTED_FILE_COMPRESSED_DATA:
215 return "ENCRYPTED-COMPRESSED";
216 case STREAM_ENCRYPTED_WIN32_DATA:
217 return "ENCRYPTED-WIN32-DATA";
218 case STREAM_ENCRYPTED_WIN32_GZIP_DATA:
219 return "ENCRYPTED-WIN32-GZIP";
220 case STREAM_ENCRYPTED_WIN32_COMPRESSED_DATA:
221 return "ENCRYPTED-WIN32-COMPRESSED";
222 case STREAM_ENCRYPTED_MACOS_FORK_DATA:
223 return "ENCRYPTED-MACOS-RSRC";
224 case STREAM_ADATA_BLOCK_HEADER:
225 return "ADATA-BLOCK-HEADER";
226 case STREAM_ADATA_RECORD_HEADER:
227 return "ADATA-RECORD-HEADER";
229 sprintf(buf, "%d", stream);
234 const char *stream_to_ascii_ex(char *buf, int stream, int fi)
237 return stream_to_ascii(buf, stream, fi);
239 const char *p = stream_to_ascii(buf, stream, fi);
243 * Return a new record entity
245 DEV_RECORD *new_record(void)
249 rec = (DEV_RECORD *)get_memory(sizeof(DEV_RECORD));
250 memset(rec, 0, sizeof(DEV_RECORD));
251 rec->data = get_pool_memory(PM_MESSAGE);
252 rec->wstate = st_none;
253 rec->rstate = st_none;
257 void empty_record(DEV_RECORD *rec)
260 rec->StartAddr = rec->Addr = 0;
261 rec->VolSessionId = rec->VolSessionTime = 0;
262 rec->FileIndex = rec->Stream = 0;
263 rec->data_len = rec->remainder = 0;
264 rec->state_bits &= ~(REC_PARTIAL_RECORD|REC_ADATA_EMPTY|REC_BLOCK_EMPTY|REC_NO_MATCH|REC_CONTINUATION);
266 rec->wstate = st_none;
267 rec->rstate = st_none;
268 rec->VolumeName = NULL;
272 * Free the record entity
275 void free_record(DEV_RECORD *rec)
277 Dmsg0(950, "Enter free_record.\n");
279 free_pool_memory(rec->data);
281 Dmsg0(950, "Data buf is freed.\n");
282 free_pool_memory((POOLMEM *)rec);
283 Dmsg0(950, "Leave free_record.\n");
286 void dump_record(DEV_RECORD *rec)
289 Dmsg11(100|DT_VOLUME, "Dump record %s 0x%p:\n\tStart=%lld addr=%lld #%d\n"
290 "\tVolSess: %ld:%ld\n"
292 "\tStream: 0x%lx\n\tLen: %ld\n\tData: %s\n",
293 rec, NPRT(rec->VolumeName),
294 rec->StartAddr, rec->Addr, rec->RecNum,
295 rec->VolSessionId, rec->VolSessionTime, rec->FileIndex,
296 rec->Stream, rec->data_len,
297 asciidump(rec->data, rec->data_len, buf, sizeof(buf)));
301 * Test if we can write whole record to the block
303 * Returns: false on failure
304 * true on success (all bytes can be written)
306 bool can_write_record_to_block(DEV_BLOCK *block, DEV_RECORD *rec)
310 remlen = block->buf_len - block->binbuf;
311 if (rec->remainder == 0) {
312 if (remlen >= WRITE_RECHDR_LENGTH) {
313 remlen -= WRITE_RECHDR_LENGTH;
314 rec->remainder = rec->data_len;
321 if (rec->remainder > 0 && remlen < rec->remainder) {
327 uint64_t get_record_address(DEV_RECORD *rec)
332 uint64_t get_record_start_address(DEV_RECORD *rec)
334 return rec->StartAddr;