Age Owner TLA Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * compress_none.c
4 : * Routines for archivers to read or write an uncompressed stream.
5 : *
6 : * Portions Copyright (c) 1996-2023, 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
45 tomas.vondra 30 UNC 0 : ReadDataFromArchiveNone(ArchiveHandle *AH, CompressorState *cs)
31 : {
32 : size_t cnt;
33 : char *buf;
34 : size_t buflen;
35 :
17 36 0 : buflen = DEFAULT_IO_BUFFER_SIZE;
37 0 : buf = pg_malloc(buflen);
38 :
45 39 0 : while ((cnt = cs->readF(AH, &buf, &buflen)))
40 : {
41 0 : ahwrite(buf, 1, cnt, AH);
42 : }
43 :
44 0 : free(buf);
45 0 : }
46 :
47 :
48 : static void
49 0 : WriteDataToArchiveNone(ArchiveHandle *AH, CompressorState *cs,
50 : const void *data, size_t dLen)
51 : {
52 0 : cs->writeF(AH, data, dLen);
53 0 : }
54 :
55 : static void
56 0 : EndCompressorNone(ArchiveHandle *AH, CompressorState *cs)
57 : {
58 : /* no op */
59 0 : }
60 :
61 : /*
62 : * Public interface
63 : */
64 :
65 : void
66 0 : InitCompressorNone(CompressorState *cs,
67 : const pg_compress_specification compression_spec)
68 : {
69 0 : cs->readData = ReadDataFromArchiveNone;
70 0 : cs->writeData = WriteDataToArchiveNone;
71 0 : cs->end = EndCompressorNone;
72 :
73 0 : cs->compression_spec = compression_spec;
74 0 : }
75 :
76 :
77 : /*----------------------
78 : * Compress File API
79 : *----------------------
80 : */
81 :
82 : /*
83 : * Private routines
84 : */
85 :
86 : static bool
17 tomas.vondra 87 GNC 19589 : read_none(void *ptr, size_t size, size_t *rsize, CompressFileHandle *CFH)
88 : {
45 89 19589 : FILE *fp = (FILE *) CFH->private_data;
90 : size_t ret;
91 :
92 19589 : if (size == 0)
17 93 1964 : return true;
94 :
45 95 17625 : ret = fread(ptr, 1, size, fp);
96 17625 : if (ret != size && !feof(fp))
45 tomas.vondra 97 UNC 0 : pg_fatal("could not read from input file: %s",
98 : strerror(errno));
99 :
17 tomas.vondra 100 GNC 17625 : if (rsize)
17 tomas.vondra 101 UNC 0 : *rsize = ret;
102 :
17 tomas.vondra 103 GNC 17625 : return true;
104 : }
105 :
106 : static bool
45 107 1948892 : write_none(const void *ptr, size_t size, CompressFileHandle *CFH)
108 : {
109 : size_t ret;
110 :
17 111 1948892 : ret = fwrite(ptr, 1, size, (FILE *) CFH->private_data);
112 1948892 : if (ret != size)
17 tomas.vondra 113 UNC 0 : return false;
114 :
17 tomas.vondra 115 GNC 1948892 : return true;
116 : }
117 :
118 : static const char *
45 tomas.vondra 119 UNC 0 : get_error_none(CompressFileHandle *CFH)
120 : {
121 0 : return strerror(errno);
122 : }
123 :
124 : static char *
45 tomas.vondra 125 GNC 6 : gets_none(char *ptr, int size, CompressFileHandle *CFH)
126 : {
127 6 : return fgets(ptr, size, (FILE *) CFH->private_data);
128 : }
129 :
130 : static int
131 163256 : getc_none(CompressFileHandle *CFH)
132 : {
133 163256 : FILE *fp = (FILE *) CFH->private_data;
134 : int ret;
135 :
136 163256 : ret = fgetc(fp);
137 163256 : if (ret == EOF)
138 : {
45 tomas.vondra 139 UNC 0 : if (!feof(fp))
140 0 : pg_fatal("could not read from input file: %s", strerror(errno));
141 : else
142 0 : pg_fatal("could not read from input file: end of file");
143 : }
144 :
45 tomas.vondra 145 GNC 163256 : return ret;
146 : }
147 :
148 : static bool
149 277 : close_none(CompressFileHandle *CFH)
150 : {
151 277 : FILE *fp = (FILE *) CFH->private_data;
152 277 : int ret = 0;
153 :
154 277 : CFH->private_data = NULL;
155 :
156 277 : if (fp)
157 277 : ret = fclose(fp);
158 :
17 159 277 : return ret == 0;
160 : }
161 :
162 : static bool
45 163 2 : eof_none(CompressFileHandle *CFH)
164 : {
17 165 2 : return feof((FILE *) CFH->private_data) != 0;
166 : }
167 :
168 : static bool
45 169 277 : open_none(const char *path, int fd, const char *mode, CompressFileHandle *CFH)
170 : {
171 277 : Assert(CFH->private_data == NULL);
172 :
173 277 : if (fd >= 0)
174 164 : CFH->private_data = fdopen(dup(fd), mode);
175 : else
176 113 : CFH->private_data = fopen(path, mode);
177 :
178 277 : if (CFH->private_data == NULL)
17 tomas.vondra 179 UNC 0 : return false;
180 :
17 tomas.vondra 181 GNC 277 : return true;
182 : }
183 :
184 : static bool
45 185 16 : open_write_none(const char *path, const char *mode, CompressFileHandle *CFH)
186 : {
187 16 : Assert(CFH->private_data == NULL);
188 :
189 16 : CFH->private_data = fopen(path, mode);
190 16 : if (CFH->private_data == NULL)
17 tomas.vondra 191 UNC 0 : return false;
192 :
17 tomas.vondra 193 GNC 16 : return true;
194 : }
195 :
196 : /*
197 : * Public interface
198 : */
199 :
200 : void
45 201 293 : InitCompressFileHandleNone(CompressFileHandle *CFH,
202 : const pg_compress_specification compression_spec)
203 : {
204 293 : CFH->open_func = open_none;
205 293 : CFH->open_write_func = open_write_none;
206 293 : CFH->read_func = read_none;
207 293 : CFH->write_func = write_none;
208 293 : CFH->gets_func = gets_none;
209 293 : CFH->getc_func = getc_none;
210 293 : CFH->close_func = close_none;
211 293 : CFH->eof_func = eof_none;
212 293 : CFH->get_error_func = get_error_none;
213 :
214 293 : CFH->private_data = NULL;
215 293 : }
|