| 1 |
/*------------------------------------------------------------------------- |
| 2 |
* |
| 3 |
* compress_io.h |
| 4 |
* Interface to compress_io.c routines |
| 5 |
* |
| 6 |
* Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group |
| 7 |
* Portions Copyright (c) 1994, Regents of the University of California |
| 8 |
* |
| 9 |
* IDENTIFICATION |
| 10 |
* src/bin/pg_dump/compress_io.h |
| 11 |
* |
| 12 |
*------------------------------------------------------------------------- |
| 13 |
*/ |
| 14 |
|
| 15 |
#ifndef __COMPRESS_IO__ |
| 16 |
#define __COMPRESS_IO__ |
| 17 |
|
| 18 |
#include "pg_backup_archiver.h" |
| 19 |
|
| 20 |
/* |
| 21 |
* Default size used for IO buffers |
| 22 |
* |
| 23 |
* When changing this value, it's necessary to check the relevant test cases |
| 24 |
* still exercise all the branches. This applies especially if the value is |
| 25 |
* increased, in which case the overflow buffer may not be needed. |
| 26 |
*/ |
| 27 |
#define DEFAULT_IO_BUFFER_SIZE 4096 |
| 28 |
|
| 29 |
extern char *supports_compression(const pg_compress_specification compression_spec); |
| 30 |
|
| 31 |
/* |
| 32 |
* Prototype for callback function used in writeData() |
| 33 |
*/ |
| 34 |
typedef void (*WriteFunc) (ArchiveHandle *AH, const char *buf, size_t len); |
| 35 |
|
| 36 |
/* |
| 37 |
* Prototype for callback function used in readData() |
| 38 |
* |
| 39 |
* readData will call the read function repeatedly, until it returns 0 to signal |
| 40 |
* EOF. readData passes a buffer to read the data into in *buf, of length |
| 41 |
* *buflen. If that's not big enough for the callback function, it can free() it |
| 42 |
* and malloc() a new one, returning the new buffer and its size in *buf and |
| 43 |
* *buflen. |
| 44 |
* |
| 45 |
* Returns the number of bytes read into *buf, or 0 on EOF. |
| 46 |
*/ |
| 47 |
typedef size_t (*ReadFunc) (ArchiveHandle *AH, char **buf, size_t *buflen); |
| 48 |
|
| 49 |
typedef struct CompressorState CompressorState; |
| 50 |
struct CompressorState |
| 51 |
{ |
| 52 |
/* |
| 53 |
* Read all compressed data from the input stream (via readF) and print it |
| 54 |
* out with ahwrite(). |
| 55 |
*/ |
| 56 |
void (*readData) (ArchiveHandle *AH, CompressorState *cs); |
| 57 |
|
| 58 |
/* |
| 59 |
* Compress and write data to the output stream (via writeF). |
| 60 |
*/ |
| 61 |
void (*writeData) (ArchiveHandle *AH, CompressorState *cs, |
| 62 |
const void *data, size_t dLen); |
| 63 |
|
| 64 |
/* |
| 65 |
* End compression and flush internal buffers if any. |
| 66 |
*/ |
| 67 |
void (*end) (ArchiveHandle *AH, CompressorState *cs); |
| 68 |
|
| 69 |
/* |
| 70 |
* Callback function to read from an already processed input stream |
| 71 |
*/ |
| 72 |
ReadFunc readF; |
| 73 |
|
| 74 |
/* |
| 75 |
* Callback function to write an already processed chunk of data. |
| 76 |
*/ |
| 77 |
WriteFunc writeF; |
| 78 |
|
| 79 |
/* |
| 80 |
* Compression specification for this state. |
| 81 |
*/ |
| 82 |
pg_compress_specification compression_spec; |
| 83 |
|
| 84 |
/* |
| 85 |
* Private data to be used by the compressor. |
| 86 |
*/ |
| 87 |
void *private_data; |
| 88 |
}; |
| 89 |
|
| 90 |
extern CompressorState *AllocateCompressor(const pg_compress_specification compression_spec, |
| 91 |
ReadFunc readF, |
| 92 |
WriteFunc writeF); |
| 93 |
extern void EndCompressor(ArchiveHandle *AH, CompressorState *cs); |
| 94 |
|
| 95 |
/* |
| 96 |
* Compress File Handle |
| 97 |
*/ |
| 98 |
typedef struct CompressFileHandle CompressFileHandle; |
| 99 |
|
| 100 |
struct CompressFileHandle |
| 101 |
{ |
| 102 |
/* |
| 103 |
* Open a file in mode. |
| 104 |
* |
| 105 |
* Pass either 'path' or 'fd' depending on whether a file path or a file |
| 106 |
* descriptor is available. 'mode' can be one of 'r', 'rb', 'w', 'wb', |
| 107 |
* 'a', and 'ab'. Requires an already initialized CompressFileHandle. |
| 108 |
* |
| 109 |
* Returns true on success and false on error. |
| 110 |
*/ |
| 111 |
bool (*open_func) (const char *path, int fd, const char *mode, |
| 112 |
CompressFileHandle *CFH); |
| 113 |
|
| 114 |
/* |
| 115 |
* Open a file for writing. |
| 116 |
* |
| 117 |
* 'mode' can be one of 'w', 'wb', 'a', and 'ab'. Requires an already |
| 118 |
* initialized CompressFileHandle. |
| 119 |
* |
| 120 |
* Returns true on success and false on error. |
| 121 |
*/ |
| 122 |
bool (*open_write_func) (const char *path, const char *mode, |
| 123 |
CompressFileHandle *CFH); |
| 124 |
|
| 125 |
/* |
| 126 |
* Read up to 'size' bytes of data from the file and store them into |
| 127 |
* 'ptr'. |
| 128 |
* |
| 129 |
* Returns number of bytes read (this might be less than 'size' if EOF was |
| 130 |
* reached). Exits via pg_fatal for all error conditions. |
| 131 |
*/ |
| 132 |
size_t (*read_func) (void *ptr, size_t size, |
| 133 |
CompressFileHandle *CFH); |
| 134 |
|
| 135 |
/* |
| 136 |
* Write 'size' bytes of data into the file from 'ptr'. |
| 137 |
* |
| 138 |
* Returns nothing, exits via pg_fatal for all error conditions. |
| 139 |
*/ |
| 140 |
void (*write_func) (const void *ptr, size_t size, |
| 141 |
CompressFileHandle *CFH); |
| 142 |
|
| 143 |
/* |
| 144 |
* Read at most size - 1 characters from the compress file handle into |
| 145 |
* 's'. |
| 146 |
* |
| 147 |
* Stop if an EOF or a newline is found first. 's' is always null |
| 148 |
* terminated and contains the newline if it was found. |
| 149 |
* |
| 150 |
* Returns 's' on success, and NULL on error or when end of file occurs |
| 151 |
* while no characters have been read. |
| 152 |
*/ |
| 153 |
char *(*gets_func) (char *s, int size, CompressFileHandle *CFH); |
| 154 |
|
| 155 |
/* |
| 156 |
* Read the next character from the compress file handle as 'unsigned |
| 157 |
* char' cast into 'int'. |
| 158 |
* |
| 159 |
* Returns the character read on success and throws an internal error |
| 160 |
* otherwise. It treats EOF as error. |
| 161 |
*/ |
| 162 |
int (*getc_func) (CompressFileHandle *CFH); |
| 163 |
|
| 164 |
/* |
| 165 |
* Test if EOF is reached in the compress file handle. |
| 166 |
* |
| 167 |
* Returns true if it is reached. |
| 168 |
*/ |
| 169 |
bool (*eof_func) (CompressFileHandle *CFH); |
| 170 |
|
| 171 |
/* |
| 172 |
* Close an open file handle. |
| 173 |
* |
| 174 |
* Returns true on success and false on error. |
| 175 |
*/ |
| 176 |
bool (*close_func) (CompressFileHandle *CFH); |
| 177 |
|
| 178 |
/* |
| 179 |
* Get a pointer to a string that describes an error that occurred during |
| 180 |
* a compress file handle operation. |
| 181 |
*/ |
| 182 |
const char *(*get_error_func) (CompressFileHandle *CFH); |
| 183 |
|
| 184 |
/* |
| 185 |
* Compression specification for this file handle. |
| 186 |
*/ |
| 187 |
pg_compress_specification compression_spec; |
| 188 |
|
| 189 |
/* |
| 190 |
* Private data to be used by the compressor. |
| 191 |
*/ |
| 192 |
void *private_data; |
| 193 |
}; |
| 194 |
|
| 195 |
/* |
| 196 |
* Initialize a compress file handle with the requested compression. |
| 197 |
*/ |
| 198 |
extern CompressFileHandle *InitCompressFileHandle(const pg_compress_specification compression_spec); |
| 199 |
|
| 200 |
/* |
| 201 |
* Initialize a compress file stream. Infer the compression algorithm |
| 202 |
* from 'path', either by examining its suffix or by appending the supported |
| 203 |
* suffixes in 'path'. |
| 204 |
*/ |
| 205 |
extern CompressFileHandle *InitDiscoverCompressFileHandle(const char *path, |
| 206 |
const char *mode); |
| 207 |
extern bool EndCompressFileHandle(CompressFileHandle *CFH); |
| 208 |
#endif |