PostgreSQL Source Code  git master
compress_none.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * compress_none.c
4  * Routines for archivers to read or write an uncompressed stream.
5  *
6  * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  * IDENTIFICATION
10  * src/bin/pg_dump/compress_none.c
11  *
12  *-------------------------------------------------------------------------
13  */
14 #include "postgres_fe.h"
15 #include <unistd.h>
16 
17 #include "compress_none.h"
18 #include "pg_backup_utils.h"
19 
20 /*----------------------
21  * Compressor API
22  *----------------------
23  */
24 
25 /*
26  * Private routines
27  */
28 
29 static void
31 {
32  size_t cnt;
33  char *buf;
34  size_t buflen;
35 
36  buflen = DEFAULT_IO_BUFFER_SIZE;
37  buf = pg_malloc(buflen);
38 
39  while ((cnt = cs->readF(AH, &buf, &buflen)))
40  {
41  ahwrite(buf, 1, cnt, AH);
42  }
43 
44  free(buf);
45 }
46 
47 
48 static void
50  const void *data, size_t dLen)
51 {
52  cs->writeF(AH, data, dLen);
53 }
54 
55 static void
57 {
58  /* no op */
59 }
60 
61 /*
62  * Public interface
63  */
64 
65 void
67  const pg_compress_specification compression_spec)
68 {
71  cs->end = EndCompressorNone;
72 
73  cs->compression_spec = compression_spec;
74 }
75 
76 
77 /*----------------------
78  * Compress File API
79  *----------------------
80  */
81 
82 /*
83  * Private routines
84  */
85 
86 static bool
87 read_none(void *ptr, size_t size, size_t *rsize, CompressFileHandle *CFH)
88 {
89  FILE *fp = (FILE *) CFH->private_data;
90  size_t ret;
91 
92  if (size == 0)
93  return true;
94 
95  ret = fread(ptr, 1, size, fp);
96  if (ret != size && !feof(fp))
97  pg_fatal("could not read from input file: %s",
98  strerror(errno));
99 
100  if (rsize)
101  *rsize = ret;
102 
103  return true;
104 }
105 
106 static bool
107 write_none(const void *ptr, size_t size, CompressFileHandle *CFH)
108 {
109  size_t ret;
110 
111  ret = fwrite(ptr, 1, size, (FILE *) CFH->private_data);
112  if (ret != size)
113  return false;
114 
115  return true;
116 }
117 
118 static const char *
120 {
121  return strerror(errno);
122 }
123 
124 static char *
125 gets_none(char *ptr, int size, CompressFileHandle *CFH)
126 {
127  return fgets(ptr, size, (FILE *) CFH->private_data);
128 }
129 
130 static int
132 {
133  FILE *fp = (FILE *) CFH->private_data;
134  int ret;
135 
136  ret = fgetc(fp);
137  if (ret == EOF)
138  {
139  if (!feof(fp))
140  pg_fatal("could not read from input file: %s", strerror(errno));
141  else
142  pg_fatal("could not read from input file: end of file");
143  }
144 
145  return ret;
146 }
147 
148 static bool
150 {
151  FILE *fp = (FILE *) CFH->private_data;
152  int ret = 0;
153 
154  CFH->private_data = NULL;
155 
156  if (fp)
157  ret = fclose(fp);
158 
159  return ret == 0;
160 }
161 
162 static bool
164 {
165  return feof((FILE *) CFH->private_data) != 0;
166 }
167 
168 static bool
169 open_none(const char *path, int fd, const char *mode, CompressFileHandle *CFH)
170 {
171  Assert(CFH->private_data == NULL);
172 
173  if (fd >= 0)
174  CFH->private_data = fdopen(dup(fd), mode);
175  else
176  CFH->private_data = fopen(path, mode);
177 
178  if (CFH->private_data == NULL)
179  return false;
180 
181  return true;
182 }
183 
184 static bool
185 open_write_none(const char *path, const char *mode, CompressFileHandle *CFH)
186 {
187  Assert(CFH->private_data == NULL);
188 
189  CFH->private_data = fopen(path, mode);
190  if (CFH->private_data == NULL)
191  return false;
192 
193  return true;
194 }
195 
196 /*
197  * Public interface
198  */
199 
200 void
202  const pg_compress_specification compression_spec)
203 {
204  CFH->open_func = open_none;
206  CFH->read_func = read_none;
207  CFH->write_func = write_none;
208  CFH->gets_func = gets_none;
209  CFH->getc_func = getc_none;
210  CFH->close_func = close_none;
211  CFH->eof_func = eof_none;
213 
214  CFH->private_data = NULL;
215 }
#define DEFAULT_IO_BUFFER_SIZE
Definition: compress_io.h:27
static bool close_none(CompressFileHandle *CFH)
static int getc_none(CompressFileHandle *CFH)
static void EndCompressorNone(ArchiveHandle *AH, CompressorState *cs)
Definition: compress_none.c:56
static bool write_none(const void *ptr, size_t size, CompressFileHandle *CFH)
void InitCompressorNone(CompressorState *cs, const pg_compress_specification compression_spec)
Definition: compress_none.c:66
static bool eof_none(CompressFileHandle *CFH)
void InitCompressFileHandleNone(CompressFileHandle *CFH, const pg_compress_specification compression_spec)
static char * gets_none(char *ptr, int size, CompressFileHandle *CFH)
static bool open_write_none(const char *path, const char *mode, CompressFileHandle *CFH)
static const char * get_error_none(CompressFileHandle *CFH)
static bool read_none(void *ptr, size_t size, size_t *rsize, CompressFileHandle *CFH)
Definition: compress_none.c:87
static void WriteDataToArchiveNone(ArchiveHandle *AH, CompressorState *cs, const void *data, size_t dLen)
Definition: compress_none.c:49
static bool open_none(const char *path, int fd, const char *mode, CompressFileHandle *CFH)
static void ReadDataFromArchiveNone(ArchiveHandle *AH, CompressorState *cs)
Definition: compress_none.c:30
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
#define free(a)
Definition: header.h:65
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
Assert(fmt[strlen(fmt) - 1] !='\n')
void ahwrite(const void *ptr, size_t size, size_t nmemb, ArchiveHandle *AH)
#define pg_fatal(...)
static PgChecksumMode mode
Definition: pg_checksums.c:56
const void * data
static char * buf
Definition: pg_test_fsync.c:73
#define strerror
Definition: port.h:251
static int fd(const char *x, int i)
Definition: preproc-init.c:105
bool(* open_write_func)(const char *path, const char *mode, CompressFileHandle *CFH)
Definition: compress_io.h:122
const char *(* get_error_func)(CompressFileHandle *CFH)
Definition: compress_io.h:181
bool(* write_func)(const void *ptr, size_t size, struct CompressFileHandle *CFH)
Definition: compress_io.h:139
int(* getc_func)(CompressFileHandle *CFH)
Definition: compress_io.h:161
char *(* gets_func)(char *s, int size, CompressFileHandle *CFH)
Definition: compress_io.h:152
bool(* eof_func)(CompressFileHandle *CFH)
Definition: compress_io.h:168
bool(* open_func)(const char *path, int fd, const char *mode, CompressFileHandle *CFH)
Definition: compress_io.h:111
bool(* close_func)(CompressFileHandle *CFH)
Definition: compress_io.h:175
bool(* read_func)(void *ptr, size_t size, size_t *rsize, CompressFileHandle *CFH)
Definition: compress_io.h:131
void(* readData)(ArchiveHandle *AH, CompressorState *cs)
Definition: compress_io.h:56
pg_compress_specification compression_spec
Definition: compress_io.h:82
void(* end)(ArchiveHandle *AH, CompressorState *cs)
Definition: compress_io.h:67
ReadFunc readF
Definition: compress_io.h:72
void(* writeData)(ArchiveHandle *AH, CompressorState *cs, const void *data, size_t dLen)
Definition: compress_io.h:61
WriteFunc writeF
Definition: compress_io.h:77