Branch data Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * walmethods.c - implementations of different ways to write received wal
4 : : *
5 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
6 : : *
7 : : * IDENTIFICATION
8 : : * src/bin/pg_basebackup/walmethods.c
9 : : *-------------------------------------------------------------------------
10 : : */
11 : :
12 : : #include "postgres_fe.h"
13 : :
14 : : #include <fcntl.h>
15 : : #include <sys/stat.h>
16 : : #include <time.h>
17 : : #include <unistd.h>
18 : :
19 : : #ifdef USE_LZ4
20 : : #include <lz4frame.h>
21 : : #endif
22 : : #ifdef HAVE_LIBZ
23 : : #include <zlib.h>
24 : : #endif
25 : :
26 : : #include "common/file_perm.h"
27 : : #include "common/file_utils.h"
28 : : #include "common/logging.h"
29 : : #include "pgtar.h"
30 : : #include "walmethods.h"
31 : :
32 : : /* Size of zlib buffer for .tar.gz */
33 : : #define ZLIB_OUT_SIZE 4096
34 : :
35 : : /* Size of LZ4 input chunk for .lz4 */
36 : : #define LZ4_IN_SIZE 4096
37 : :
38 : : /*-------------------------------------------------------------------------
39 : : * WalDirectoryMethod - write wal to a directory looking like pg_wal
40 : : *-------------------------------------------------------------------------
41 : : */
42 : :
43 : : static Walfile *dir_open_for_write(WalWriteMethod *wwmethod,
44 : : const char *pathname,
45 : : const char *temp_suffix,
46 : : size_t pad_to_size);
47 : : static int dir_close(Walfile *f, WalCloseMethod method);
48 : : static bool dir_existsfile(WalWriteMethod *wwmethod, const char *pathname);
49 : : static ssize_t dir_get_file_size(WalWriteMethod *wwmethod,
50 : : const char *pathname);
51 : : static char *dir_get_file_name(WalWriteMethod *wwmethod,
52 : : const char *pathname, const char *temp_suffix);
53 : : static ssize_t dir_write(Walfile *f, const void *buf, size_t count);
54 : : static int dir_sync(Walfile *f);
55 : : static bool dir_finish(WalWriteMethod *wwmethod);
56 : : static void dir_free(WalWriteMethod *wwmethod);
57 : :
58 : : static const WalWriteMethodOps WalDirectoryMethodOps = {
59 : : .open_for_write = dir_open_for_write,
60 : : .close = dir_close,
61 : : .existsfile = dir_existsfile,
62 : : .get_file_size = dir_get_file_size,
63 : : .get_file_name = dir_get_file_name,
64 : : .write = dir_write,
65 : : .sync = dir_sync,
66 : : .finish = dir_finish,
67 : : .free = dir_free
68 : : };
69 : :
70 : : /*
71 : : * Global static data for this method
72 : : */
73 : : typedef struct DirectoryMethodData
74 : : {
75 : : WalWriteMethod base;
76 : : char *basedir;
77 : : } DirectoryMethodData;
78 : :
79 : : /*
80 : : * Local file handle
81 : : */
82 : : typedef struct DirectoryMethodFile
83 : : {
84 : : Walfile base;
85 : : int fd;
86 : : char *fullpath;
87 : : char *temp_suffix;
88 : : #ifdef HAVE_LIBZ
89 : : gzFile gzfp;
90 : : #endif
91 : : #ifdef USE_LZ4
92 : : LZ4F_compressionContext_t ctx;
93 : : size_t lz4bufsize;
94 : : void *lz4buf;
95 : : #endif
96 : : } DirectoryMethodFile;
97 : :
98 : : #define clear_error(wwmethod) \
99 : : ((wwmethod)->lasterrstring = NULL, (wwmethod)->lasterrno = 0)
100 : :
101 : : static char *
102 : 540 : dir_get_file_name(WalWriteMethod *wwmethod,
103 : : const char *pathname, const char *temp_suffix)
104 : : {
105 : 540 : char *filename = pg_malloc0_array(char, MAXPGPATH);
106 : :
107 [ + + ]: 1080 : snprintf(filename, MAXPGPATH, "%s%s%s",
108 : : pathname,
109 [ + + ]: 540 : wwmethod->compression_algorithm == PG_COMPRESSION_GZIP ? ".gz" :
110 [ + + ]: 532 : wwmethod->compression_algorithm == PG_COMPRESSION_LZ4 ? ".lz4" : "",
111 : : temp_suffix ? temp_suffix : "");
112 : :
113 : 540 : return filename;
114 : : }
115 : :
116 : : static Walfile *
117 : 184 : dir_open_for_write(WalWriteMethod *wwmethod, const char *pathname,
118 : : const char *temp_suffix, size_t pad_to_size)
119 : : {
120 : 184 : DirectoryMethodData *dir_data = (DirectoryMethodData *) wwmethod;
121 : : char tmppath[MAXPGPATH];
122 : : char *filename;
123 : : int fd;
124 : : DirectoryMethodFile *f;
125 : : #ifdef HAVE_LIBZ
126 : 184 : gzFile gzfp = NULL;
127 : : #endif
128 : : #ifdef USE_LZ4
129 : 184 : LZ4F_compressionContext_t ctx = NULL;
130 : 184 : size_t lz4bufsize = 0;
131 : 184 : void *lz4buf = NULL;
132 : : #endif
133 : :
134 : 184 : clear_error(wwmethod);
135 : :
136 : 184 : filename = dir_get_file_name(wwmethod, pathname, temp_suffix);
137 : 184 : snprintf(tmppath, sizeof(tmppath), "%s/%s",
138 : : dir_data->basedir, filename);
139 : 184 : pg_free(filename);
140 : :
141 : : /*
142 : : * Open a file for non-compressed as well as compressed files. Tracking
143 : : * the file descriptor is important for dir_sync() method as gzflush()
144 : : * does not do any system calls to fsync() to make changes permanent on
145 : : * disk.
146 : : */
147 : 184 : fd = open(tmppath, O_WRONLY | O_CREAT | PG_BINARY, pg_file_create_mode);
148 [ - + ]: 184 : if (fd < 0)
149 : : {
150 : 0 : wwmethod->lasterrno = errno;
151 : 0 : return NULL;
152 : : }
153 : :
154 : : #ifdef HAVE_LIBZ
155 [ + + ]: 184 : if (wwmethod->compression_algorithm == PG_COMPRESSION_GZIP)
156 : : {
157 : 2 : gzfp = gzdopen(fd, "wb");
158 [ - + ]: 2 : if (gzfp == NULL)
159 : : {
160 : 0 : wwmethod->lasterrno = errno;
161 : 0 : close(fd);
162 : 0 : return NULL;
163 : : }
164 : :
165 [ - + ]: 2 : if (gzsetparams(gzfp, wwmethod->compression_level,
166 : : Z_DEFAULT_STRATEGY) != Z_OK)
167 : : {
168 : 0 : wwmethod->lasterrno = errno;
169 : 0 : gzclose(gzfp);
170 : 0 : return NULL;
171 : : }
172 : : }
173 : : #endif
174 : : #ifdef USE_LZ4
175 [ + + ]: 184 : if (wwmethod->compression_algorithm == PG_COMPRESSION_LZ4)
176 : : {
177 : : size_t ctx_out;
178 : : size_t header_size;
179 : : LZ4F_preferences_t prefs;
180 : :
181 : 2 : ctx_out = LZ4F_createCompressionContext(&ctx, LZ4F_VERSION);
182 [ - + ]: 2 : if (LZ4F_isError(ctx_out))
183 : : {
184 : 0 : wwmethod->lasterrstring = LZ4F_getErrorName(ctx_out);
185 : 0 : close(fd);
186 : 0 : return NULL;
187 : : }
188 : :
189 : 2 : lz4bufsize = LZ4F_compressBound(LZ4_IN_SIZE, NULL);
190 : 2 : lz4buf = pg_malloc0(lz4bufsize);
191 : :
192 : : /* assign the compression level, default is 0 */
193 : 2 : memset(&prefs, 0, sizeof(prefs));
194 : 2 : prefs.compressionLevel = wwmethod->compression_level;
195 : :
196 : : /* add the header */
197 : 2 : header_size = LZ4F_compressBegin(ctx, lz4buf, lz4bufsize, &prefs);
198 [ - + ]: 2 : if (LZ4F_isError(header_size))
199 : : {
200 : 0 : wwmethod->lasterrstring = LZ4F_getErrorName(header_size);
201 : 0 : (void) LZ4F_freeCompressionContext(ctx);
202 : 0 : pg_free(lz4buf);
203 : 0 : close(fd);
204 : 0 : return NULL;
205 : : }
206 : :
207 : 2 : errno = 0;
208 [ - + ]: 2 : if (write(fd, lz4buf, header_size) != header_size)
209 : : {
210 : : /* If write didn't set errno, assume problem is no disk space */
211 [ # # ]: 0 : wwmethod->lasterrno = errno ? errno : ENOSPC;
212 : 0 : (void) LZ4F_freeCompressionContext(ctx);
213 : 0 : pg_free(lz4buf);
214 : 0 : close(fd);
215 : 0 : return NULL;
216 : : }
217 : : }
218 : : #endif
219 : :
220 : : /* Do pre-padding on non-compressed files */
221 [ + + + + ]: 184 : if (pad_to_size && wwmethod->compression_algorithm == PG_COMPRESSION_NONE)
222 : : {
223 : : ssize_t rc;
224 : :
225 : 163 : rc = pg_pwrite_zeros(fd, pad_to_size, 0);
226 : :
227 [ - + ]: 163 : if (rc < 0)
228 : : {
229 : 0 : wwmethod->lasterrno = errno;
230 : 0 : close(fd);
231 : 0 : return NULL;
232 : : }
233 : :
234 : : /*
235 : : * pg_pwrite() (called via pg_pwrite_zeros()) may have moved the file
236 : : * position, so reset it (see win32pwrite.c).
237 : : */
238 [ - + ]: 163 : if (lseek(fd, 0, SEEK_SET) != 0)
239 : : {
240 : 0 : wwmethod->lasterrno = errno;
241 : 0 : close(fd);
242 : 0 : return NULL;
243 : : }
244 : : }
245 : :
246 : : /*
247 : : * fsync WAL file and containing directory, to ensure the file is
248 : : * persistently created and zeroed (if padded). That's particularly
249 : : * important when using synchronous mode, where the file is modified and
250 : : * fsynced in-place, without a directory fsync.
251 : : */
252 [ + + ]: 184 : if (wwmethod->sync)
253 : : {
254 [ + - - + ]: 14 : if (fsync_fname(tmppath, false) != 0 ||
255 : 7 : fsync_parent_path(tmppath) != 0)
256 : : {
257 : 0 : wwmethod->lasterrno = errno;
258 : : #ifdef HAVE_LIBZ
259 [ # # ]: 0 : if (wwmethod->compression_algorithm == PG_COMPRESSION_GZIP)
260 : 0 : gzclose(gzfp);
261 : : else
262 : : #endif
263 : : #ifdef USE_LZ4
264 [ # # ]: 0 : if (wwmethod->compression_algorithm == PG_COMPRESSION_LZ4)
265 : : {
266 : 0 : (void) LZ4F_compressEnd(ctx, lz4buf, lz4bufsize, NULL);
267 : 0 : (void) LZ4F_freeCompressionContext(ctx);
268 : 0 : pg_free(lz4buf);
269 : 0 : close(fd);
270 : : }
271 : : else
272 : : #endif
273 : 0 : close(fd);
274 : 0 : return NULL;
275 : : }
276 : : }
277 : :
278 : 184 : f = pg_malloc0_object(DirectoryMethodFile);
279 : : #ifdef HAVE_LIBZ
280 [ + + ]: 184 : if (wwmethod->compression_algorithm == PG_COMPRESSION_GZIP)
281 : 2 : f->gzfp = gzfp;
282 : : #endif
283 : : #ifdef USE_LZ4
284 [ + + ]: 184 : if (wwmethod->compression_algorithm == PG_COMPRESSION_LZ4)
285 : : {
286 : 2 : f->ctx = ctx;
287 : 2 : f->lz4buf = lz4buf;
288 : 2 : f->lz4bufsize = lz4bufsize;
289 : : }
290 : : #endif
291 : :
292 : 184 : f->base.wwmethod = wwmethod;
293 : 184 : f->base.currpos = 0;
294 : 184 : f->base.pathname = pg_strdup(pathname);
295 : 184 : f->fd = fd;
296 : 184 : f->fullpath = pg_strdup(tmppath);
297 [ + + ]: 184 : if (temp_suffix)
298 : 17 : f->temp_suffix = pg_strdup(temp_suffix);
299 : :
300 : 184 : return &f->base;
301 : : }
302 : :
303 : : static ssize_t
304 : 1695 : dir_write(Walfile *f, const void *buf, size_t count)
305 : : {
306 : : ssize_t r;
307 : 1695 : DirectoryMethodFile *df = (DirectoryMethodFile *) f;
308 : :
309 : : Assert(f != NULL);
310 : 1695 : clear_error(f->wwmethod);
311 : :
312 : : #ifdef HAVE_LIBZ
313 [ + + ]: 1695 : if (f->wwmethod->compression_algorithm == PG_COMPRESSION_GZIP)
314 : : {
315 : 9 : errno = 0;
316 : 9 : r = (ssize_t) gzwrite(df->gzfp, buf, count);
317 [ - + ]: 9 : if (r != count)
318 : : {
319 : : /* If write didn't set errno, assume problem is no disk space */
320 [ # # ]: 0 : f->wwmethod->lasterrno = errno ? errno : ENOSPC;
321 : : }
322 : : }
323 : : else
324 : : #endif
325 : : #ifdef USE_LZ4
326 [ + + ]: 1686 : if (f->wwmethod->compression_algorithm == PG_COMPRESSION_LZ4)
327 : : {
328 : : size_t chunk;
329 : : size_t remaining;
330 : 9 : const void *inbuf = buf;
331 : :
332 : 9 : remaining = count;
333 [ + + ]: 266 : while (remaining > 0)
334 : : {
335 : : size_t compressed;
336 : :
337 [ + + ]: 257 : if (remaining > LZ4_IN_SIZE)
338 : 248 : chunk = LZ4_IN_SIZE;
339 : : else
340 : 9 : chunk = remaining;
341 : :
342 : 257 : remaining -= chunk;
343 : 257 : compressed = LZ4F_compressUpdate(df->ctx,
344 : : df->lz4buf, df->lz4bufsize,
345 : : inbuf, chunk,
346 : : NULL);
347 : :
348 [ - + ]: 257 : if (LZ4F_isError(compressed))
349 : : {
350 : 0 : f->wwmethod->lasterrstring = LZ4F_getErrorName(compressed);
351 : 0 : return -1;
352 : : }
353 : :
354 : 257 : errno = 0;
355 [ - + ]: 257 : if (write(df->fd, df->lz4buf, compressed) != compressed)
356 : : {
357 : : /* If write didn't set errno, assume problem is no disk space */
358 [ # # ]: 0 : f->wwmethod->lasterrno = errno ? errno : ENOSPC;
359 : 0 : return -1;
360 : : }
361 : :
362 : 257 : inbuf = ((const char *) inbuf) + chunk;
363 : : }
364 : :
365 : : /* Our caller keeps track of the uncompressed size. */
366 : 9 : r = (ssize_t) count;
367 : : }
368 : : else
369 : : #endif
370 : : {
371 : 1677 : errno = 0;
372 : 1677 : r = write(df->fd, buf, count);
373 [ - + ]: 1677 : if (r != count)
374 : : {
375 : : /* If write didn't set errno, assume problem is no disk space */
376 [ # # ]: 0 : f->wwmethod->lasterrno = errno ? errno : ENOSPC;
377 : : }
378 : : }
379 [ + - ]: 1695 : if (r > 0)
380 : 1695 : df->base.currpos += r;
381 : 1695 : return r;
382 : : }
383 : :
384 : : static int
385 : 184 : dir_close(Walfile *f, WalCloseMethod method)
386 : : {
387 : 184 : int r = -1;
388 : 184 : DirectoryMethodFile *df = (DirectoryMethodFile *) f;
389 : 184 : DirectoryMethodData *dir_data = (DirectoryMethodData *) f->wwmethod;
390 : : char tmppath[MAXPGPATH];
391 : : char tmppath2[MAXPGPATH];
392 : :
393 : : Assert(f != NULL);
394 : 184 : clear_error(f->wwmethod);
395 : :
396 : : #ifdef HAVE_LIBZ
397 [ + + ]: 184 : if (f->wwmethod->compression_algorithm == PG_COMPRESSION_GZIP)
398 : : {
399 : 2 : errno = 0; /* in case gzclose() doesn't set it */
400 : 2 : r = gzclose(df->gzfp);
401 : : }
402 : : else
403 : : #endif
404 : : #ifdef USE_LZ4
405 [ + + ]: 182 : if (f->wwmethod->compression_algorithm == PG_COMPRESSION_LZ4)
406 : : {
407 : : size_t compressed;
408 : :
409 : 2 : compressed = LZ4F_compressEnd(df->ctx,
410 : : df->lz4buf, df->lz4bufsize,
411 : : NULL);
412 : :
413 [ - + ]: 2 : if (LZ4F_isError(compressed))
414 : : {
415 : 0 : f->wwmethod->lasterrstring = LZ4F_getErrorName(compressed);
416 : 0 : (void) close(df->fd);
417 : 0 : goto cleanup;
418 : : }
419 : :
420 : 2 : errno = 0;
421 [ - + ]: 2 : if (write(df->fd, df->lz4buf, compressed) != compressed)
422 : : {
423 : : /* If write didn't set errno, assume problem is no disk space */
424 [ # # ]: 0 : f->wwmethod->lasterrno = errno ? errno : ENOSPC;
425 : 0 : (void) close(df->fd);
426 : 0 : goto cleanup;
427 : : }
428 : :
429 : 2 : r = close(df->fd);
430 : : }
431 : : else
432 : : #endif
433 : 180 : r = close(df->fd);
434 : :
435 [ + - + - ]: 184 : if (r == 0 && method != CLOSE_UNLINK)
436 : : {
437 : : /* Build path to the current version of the file */
438 [ + + + + ]: 184 : if (method == CLOSE_NORMAL && df->temp_suffix)
439 : 11 : {
440 : : char *filename;
441 : : char *filename2;
442 : :
443 : : /*
444 : : * If we have a temp prefix, normal operation is to rename the
445 : : * file.
446 : : */
447 : 11 : filename = dir_get_file_name(f->wwmethod, df->base.pathname,
448 : 11 : df->temp_suffix);
449 : 11 : snprintf(tmppath, sizeof(tmppath), "%s/%s",
450 : : dir_data->basedir, filename);
451 : 11 : pg_free(filename);
452 : :
453 : : /* permanent name, so no need for the prefix */
454 : 11 : filename2 = dir_get_file_name(f->wwmethod, df->base.pathname, NULL);
455 : 11 : snprintf(tmppath2, sizeof(tmppath2), "%s/%s",
456 : : dir_data->basedir, filename2);
457 : 11 : pg_free(filename2);
458 [ + + ]: 11 : if (f->wwmethod->sync)
459 : 3 : r = durable_rename(tmppath, tmppath2);
460 : : else
461 : : {
462 [ - + ]: 8 : if (rename(tmppath, tmppath2) != 0)
463 : : {
464 : 0 : pg_log_error("could not rename file \"%s\" to \"%s\": %m",
465 : : tmppath, tmppath2);
466 : 0 : r = -1;
467 : : }
468 : : }
469 : : }
470 : : else
471 : : {
472 : : /*
473 : : * Else either CLOSE_NORMAL and no temp suffix, or
474 : : * CLOSE_NO_RENAME. In this case, fsync the file and containing
475 : : * directory if sync mode is requested.
476 : : */
477 [ + + ]: 173 : if (f->wwmethod->sync)
478 : : {
479 : 4 : r = fsync_fname(df->fullpath, false);
480 [ + - ]: 4 : if (r == 0)
481 : 4 : r = fsync_parent_path(df->fullpath);
482 : : }
483 : : }
484 : : }
485 : :
486 [ + - ]: 184 : if (r != 0)
487 : 0 : f->wwmethod->lasterrno = errno;
488 : :
489 : : #ifdef USE_LZ4
490 : 184 : cleanup:
491 : 184 : pg_free(df->lz4buf);
492 : : /* supports free on NULL */
493 : 184 : LZ4F_freeCompressionContext(df->ctx);
494 : : #endif
495 : :
496 [ - + ]: 184 : if (method == CLOSE_UNLINK)
497 : : {
498 : : /*
499 : : * If unlink fails, report its error if the preceding operations
500 : : * succeeded; otherwise, preserve the earlier error.
501 : : */
502 [ # # # # ]: 0 : if (unlink(df->fullpath) != 0 && r == 0)
503 : : {
504 : 0 : f->wwmethod->lasterrno = errno;
505 : 0 : r = -1;
506 : : }
507 : : }
508 : :
509 : 184 : pg_free(df->base.pathname);
510 : 184 : pg_free(df->fullpath);
511 : 184 : pg_free(df->temp_suffix);
512 : 184 : pg_free(df);
513 : :
514 : 184 : return r;
515 : : }
516 : :
517 : : static int
518 : 0 : dir_sync(Walfile *f)
519 : : {
520 : : int r;
521 : :
522 : : Assert(f != NULL);
523 : 0 : clear_error(f->wwmethod);
524 : :
525 [ # # ]: 0 : if (!f->wwmethod->sync)
526 : 0 : return 0;
527 : :
528 : : #ifdef HAVE_LIBZ
529 [ # # ]: 0 : if (f->wwmethod->compression_algorithm == PG_COMPRESSION_GZIP)
530 : : {
531 [ # # ]: 0 : if (gzflush(((DirectoryMethodFile *) f)->gzfp, Z_SYNC_FLUSH) != Z_OK)
532 : : {
533 : 0 : f->wwmethod->lasterrno = errno;
534 : 0 : return -1;
535 : : }
536 : : }
537 : : #endif
538 : : #ifdef USE_LZ4
539 [ # # ]: 0 : if (f->wwmethod->compression_algorithm == PG_COMPRESSION_LZ4)
540 : : {
541 : 0 : DirectoryMethodFile *df = (DirectoryMethodFile *) f;
542 : : size_t compressed;
543 : :
544 : : /* Flush any internal buffers */
545 : 0 : compressed = LZ4F_flush(df->ctx, df->lz4buf, df->lz4bufsize, NULL);
546 [ # # ]: 0 : if (LZ4F_isError(compressed))
547 : : {
548 : 0 : f->wwmethod->lasterrstring = LZ4F_getErrorName(compressed);
549 : 0 : return -1;
550 : : }
551 : :
552 : 0 : errno = 0;
553 [ # # ]: 0 : if (write(df->fd, df->lz4buf, compressed) != compressed)
554 : : {
555 : : /* If write didn't set errno, assume problem is no disk space */
556 [ # # ]: 0 : f->wwmethod->lasterrno = errno ? errno : ENOSPC;
557 : 0 : return -1;
558 : : }
559 : : }
560 : : #endif
561 : :
562 : 0 : r = fsync(((DirectoryMethodFile *) f)->fd);
563 [ # # ]: 0 : if (r < 0)
564 : 0 : f->wwmethod->lasterrno = errno;
565 : 0 : return r;
566 : : }
567 : :
568 : : static ssize_t
569 : 0 : dir_get_file_size(WalWriteMethod *wwmethod, const char *pathname)
570 : : {
571 : 0 : DirectoryMethodData *dir_data = (DirectoryMethodData *) wwmethod;
572 : : struct stat statbuf;
573 : : char tmppath[MAXPGPATH];
574 : :
575 : 0 : snprintf(tmppath, sizeof(tmppath), "%s/%s",
576 : : dir_data->basedir, pathname);
577 : :
578 [ # # ]: 0 : if (stat(tmppath, &statbuf) != 0)
579 : : {
580 : 0 : wwmethod->lasterrno = errno;
581 : 0 : return -1;
582 : : }
583 : :
584 : 0 : return statbuf.st_size;
585 : : }
586 : :
587 : : static bool
588 : 168 : dir_existsfile(WalWriteMethod *wwmethod, const char *pathname)
589 : : {
590 : 168 : DirectoryMethodData *dir_data = (DirectoryMethodData *) wwmethod;
591 : : char tmppath[MAXPGPATH];
592 : : int fd;
593 : :
594 : 168 : clear_error(wwmethod);
595 : :
596 : 168 : snprintf(tmppath, sizeof(tmppath), "%s/%s",
597 : : dir_data->basedir, pathname);
598 : :
599 : 168 : fd = open(tmppath, O_RDONLY | PG_BINARY, 0);
600 [ + - ]: 168 : if (fd < 0)
601 : :
602 : : /*
603 : : * Skip setting dir_data->lasterrno here because we are only checking
604 : : * for existence.
605 : : */
606 : 168 : return false;
607 : 0 : close(fd);
608 : 0 : return true;
609 : : }
610 : :
611 : : static bool
612 : 159 : dir_finish(WalWriteMethod *wwmethod)
613 : : {
614 : 159 : clear_error(wwmethod);
615 : :
616 [ + + ]: 159 : if (wwmethod->sync)
617 : : {
618 : 4 : DirectoryMethodData *dir_data = (DirectoryMethodData *) wwmethod;
619 : :
620 : : /*
621 : : * Files are fsynced when they are closed, but we need to fsync the
622 : : * directory entry here as well.
623 : : */
624 [ - + ]: 4 : if (fsync_fname(dir_data->basedir, true) != 0)
625 : : {
626 : 0 : wwmethod->lasterrno = errno;
627 : 0 : return false;
628 : : }
629 : : }
630 : 159 : return true;
631 : : }
632 : :
633 : : static void
634 : 159 : dir_free(WalWriteMethod *wwmethod)
635 : : {
636 : 159 : DirectoryMethodData *dir_data = (DirectoryMethodData *) wwmethod;
637 : :
638 : 159 : pg_free(dir_data->basedir);
639 : 159 : pg_free(wwmethod);
640 : 159 : }
641 : :
642 : :
643 : : WalWriteMethod *
644 : 161 : CreateWalDirectoryMethod(const char *basedir,
645 : : pg_compress_algorithm compression_algorithm,
646 : : int compression_level, bool sync)
647 : : {
648 : : DirectoryMethodData *wwmethod;
649 : :
650 : 161 : wwmethod = pg_malloc0_object(DirectoryMethodData);
651 : 161 : *((const WalWriteMethodOps **) &wwmethod->base.ops) =
652 : : &WalDirectoryMethodOps;
653 : 161 : wwmethod->base.compression_algorithm = compression_algorithm;
654 : 161 : wwmethod->base.compression_level = compression_level;
655 : 161 : wwmethod->base.sync = sync;
656 : 161 : clear_error(&wwmethod->base);
657 : 161 : wwmethod->basedir = pg_strdup(basedir);
658 : :
659 : 161 : return &wwmethod->base;
660 : : }
661 : :
662 : :
663 : : /*-------------------------------------------------------------------------
664 : : * WalTarMethod - write wal to a tar file containing pg_wal contents
665 : : *-------------------------------------------------------------------------
666 : : */
667 : :
668 : : static Walfile *tar_open_for_write(WalWriteMethod *wwmethod,
669 : : const char *pathname,
670 : : const char *temp_suffix,
671 : : size_t pad_to_size);
672 : : static int tar_close(Walfile *f, WalCloseMethod method);
673 : : static bool tar_existsfile(WalWriteMethod *wwmethod, const char *pathname);
674 : : static ssize_t tar_get_file_size(WalWriteMethod *wwmethod,
675 : : const char *pathname);
676 : : static char *tar_get_file_name(WalWriteMethod *wwmethod,
677 : : const char *pathname, const char *temp_suffix);
678 : : static ssize_t tar_write(Walfile *f, const void *buf, size_t count);
679 : : static int tar_sync(Walfile *f);
680 : : static bool tar_finish(WalWriteMethod *wwmethod);
681 : : static void tar_free(WalWriteMethod *wwmethod);
682 : :
683 : : static const WalWriteMethodOps WalTarMethodOps = {
684 : : .open_for_write = tar_open_for_write,
685 : : .close = tar_close,
686 : : .existsfile = tar_existsfile,
687 : : .get_file_size = tar_get_file_size,
688 : : .get_file_name = tar_get_file_name,
689 : : .write = tar_write,
690 : : .sync = tar_sync,
691 : : .finish = tar_finish,
692 : : .free = tar_free
693 : : };
694 : :
695 : : typedef struct TarMethodFile
696 : : {
697 : : Walfile base;
698 : : pgoff_t ofs_start; /* Where does the *header* for this file start */
699 : : char header[TAR_BLOCK_SIZE];
700 : : size_t pad_to_size;
701 : : } TarMethodFile;
702 : :
703 : : typedef struct TarMethodData
704 : : {
705 : : WalWriteMethod base;
706 : : char *tarfilename;
707 : : int fd;
708 : : TarMethodFile *currentfile;
709 : : #ifdef HAVE_LIBZ
710 : : z_streamp zp;
711 : : void *zlibOut;
712 : : #endif
713 : : } TarMethodData;
714 : :
715 : : #ifdef HAVE_LIBZ
716 : : static bool
717 : 8076 : tar_write_compressed_data(TarMethodData *tar_data, const void *buf, size_t count,
718 : : bool flush)
719 : : {
720 : 8076 : tar_data->zp->next_in = buf;
721 : 8076 : tar_data->zp->avail_in = count;
722 : :
723 [ + + + + ]: 16154 : while (tar_data->zp->avail_in || flush)
724 : : {
725 : : int r;
726 : :
727 [ + + ]: 8102 : r = deflate(tar_data->zp, flush ? Z_FINISH : Z_NO_FLUSH);
728 [ - + ]: 8102 : if (r == Z_STREAM_ERROR)
729 : : {
730 : 0 : tar_data->base.lasterrstring = _("could not compress data");
731 : 0 : return false;
732 : : }
733 : :
734 [ + + ]: 8102 : if (tar_data->zp->avail_out < ZLIB_OUT_SIZE)
735 : : {
736 : 83 : size_t len = ZLIB_OUT_SIZE - tar_data->zp->avail_out;
737 : :
738 : 83 : errno = 0;
739 [ - + ]: 83 : if (write(tar_data->fd, tar_data->zlibOut, len) != len)
740 : : {
741 : : /* If write didn't set errno, assume problem is no disk space */
742 [ # # ]: 0 : tar_data->base.lasterrno = errno ? errno : ENOSPC;
743 : 0 : return false;
744 : : }
745 : :
746 : 83 : tar_data->zp->next_out = tar_data->zlibOut;
747 : 83 : tar_data->zp->avail_out = ZLIB_OUT_SIZE;
748 : : }
749 : :
750 [ + + ]: 8102 : if (r == Z_STREAM_END)
751 : 24 : break;
752 : : }
753 : :
754 [ + + ]: 8076 : if (flush)
755 : : {
756 : : /* Reset the stream for writing */
757 [ - + ]: 24 : if (deflateReset(tar_data->zp) != Z_OK)
758 : : {
759 : 0 : tar_data->base.lasterrstring = _("could not reset compression stream");
760 : 0 : return false;
761 : : }
762 : : }
763 : :
764 : 8076 : return true;
765 : : }
766 : : #endif
767 : :
768 : : static ssize_t
769 : 30607 : tar_write(Walfile *f, const void *buf, size_t count)
770 : : {
771 : 30607 : TarMethodData *tar_data = (TarMethodData *) f->wwmethod;
772 : : ssize_t r;
773 : :
774 : : Assert(f != NULL);
775 : 30607 : clear_error(f->wwmethod);
776 : :
777 : : /* Tarfile will always be positioned at the end */
778 [ + + ]: 30607 : if (f->wwmethod->compression_algorithm == PG_COMPRESSION_NONE)
779 : : {
780 : 22559 : errno = 0;
781 : 22559 : r = write(tar_data->fd, buf, count);
782 [ - + ]: 22559 : if (r != count)
783 : : {
784 : : /* If write didn't set errno, assume problem is no disk space */
785 [ # # ]: 0 : f->wwmethod->lasterrno = errno ? errno : ENOSPC;
786 : 0 : return -1;
787 : : }
788 : 22559 : f->currpos += r;
789 : 22559 : return r;
790 : : }
791 : : #ifdef HAVE_LIBZ
792 [ + - ]: 8048 : else if (f->wwmethod->compression_algorithm == PG_COMPRESSION_GZIP)
793 : : {
794 [ - + ]: 8048 : if (!tar_write_compressed_data(tar_data, buf, count, false))
795 : 0 : return -1;
796 : 8048 : f->currpos += count;
797 : 8048 : return count;
798 : : }
799 : : #endif
800 : : else
801 : : {
802 : : /* Can't happen - compression enabled with no method set */
803 : 0 : f->wwmethod->lasterrno = ENOSYS;
804 : 0 : return -1;
805 : : }
806 : : }
807 : :
808 : : static bool
809 : 15 : tar_write_padding_data(TarMethodFile *f, size_t bytes)
810 : : {
811 : : PGAlignedXLogBlock zerobuf;
812 : 15 : size_t bytesleft = bytes;
813 : :
814 : 15 : memset(zerobuf.data, 0, XLOG_BLCKSZ);
815 [ + + ]: 30575 : while (bytesleft)
816 : : {
817 : 30560 : size_t bytestowrite = Min(bytesleft, XLOG_BLCKSZ);
818 : 30560 : ssize_t r = tar_write(&f->base, zerobuf.data, bytestowrite);
819 : :
820 [ - + ]: 30560 : if (r < 0)
821 : 0 : return false;
822 : 30560 : bytesleft -= r;
823 : : }
824 : :
825 : 15 : return true;
826 : : }
827 : :
828 : : static char *
829 : 49 : tar_get_file_name(WalWriteMethod *wwmethod, const char *pathname,
830 : : const char *temp_suffix)
831 : : {
832 : 49 : char *filename = pg_malloc0_array(char, MAXPGPATH);
833 : :
834 [ + + ]: 49 : snprintf(filename, MAXPGPATH, "%s%s",
835 : : pathname, temp_suffix ? temp_suffix : "");
836 : :
837 : 49 : return filename;
838 : : }
839 : :
840 : : static Walfile *
841 : 19 : tar_open_for_write(WalWriteMethod *wwmethod, const char *pathname,
842 : : const char *temp_suffix, size_t pad_to_size)
843 : : {
844 : 19 : TarMethodData *tar_data = (TarMethodData *) wwmethod;
845 : : char *tmppath;
846 : :
847 : 19 : clear_error(wwmethod);
848 : :
849 [ + + ]: 19 : if (tar_data->fd < 0)
850 : : {
851 : : /*
852 : : * We open the tar file only when we first try to write to it.
853 : : */
854 : 15 : tar_data->fd = open(tar_data->tarfilename,
855 : : O_WRONLY | O_CREAT | PG_BINARY,
856 : : pg_file_create_mode);
857 [ - + ]: 15 : if (tar_data->fd < 0)
858 : : {
859 : 0 : wwmethod->lasterrno = errno;
860 : 0 : return NULL;
861 : : }
862 : :
863 : : #ifdef HAVE_LIBZ
864 [ + + ]: 15 : if (wwmethod->compression_algorithm == PG_COMPRESSION_GZIP)
865 : : {
866 : 4 : tar_data->zp = pg_malloc_object(z_stream);
867 : 4 : tar_data->zp->zalloc = Z_NULL;
868 : 4 : tar_data->zp->zfree = Z_NULL;
869 : 4 : tar_data->zp->opaque = Z_NULL;
870 : 4 : tar_data->zp->next_out = tar_data->zlibOut;
871 : 4 : tar_data->zp->avail_out = ZLIB_OUT_SIZE;
872 : :
873 : : /*
874 : : * Initialize deflation library. Adding the magic value 16 to the
875 : : * default 15 for the windowBits parameter makes the output be
876 : : * gzip instead of zlib.
877 : : */
878 [ - + ]: 4 : if (deflateInit2(tar_data->zp, wwmethod->compression_level,
879 : : Z_DEFLATED, 15 + 16, 8, Z_DEFAULT_STRATEGY) != Z_OK)
880 : : {
881 : 0 : pg_free(tar_data->zp);
882 : 0 : tar_data->zp = NULL;
883 : 0 : wwmethod->lasterrstring =
884 : 0 : _("could not initialize compression library");
885 : 0 : return NULL;
886 : : }
887 : : }
888 : : #endif
889 : :
890 : : /* There's no tar header itself, the file starts with regular files */
891 : : }
892 : :
893 [ - + ]: 19 : if (tar_data->currentfile != NULL)
894 : : {
895 : 0 : wwmethod->lasterrstring =
896 : 0 : _("implementation error: tar files can't have more than one open file");
897 : 0 : return NULL;
898 : : }
899 : :
900 : 19 : tar_data->currentfile = pg_malloc0_object(TarMethodFile);
901 : 19 : tar_data->currentfile->base.wwmethod = wwmethod;
902 : :
903 : 19 : tmppath = tar_get_file_name(wwmethod, pathname, temp_suffix);
904 : :
905 : : /* Create a header with size set to 0 - we will fill out the size on close */
906 [ - + ]: 19 : if (tarCreateHeader(tar_data->currentfile->header, tmppath, NULL, 0, S_IRUSR | S_IWUSR, 0, 0, time(NULL)) != TAR_OK)
907 : : {
908 : 0 : pg_free(tar_data->currentfile);
909 : 0 : pg_free(tmppath);
910 : 0 : tar_data->currentfile = NULL;
911 : 0 : wwmethod->lasterrstring = _("could not create tar header");
912 : 0 : return NULL;
913 : : }
914 : :
915 : 19 : pg_free(tmppath);
916 : :
917 : : #ifdef HAVE_LIBZ
918 [ + + ]: 19 : if (wwmethod->compression_algorithm == PG_COMPRESSION_GZIP)
919 : : {
920 : : /* Flush existing data */
921 [ - + ]: 6 : if (!tar_write_compressed_data(tar_data, NULL, 0, true))
922 : 0 : return NULL;
923 : :
924 : : /* Turn off compression for header */
925 [ - + ]: 6 : if (deflateParams(tar_data->zp, 0, Z_DEFAULT_STRATEGY) != Z_OK)
926 : : {
927 : 0 : wwmethod->lasterrstring =
928 : 0 : _("could not change compression parameters");
929 : 0 : return NULL;
930 : : }
931 : : }
932 : : #endif
933 : :
934 : 19 : tar_data->currentfile->ofs_start = lseek(tar_data->fd, 0, SEEK_CUR);
935 [ - + ]: 19 : if (tar_data->currentfile->ofs_start == -1)
936 : : {
937 : 0 : wwmethod->lasterrno = errno;
938 : 0 : pg_free(tar_data->currentfile);
939 : 0 : tar_data->currentfile = NULL;
940 : 0 : return NULL;
941 : : }
942 : 19 : tar_data->currentfile->base.currpos = 0;
943 : :
944 [ + + ]: 19 : if (wwmethod->compression_algorithm == PG_COMPRESSION_NONE)
945 : : {
946 : 13 : errno = 0;
947 [ - + ]: 13 : if (write(tar_data->fd, tar_data->currentfile->header,
948 : : TAR_BLOCK_SIZE) != TAR_BLOCK_SIZE)
949 : : {
950 : : /* If write didn't set errno, assume problem is no disk space */
951 [ # # ]: 0 : wwmethod->lasterrno = errno ? errno : ENOSPC;
952 : 0 : pg_free(tar_data->currentfile);
953 : 0 : tar_data->currentfile = NULL;
954 : 0 : return NULL;
955 : : }
956 : : }
957 : : #ifdef HAVE_LIBZ
958 [ + - ]: 6 : else if (wwmethod->compression_algorithm == PG_COMPRESSION_GZIP)
959 : : {
960 : : /* Write header through the zlib APIs but with no compression */
961 [ - + ]: 6 : if (!tar_write_compressed_data(tar_data, tar_data->currentfile->header,
962 : : TAR_BLOCK_SIZE, true))
963 : 0 : return NULL;
964 : :
965 : : /* Re-enable compression for the rest of the file */
966 [ - + ]: 6 : if (deflateParams(tar_data->zp, wwmethod->compression_level,
967 : : Z_DEFAULT_STRATEGY) != Z_OK)
968 : : {
969 : 0 : wwmethod->lasterrstring = _("could not change compression parameters");
970 : 0 : return NULL;
971 : : }
972 : : }
973 : : #endif
974 : : else
975 : : {
976 : : /* not reachable */
977 : : Assert(false);
978 : : }
979 : :
980 : 19 : tar_data->currentfile->base.pathname = pg_strdup(pathname);
981 : :
982 : : /*
983 : : * Uncompressed files are padded on creation, but for compression we can't
984 : : * do that
985 : : */
986 [ + + ]: 19 : if (pad_to_size)
987 : : {
988 : 15 : tar_data->currentfile->pad_to_size = pad_to_size;
989 [ + + ]: 15 : if (wwmethod->compression_algorithm == PG_COMPRESSION_NONE)
990 : : {
991 : : /* Uncompressed, so pad now */
992 [ - + ]: 11 : if (!tar_write_padding_data(tar_data->currentfile, pad_to_size))
993 : 0 : return NULL;
994 : : /* Seek back to start */
995 : 22 : if (lseek(tar_data->fd,
996 : 11 : tar_data->currentfile->ofs_start + TAR_BLOCK_SIZE,
997 [ - + ]: 11 : SEEK_SET) != tar_data->currentfile->ofs_start + TAR_BLOCK_SIZE)
998 : : {
999 : 0 : wwmethod->lasterrno = errno;
1000 : 0 : return NULL;
1001 : : }
1002 : :
1003 : 11 : tar_data->currentfile->base.currpos = 0;
1004 : : }
1005 : : }
1006 : :
1007 : 19 : return &tar_data->currentfile->base;
1008 : : }
1009 : :
1010 : : static ssize_t
1011 : 0 : tar_get_file_size(WalWriteMethod *wwmethod, const char *pathname)
1012 : : {
1013 : 0 : clear_error(wwmethod);
1014 : :
1015 : : /* Currently not used, so not supported */
1016 : 0 : wwmethod->lasterrno = ENOSYS;
1017 : 0 : return -1;
1018 : : }
1019 : :
1020 : : static int
1021 : 19 : tar_sync(Walfile *f)
1022 : : {
1023 : 19 : TarMethodData *tar_data = (TarMethodData *) f->wwmethod;
1024 : : int r;
1025 : :
1026 : : Assert(f != NULL);
1027 : 19 : clear_error(f->wwmethod);
1028 : :
1029 [ + - ]: 19 : if (!f->wwmethod->sync)
1030 : 19 : return 0;
1031 : :
1032 : : /*
1033 : : * Always sync the whole tarfile, because that's all we can do. This makes
1034 : : * no sense on compressed files, so just ignore those.
1035 : : */
1036 [ # # ]: 0 : if (f->wwmethod->compression_algorithm != PG_COMPRESSION_NONE)
1037 : 0 : return 0;
1038 : :
1039 : 0 : r = fsync(tar_data->fd);
1040 [ # # ]: 0 : if (r < 0)
1041 : 0 : f->wwmethod->lasterrno = errno;
1042 : 0 : return r;
1043 : : }
1044 : :
1045 : : static int
1046 : 19 : tar_close(Walfile *f, WalCloseMethod method)
1047 : : {
1048 : : ssize_t filesize;
1049 : : int padding;
1050 : 19 : TarMethodData *tar_data = (TarMethodData *) f->wwmethod;
1051 : 19 : TarMethodFile *tf = (TarMethodFile *) f;
1052 : :
1053 : : Assert(f != NULL);
1054 : 19 : clear_error(f->wwmethod);
1055 : :
1056 [ - + ]: 19 : if (method == CLOSE_UNLINK)
1057 : : {
1058 [ # # ]: 0 : if (f->wwmethod->compression_algorithm != PG_COMPRESSION_NONE)
1059 : : {
1060 : 0 : f->wwmethod->lasterrstring = _("unlink not supported with compression");
1061 : 0 : return -1;
1062 : : }
1063 : :
1064 : : /*
1065 : : * Unlink the file that we just wrote to the tar. We do this by
1066 : : * truncating it to the start of the header. This is safe as we only
1067 : : * allow writing of the very last file.
1068 : : */
1069 [ # # ]: 0 : if (ftruncate(tar_data->fd, tf->ofs_start) != 0)
1070 : : {
1071 : 0 : f->wwmethod->lasterrno = errno;
1072 : 0 : return -1;
1073 : : }
1074 : :
1075 : 0 : pg_free(tf->base.pathname);
1076 : 0 : pg_free(tf);
1077 : 0 : tar_data->currentfile = NULL;
1078 : :
1079 : 0 : return 0;
1080 : : }
1081 : :
1082 : : /*
1083 : : * Pad the file itself with zeroes if necessary. Note that this is
1084 : : * different from the tar format padding -- this is the padding we asked
1085 : : * for when the file was opened.
1086 : : */
1087 [ + + ]: 19 : if (tf->pad_to_size)
1088 : : {
1089 [ + + ]: 15 : if (f->wwmethod->compression_algorithm == PG_COMPRESSION_GZIP)
1090 : : {
1091 : : /*
1092 : : * A compressed tarfile is padded on close since we cannot know
1093 : : * the size of the compressed output until the end.
1094 : : */
1095 : 4 : size_t sizeleft = tf->pad_to_size - tf->base.currpos;
1096 : :
1097 [ + - ]: 4 : if (sizeleft)
1098 : : {
1099 [ - + ]: 4 : if (!tar_write_padding_data(tf, sizeleft))
1100 : 0 : return -1;
1101 : : }
1102 : : }
1103 : : else
1104 : : {
1105 : : /*
1106 : : * An uncompressed tarfile was padded on creation, so just adjust
1107 : : * the current position as if we seeked to the end.
1108 : : */
1109 : 11 : tf->base.currpos = tf->pad_to_size;
1110 : : }
1111 : : }
1112 : :
1113 : : /*
1114 : : * Get the size of the file, and pad out to a multiple of the tar block
1115 : : * size.
1116 : : */
1117 : 19 : filesize = f->currpos;
1118 : 19 : padding = tarPaddingBytesRequired(filesize);
1119 [ + + ]: 19 : if (padding)
1120 : : {
1121 : 2 : char zerobuf[TAR_BLOCK_SIZE] = {0};
1122 : :
1123 [ - + ]: 2 : if (tar_write(f, zerobuf, padding) != padding)
1124 : 0 : return -1;
1125 : : }
1126 : :
1127 : :
1128 : : #ifdef HAVE_LIBZ
1129 [ + + ]: 19 : if (f->wwmethod->compression_algorithm == PG_COMPRESSION_GZIP)
1130 : : {
1131 : : /* Flush the current buffer */
1132 [ - + ]: 6 : if (!tar_write_compressed_data(tar_data, NULL, 0, true))
1133 : 0 : return -1;
1134 : : }
1135 : : #endif
1136 : :
1137 : : /*
1138 : : * Now go back and update the header with the correct filesize and
1139 : : * possibly also renaming the file. We overwrite the entire current header
1140 : : * when done, including the checksum.
1141 : : */
1142 : 19 : print_tar_number(&(tf->header[TAR_OFFSET_SIZE]), 12, filesize);
1143 : :
1144 [ + - ]: 19 : if (method == CLOSE_NORMAL)
1145 : :
1146 : : /*
1147 : : * We overwrite it with what it was before if we have no tempname,
1148 : : * since we're going to write the buffer anyway.
1149 : : */
1150 : 19 : strlcpy(&(tf->header[TAR_OFFSET_NAME]), tf->base.pathname, 100);
1151 : :
1152 : 19 : print_tar_number(&(tf->header[TAR_OFFSET_CHECKSUM]), 8,
1153 : 19 : tarChecksum(((TarMethodFile *) f)->header));
1154 [ - + ]: 19 : if (lseek(tar_data->fd, tf->ofs_start, SEEK_SET) != ((TarMethodFile *) f)->ofs_start)
1155 : : {
1156 : 0 : f->wwmethod->lasterrno = errno;
1157 : 0 : return -1;
1158 : : }
1159 [ + + ]: 19 : if (f->wwmethod->compression_algorithm == PG_COMPRESSION_NONE)
1160 : : {
1161 : 13 : errno = 0;
1162 [ - + ]: 13 : if (write(tar_data->fd, tf->header, TAR_BLOCK_SIZE) != TAR_BLOCK_SIZE)
1163 : : {
1164 : : /* If write didn't set errno, assume problem is no disk space */
1165 [ # # ]: 0 : f->wwmethod->lasterrno = errno ? errno : ENOSPC;
1166 : 0 : return -1;
1167 : : }
1168 : : }
1169 : : #ifdef HAVE_LIBZ
1170 [ + - ]: 6 : else if (f->wwmethod->compression_algorithm == PG_COMPRESSION_GZIP)
1171 : : {
1172 : : /* Turn off compression */
1173 [ - + ]: 6 : if (deflateParams(tar_data->zp, 0, Z_DEFAULT_STRATEGY) != Z_OK)
1174 : : {
1175 : 0 : f->wwmethod->lasterrstring = _("could not change compression parameters");
1176 : 0 : return -1;
1177 : : }
1178 : :
1179 : : /* Overwrite the header, assuming the size will be the same */
1180 [ - + ]: 6 : if (!tar_write_compressed_data(tar_data, tar_data->currentfile->header,
1181 : : TAR_BLOCK_SIZE, true))
1182 : 0 : return -1;
1183 : :
1184 : : /* Turn compression back on */
1185 [ - + ]: 6 : if (deflateParams(tar_data->zp, f->wwmethod->compression_level,
1186 : : Z_DEFAULT_STRATEGY) != Z_OK)
1187 : : {
1188 : 0 : f->wwmethod->lasterrstring = _("could not change compression parameters");
1189 : 0 : return -1;
1190 : : }
1191 : : }
1192 : : #endif
1193 : : else
1194 : : {
1195 : : /* not reachable */
1196 : : Assert(false);
1197 : : }
1198 : :
1199 : : /* Move file pointer back down to end, so we can write the next file */
1200 [ - + ]: 19 : if (lseek(tar_data->fd, 0, SEEK_END) < 0)
1201 : : {
1202 : 0 : f->wwmethod->lasterrno = errno;
1203 : 0 : return -1;
1204 : : }
1205 : :
1206 : : /* Always fsync on close, so the padding gets fsynced */
1207 [ - + ]: 19 : if (tar_sync(f) < 0)
1208 : : {
1209 : : /* XXX this seems pretty bogus; why is only this case fatal? */
1210 : 0 : pg_fatal("could not fsync file \"%s\": %s",
1211 : : tf->base.pathname, GetLastWalMethodError(f->wwmethod));
1212 : : }
1213 : :
1214 : : /* Clean up and done */
1215 : 19 : pg_free(tf->base.pathname);
1216 : 19 : pg_free(tf);
1217 : 19 : tar_data->currentfile = NULL;
1218 : :
1219 : 19 : return 0;
1220 : : }
1221 : :
1222 : : static bool
1223 : 13 : tar_existsfile(WalWriteMethod *wwmethod, const char *pathname)
1224 : : {
1225 : 13 : clear_error(wwmethod);
1226 : : /* We only deal with new tarfiles, so nothing externally created exists */
1227 : 13 : return false;
1228 : : }
1229 : :
1230 : : static bool
1231 : 15 : tar_finish(WalWriteMethod *wwmethod)
1232 : : {
1233 : 15 : TarMethodData *tar_data = (TarMethodData *) wwmethod;
1234 : 15 : char zerobuf[1024] = {0};
1235 : :
1236 : 15 : clear_error(wwmethod);
1237 : :
1238 [ - + ]: 15 : if (tar_data->currentfile)
1239 : : {
1240 [ # # ]: 0 : if (tar_close(&tar_data->currentfile->base, CLOSE_NORMAL) != 0)
1241 : 0 : return false;
1242 : : }
1243 : :
1244 : : /* A tarfile always ends with two empty blocks */
1245 [ + + ]: 15 : if (wwmethod->compression_algorithm == PG_COMPRESSION_NONE)
1246 : : {
1247 : 11 : errno = 0;
1248 [ - + ]: 11 : if (write(tar_data->fd, zerobuf, sizeof(zerobuf)) != sizeof(zerobuf))
1249 : : {
1250 : : /* If write didn't set errno, assume problem is no disk space */
1251 [ # # ]: 0 : wwmethod->lasterrno = errno ? errno : ENOSPC;
1252 : 0 : return false;
1253 : : }
1254 : : }
1255 : : #ifdef HAVE_LIBZ
1256 [ + - ]: 4 : else if (wwmethod->compression_algorithm == PG_COMPRESSION_GZIP)
1257 : : {
1258 [ - + ]: 4 : if (!tar_write_compressed_data(tar_data, zerobuf, sizeof(zerobuf),
1259 : : false))
1260 : 0 : return false;
1261 : :
1262 : : /* Also flush all data to make sure the gzip stream is finished */
1263 : 4 : tar_data->zp->next_in = NULL;
1264 : 4 : tar_data->zp->avail_in = 0;
1265 : : while (true)
1266 : 0 : {
1267 : : int r;
1268 : :
1269 : 4 : r = deflate(tar_data->zp, Z_FINISH);
1270 : :
1271 [ - + ]: 4 : if (r == Z_STREAM_ERROR)
1272 : : {
1273 : 0 : wwmethod->lasterrstring = _("could not compress data");
1274 : 0 : return false;
1275 : : }
1276 [ + - ]: 4 : if (tar_data->zp->avail_out < ZLIB_OUT_SIZE)
1277 : : {
1278 : 4 : size_t len = ZLIB_OUT_SIZE - tar_data->zp->avail_out;
1279 : :
1280 : 4 : errno = 0;
1281 [ - + ]: 4 : if (write(tar_data->fd, tar_data->zlibOut, len) != len)
1282 : : {
1283 : : /*
1284 : : * If write didn't set errno, assume problem is no disk
1285 : : * space.
1286 : : */
1287 [ # # ]: 0 : wwmethod->lasterrno = errno ? errno : ENOSPC;
1288 : 0 : return false;
1289 : : }
1290 : : }
1291 [ + - ]: 4 : if (r == Z_STREAM_END)
1292 : 4 : break;
1293 : : }
1294 : :
1295 [ - + ]: 4 : if (deflateEnd(tar_data->zp) != Z_OK)
1296 : : {
1297 : 0 : wwmethod->lasterrstring = _("could not close compression stream");
1298 : 0 : return false;
1299 : : }
1300 : : }
1301 : : #endif
1302 : : else
1303 : : {
1304 : : /* not reachable */
1305 : : Assert(false);
1306 : : }
1307 : :
1308 : : /* sync the empty blocks as well, since they're after the last file */
1309 [ - + ]: 15 : if (wwmethod->sync)
1310 : : {
1311 [ # # ]: 0 : if (fsync(tar_data->fd) != 0)
1312 : : {
1313 : 0 : wwmethod->lasterrno = errno;
1314 : 0 : return false;
1315 : : }
1316 : : }
1317 : :
1318 [ - + ]: 15 : if (close(tar_data->fd) != 0)
1319 : : {
1320 : 0 : wwmethod->lasterrno = errno;
1321 : 0 : return false;
1322 : : }
1323 : :
1324 : 15 : tar_data->fd = -1;
1325 : :
1326 [ - + ]: 15 : if (wwmethod->sync)
1327 : : {
1328 [ # # # # ]: 0 : if (fsync_fname(tar_data->tarfilename, false) != 0 ||
1329 : 0 : fsync_parent_path(tar_data->tarfilename) != 0)
1330 : : {
1331 : 0 : wwmethod->lasterrno = errno;
1332 : 0 : return false;
1333 : : }
1334 : : }
1335 : :
1336 : 15 : return true;
1337 : : }
1338 : :
1339 : : static void
1340 : 15 : tar_free(WalWriteMethod *wwmethod)
1341 : : {
1342 : 15 : TarMethodData *tar_data = (TarMethodData *) wwmethod;
1343 : :
1344 : 15 : pg_free(tar_data->tarfilename);
1345 : : #ifdef HAVE_LIBZ
1346 [ + + ]: 15 : if (wwmethod->compression_algorithm == PG_COMPRESSION_GZIP)
1347 : 4 : pg_free(tar_data->zlibOut);
1348 : : #endif
1349 : 15 : pg_free(wwmethod);
1350 : 15 : }
1351 : :
1352 : : /*
1353 : : * The argument compression_algorithm is currently ignored. It is in place for
1354 : : * symmetry with CreateWalDirectoryMethod which uses it for distinguishing
1355 : : * between the different compression methods. CreateWalTarMethod and its family
1356 : : * of functions handle only zlib compression.
1357 : : */
1358 : : WalWriteMethod *
1359 : 15 : CreateWalTarMethod(const char *tarbase,
1360 : : pg_compress_algorithm compression_algorithm,
1361 : : int compression_level, bool sync)
1362 : : {
1363 : : TarMethodData *wwmethod;
1364 : 15 : const char *suffix = (compression_algorithm == PG_COMPRESSION_GZIP) ?
1365 [ + + ]: 15 : ".tar.gz" : ".tar";
1366 : :
1367 : 15 : wwmethod = pg_malloc0_object(TarMethodData);
1368 : 15 : *((const WalWriteMethodOps **) &wwmethod->base.ops) =
1369 : : &WalTarMethodOps;
1370 : 15 : wwmethod->base.compression_algorithm = compression_algorithm;
1371 : 15 : wwmethod->base.compression_level = compression_level;
1372 : 15 : wwmethod->base.sync = sync;
1373 : 15 : clear_error(&wwmethod->base);
1374 : :
1375 : 15 : wwmethod->tarfilename = pg_malloc0(strlen(tarbase) + strlen(suffix) + 1);
1376 : 15 : sprintf(wwmethod->tarfilename, "%s%s", tarbase, suffix);
1377 : 15 : wwmethod->fd = -1;
1378 : : #ifdef HAVE_LIBZ
1379 [ + + ]: 15 : if (compression_algorithm == PG_COMPRESSION_GZIP)
1380 : 4 : wwmethod->zlibOut = (char *) pg_malloc(ZLIB_OUT_SIZE + 1);
1381 : : #endif
1382 : :
1383 : 15 : return &wwmethod->base;
1384 : : }
1385 : :
1386 : : const char *
1387 : 0 : GetLastWalMethodError(WalWriteMethod *wwmethod)
1388 : : {
1389 [ # # ]: 0 : if (wwmethod->lasterrstring)
1390 : 0 : return wwmethod->lasterrstring;
1391 : 0 : return strerror(wwmethod->lasterrno);
1392 : : }
|