PostgreSQL Source Code: src/fe_utils/astreamer_file.c Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
17
19
23
25{
31
33{
36 const char *(*link_map) (const char *);
41
48
53};
54
64
69};
70
71
72
73
74
75
76
77
78
79
82{
84
88
90 streamer->file = file;
91
92 if (file == NULL)
93 {
94 streamer->file = fopen(pathname, "wb");
95 if (streamer->file == NULL)
96 pg_fatal("could not create file \"%s\": %m", pathname);
98 }
99
100 return &streamer->base;
101}
102
103
104
105
106static void
110{
112
114
115 if (len == 0)
116 return;
117
118 errno = 0;
119 if (fwrite(data, len, 1, mystreamer->file) != 1)
120 {
121
122 if (errno == 0)
123 errno = ENOSPC;
124 pg_fatal("could not write to file \"%s\": %m",
126 }
127}
128
129
130
131
132
133static void
135{
137
139
141 pg_fatal("could not close file \"%s\": %m",
143
144 mystreamer->file = NULL;
146}
147
148
149
150
151static void
153{
155
157
160
162 pfree(mystreamer);
163}
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
187 const char *(*link_map) (const char *),
188 void (*report_output_file) (const char *))
189{
191
196 streamer->link_map = link_map;
198
199 return &streamer->base;
200}
201
202
203
204
205static void
207 const char *data, int len,
209{
211 int fnamelen;
212
215
216 switch (context)
217 {
220
221
224
225
226 fnamelen = strlen(mystreamer->filename);
227 if (mystreamer->filename[fnamelen - 1] == '/')
228 mystreamer->filename[fnamelen - 1] = '\0';
229
230
233 else if (member->is_link)
234 {
235 const char *linktarget = member->linktarget;
236
238 linktarget = mystreamer->link_map(linktarget);
240 }
241 else
242 mystreamer->file =
244 member->mode);
245
246
249 break;
250
252 if (mystreamer->file == NULL)
253 break;
254
255 errno = 0;
256 if (len > 0 && fwrite(data, len, 1, mystreamer->file) != 1)
257 {
258
259 if (errno == 0)
260 errno = ENOSPC;
261 pg_fatal("could not write to file \"%s\": %m",
263 }
264 break;
265
267 if (mystreamer->file == NULL)
268 break;
269 fclose(mystreamer->file);
270 mystreamer->file = NULL;
271 break;
272
274 break;
275
276 default:
277
278 pg_fatal("unexpected state while extracting archive");
279 }
280}
281
282
283
284
285
286
287
288
289
290
291
292
293
294static bool
296{
298
299 if (strcmp(filename, "pg_wal") == 0 ||
300 strcmp(filename, "pg_xlog") == 0 ||
301 strcmp(filename, "archive_status") == 0 ||
302 strcmp(filename, "summaries") == 0 ||
303 strcmp(filename, "pg_tblspc") == 0)
304 return true;
305
307 {
308 const char *pg_tblspc = strstr(pathname, "/pg_tblspc/");
309
310 return pg_tblspc != NULL && pg_tblspc + 11 == filename;
311 }
312
313 return false;
314}
315
316
317
318
319static void
321{
324 pg_fatal("could not create directory \"%s\": %m",
326
327#ifndef WIN32
329 pg_fatal("could not set permissions on directory \"%s\": %m",
331#endif
332}
333
334
335
336
337
338
339
340
341
342
343
344static void
346{
348 pg_fatal("could not create symbolic link from \"%s\" to \"%s\": %m",
350}
351
352
353
354
355
356
357static FILE *
359{
360 FILE *file;
361
362 file = fopen(filename, "wb");
363 if (file == NULL)
365
366#ifndef WIN32
368 pg_fatal("could not set permissions on file \"%s\": %m",
370#endif
371
372 return file;
373}
374
375
376
377
378
379
380static void
382{
385
386 Assert(mystreamer->file == NULL);
387}
388
389
390
391
392static void
394{
396
398 pfree(mystreamer);
399}
astreamer_archive_context
@ ASTREAMER_MEMBER_HEADER
@ ASTREAMER_MEMBER_CONTENTS
@ ASTREAMER_MEMBER_TRAILER
@ ASTREAMER_ARCHIVE_TRAILER
astreamer * astreamer_plain_writer_new(char *pathname, FILE *file)
static void astreamer_plain_writer_content(astreamer *streamer, astreamer_member *member, const char *data, int len, astreamer_archive_context context)
static void astreamer_extractor_finalize(astreamer *streamer)
static void extract_directory(const char *filename, mode_t mode)
static FILE * create_file_for_extract(const char *filename, mode_t mode)
static const astreamer_ops astreamer_extractor_ops
struct astreamer_extractor astreamer_extractor
astreamer * astreamer_extractor_new(const char *basepath, const char *(*link_map)(const char *), void(*report_output_file)(const char *))
struct astreamer_plain_writer astreamer_plain_writer
static void astreamer_plain_writer_finalize(astreamer *streamer)
static void astreamer_extractor_free(astreamer *streamer)
static void astreamer_extractor_content(astreamer *streamer, astreamer_member *member, const char *data, int len, astreamer_archive_context context)
static const astreamer_ops astreamer_plain_writer_ops
static bool should_allow_existing_directory(const char *pathname)
static void extract_link(const char *filename, const char *linktarget)
static void astreamer_plain_writer_free(astreamer *streamer)
#define PG_USED_FOR_ASSERTS_ONLY
Assert(PointerIsAligned(start, uint64))
char * pstrdup(const char *in)
void pfree(void *pointer)
void * palloc0(Size size)
static PgChecksumMode mode
char * last_dir_separator(const char *filename)
char linktarget[MAXPGPATH]
void(* content)(astreamer *streamer, astreamer_member *member, const char *data, int len, astreamer_archive_context context)
const astreamer_ops * bbs_ops
#define symlink(oldpath, newpath)