36 #define ZLIB_OUT_SIZE 4096
39 #define LZ4_IN_SIZE 4096
74 LZ4F_compressionContext_t ctx;
80 #define dir_clear_error() \
81 (dir_data->lasterrstring = NULL, dir_data->lasterrno = 0)
82 #define dir_set_error(msg) \
83 (dir_data->lasterrstring = _(msg))
102 temp_suffix ? temp_suffix :
"");
118 LZ4F_compressionContext_t ctx = NULL;
119 size_t lz4bufsize = 0;
126 snprintf(tmppath,
sizeof(tmppath),
"%s/%s",
146 gzfp = gzdopen(
fd,
"wb");
155 Z_DEFAULT_STRATEGY) != Z_OK)
168 LZ4F_preferences_t prefs;
170 ctx_out = LZ4F_createCompressionContext(&ctx, LZ4F_VERSION);
171 if (LZ4F_isError(ctx_out))
178 lz4bufsize = LZ4F_compressBound(
LZ4_IN_SIZE, NULL);
182 memset(&prefs, 0,
sizeof(prefs));
186 header_size = LZ4F_compressBegin(ctx, lz4buf, lz4bufsize, &prefs);
187 if (LZ4F_isError(header_size))
190 (void) LZ4F_freeCompressionContext(ctx);
197 if (
write(
fd, lz4buf, header_size) != header_size)
201 (void) LZ4F_freeCompressionContext(ctx);
215 memset(zerobuf.
data, 0, XLOG_BLCKSZ);
216 for (bytes = 0; bytes < pad_to_size; bytes += XLOG_BLCKSZ)
219 if (
write(
fd, zerobuf.
data, XLOG_BLCKSZ) != XLOG_BLCKSZ)
228 if (lseek(
fd, 0, SEEK_SET) != 0)
256 (void) LZ4F_compressEnd(ctx, lz4buf, lz4bufsize, NULL);
257 (void) LZ4F_freeCompressionContext(ctx);
278 f->lz4bufsize = lz4bufsize;
305 r = (ssize_t) gzwrite(df->gzfp,
buf, count);
319 const void *inbuf =
buf;
332 compressed = LZ4F_compressUpdate(df->ctx,
333 df->lz4buf, df->lz4bufsize,
337 if (LZ4F_isError(compressed))
344 if (
write(df->
fd, df->lz4buf, compressed) != compressed)
351 inbuf = ((
char *) inbuf) + chunk;
398 r = gzclose(df->gzfp);
407 compressed = LZ4F_compressEnd(df->ctx,
408 df->lz4buf, df->lz4bufsize,
411 if (LZ4F_isError(compressed))
418 if (
write(df->
fd, df->lz4buf, compressed) != compressed)
444 snprintf(tmppath,
sizeof(tmppath),
"%s/%s",
450 snprintf(tmppath2,
sizeof(tmppath2),
"%s/%s",
457 if (rename(tmppath, tmppath2) != 0)
459 pg_log_error(
"could not rename file \"%s\" to \"%s\": %m",
471 snprintf(tmppath,
sizeof(tmppath),
"%s/%s",
498 LZ4F_freeCompressionContext(df->ctx);
538 compressed = LZ4F_flush(df->ctx, df->lz4buf, df->lz4bufsize, NULL);
539 if (LZ4F_isError(compressed))
546 if (
write(df->
fd, df->lz4buf, compressed) != compressed)
567 snprintf(tmppath,
sizeof(tmppath),
"%s/%s",
570 if (
stat(tmppath, &statbuf) != 0)
593 snprintf(tmppath,
sizeof(tmppath),
"%s/%s",
627 int compression_level,
bool sync)
693 #define tar_clear_error() \
694 (tar_data->lasterrstring = NULL, tar_data->lasterrno = 0)
695 #define tar_set_error(msg) \
696 (tar_data->lasterrstring = _(msg))
708 tar_write_compressed_data(
void *
buf,
size_t count,
bool flush)
713 while (
tar_data->zp->avail_in || flush)
717 r = deflate(
tar_data->zp, flush ? Z_FINISH : Z_NO_FLUSH);
718 if (r == Z_STREAM_ERROR)
740 if (r == Z_STREAM_END)
747 if (deflateReset(
tar_data->zp) != Z_OK)
783 if (!tar_write_compressed_data(
unconstify(
void *,
buf), count,
false))
801 size_t bytesleft = bytes;
803 memset(zerobuf.
data, 0, XLOG_BLCKSZ);
806 size_t bytestowrite =
Min(bytesleft, XLOG_BLCKSZ);
823 pathname, temp_suffix ? temp_suffix :
"");
865 Z_DEFLATED, 15 + 16, 8, Z_DEFAULT_STRATEGY) != Z_OK)
880 tar_set_error(
"implementation error: tar files can't have more than one open file");
904 if (!tar_write_compressed_data(NULL, 0,
true))
908 if (deflateParams(
tar_data->zp, 0, 0) != Z_OK)
1117 MemSet(zerobuf, 0, padding);
1118 if (
tar_write(f, zerobuf, padding) != padding)
1127 if (!tar_write_compressed_data(NULL, 0,
true))
1167 if (deflateParams(
tar_data->zp, 0, 0) != Z_OK)
1203 pg_fatal(
"could not fsync file \"%s\": %s",
1237 MemSet(zerobuf, 0,
sizeof(zerobuf));
1251 if (!tar_write_compressed_data(zerobuf,
sizeof(zerobuf),
false))
1261 r = deflate(
tar_data->zp, Z_FINISH);
1263 if (r == Z_STREAM_ERROR)
1283 if (r == Z_STREAM_END)
1287 if (deflateEnd(
tar_data->zp) != Z_OK)
1340 int compression_level,
bool sync)
#define unconstify(underlying_type, expr)
#define MemSet(start, val, len)
int durable_rename(const char *oldfile, const char *newfile, int elevel)
void fsync_fname(const char *fname, bool isdir)
static int fsync_parent_path(const char *fname, int elevel)
void * pg_malloc0(size_t size)
char * pg_strdup(const char *in)
void * pg_malloc(size_t size)
Assert(fmt[strlen(fmt) - 1] !='\n')
#define pg_log_error(...)
static pg_compress_algorithm compression_algorithm
static void header(const char *fmt,...) pg_attribute_printf(1
static size_t tarPaddingBytesRequired(size_t len)
int tarChecksum(char *header)
enum tarError tarCreateHeader(char *h, const char *filename, const char *linktarget, pgoff_t size, mode_t mode, uid_t uid, gid_t gid, time_t mtime)
void print_tar_number(char *s, int len, uint64 val)
size_t strlcpy(char *dst, const char *src, size_t siz)
static int fd(const char *x, int i)
const char * lasterrstring
pg_compress_algorithm compression_algorithm
pg_compress_algorithm compression_algorithm
TarMethodFile * currentfile
const char * lasterrstring
char header[TAR_BLOCK_SIZE]
const char *(* getlasterror)(void)
off_t(* get_current_pos)(Walfile f)
ssize_t(* get_file_size)(const char *pathname)
Walfile(* open_for_write)(const char *pathname, const char *temp_suffix, size_t pad_to_size)
bool(* existsfile)(const char *pathname)
int(* close)(Walfile f, WalCloseMethod method)
char *(* get_file_name)(const char *pathname, const char *temp_suffix)
ssize_t(* write)(Walfile f, const void *buf, size_t count)
pg_compress_algorithm(* compression_algorithm)(void)
static bool dir_existsfile(const char *pathname)
static bool tar_finish(void)
static Walfile dir_open_for_write(const char *pathname, const char *temp_suffix, size_t pad_to_size)
WalWriteMethod * CreateWalDirectoryMethod(const char *basedir, pg_compress_algorithm compression_algorithm, int compression_level, bool sync)
static int tar_sync(Walfile f)
struct DirectoryMethodData DirectoryMethodData
#define dir_clear_error()
static const char * dir_getlasterror(void)
void FreeWalTarMethod(void)
#define tar_clear_error()
static ssize_t dir_write(Walfile f, const void *buf, size_t count)
static bool dir_finish(void)
static pg_compress_algorithm tar_compression_algorithm(void)
static char * dir_get_file_name(const char *pathname, const char *temp_suffix)
static Walfile tar_open_for_write(const char *pathname, const char *temp_suffix, size_t pad_to_size)
static const char * tar_getlasterror(void)
static ssize_t dir_get_file_size(const char *pathname)
static off_t tar_get_current_pos(Walfile f)
struct TarMethodData TarMethodData
WalWriteMethod * CreateWalTarMethod(const char *tarbase, pg_compress_algorithm compression_algorithm, int compression_level, bool sync)
static off_t dir_get_current_pos(Walfile f)
static int tar_close(Walfile f, WalCloseMethod method)
static TarMethodData * tar_data
static ssize_t tar_get_file_size(const char *pathname)
static char * tar_get_file_name(const char *pathname, const char *temp_suffix)
struct DirectoryMethodFile DirectoryMethodFile
static int dir_close(Walfile f, WalCloseMethod method)
void FreeWalDirectoryMethod(void)
static ssize_t tar_write(Walfile f, const void *buf, size_t count)
#define tar_set_error(msg)
static int dir_sync(Walfile f)
struct TarMethodFile TarMethodFile
static DirectoryMethodData * dir_data
static bool tar_existsfile(const char *pathname)
static pg_compress_algorithm dir_compression_algorithm(void)
static bool tar_write_padding_data(TarMethodFile *f, size_t bytes)