-
-
Notifications
You must be signed in to change notification settings - Fork 267
Expand file tree
/
Copy pathmvol.cpp
More file actions
2202 lines (1849 loc) · 53.5 KB
/
mvol.cpp
File metadata and controls
2202 lines (1849 loc) · 53.5 KB
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
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* PROGRAM: JRD Backup and Restore Program
* MODULE: multivol.cpp
* DESCRIPTION:
*
* The contents of this file are subject to the Interbase Public
* License Version 1.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy
* of the License at http://www.Inprise.com/IPL.html
*
* Software distributed under the License is distributed on an
* "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
* or implied. See the License for the specific language governing
* rights and limitations under the License.
*
* The Original Code was created by Inprise Corporation
* and its predecessors. Portions created by Inprise Corporation are
* Copyright (C) Inprise Corporation.
*
* All Rights Reserved.
* Contributor(s): ______________________________________.
*
* 2002.10.29 Sean Leyne - Removed obsolete "Netware" port
*
*/
#include "firebird.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#ifdef TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# ifdef HAVE_SYS_TIME_H
# include <sys/time.h>
# else
# include <time.h>
# endif
#endif
#include "../burp/burp.h"
#include "../burp/burp_proto.h"
#include "../burp/mvol_proto.h"
#include "../burp/split/spit.h"
#include "../burp/BurpTasks.h"
#include "../yvalve/gds_proto.h"
#include "../common/gdsassert.h"
#include "../common/os/os_utils.h"
#include <fcntl.h>
#include <sys/types.h>
#ifdef HAVE_IO_H
#include <io.h> // isatty
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include "../common/os/guid.h"
#include "../common/classes/ImplementHelper.h"
#include "../common/classes/GetPlugins.h"
#include "../common/classes/auto.h"
#include "../common/classes/SafeArg.h"
#include "../common/StatusHolder.h"
#include "../common/db_alias.h"
#include "../common/status.h"
#include "../common/classes/zip.h"
using MsgFormat::SafeArg;
using Firebird::FbLocalStatus;
using namespace Burp;
inline constexpr int open_mask = 0666;
#ifdef WIN_NT
inline constexpr const char* TERM_INPUT = "CONIN$";
inline constexpr const char* TERM_OUTPUT = "CONOUT$";
#else
inline constexpr const char* TERM_INPUT = "/dev/tty";
inline constexpr const char* TERM_OUTPUT = "/dev/tty";
#endif
static int mvol_read(int*, UCHAR**);
static FB_UINT64 mvol_fini_read(BurpGlobals*);
static void mvol_init_read(BurpGlobals*, const char*, USHORT*, int*, UCHAR**);
static UCHAR mvol_write(const UCHAR, int*, UCHAR**);
static const UCHAR* mvol_write_block(BurpGlobals*, const UCHAR*, ULONG);
static FB_UINT64 mvol_fini_write(BurpGlobals*, int*, UCHAR**);
static void mvol_init_write(BurpGlobals*, const char*, int*, UCHAR**);
static void brio_fini(BurpGlobals*);
static inline constexpr int MAX_HEADER_SIZE = 512;
static inline constexpr int ZC_BUFSIZE = IO_BUFFER_SIZE;
static inline int get(BurpGlobals* tdgbl)
{
if (tdgbl->mvol_io_cnt <= 0)
mvol_read(NULL, NULL);
return (--tdgbl->mvol_io_cnt >= 0 ? *tdgbl->mvol_io_ptr++ : 255);
}
static inline void put(BurpGlobals* tdgbl, UCHAR c) noexcept
{
--tdgbl->mvol_io_cnt;
*tdgbl->mvol_io_ptr++ = c;
}
#ifdef DEBUG
static UCHAR debug_on = 0; // able to turn this on in debug mode
#endif
#ifdef HAVE_ZLIB_H
static Firebird::InitInstance<Firebird::ZLib> zlib;
#endif // HAVE_ZLIB_H
static void bad_attribute(int, USHORT);
static void file_not_empty();
static SLONG get_numeric();
static int get_text(UCHAR*, SSHORT);
static void prompt_for_name(SCHAR*, int);
static void put_asciz(SCHAR, const SCHAR*);
static void put_numeric(SCHAR, int);
static bool read_header(DESC, ULONG*, USHORT*, bool);
static bool write_header(DESC, ULONG, bool);
static DESC next_volume(DESC, ULONG, bool);
static void os_read(int*, UCHAR**);
static void crypt_write_block(BurpGlobals*, const UCHAR*, FB_SIZE_T, bool);
static ULONG crypt_read_block(BurpGlobals*, UCHAR*, FB_SIZE_T);
static void zip_write_block(BurpGlobals*, const UCHAR*, FB_SIZE_T, bool);
static ULONG unzip_read_block(BurpGlobals*, UCHAR*, FB_SIZE_T);
// Portion of data passed to crypt plugin
inline constexpr ULONG CRYPT_STEP = 256;
class DbInfo final : public Firebird::RefCntIface<Firebird::IDbCryptInfoImpl<DbInfo, Firebird::CheckStatusWrapper> >
{
public:
DbInfo(BurpGlobals* bg)
: tdgbl(bg)
{ }
// IDbCryptInfo implementation
const char* getDatabaseFullPath(Firebird::CheckStatusWrapper*)
{
return tdgbl->gbl_database_file_name;
}
private:
BurpGlobals* tdgbl;
};
struct BurpCrypt
{
BurpCrypt()
: crypt_plugin(NULL), db_info(NULL), holder_plugin(NULL), crypt_callback(NULL)
{ }
~BurpCrypt()
{
if (crypt_plugin)
Firebird::PluginManagerInterfacePtr()->releasePlugin(crypt_plugin);
if (holder_plugin)
Firebird::PluginManagerInterfacePtr()->releasePlugin(holder_plugin);
}
Firebird::IDbCryptPlugin* crypt_plugin;
Firebird::RefPtr<DbInfo> db_info;
Firebird::IKeyHolderPlugin* holder_plugin;
Firebird::ICryptKeyCallback* crypt_callback;
};
//____________________________________________________________
//
//
static void calc_hash(Firebird::string& valid, Firebird::IDbCryptPlugin* plugin)
{
// crypt verifier
const char* sample = "0123456789ABCDEF";
char result[16];
FbLocalStatus sv;
plugin->encrypt(&sv, sizeof(result), sample, result);
check(&sv);
// calculate its hash
const Firebird::string verifier(result, sizeof(result));
Firebird::Sha1::hashBased64(valid, verifier);
}
//____________________________________________________________
//
//
static Firebird::IKeyHolderPlugin* mvol_get_holder(BurpGlobals* tdgbl, Firebird::RefPtr<const Firebird::Config>& config)
{
fb_assert(tdgbl->gbl_sw_keyholder);
if (!tdgbl->gbl_crypt)
{
Firebird::GetPlugins<Firebird::IKeyHolderPlugin>
keyControl(Firebird::IPluginManager::TYPE_KEY_HOLDER, config, tdgbl->gbl_sw_keyholder);
if (!keyControl.hasData())
(Firebird::Arg::Gds(isc_no_keyholder_plugin) << tdgbl->gbl_sw_keyholder).raise();
BurpCrypt* g = tdgbl->gbl_crypt = FB_NEW_POOL(tdgbl->getPool()) BurpCrypt;
g->holder_plugin = keyControl.plugin();
g->holder_plugin->addRef();
// Also do not forget about keys from services manager
Firebird::ICryptKeyCallback* cb = tdgbl->uSvc->getCryptCallback();
if (cb)
g->holder_plugin->keyCallback(&tdgbl->throwStatus, cb);
}
return tdgbl->gbl_crypt->holder_plugin;
}
//____________________________________________________________
//
//
Firebird::ICryptKeyCallback* MVOL_get_crypt(BurpGlobals* tdgbl)
{
fb_assert(tdgbl->gbl_sw_keyholder);
if (!tdgbl->gbl_crypt)
{
// Get per-DB config
Firebird::PathName dummy;
Firebird::RefPtr<const Firebird::Config> config;
expandDatabaseName(tdgbl->gbl_database_file_name, dummy, &config);
mvol_get_holder(tdgbl, config);
}
BurpCrypt* g = tdgbl->gbl_crypt;
if (!g->crypt_callback)
{
FbLocalStatus status;
g->crypt_callback = g->holder_plugin->chainHandle(&status);
check(&status);
}
return g->crypt_callback;
}
//____________________________________________________________
//
//
static void start_crypt(BurpGlobals* tdgbl)
{
fb_assert(tdgbl->gbl_sw_keyholder);
if (tdgbl->gbl_crypt && tdgbl->gbl_crypt->crypt_plugin)
return;
FbLocalStatus status;
// Get per-DB config
Firebird::PathName dummy;
Firebird::RefPtr<const Firebird::Config> config;
expandDatabaseName(tdgbl->gbl_database_file_name, dummy, &config);
// Prepare key holders
Firebird::IKeyHolderPlugin* keyHolder = mvol_get_holder(tdgbl, config);
// Load crypt plugin
if (!tdgbl->mvol_crypt)
tdgbl->mvol_crypt = tdgbl->gbl_sw_crypt;
if (!tdgbl->mvol_crypt)
BURP_error(378, true);
Firebird::GetPlugins<Firebird::IDbCryptPlugin>
cryptControl(Firebird::IPluginManager::TYPE_DB_CRYPT, config, tdgbl->mvol_crypt);
if (!cryptControl.hasData())
(Firebird::Arg::Gds(isc_no_crypt_plugin) << tdgbl->mvol_crypt).raise();
Firebird::RefPtr<DbInfo> dbInfo(FB_NEW DbInfo(tdgbl));
Firebird::IDbCryptPlugin* p = cryptControl.plugin();
p->setInfo(&status, dbInfo);
if (!status.isSuccess())
{
const ISC_STATUS* v = status->getErrors();
if (v[0] == isc_arg_gds && v[1] != isc_arg_end && v[1] != isc_interface_version_too_old)
Firebird::status_exception::raise(&status);
}
// Initialize key in crypt plugin
p->setKey(&status, 1, &keyHolder, tdgbl->mvol_keyname);
check(&status);
// Validate hash
if (tdgbl->gbl_key_hash[0])
{
Firebird::string hash;
calc_hash(hash, p);
if (hash != tdgbl->gbl_key_hash)
(Firebird::Arg::Gds(isc_bad_crypt_key) << tdgbl->mvol_keyname).raise();
}
// crypt plugin is ready
BurpCrypt* g = tdgbl->gbl_crypt;
g->db_info.moveFrom(dbInfo);
g->crypt_plugin = p;
p->addRef();
}
//____________________________________________________________
//
//
static void crypt_write_block(BurpGlobals* tdgbl, const UCHAR* buffer, FB_SIZE_T buffer_length, bool flash)
{
if (!(tdgbl->gbl_sw_keyholder))
{
mvol_write_block(tdgbl, buffer, buffer_length);
return;
}
start_crypt(tdgbl);
fb_assert(tdgbl->gbl_crypt);
while (buffer_length)
{
ULONG step = MIN(buffer_length + tdgbl->gbl_crypt_left, ZC_BUFSIZE);
fb_assert(CRYPT_STEP < ZC_BUFSIZE);
ULONG move = step - tdgbl->gbl_crypt_left;
memcpy(tdgbl->gbl_crypt_buffer + tdgbl->gbl_crypt_left, buffer, move);
buffer_length -= move;
buffer += move;
tdgbl->gbl_crypt_left = step % CRYPT_STEP;
step -= tdgbl->gbl_crypt_left;
if (flash && buffer_length == 0 && tdgbl->gbl_crypt_left)
{
step += CRYPT_STEP;
tdgbl->gbl_crypt_left = 0;
}
FbLocalStatus status;
for (ULONG offset = 0; offset < step; offset += CRYPT_STEP)
{
UCHAR* b = &tdgbl->gbl_crypt_buffer[offset];
tdgbl->gbl_crypt->crypt_plugin->encrypt(&status, CRYPT_STEP, b, b);
check(&status);
}
mvol_write_block(tdgbl, tdgbl->gbl_crypt_buffer, step);
memmove(tdgbl->gbl_crypt_buffer, &tdgbl->gbl_crypt_buffer[step], tdgbl->gbl_crypt_left);
}
}
//____________________________________________________________
//
//
static ULONG crypt_read_block(BurpGlobals* tdgbl, UCHAR* buffer, FB_SIZE_T buffer_length)
{
ULONG& length = tdgbl->gbl_crypt_left;
while (length < (tdgbl->gbl_key_hash[0] ? CRYPT_STEP : 1))
{
// free bytes in gbl_crypt_buffer
ULONG count = ZC_BUFSIZE - length;
UCHAR* endPtr = &tdgbl->gbl_crypt_buffer[length];
// If IO buffer empty, reload it
if (tdgbl->blk_io_cnt <= 0)
{
*endPtr++ = mvol_read(&tdgbl->blk_io_cnt, &tdgbl->blk_io_ptr);
--count;
++length;
}
// Copy data from the IO buffer
const ULONG n = MIN(count, (ULONG) tdgbl->blk_io_cnt);
memcpy(endPtr, tdgbl->blk_io_ptr, n);
endPtr += n;
length += n;
// Move IO pointer
tdgbl->blk_io_cnt -= n;
tdgbl->blk_io_ptr += n;
}
UCHAR* ptr = tdgbl->gbl_crypt_buffer;
buffer_length = MIN(length, buffer_length);
if (!tdgbl->gbl_key_hash[0])
{
memcpy(buffer, ptr, buffer_length);
}
else
{
start_crypt(tdgbl);
fb_assert(tdgbl->gbl_crypt);
ULONG left = buffer_length % CRYPT_STEP;
buffer_length -= left;
FbLocalStatus status;
for (ULONG offset = 0; offset < buffer_length; offset += CRYPT_STEP)
{
tdgbl->gbl_crypt->crypt_plugin->decrypt(&status, CRYPT_STEP, &ptr[offset], &buffer[offset]);
check(&status);
}
}
// Some data may stay in gbl_crypt_buffer for next time
length -= buffer_length;
memmove(ptr, ptr + buffer_length, length);
return buffer_length;
}
//____________________________________________________________
//
//
static ULONG unzip_read_block(BurpGlobals* tdgbl, UCHAR* buffer, FB_SIZE_T buffer_length)
{
if (!tdgbl->gbl_sw_zip)
{
return crypt_read_block(tdgbl, buffer, buffer_length);
}
#ifdef HAVE_ZLIB_H
z_stream& strm = tdgbl->gbl_stream;
strm.avail_out = buffer_length;
strm.next_out = buffer;
for (;;)
{
if (strm.avail_in)
{
#ifdef COMPRESS_DEBUG
fprintf(stderr, "Data to inflate %d port %p\n", strm.avail_in, port);
#if COMPRESS_DEBUG > 1
for (unsigned n = 0; n < strm.avail_in; ++n) fprintf(stderr, "%02x ", strm.next_in[n]);
fprintf(stderr, "\n");
#endif
#endif
ULONG wasAvail = strm.avail_out;
int ret = zlib().inflate(&strm, Z_NO_FLUSH);
if (ret == Z_DATA_ERROR && wasAvail != strm.avail_out)
ret = Z_OK;
if (ret != Z_OK)
{
#ifdef COMPRESS_DEBUG
fprintf(stderr, "Inflate error %d\n", ret);
#endif
BURP_error(379, true, SafeArg() << ret);
}
#ifdef COMPRESS_DEBUG
fprintf(stderr, "Inflated data %d\n", buffer_length - strm.avail_out);
#if COMPRESS_DEBUG > 1
for (unsigned n = 0; n < buffer_length - strm.avail_out; ++n) fprintf(stderr, "%02x ", buffer[n]);
fprintf(stderr, "\n");
#endif
#endif
if (strm.next_out != buffer)
break;
UCHAR* compressed = tdgbl->gbl_decompress;
if (strm.next_in != compressed)
{
memmove(compressed, strm.next_in, strm.avail_in);
strm.next_in = compressed;
}
}
else
strm.next_in = tdgbl->gbl_decompress;
ULONG l = ZC_BUFSIZE - strm.avail_in;
l = crypt_read_block(tdgbl, strm.next_in, l);
strm.avail_in += l;
}
return buffer_length - strm.avail_out;
#else
(Firebird::Arg::Gds(isc_random) << "No inflate support").raise();
#endif
}
static void zip_write_block(BurpGlobals* tdgbl, const UCHAR* buffer, FB_SIZE_T buffer_length, bool flash)
{
if (!tdgbl->gbl_sw_zip)
{
crypt_write_block(tdgbl, buffer, buffer_length, flash);
return;
}
#ifdef HAVE_ZLIB_H
z_stream& strm = tdgbl->gbl_stream;
strm.avail_in = buffer_length;
strm.next_in = (Bytef*)buffer;
UCHAR* compressed = tdgbl->gbl_decompress;
if (!strm.next_out)
{
strm.avail_out = ZC_BUFSIZE;
strm.next_out = (Bytef*)compressed;
}
bool expectMoreOut = flash;
while (strm.avail_in || expectMoreOut)
{
#ifdef COMPRESS_DEBUG
fprintf(stderr, "Data to deflate %d port %p\n", strm.avail_in, port);
#if COMPRESS_DEBUG>1
for (unsigned n = 0; n < strm.avail_in; ++n) fprintf(stderr, "%02x ", strm.next_in[n]);
fprintf(stderr, "\n");
#endif
#endif
int ret = zlib().deflate(&strm, flash ? Z_FULL_FLUSH : Z_NO_FLUSH);
if (ret == Z_BUF_ERROR)
ret = 0;
if (ret != 0)
{
#ifdef COMPRESS_DEBUG
fprintf(stderr, "Deflate error %d\n", ret);
#endif
BURP_error(380, true, SafeArg() << ret);
}
#ifdef COMPRESS_DEBUG
fprintf(stderr, "Deflated data %d\n", ZC_BUFSIZE - strm.avail_out);
#if COMPRESS_DEBUG>1
for (unsigned n = 0; n < port->port_buff_size - strm.avail_out; ++n)
fprintf(stderr, "%02x ", compressed[n]);
fprintf(stderr, "\n");
#endif
#endif
expectMoreOut = !strm.avail_out;
if ((ZC_BUFSIZE != strm.avail_out) && (flash || !strm.avail_out))
{
crypt_write_block(tdgbl, compressed, ZC_BUFSIZE - strm.avail_out, flash);
strm.avail_out = ZC_BUFSIZE;
strm.next_out = (Bytef*)compressed;
}
}
#else
(Firebird::Arg::Gds(isc_random) << "No deflate support").raise();
#endif
}
//____________________________________________________________
//
//
FB_UINT64 MVOL_fini_read()
{
BurpGlobals* tdgbl = BurpGlobals::getSpecific();
#ifdef HAVE_ZLIB_H
if (tdgbl->gbl_sw_zip)
{
zlib().inflateEnd(&tdgbl->gbl_stream);
}
#endif
brio_fini(tdgbl);
return mvol_fini_read(tdgbl);
}
FB_UINT64 mvol_fini_read(BurpGlobals* tdgbl)
{
if (!tdgbl->stdIoMode)
{
close_platf(tdgbl->file_desc);
}
for (burp_fil* file = tdgbl->gbl_sw_backup_files; file; file = file->fil_next)
{
if (file->fil_fd == tdgbl->file_desc)
{
file->fil_fd = INVALID_HANDLE_VALUE;
}
}
tdgbl->file_desc = INVALID_HANDLE_VALUE;
BURP_free(tdgbl->mvol_io_buffer);
tdgbl->mvol_io_buffer = NULL;
tdgbl->blk_io_cnt = 0;
tdgbl->blk_io_ptr = NULL;
return tdgbl->mvol_cumul_count;
}
//____________________________________________________________
//
//
FB_UINT64 MVOL_fini_write()
{
BurpGlobals* tdgbl = BurpGlobals::getSpecific();
zip_write_block(tdgbl, tdgbl->gbl_compress_buffer, tdgbl->gbl_io_ptr - tdgbl->gbl_compress_buffer, true);
#ifdef HAVE_ZLIB_H
if (tdgbl->gbl_sw_zip)
{
zlib().deflateEnd(&tdgbl->gbl_stream);
}
#endif
brio_fini(tdgbl);
return mvol_fini_write(tdgbl, &tdgbl->blk_io_cnt, &tdgbl->blk_io_ptr);
}
FB_UINT64 mvol_fini_write(BurpGlobals* tdgbl, int* io_cnt, UCHAR** io_ptr)
{
mvol_write(rec_end, io_cnt, io_ptr);
flush_platf(tdgbl->file_desc);
if (!tdgbl->stdIoMode)
{
close_platf(tdgbl->file_desc);
}
for (burp_fil* file = tdgbl->gbl_sw_backup_files; file; file = file->fil_next)
{
if (file->fil_fd == tdgbl->file_desc)
{
file->fil_fd = INVALID_HANDLE_VALUE;
}
}
tdgbl->file_desc = INVALID_HANDLE_VALUE;
BURP_free(tdgbl->mvol_io_memory);
tdgbl->mvol_io_memory = NULL;
tdgbl->mvol_io_header = NULL;
tdgbl->mvol_io_buffer = NULL;
tdgbl->blk_io_cnt = 0;
tdgbl->blk_io_ptr = NULL;
return tdgbl->mvol_cumul_count;
}
//____________________________________________________________
//
//
void MVOL_init(ULONG io_buf_size)
{
BurpGlobals* tdgbl = BurpGlobals::getSpecific();
tdgbl->mvol_io_buffer_size = io_buf_size;
tdgbl->gbl_compress_buffer = FB_NEW_POOL(tdgbl->getPool()) UCHAR[ZC_BUFSIZE];
tdgbl->gbl_crypt_buffer = FB_NEW_POOL(tdgbl->getPool()) UCHAR[ZC_BUFSIZE];
tdgbl->gbl_decompress = FB_NEW_POOL(tdgbl->getPool()) UCHAR[ZC_BUFSIZE];
}
static void brio_fini(BurpGlobals* tdgbl)
{
delete[] tdgbl->gbl_compress_buffer;
tdgbl->gbl_compress_buffer = NULL;
delete[] tdgbl->gbl_crypt_buffer;
tdgbl->gbl_crypt_buffer = NULL;
delete[] tdgbl->gbl_decompress;
tdgbl->gbl_decompress = NULL;;
}
static void checkCompression()
{
#ifdef HAVE_ZLIB_H
if (!zlib())
{
(Firebird::Arg::Gds(isc_random) << "Compession support library not loaded" <<
Firebird::Arg::StatusVector(zlib().status)).raise();
}
#endif
}
//____________________________________________________________
//
// Read init record from backup file
//
void MVOL_init_read(const char* file_name, USHORT* format)
{
BurpGlobals* tdgbl = BurpGlobals::getSpecific();
mvol_init_read(tdgbl, file_name, format, &tdgbl->blk_io_cnt, &tdgbl->blk_io_ptr);
tdgbl->gbl_io_cnt = 0;
tdgbl->gbl_io_ptr = NULL;
if (tdgbl->gbl_sw_zip)
{
#ifdef HAVE_ZLIB_H
z_stream& strm = tdgbl->gbl_stream;
strm.zalloc = Firebird::ZLib::allocFunc;
strm.zfree = Firebird::ZLib::freeFunc;
strm.opaque = Z_NULL;
strm.avail_in = 0;
strm.next_in = Z_NULL;
checkCompression();
int ret = zlib().inflateInit(&strm);
if (ret != Z_OK)
#endif
BURP_error(383, true, SafeArg() << 127);
}
}
void mvol_init_read(BurpGlobals* tdgbl, const char* file_name, USHORT* format, int* cnt, UCHAR** ptr)
{
tdgbl->mvol_volume_count = 1;
tdgbl->mvol_empty_file = true;
if (file_name != NULL)
{
strncpy(tdgbl->mvol_old_file, file_name, MAX_FILE_NAME_SIZE);
tdgbl->mvol_old_file[MAX_FILE_NAME_SIZE - 1] = 0;
}
else
{
tdgbl->mvol_old_file[0] = 0;
}
ULONG temp_buffer_size = tdgbl->mvol_io_buffer_size;
tdgbl->mvol_actual_buffer_size = temp_buffer_size;
tdgbl->mvol_io_buffer = BURP_alloc(temp_buffer_size);
tdgbl->gbl_backup_start_time[0] = 0;
read_header(tdgbl->file_desc, &temp_buffer_size, format, true);
if (temp_buffer_size > tdgbl->mvol_actual_buffer_size)
{
UCHAR* new_buffer = BURP_alloc(temp_buffer_size);
memcpy(new_buffer, tdgbl->mvol_io_buffer, tdgbl->mvol_io_buffer_size);
BURP_free(tdgbl->mvol_io_buffer);
tdgbl->mvol_io_ptr = new_buffer + (tdgbl->mvol_io_ptr - tdgbl->mvol_io_buffer);
tdgbl->mvol_io_buffer = new_buffer;
}
else
{
temp_buffer_size = (tdgbl->mvol_actual_buffer_size / temp_buffer_size) * temp_buffer_size;
}
tdgbl->mvol_actual_buffer_size = tdgbl->mvol_io_buffer_size = temp_buffer_size;
*cnt = tdgbl->mvol_io_cnt;
*ptr = tdgbl->mvol_io_ptr;
}
//____________________________________________________________
//
// Write init record to the backup file
//
void MVOL_init_write(const char* file_name)
{
BurpGlobals* tdgbl = BurpGlobals::getSpecific();
mvol_init_write(tdgbl, file_name, &tdgbl->blk_io_cnt, &tdgbl->blk_io_ptr);
tdgbl->gbl_io_cnt = ZC_BUFSIZE;
tdgbl->gbl_io_ptr = tdgbl->gbl_compress_buffer;
#ifdef HAVE_ZLIB_H
if (tdgbl->gbl_sw_zip)
{
z_stream& strm = tdgbl->gbl_stream;
strm.zalloc = Firebird::ZLib::allocFunc;
strm.zfree = Firebird::ZLib::freeFunc;
strm.opaque = Z_NULL;
checkCompression();
int ret = zlib().deflateInit(&strm, Z_DEFAULT_COMPRESSION);
if (ret != Z_OK)
BURP_error(384, true, SafeArg() << ret);
strm.next_out = Z_NULL;
}
#endif
}
void mvol_init_write(BurpGlobals* tdgbl, const char* file_name, int* cnt, UCHAR** ptr)
{
tdgbl->mvol_volume_count = 1;
tdgbl->mvol_empty_file = true;
if (file_name != NULL)
{
strncpy(tdgbl->mvol_old_file, file_name, MAX_FILE_NAME_SIZE);
tdgbl->mvol_old_file[MAX_FILE_NAME_SIZE - 1] = 0;
}
else
{
tdgbl->mvol_old_file[0] = 0;
}
tdgbl->mvol_actual_buffer_size = tdgbl->mvol_io_buffer_size;
const ULONG temp_buffer_size = tdgbl->mvol_io_buffer_size * tdgbl->gbl_sw_blk_factor;
tdgbl->mvol_io_memory = BURP_alloc(temp_buffer_size + MAX_HEADER_SIZE * 2);
tdgbl->mvol_io_ptr = tdgbl->mvol_io_buffer =
(UCHAR*) FB_ALIGN((U_IPTR) tdgbl->mvol_io_memory, MAX_HEADER_SIZE);
tdgbl->mvol_io_cnt = tdgbl->mvol_actual_buffer_size;
while (!write_header(tdgbl->file_desc, temp_buffer_size, false))
{
if (tdgbl->action->act_action == ACT_backup_split)
{
BURP_error(269, true, tdgbl->action->act_file->fil_name.c_str());
// msg 269 can't write a header record to file %s
}
tdgbl->file_desc = next_volume(tdgbl->file_desc, MODE_WRITE, false);
}
tdgbl->mvol_actual_buffer_size = temp_buffer_size;
*cnt = tdgbl->mvol_io_cnt;
*ptr = tdgbl->mvol_io_ptr;
}
//____________________________________________________________
//
// Read a buffer's worth of data. (common)
//
void MVOL_read(BurpGlobals* tdgbl)
{
// Setup our pointer
if (!tdgbl->master)
{
// hvlad: it will throw ExcReadDone exception when there is nothing to read
RestoreRelationTask::renewBuffer(tdgbl);
tdgbl->mvol_io_ptr = tdgbl->mvol_io_buffer;
return;
}
tdgbl->gbl_io_ptr = tdgbl->gbl_compress_buffer;
tdgbl->gbl_io_cnt = unzip_read_block(tdgbl, tdgbl->gbl_io_ptr, ZC_BUFSIZE);
}
int mvol_read(int* cnt, UCHAR** ptr)
{
BurpGlobals* tdgbl = BurpGlobals::getSpecific();
if (tdgbl->stdIoMode && tdgbl->uSvc->isService())
{
tdgbl->uSvc->started();
tdgbl->mvol_io_cnt = tdgbl->uSvc->getBytes(tdgbl->mvol_io_buffer, tdgbl->mvol_io_buffer_size);
if (!tdgbl->mvol_io_cnt)
{
BURP_error_redirect(0, 220);
// msg 220 Unexpected I/O error while reading from backup file
}
tdgbl->mvol_io_ptr = tdgbl->mvol_io_buffer;
}
else
{
os_read(cnt, ptr);
}
tdgbl->mvol_cumul_count += tdgbl->mvol_io_cnt;
file_not_empty();
if (ptr)
*ptr = tdgbl->mvol_io_ptr + 1;
if (cnt)
*cnt = tdgbl->mvol_io_cnt - 1;
return *tdgbl->mvol_io_ptr;
}
#ifndef WIN_NT
//____________________________________________________________
//
// Read a buffer's worth of data. (non-WIN_NT)
//
static void os_read(int* cnt, UCHAR** ptr)
{
BurpGlobals* tdgbl = BurpGlobals::getSpecific();
fb_assert(tdgbl->master);
for (;;)
{
tdgbl->mvol_io_cnt = read(tdgbl->file_desc, tdgbl->mvol_io_buffer, tdgbl->mvol_io_buffer_size);
tdgbl->mvol_io_ptr = tdgbl->mvol_io_buffer;
if (tdgbl->mvol_io_cnt > 0) {
break;
}
if (!tdgbl->mvol_io_cnt || errno == EIO)
{
tdgbl->file_desc = next_volume(tdgbl->file_desc, MODE_READ, false);
if (tdgbl->mvol_io_cnt > 0)
{
break;
}
}
else if (!SYSCALL_INTERRUPTED(errno))
{
if (cnt)
{
BURP_error_redirect(0, 220);
// msg 220 Unexpected I/O error while reading from backup file
}
else
{
BURP_error_redirect(0, 50);
// msg 50 unexpected end of file on backup file
}
}
}
}
#else
//____________________________________________________________
//
// Read a buffer's worth of data. (WIN_NT)
//
static void os_read(int* cnt, UCHAR** ptr)
{
BurpGlobals* tdgbl = BurpGlobals::getSpecific();
fb_assert(tdgbl->master);
fb_assert(tdgbl->blk_io_cnt <= 0);
for (;;)
{
DWORD bytesRead = 0;
const BOOL ret = ReadFile(tdgbl->file_desc, tdgbl->mvol_io_buffer,
tdgbl->mvol_io_buffer_size, &bytesRead, NULL);
tdgbl->mvol_io_cnt = bytesRead;
tdgbl->mvol_io_ptr = tdgbl->mvol_io_buffer;
if (tdgbl->mvol_io_cnt > 0)
break;
if (!tdgbl->mvol_io_cnt && ret)
{
tdgbl->file_desc = next_volume(tdgbl->file_desc, MODE_READ, false);
if (tdgbl->mvol_io_cnt > 0)
break;
}
else if (GetLastError() != ERROR_HANDLE_EOF)
{
if (cnt)
BURP_error_redirect(NULL, 220);
// msg 220 Unexpected I/O error while reading from backup file
else
BURP_error_redirect(NULL, 50);
// msg 50 unexpected end of file on backup file
}
}
}
#endif // !WIN_NT
//____________________________________________________________
//
// Read a chunk of data from the IO buffer.
// Return a pointer to the first position NOT read into.
//
UCHAR* MVOL_read_block(BurpGlobals* tdgbl, UCHAR* ptr, ULONG count)
{
while (count)
{
// If buffer empty, reload it
if (tdgbl->gbl_io_cnt <= 0)
MVOL_read(tdgbl);
const ULONG n = MIN(count, (ULONG) tdgbl->gbl_io_cnt);
// Copy data from the IO buffer
memcpy(ptr, tdgbl->gbl_io_ptr, n);
ptr += n;
// Skip ahead in current buffer
count -= n;
tdgbl->gbl_io_cnt -= n;
tdgbl->gbl_io_ptr += n;
}
return ptr;
}