13
13
#include <unistd.h>
14
14
15
15
static void push_wal_file (const char * from_path , const char * to_path ,
16
- bool is_compress , bool overwrite , int compress_level );
16
+ CompressAlg compress_alg , bool overwrite , int compress_level );
17
17
static void get_wal_file (const char * from_path , const char * to_path );
18
- #ifdef HAVE_LIBZ
18
+
19
19
static const char * get_gz_error (gzFile gzf , int errnum );
20
- #endif
21
- static bool fileEqualCRC (const char * path1 , const char * path2 ,
22
- bool path2_is_compressed );
20
+ static bool fileEqualCRC (const char * path1 , const char * path2 , CompressAlg compress_alg );
23
21
static void copy_file_attributes (const char * from_path ,
24
22
fio_location from_location ,
25
23
const char * to_path , fio_location to_location ,
@@ -40,7 +38,7 @@ do_archive_push(InstanceConfig *instance,
40
38
char absolute_wal_file_path [MAXPGPATH ];
41
39
char current_dir [MAXPGPATH ];
42
40
uint64 system_id ;
43
- bool is_compress = false ;
41
+ CompressAlg compress_alg = NONE_COMPRESS ;
44
42
45
43
if (wal_file_name == NULL && wal_file_path == NULL )
46
44
elog (ERROR , "required parameters are not specified: --wal-file-name %%f --wal-file-path %%p" );
@@ -80,11 +78,15 @@ do_archive_push(InstanceConfig *instance,
80
78
elog (ERROR , "pglz compression is not supported" );
81
79
82
80
#ifdef HAVE_LIBZ
83
- if (instance -> compress_alg == ZLIB_COMPRESS )
84
- is_compress = IsXLogFileName (wal_file_name );
81
+ if (instance -> compress_alg == ZLIB_COMPRESS && IsXLogFileName (wal_file_name ))
82
+ compress_alg = ZLIB_COMPRESS ;
83
+ #endif
84
+ #ifdef HAVE_LZ4
85
+ if (instance -> compress_alg == LZ4_COMPRESS && IsXLogFileName (wal_file_name ))
86
+ compress_alg = LZ4_COMPRESS ;
85
87
#endif
86
88
87
- push_wal_file (absolute_wal_file_path , backup_wal_file_path , is_compress ,
89
+ push_wal_file (absolute_wal_file_path , backup_wal_file_path , compress_alg ,
88
90
overwrite , instance -> compress_level );
89
91
elog (INFO , "pg_probackup archive-push completed successfully" );
90
92
@@ -133,7 +135,7 @@ do_archive_get(InstanceConfig *instance,
133
135
* Copy WAL segment from pgdata to archive catalog with possible compression.
134
136
*/
135
137
void
136
- push_wal_file (const char * from_path , const char * to_path , bool is_compress ,
138
+ push_wal_file (const char * from_path , const char * to_path , CompressAlg compress_alg ,
137
139
bool overwrite , int compress_level )
138
140
{
139
141
FILE * in = NULL ;
@@ -148,17 +150,17 @@ push_wal_file(const char *from_path, const char *to_path, bool is_compress,
148
150
int partial_file_size = 0 ;
149
151
bool partial_file_exists = false;
150
152
151
- #ifdef HAVE_LIBZ
152
153
char gz_to_path [MAXPGPATH ];
153
154
gzFile gz_out = NULL ;
154
155
155
- if (is_compress )
156
+ if (compress_alg != NONE_COMPRESS )
156
157
{
157
- snprintf (gz_to_path , sizeof (gz_to_path ), "%s.gz" , to_path );
158
+ Assert (compress_alg == ZLIB_COMPRESS || compress_alg == LZ4_COMPRESS );
159
+ snprintf (gz_to_path , sizeof (gz_to_path ), "%s.%s" ,
160
+ to_path , compress_alg == ZLIB_COMPRESS ? "gz" : "lz4" );
158
161
to_path_p = gz_to_path ;
159
162
}
160
163
else
161
- #endif
162
164
to_path_p = to_path ;
163
165
164
166
/* open file for read */
@@ -170,20 +172,19 @@ push_wal_file(const char *from_path, const char *to_path, bool is_compress,
170
172
/* Check if possible to skip copying */
171
173
if (fileExists (to_path_p , FIO_BACKUP_HOST ))
172
174
{
173
- if (fileEqualCRC (from_path , to_path_p , is_compress ))
175
+ if (fileEqualCRC (from_path , to_path_p , compress_alg ))
174
176
return ;
175
177
/* Do not copy and do not rise error. Just quit as normal. */
176
178
else if (!overwrite )
177
179
elog (ERROR , "WAL segment \"%s\" already exists." , to_path_p );
178
180
}
179
181
180
182
/* open backup file for write */
181
- #ifdef HAVE_LIBZ
182
- if (is_compress )
183
+ if (compress_alg != NONE_COMPRESS )
183
184
{
184
185
snprintf (to_path_temp , sizeof (to_path_temp ), "%s.part" , gz_to_path );
185
186
186
- gz_out = fio_gzopen (to_path_temp , PG_BINARY_W , compress_level , FIO_BACKUP_HOST );
187
+ gz_out = fio_gzopen (to_path_temp , PG_BINARY_W , compress_level , FIO_BACKUP_HOST , compress_alg );
187
188
if (gz_out == NULL )
188
189
{
189
190
partial_file_exists = true;
@@ -192,7 +193,6 @@ push_wal_file(const char *from_path, const char *to_path, bool is_compress,
192
193
}
193
194
}
194
195
else
195
- #endif
196
196
{
197
197
snprintf (to_path_temp , sizeof (to_path_temp ), "%s.part" , to_path );
198
198
@@ -243,16 +243,14 @@ push_wal_file(const char *from_path, const char *to_path, bool is_compress,
243
243
elog (WARNING , "Reusing stale destination temporary WAL file \"%s\"" , to_path_temp );
244
244
fio_unlink (to_path_temp , FIO_BACKUP_HOST );
245
245
246
- #ifdef HAVE_LIBZ
247
- if (is_compress )
246
+ if (compress_alg != NONE_COMPRESS )
248
247
{
249
- gz_out = fio_gzopen (to_path_temp , PG_BINARY_W , compress_level , FIO_BACKUP_HOST );
248
+ gz_out = fio_gzopen (to_path_temp , PG_BINARY_W , compress_level , FIO_BACKUP_HOST , compress_alg );
250
249
if (gz_out == NULL )
251
250
elog (ERROR , "Cannot open destination temporary WAL file \"%s\": %s" ,
252
251
to_path_temp , strerror (errno ));
253
252
}
254
253
else
255
- #endif
256
254
{
257
255
out = fio_open (to_path_temp , O_RDWR | O_CREAT | O_EXCL | PG_BINARY , FIO_BACKUP_HOST );
258
256
if (out < 0 )
@@ -279,8 +277,7 @@ push_wal_file(const char *from_path, const char *to_path, bool is_compress,
279
277
280
278
if (read_len > 0 )
281
279
{
282
- #ifdef HAVE_LIBZ
283
- if (is_compress )
280
+ if (compress_alg != NONE_COMPRESS )
284
281
{
285
282
if (fio_gzwrite (gz_out , buf , read_len ) != read_len )
286
283
{
@@ -291,7 +288,6 @@ push_wal_file(const char *from_path, const char *to_path, bool is_compress,
291
288
}
292
289
}
293
290
else
294
- #endif
295
291
{
296
292
if (fio_write (out , buf , read_len ) != read_len )
297
293
{
@@ -307,8 +303,7 @@ push_wal_file(const char *from_path, const char *to_path, bool is_compress,
307
303
break ;
308
304
}
309
305
310
- #ifdef HAVE_LIBZ
311
- if (is_compress )
306
+ if (compress_alg != NONE_COMPRESS )
312
307
{
313
308
if (fio_gzclose (gz_out ) != 0 )
314
309
{
@@ -319,7 +314,6 @@ push_wal_file(const char *from_path, const char *to_path, bool is_compress,
319
314
}
320
315
}
321
316
else
322
- #endif
323
317
{
324
318
if (fio_flush (out ) != 0 || fio_close (out ) != 0 )
325
319
{
@@ -349,10 +343,8 @@ push_wal_file(const char *from_path, const char *to_path, bool is_compress,
349
343
to_path_temp , to_path_p , strerror (errno_temp ));
350
344
}
351
345
352
- #ifdef HAVE_LIBZ
353
- if (is_compress )
346
+ if (compress_alg != NONE_COMPRESS )
354
347
elog (INFO , "WAL file compressed to \"%s\"" , gz_to_path );
355
- #endif
356
348
}
357
349
358
350
/*
@@ -367,17 +359,13 @@ get_wal_file(const char *from_path, const char *to_path)
367
359
const char * from_path_p = from_path ;
368
360
char to_path_temp [MAXPGPATH ];
369
361
int errno_temp ;
370
- bool is_decompress = false;
371
-
372
- #ifdef HAVE_LIBZ
362
+ CompressAlg compress_alg = NONE_COMPRESS ;
373
363
char gz_from_path [MAXPGPATH ];
374
364
gzFile gz_in = NULL ;
375
- #endif
376
365
377
366
/* First check source file for existance */
378
367
if (fio_access (from_path , F_OK , FIO_BACKUP_HOST ) != 0 )
379
368
{
380
- #ifdef HAVE_LIBZ
381
369
/*
382
370
* Maybe we need to decompress the file. Check it with .gz
383
371
* extension.
@@ -386,34 +374,41 @@ get_wal_file(const char *from_path, const char *to_path)
386
374
if (fio_access (gz_from_path , F_OK , FIO_BACKUP_HOST ) == 0 )
387
375
{
388
376
/* Found compressed file */
389
- is_decompress = true ;
377
+ compress_alg = ZLIB_COMPRESS ;
390
378
from_path_p = gz_from_path ;
391
379
}
392
- #endif
380
+ else
381
+ {
382
+ snprintf (gz_from_path , sizeof (gz_from_path ), "%s.lz4" , from_path );
383
+ if (fio_access (gz_from_path , F_OK , FIO_BACKUP_HOST ) == 0 )
384
+ {
385
+ /* Found compressed file */
386
+ compress_alg = LZ4_COMPRESS ;
387
+ from_path_p = gz_from_path ;
388
+ }
389
+ }
393
390
/* Didn't find compressed file */
394
- if (! is_decompress )
391
+ if (compress_alg == NONE_COMPRESS )
395
392
elog (ERROR , "Source WAL file \"%s\" doesn't exist" ,
396
393
from_path );
397
394
}
398
395
399
396
/* open file for read */
400
- if (! is_decompress )
397
+ if (compress_alg == NONE_COMPRESS )
401
398
{
402
399
in = fio_fopen (from_path , PG_BINARY_R , FIO_BACKUP_HOST );
403
400
if (in == NULL )
404
401
elog (ERROR , "Cannot open source WAL file \"%s\": %s" ,
405
402
from_path , strerror (errno ));
406
403
}
407
- #ifdef HAVE_LIBZ
408
404
else
409
405
{
410
406
gz_in = fio_gzopen (gz_from_path , PG_BINARY_R , Z_DEFAULT_COMPRESSION ,
411
- FIO_BACKUP_HOST );
407
+ FIO_BACKUP_HOST , compress_alg );
412
408
if (gz_in == NULL )
413
409
elog (ERROR , "Cannot open compressed WAL file \"%s\": %s" ,
414
410
gz_from_path , strerror (errno ));
415
411
}
416
- #endif
417
412
418
413
/* open backup file for write */
419
414
snprintf (to_path_temp , sizeof (to_path_temp ), "%s.part" , to_path );
@@ -428,8 +423,7 @@ get_wal_file(const char *from_path, const char *to_path)
428
423
{
429
424
int read_len = 0 ;
430
425
431
- #ifdef HAVE_LIBZ
432
- if (is_decompress )
426
+ if (compress_alg != NONE_COMPRESS )
433
427
{
434
428
read_len = fio_gzread (gz_in , buf , sizeof (buf ));
435
429
if (read_len <= 0 && !fio_gzeof (gz_in ))
@@ -441,7 +435,6 @@ get_wal_file(const char *from_path, const char *to_path)
441
435
}
442
436
}
443
437
else
444
- #endif
445
438
{
446
439
read_len = fio_fread (in , buf , sizeof (buf ));
447
440
if (read_len < 0 )
@@ -465,14 +458,12 @@ get_wal_file(const char *from_path, const char *to_path)
465
458
}
466
459
467
460
/* Check for EOF */
468
- #ifdef HAVE_LIBZ
469
- if (is_decompress )
461
+ if (compress_alg != NONE_COMPRESS )
470
462
{
471
463
if (fio_gzeof (gz_in ) || read_len == 0 )
472
464
break ;
473
465
}
474
466
else
475
- #endif
476
467
{
477
468
if (/* feof(in) || */ read_len == 0 )
478
469
break ;
@@ -487,8 +478,7 @@ get_wal_file(const char *from_path, const char *to_path)
487
478
to_path_temp , strerror (errno_temp ));
488
479
}
489
480
490
- #ifdef HAVE_LIBZ
491
- if (is_decompress )
481
+ if (compress_alg != NONE_COMPRESS )
492
482
{
493
483
if (fio_gzclose (gz_in ) != 0 )
494
484
{
@@ -499,7 +489,6 @@ get_wal_file(const char *from_path, const char *to_path)
499
489
}
500
490
}
501
491
else
502
- #endif
503
492
{
504
493
if (fio_fclose (in ))
505
494
{
@@ -521,13 +510,10 @@ get_wal_file(const char *from_path, const char *to_path)
521
510
to_path_temp , to_path , strerror (errno_temp ));
522
511
}
523
512
524
- #ifdef HAVE_LIBZ
525
- if (is_decompress )
513
+ if (compress_alg != NONE_COMPRESS )
526
514
elog (INFO , "WAL file decompressed from \"%s\"" , gz_from_path );
527
- #endif
528
515
}
529
516
530
- #ifdef HAVE_LIBZ
531
517
/*
532
518
* Show error during work with compressed file
533
519
*/
@@ -543,27 +529,25 @@ get_gz_error(gzFile gzf, int errnum)
543
529
else
544
530
return errmsg ;
545
531
}
546
- #endif
547
532
548
533
/*
549
534
* compare CRC of two WAL files.
550
535
* If necessary, decompress WAL file from path2
551
536
*/
552
537
static bool
553
- fileEqualCRC (const char * path1 , const char * path2 , bool path2_is_compressed )
538
+ fileEqualCRC (const char * path1 , const char * path2 , CompressAlg compress_alg )
554
539
{
555
540
pg_crc32 crc1 ;
556
541
pg_crc32 crc2 ;
557
542
558
543
/* Get checksum of backup file */
559
- #ifdef HAVE_LIBZ
560
- if (path2_is_compressed )
544
+ if (compress_alg != NONE_COMPRESS )
561
545
{
562
546
char buf [1024 ];
563
547
gzFile gz_in = NULL ;
564
548
565
549
INIT_FILE_CRC32 (true, crc2 );
566
- gz_in = fio_gzopen (path2 , PG_BINARY_R , Z_DEFAULT_COMPRESSION , FIO_BACKUP_HOST );
550
+ gz_in = fio_gzopen (path2 , PG_BINARY_R , Z_DEFAULT_COMPRESSION , FIO_BACKUP_HOST , compress_alg );
567
551
if (gz_in == NULL )
568
552
/* File cannot be read */
569
553
elog (ERROR ,
@@ -592,7 +576,6 @@ fileEqualCRC(const char *path1, const char *path2, bool path2_is_compressed)
592
576
path2 , get_gz_error (gz_in , errno ));
593
577
}
594
578
else
595
- #endif
596
579
{
597
580
crc2 = pgFileGetCRC (path2 , true, true, NULL , FIO_BACKUP_HOST );
598
581
}
0 commit comments