-
Notifications
You must be signed in to change notification settings - Fork 256
/
win32.c
825 lines (710 loc) · 25.1 KB
/
win32.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2013-2013 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
License as published by the Free Software Foundation and included
in the file LICENSE.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
/*
* Windows specific functions.
*/
#include "bareos.h"
#if defined(HAVE_WIN32)
#include "find.h"
#include "lib/cbuf.h"
/*
* We need to analyze if a fileset contains onefs=no as option, because only then
* we need to snapshot submounted vmps
*/
bool win32_onefs_is_disabled(findFILESET *fileset)
{
findINCEXE *incexe;
for (int i = 0; i < fileset->include_list.size(); i++) {
incexe = (findINCEXE *)fileset->include_list.get(i);
/*
* Look through all files and check
*/
for (int j = 0; j < incexe->opts_list.size(); j++) {
findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
if (bit_is_set(FO_MULTIFS, fo->flags)) {
return true;
}
}
}
return false;
}
/*
* For VSS we need to know which windows drives are used, because we create a snapshot
* of all used drives. This function returns the number of used drives and fills
* szDrives with up to 26 (A..Z) drive names.
*/
int get_win32_driveletters(findFILESET *fileset, char *szDrives)
{
int i;
int nCount;
char *fname, ch;
char drive[4], dt[16];
struct stat sb;
dlistString *node;
findINCEXE *incexe;
/*
* szDrives must be at least 27 bytes long
* Can be already filled by plugin, so check that all
* letters are in upper case. There should be no duplicates.
*/
for (nCount = 0; nCount < 27 && szDrives[nCount]; nCount++) {
szDrives[nCount] = toupper(szDrives[nCount]);
}
/*
* First check if there are any non fixed drives in the list
* filled by the plugin. VSS can only snapshot fixed drives.
*/
for (nCount = 0; nCount < 27 && szDrives[nCount]; nCount++) {
bsnprintf(drive, sizeof(drive), "%c:\\", szDrives[nCount]);
if (drivetype(drive, dt, sizeof(dt))) {
if (bstrcmp(dt, "fixed")) {
continue;
}
/*
* Inline copy the rest of the string over the current
* drive letter.
*/
bstrinlinecpy(szDrives + nCount, szDrives + nCount + 1);
}
}
if (fileset) {
for (i = 0; i < fileset->include_list.size(); i++) {
incexe = (findINCEXE *)fileset->include_list.get(i);
/*
* Look through all files and check
*/
foreach_dlist(node, &incexe->name_list) {
fname = node->c_str();
/*
* See if the entry doesn't have the FILE_ATTRIBUTE_VOLUME_MOUNT_POINT flag set.
*/
if (stat(fname, &sb) == 0) {
if (sb.st_rdev & FILE_ATTRIBUTE_VOLUME_MOUNT_POINT) {
continue;
}
}
/*
* fname should match x:/
*/
if (strlen(fname) >= 2 && B_ISALPHA(fname[0]) && fname[1] == ':') {
/*
* VSS can only snapshot fixed drives.
*/
bstrncpy(drive, fname, sizeof(drive));
drive[2] = '\\';
drive[3] = '\0';
/*
* Lookup the drive type.
*/
if (drivetype(drive, dt, sizeof(dt))) {
if (bstrcmp(dt, "fixed")) {
/*
* Always add in uppercase
*/
ch = toupper(fname[0]);
/*
* If not found in string, add drive letter
*/
if (!strchr(szDrives, ch)) {
szDrives[nCount] = ch;
szDrives[nCount + 1] = 0;
nCount++;
}
}
}
}
}
}
}
return nCount;
}
/*
* For VSS we need to know which windows virtual mountpoints are used, because we create
* a snapshot of all used drives and virtual mountpoints. This function returns the number
* of used virtual mountpoints and fills szVmps with a list of all virtual mountpoints.
*/
int get_win32_virtualmountpoints(findFILESET *fileset, dlist **szVmps)
{
int i, cnt;
char *fname;
struct stat sb;
dlistString *node;
findINCEXE *incexe;
POOLMEM *devicename;
cnt = 0;
if (fileset) {
devicename = get_pool_memory(PM_FNAME);
for (i = 0; i < fileset->include_list.size(); i++) {
incexe = (findINCEXE *)fileset->include_list.get(i);
/*
* Look through all files and check
*/
foreach_dlist(node, &incexe->name_list) {
fname = node->c_str();
/*
* See if the entry has the FILE_ATTRIBUTE_VOLUME_MOUNT_POINT flag set.
*/
if (stat(fname, &sb) == 0) {
if (!(sb.st_rdev & FILE_ATTRIBUTE_VOLUME_MOUNT_POINT)) {
continue;
}
} else {
continue;
}
if (win32_get_vmp_devicename(fname, &devicename)) {
/*
* See if we need to allocate a new dlist.
*/
if (!cnt) {
if (!*szVmps) {
*szVmps = (dlist *)malloc(sizeof(dlist));
(*szVmps)->init();
}
}
(*szVmps)->append(new_dlistString(devicename));
cnt++;
}
}
}
free_pool_memory(devicename);
}
return cnt;
}
static inline bool wanted_drive_type(const char *drive, findINCEXE *incexe)
{
int i,j;
char dt[16];
findFOPTS *fo;
bool done = false;
bool wanted = true;
/*
* Lookup the drive type.
*/
if (!drivetype(drive, dt, sizeof(dt))) {
return false;
}
/*
* We start the loop with done set to false and wanted
* to true so when there are no drivetype selections we
* select any drivetype.
*/
for (i = 0; !done && i < incexe->opts_list.size(); i++) {
fo = (findFOPTS *)incexe->opts_list.get(i);
/*
* If there is any drivetype selection set the default
* selection to false.
*/
if (fo->drivetype.size()) {
wanted = false;
}
for (j = 0; !done && j < fo->drivetype.size(); j++) {
if (bstrcasecmp(dt, (char *)fo->drivetype.get(j))) {
wanted = true;
done = true;
}
}
}
return wanted;
}
bool expand_win32_fileset(findFILESET *fileset)
{
int i;
char *bp;
dlistString *node;
findINCEXE *incexe;
char drives[MAX_NAME_LENGTH];
for (i = 0; i < fileset->include_list.size(); i++) {
incexe = (findINCEXE *)fileset->include_list.get(i);
foreach_dlist(node, &incexe->name_list) {
Dmsg1(000, "Checking %s\n", node->c_str());
if (bstrcmp(node->c_str(), "/")) {
/*
* Request for auto expansion but no support for it.
*/
if (!p_GetLogicalDriveStringsA) {
return false;
}
/*
* we want to add all available local drives to our fileset
* if we have "/" specified in the fileset. We want to remove
* the "/" pattern itself that gets expanded into all
* available drives.
*/
incexe->name_list.remove(node);
if (p_GetLogicalDriveStringsA(sizeof(drives), drives) != 0) {
bp = drives;
while (bp && strlen(bp) > 0) {
/*
* Apply any drivetype selection to the currently
* processed item.
*/
if (wanted_drive_type(bp, incexe)) {
if (*(bp + 2) == '\\') {
*(bp + 2) = '/'; /* 'x:\' -> 'x:/' */
}
Dmsg1(000, "adding drive %s\n", bp);
incexe->name_list.append(new_dlistString(bp));
}
if ((bp = strchr(bp, '\0'))) {
bp++;
}
}
} else {
return false;
}
/*
* No need to search further in the include list when we have
* found what we were looking for.
*/
break;
}
}
}
return true;
}
static inline int count_include_list_file_entries(FF_PKT *ff)
{
int cnt = 0;
findFILESET *fileset;
findINCEXE *incexe;
fileset = ff->fileset;
if (fileset) {
for (int i = 0; i < fileset->include_list.size(); i++) {
incexe = (findINCEXE *)fileset->include_list.get(i);
cnt += incexe->name_list.size();
}
}
return cnt;
}
/*
* Automatically exclude all files and paths defined in Registry Key
* "SYSTEM\\CurrentControlSet\\Control\\BackupRestore\\FilesNotToBackup"
*
* Files/directories with wildcard characters are added to an
* options block with flags "exclude=yes" as "wild=dir/file".
* This options block is created and prepended by us.
*
*/
#define MAX_VALUE_NAME 16383
#define REGISTRY_KEY "SYSTEM\\CurrentControlSet\\Control\\BackupRestore\\FilesNotToBackup"
bool exclude_win32_not_to_backup_registry_entries(JCR *jcr, FF_PKT *ff)
{
bool retval = false;
uint32_t wild_count = 0;
DWORD retCode;
HKEY hKey;
/*
* If we do not have "File = " directives (e.g. only plugin calls)
* we do not create excludes for the NotForBackup RegKey
*/
if (count_include_list_file_entries(ff) == 0 ) {
Qmsg(jcr, M_INFO, 1, _("Fileset has no \"File=\" directives, ignoring FilesNotToBackup Registry key\n"));
return true;
}
/*
* If autoexclude is set to no in fileset options somewhere, we do not
* automatically exclude files from FilesNotToBackup Registry Key
*/
for (int i = 0; i < ff->fileset->include_list.size(); i++) {
findINCEXE *incexe = (findINCEXE *)ff->fileset->include_list.get(i);
for (int j = 0; j < incexe->opts_list.size(); j++) {
findFOPTS *fo = (findFOPTS *)incexe->opts_list.get(j);
if (bit_is_set(FO_NO_AUTOEXCL, fo->flags)) {
Qmsg(jcr, M_INFO, 1, _("Fileset has autoexclude disabled, ignoring FilesNotToBackup Registry key\n"));
return true;
}
}
}
retCode = RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT(REGISTRY_KEY), 0, KEY_READ, &hKey);
if (retCode == ERROR_SUCCESS ) {
POOL_MEM achClass(PM_MESSAGE),
achValue(PM_MESSAGE),
dwKeyEn(PM_MESSAGE),
expandedKey(PM_MESSAGE),
destination(PM_MESSAGE);
DWORD cchClassName; /* Size of class string */
DWORD cSubKeys = 0; /* Number of subkeys */
DWORD cbMaxSubKey; /* Longest subkey size */
DWORD cchMaxClass; /* Longest class string */
DWORD cValues; /* Number of values for key */
DWORD cchMaxValue; /* Longest value name */
DWORD cbMaxValueData; /* Longest value data */
DWORD cbSecurityDescriptor; /* Size of security descriptor */
FILETIME ftLastWriteTime; /* Last write time */
DWORD cchValue; /* Size of value string */
findINCEXE *include;
/*
* Make sure the variable are big enough to contain the data.
*/
achClass.check_size(MAX_PATH);
/*
* Get the class name and the value count.
*/
cchClassName = achClass.size();
retCode = RegQueryInfoKey(hKey, /* Key handle */
achClass.c_str(), /* Buffer for class name */
&cchClassName, /* Size of class string */
NULL, /* Reserved */
&cSubKeys, /* Number of subkeys */
&cbMaxSubKey, /* Longest subkey size */
&cchMaxClass, /* Longest class string */
&cValues, /* Number of values for this key */
&cchMaxValue, /* Longest value name */
&cbMaxValueData, /* Longest value data */
&cbSecurityDescriptor, /* Security descriptor */
&ftLastWriteTime); /* Last write time */
if (cValues) {
findFOPTS *fo;
/*
* Prepare include block to do exclusion via wildcards in options
*/
if (ff->fileset->include_list.size() == 0) {
Dmsg0(100, "Fileset has no include block, inserting one at first position\n");
new_preinclude(ff->fileset);
} else {
Dmsg0(100, "Fileset has include block(s), using first one\n");
}
include = (findINCEXE*)ff->fileset->include_list.get(0);
if (include->opts_list.size() == 0) {
/*
* Create new options block in include block for the wildcard excludes
*/
Dmsg0(100, "prepending new options block\n");
new_options(ff, ff->fileset->incexe);
} else {
Dmsg0(100, "reusing existing options block\n");
}
fo = (findFOPTS *)include->opts_list.get(0);
set_bit(FO_EXCLUDE, fo->flags); /* exclude = yes */
set_bit(FO_IGNORECASE, fo->flags); /* ignore case = yes */
/*
* Make sure the variables are big enough to contain the data.
*/
achValue.check_size(MAX_VALUE_NAME);
dwKeyEn.check_size(MAX_PATH);
expandedKey.check_size(MAX_PATH);
destination.check_size(MAX_PATH);
for (unsigned int i = 0; i < cValues; i++) {
pm_strcpy(achValue, "");
cchValue = achValue.size();
retCode = RegEnumValue(hKey, i, achValue.c_str(), &cchValue, NULL, NULL, NULL, NULL);
if (retCode == ERROR_SUCCESS ){
DWORD dwLen;
Dmsg2(100 , "(%d) \"%s\" : \n", i + 1, achValue.c_str());
dwLen = dwKeyEn.size();
retCode = RegQueryValueEx(hKey, achValue.c_str(), 0, NULL, (LPBYTE)dwKeyEn.c_str(), &dwLen);
if (retCode == ERROR_SUCCESS) {
char *lpValue;
/*
* Iterate over each string, expand the %xxx% variables and
* process them for addition to exclude block or wildcard options block
*/
for (lpValue = dwKeyEn.c_str();
lpValue && *lpValue;
lpValue = strchr(lpValue, '\0') + 1) {
char *s, *d;
ExpandEnvironmentStrings(lpValue, expandedKey.c_str(), expandedKey.size());
Dmsg1(100, " \"%s\"\n", expandedKey.c_str());
/*
* Never add single character entries. Probably known buggy DRM Entry in Windows XP / 2003
*/
if (strlen(expandedKey.c_str()) <= 1) {
Dmsg0(100, TEXT("Single character entry ignored. Probably reading buggy DRM entry on Windows XP/2003 SP 1 and later\n"));
} else {
pm_strcpy(destination, "");
/*
* Do all post processing.
* - Replace '\' by '/'
* - Strip any trailing /s patterns.
* - Check if wildcards are used.
*/
s = expandedKey.c_str();
d = destination.c_str();
/*
* Begin with \ means all drives
*/
if (*s == '\\') {
d += pm_strcpy(destination, "[A-Z]:");
}
while (*s) {
switch (*s) {
case '\\':
*d++ = '/';
s++;
break;
case ' ':
if (bstrcmp(s, " /s")) {
*d = '\0';
*s = '\0';
continue;
}
/* FALLTHROUGH */
default:
*d++ = *s++;
break;
}
}
*d = '\0';
Dmsg1(100, " -> \"%s\"\n", destination.c_str() );
fo->wild.append(bstrdup(destination.c_str()));
wild_count++;
}
}
} else {
Dmsg0(100, TEXT("RegGetValue failed \n"));
}
}
}
}
Qmsg(jcr, M_INFO, 0, _("Created %d wildcard excludes from FilesNotToBackup Registry key\n"), wild_count);
RegCloseKey(hKey);
retval = true;
} else {
Qmsg(jcr, M_ERROR, 0, _("Failed to open FilesNotToBackup Registry Key\n"));
}
return retval;
}
/*
* Windows specific code for restoring EFS data.
*/
struct CP_THREAD_SAVE_DATA {
uint32_t data_len; /* Length of Data */
POOLMEM *data; /* Data */
};
struct CP_THREAD_CTX {
BFILE *bfd; /* Filehandle */
int nr_save_elements; /* Number of save items in save_data */
CP_THREAD_SAVE_DATA *save_data; /* To save data (cached structure build during restore) */
circbuf *cb; /* Circular buffer for passing work to copy thread */
bool started; /* Copy thread consuming data */
bool flushed; /* Copy thread flushed data */
pthread_t thread_id; /* Id of copy thread */
pthread_mutex_t lock; /* Lock the structure */
pthread_cond_t start; /* Start consuming data from the Circular buffer */
pthread_cond_t flush; /* Flush data from the Circular buffer */
};
/*
* Callback method for WriteEncryptedFileRaw()
*/
static DWORD WINAPI receive_efs_data(PBYTE pbData, PVOID pvCallbackContext, PULONG ulLength)
{
CP_THREAD_SAVE_DATA *save_data;
CP_THREAD_CTX *context = (CP_THREAD_CTX *)pvCallbackContext;
/*
* Dequeue an item from the circular buffer.
*/
save_data = (CP_THREAD_SAVE_DATA *)context->cb->dequeue();
if (save_data) {
if (save_data->data_len > *ulLength) {
Dmsg2(100, "Restore of data bigger then allowed EFS buffer %d vs %d\n", save_data->data_len, *ulLength);
*ulLength = 0;
} else {
memcpy(pbData, save_data->data, save_data->data_len);
*ulLength = save_data->data_len;
}
} else {
*ulLength = 0;
}
return ERROR_SUCCESS;
}
/*
* Copy thread cancel handler.
*/
static void copy_cleanup_thread(void *data)
{
CP_THREAD_CTX *context = (CP_THREAD_CTX *)data;
pthread_mutex_unlock(&context->lock);
}
/*
* Actual copy thread that restores EFS data.
*/
static void *copy_thread(void *data)
{
CP_THREAD_CTX *context = (CP_THREAD_CTX *)data;
if (pthread_mutex_lock(&context->lock) != 0) {
goto bail_out;
}
/*
* When we get canceled make sure we run the cleanup function.
*/
pthread_cleanup_push(copy_cleanup_thread, data);
while (1) {
/*
* Wait for the moment we are supposed to start.
* We are signalled by the restore thread.
*/
pthread_cond_wait(&context->start, &context->lock);
context->started = true;
pthread_mutex_unlock(&context->lock);
if (p_WriteEncryptedFileRaw((PFE_IMPORT_FUNC)receive_efs_data, context, context->bfd->pvContext)) {
goto bail_out;
}
/*
* Need to synchronize the main thread and this one so the main thread cannot miss the conditional signal.
*/
if (pthread_mutex_lock(&context->lock) != 0) {
goto bail_out;
}
/*
* Signal the main thread we flushed the data and the BFD can be closed.
*/
pthread_cond_signal(&context->flush);
context->started = false;
context->flushed = true;
}
pthread_cleanup_pop(1);
bail_out:
return NULL;
}
/*
* Create a copy thread that restores the EFS data.
*/
static inline bool setup_copy_thread(JCR *jcr, BFILE *bfd)
{
int nr_save_elements;
CP_THREAD_CTX *new_context;
new_context = (CP_THREAD_CTX *)malloc(sizeof(CP_THREAD_CTX));
new_context->started = false;
new_context->flushed = false;
new_context->cb = New(circbuf);
nr_save_elements = new_context->cb->capacity();
new_context->save_data = (CP_THREAD_SAVE_DATA *)malloc(nr_save_elements * sizeof(CP_THREAD_SAVE_DATA));
memset(new_context->save_data, 0, nr_save_elements * sizeof(CP_THREAD_SAVE_DATA));
new_context->nr_save_elements = nr_save_elements;
if (pthread_mutex_init(&new_context->lock, NULL) != 0) {
goto bail_out;
}
if (pthread_cond_init(&new_context->start, NULL) != 0) {
pthread_mutex_destroy(&new_context->lock);
goto bail_out;
}
if (pthread_create(&new_context->thread_id, NULL, copy_thread, (void *)new_context) != 0) {
pthread_cond_destroy(&new_context->start);
pthread_mutex_destroy(&new_context->lock);
goto bail_out;
}
jcr->cp_thread = new_context;
return true;
bail_out:
free(new_context->save_data);
delete new_context->cb;
free(new_context);
return false;
}
/*
* Send data to the copy thread that restores EFS data.
*/
int win32_send_to_copy_thread(JCR *jcr, BFILE *bfd, char *data, const int32_t length)
{
circbuf *cb;
int slotnr;
CP_THREAD_SAVE_DATA *save_data;
if (!p_WriteEncryptedFileRaw) {
Jmsg0(jcr, M_FATAL, 0, _("Encrypted file restore but no EFS support functions\n"));
}
/*
* If no copy thread started do it now.
*/
if (!jcr->cp_thread) {
if (!setup_copy_thread(jcr, bfd)) {
Jmsg0(jcr, M_FATAL, 0, _("Failed to start encrypted data restore copy thread\n"));
return -1;
}
}
cb = jcr->cp_thread->cb;
/*
* See if the bfd changed.
*/
if (jcr->cp_thread->bfd != bfd) {
jcr->cp_thread->bfd = bfd;
}
/*
* Find out which next slot will be used on the Circular Buffer.
* The method will block when the circular buffer is full until a slot is available.
*/
slotnr = cb->next_slot();
save_data = &jcr->cp_thread->save_data[slotnr];
/*
* If this is the first time we use this slot we need to allocate some memory.
*/
if (!save_data->data) {
save_data->data = get_pool_memory(PM_BSOCK);
}
save_data->data = check_pool_memory_size(save_data->data, length + 1);
memcpy(save_data->data, data, length);
save_data->data_len = length;
cb->enqueue(save_data);
/*
* Signal the copy thread its time to start if it didn't start yet.
*/
if (!jcr->cp_thread->started) {
pthread_cond_signal(&jcr->cp_thread->start);
}
return length;
}
/*
* Flush the copy thread so we can close the BFD.
*/
void win32_flush_copy_thread(JCR *jcr)
{
CP_THREAD_CTX *context = jcr->cp_thread;
if (pthread_mutex_lock(&context->lock) != 0) {
return;
}
/*
* In essence the flush should work in one shot but be a bit more conservative.
*/
while (!context->flushed) {
/*
* Tell the copy thread to flush out all data.
*/
context->cb->flush();
/*
* Wait for the copy thread to say it flushed the data out.
*/
pthread_cond_wait(&context->flush, &context->lock);
}
context->flushed = false;
pthread_mutex_unlock(&context->lock);
}
/*
* Cleanup all data allocated for the copy thread.
*/
void win32_cleanup_copy_thread(JCR *jcr)
{
int slotnr;
/*
* Stop the copy thread.
*/
if (!pthread_equal(jcr->cp_thread->thread_id, pthread_self())) {
pthread_cancel(jcr->cp_thread->thread_id);
pthread_join(jcr->cp_thread->thread_id, NULL);
}
/*
* Free all data allocated along the way.
*/
for (slotnr = 0; slotnr < jcr->cp_thread->nr_save_elements; slotnr++) {
if (jcr->cp_thread->save_data[slotnr].data) {
free_pool_memory(jcr->cp_thread->save_data[slotnr].data);
}
}
free(jcr->cp_thread->save_data);
delete jcr->cp_thread->cb;
free(jcr->cp_thread);
jcr->cp_thread = NULL;
}
#endif