Age Owner Branch data 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-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_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
926 tomas.vondra@postgre 30 :UBC 0 : ReadDataFromArchiveNone(ArchiveHandle *AH, CompressorState *cs)
31 : : {
32 : : size_t cnt;
33 : : char *buf;
34 : : size_t buflen;
35 : :
898 36 : 0 : buflen = DEFAULT_IO_BUFFER_SIZE;
37 : 0 : buf = pg_malloc(buflen);
38 : :
926 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 size_t
8 dgustafsson@postgres 87 :CBC 27828 : read_none(void *ptr, size_t size, CompressFileHandle *CFH)
88 : : {
926 tomas.vondra@postgre 89 : 27828 : FILE *fp = (FILE *) CFH->private_data;
90 : : size_t ret;
91 : :
92 : 27828 : ret = fread(ptr, 1, size, fp);
8 dgustafsson@postgres 93 [ - + ]: 27828 : if (ferror(fp))
543 michael@paquier.xyz 94 :UBC 0 : pg_fatal("could not read from input file: %m");
95 : :
8 dgustafsson@postgres 96 :CBC 27828 : return ret;
97 : : }
98 : :
99 : : static void
926 tomas.vondra@postgre 100 : 2269556 : write_none(const void *ptr, size_t size, CompressFileHandle *CFH)
101 : : {
102 : : size_t ret;
103 : :
8 dgustafsson@postgres 104 : 2269556 : errno = 0;
898 tomas.vondra@postgre 105 : 2269556 : ret = fwrite(ptr, 1, size, (FILE *) CFH->private_data);
106 [ - + ]: 2269556 : if (ret != size)
107 : : {
8 dgustafsson@postgres 108 [ # # ]:UBC 0 : errno = (errno) ? errno : ENOSPC;
109 : 0 : pg_fatal("could not write to file: %m");
110 : : }
926 tomas.vondra@postgre 111 :CBC 2269556 : }
112 : :
113 : : static const char *
926 tomas.vondra@postgre 114 :UBC 0 : get_error_none(CompressFileHandle *CFH)
115 : : {
116 : 0 : return strerror(errno);
117 : : }
118 : :
119 : : static char *
926 tomas.vondra@postgre 120 :CBC 12 : gets_none(char *ptr, int size, CompressFileHandle *CFH)
121 : : {
122 : 12 : return fgets(ptr, size, (FILE *) CFH->private_data);
123 : : }
124 : :
125 : : static int
126 : 247306 : getc_none(CompressFileHandle *CFH)
127 : : {
128 : 247306 : FILE *fp = (FILE *) CFH->private_data;
129 : : int ret;
130 : :
131 : 247306 : ret = fgetc(fp);
132 [ - + ]: 247306 : if (ret == EOF)
133 : : {
926 tomas.vondra@postgre 134 [ # # ]:UBC 0 : if (!feof(fp))
543 michael@paquier.xyz 135 : 0 : pg_fatal("could not read from input file: %m");
136 : : else
926 tomas.vondra@postgre 137 : 0 : pg_fatal("could not read from input file: end of file");
138 : : }
139 : :
926 tomas.vondra@postgre 140 :CBC 247306 : return ret;
141 : : }
142 : :
143 : : static bool
144 : 422 : close_none(CompressFileHandle *CFH)
145 : : {
146 : 422 : FILE *fp = (FILE *) CFH->private_data;
147 : 422 : int ret = 0;
148 : :
149 : 422 : CFH->private_data = NULL;
150 : :
151 [ + - ]: 422 : if (fp)
152 : : {
8 dgustafsson@postgres 153 : 422 : errno = 0;
926 tomas.vondra@postgre 154 : 422 : ret = fclose(fp);
8 dgustafsson@postgres 155 [ - + ]: 422 : if (ret != 0)
8 dgustafsson@postgres 156 :UBC 0 : pg_log_error("could not close file: %m");
157 : : }
158 : :
898 tomas.vondra@postgre 159 :CBC 422 : return ret == 0;
160 : : }
161 : :
162 : : static bool
926 163 : 6 : eof_none(CompressFileHandle *CFH)
164 : : {
898 165 : 6 : return feof((FILE *) CFH->private_data) != 0;
166 : : }
167 : :
168 : : static bool
926 169 : 422 : open_none(const char *path, int fd, const char *mode, CompressFileHandle *CFH)
170 : : {
171 [ - + ]: 422 : Assert(CFH->private_data == NULL);
172 : :
173 [ + + ]: 422 : if (fd >= 0)
174 : 264 : CFH->private_data = fdopen(dup(fd), mode);
175 : : else
176 : 158 : CFH->private_data = fopen(path, mode);
177 : :
178 [ - + ]: 422 : if (CFH->private_data == NULL)
898 tomas.vondra@postgre 179 :UBC 0 : return false;
180 : :
898 tomas.vondra@postgre 181 :CBC 422 : return true;
182 : : }
183 : :
184 : : static bool
926 185 : 21 : open_write_none(const char *path, const char *mode, CompressFileHandle *CFH)
186 : : {
187 [ - + ]: 21 : Assert(CFH->private_data == NULL);
188 : :
189 : 21 : CFH->private_data = fopen(path, mode);
190 [ - + ]: 21 : if (CFH->private_data == NULL)
898 tomas.vondra@postgre 191 :UBC 0 : return false;
192 : :
898 tomas.vondra@postgre 193 :CBC 21 : return true;
194 : : }
195 : :
196 : : /*
197 : : * Public interface
198 : : */
199 : :
200 : : void
926 201 : 443 : InitCompressFileHandleNone(CompressFileHandle *CFH,
202 : : const pg_compress_specification compression_spec)
203 : : {
204 : 443 : CFH->open_func = open_none;
205 : 443 : CFH->open_write_func = open_write_none;
206 : 443 : CFH->read_func = read_none;
207 : 443 : CFH->write_func = write_none;
208 : 443 : CFH->gets_func = gets_none;
209 : 443 : CFH->getc_func = getc_none;
210 : 443 : CFH->close_func = close_none;
211 : 443 : CFH->eof_func = eof_none;
212 : 443 : CFH->get_error_func = get_error_none;
213 : :
214 : 443 : CFH->private_data = NULL;
215 : 443 : }
|