Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * pg_backup_directory.c
4 : : *
5 : : * A directory format dump is a directory, which contains a "toc.dat" file
6 : : * for the TOC, and a separate file for each data entry, named "<oid>.dat".
7 : : * Large objects are stored in separate files named "blob_<oid>.dat",
8 : : * and there's a plain-text TOC file for each BLOBS TOC entry named
9 : : * "blobs_<dumpID>.toc" (or just "blobs.toc" in archive versions before 16).
10 : : *
11 : : * If compression is used, each data file is individually compressed and the
12 : : * ".gz" suffix is added to the filenames. The TOC files are never
13 : : * compressed by pg_dump, however they are accepted with the .gz suffix too,
14 : : * in case the user has manually compressed them with 'gzip'.
15 : : *
16 : : * NOTE: This format is identical to the files written in the tar file in
17 : : * the 'tar' format, except that we don't write the restore.sql file (TODO),
18 : : * and the tar format doesn't support compression. Please keep the formats in
19 : : * sync.
20 : : *
21 : : *
22 : : * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
23 : : * Portions Copyright (c) 1994, Regents of the University of California
24 : : * Portions Copyright (c) 2000, Philip Warner
25 : : *
26 : : * Rights are granted to use this software in any way so long
27 : : * as this notice is not removed.
28 : : *
29 : : * The author is not responsible for loss or damages that may
30 : : * result from its use.
31 : : *
32 : : * IDENTIFICATION
33 : : * src/bin/pg_dump/pg_backup_directory.c
34 : : *
35 : : *-------------------------------------------------------------------------
36 : : */
37 : : #include "postgres_fe.h"
38 : :
39 : : #include <dirent.h>
40 : : #include <sys/stat.h>
41 : :
42 : : #include "common/file_utils.h"
43 : : #include "compress_io.h"
44 : : #include "dumputils.h"
45 : : #include "parallel.h"
46 : : #include "pg_backup_utils.h"
47 : :
48 : : typedef struct
49 : : {
50 : : /*
51 : : * Our archive location. This is basically what the user specified as his
52 : : * backup file but of course here it is a directory.
53 : : */
54 : : char *directory;
55 : :
56 : : CompressFileHandle *dataFH; /* currently open data file */
57 : : CompressFileHandle *LOsTocFH; /* file handle for blobs_NNN.toc */
58 : : ParallelState *pstate; /* for parallel backup / restore */
59 : : } lclContext;
60 : :
61 : : typedef struct
62 : : {
63 : : char *filename; /* filename excluding the directory (basename) */
64 : : } lclTocEntry;
65 : :
66 : : /* prototypes for private functions */
67 : : static void _ArchiveEntry(ArchiveHandle *AH, TocEntry *te);
68 : : static void _StartData(ArchiveHandle *AH, TocEntry *te);
69 : : static void _EndData(ArchiveHandle *AH, TocEntry *te);
70 : : static void _WriteData(ArchiveHandle *AH, const void *data, size_t dLen);
71 : : static int _WriteByte(ArchiveHandle *AH, const int i);
72 : : static int _ReadByte(ArchiveHandle *AH);
73 : : static void _WriteBuf(ArchiveHandle *AH, const void *buf, size_t len);
74 : : static void _ReadBuf(ArchiveHandle *AH, void *buf, size_t len);
75 : : static void _CloseArchive(ArchiveHandle *AH);
76 : : static void _ReopenArchive(ArchiveHandle *AH);
77 : : static void _PrintTocData(ArchiveHandle *AH, TocEntry *te);
78 : :
79 : : static void _WriteExtraToc(ArchiveHandle *AH, TocEntry *te);
80 : : static void _ReadExtraToc(ArchiveHandle *AH, TocEntry *te);
81 : : static void _PrintExtraToc(ArchiveHandle *AH, TocEntry *te);
82 : :
83 : : static void _StartLOs(ArchiveHandle *AH, TocEntry *te);
84 : : static void _StartLO(ArchiveHandle *AH, TocEntry *te, Oid oid);
85 : : static void _EndLO(ArchiveHandle *AH, TocEntry *te, Oid oid);
86 : : static void _EndLOs(ArchiveHandle *AH, TocEntry *te);
87 : : static void _LoadLOs(ArchiveHandle *AH, TocEntry *te);
88 : :
89 : : static void _PrepParallelRestore(ArchiveHandle *AH);
90 : : static void _Clone(ArchiveHandle *AH);
91 : : static void _DeClone(ArchiveHandle *AH);
92 : :
93 : : static int _WorkerJobRestoreDirectory(ArchiveHandle *AH, TocEntry *te);
94 : : static int _WorkerJobDumpDirectory(ArchiveHandle *AH, TocEntry *te);
95 : :
96 : : static void setFilePath(ArchiveHandle *AH, char *buf,
97 : : const char *relativeFilename);
98 : :
99 : : /*
100 : : * Init routine required by ALL formats. This is a global routine
101 : : * and should be declared in pg_backup_archiver.h
102 : : *
103 : : * Its task is to create any extra archive context (using AH->formatData),
104 : : * and to initialize the supported function pointers.
105 : : *
106 : : * It should also prepare whatever its input source is for reading/writing,
107 : : * and in the case of a read mode connection, it should load the Header & TOC.
108 : : */
109 : : void
5340 heikki.linnakangas@i 110 :CBC 24 : InitArchiveFmt_Directory(ArchiveHandle *AH)
111 : : {
112 : : lclContext *ctx;
113 : :
114 : : /* Assuming static functions, this can be copied for each format. */
115 : 24 : AH->ArchiveEntryPtr = _ArchiveEntry;
116 : 24 : AH->StartDataPtr = _StartData;
117 : 24 : AH->WriteDataPtr = _WriteData;
118 : 24 : AH->EndDataPtr = _EndData;
119 : 24 : AH->WriteBytePtr = _WriteByte;
120 : 24 : AH->ReadBytePtr = _ReadByte;
121 : 24 : AH->WriteBufPtr = _WriteBuf;
122 : 24 : AH->ReadBufPtr = _ReadBuf;
123 : 24 : AH->ClosePtr = _CloseArchive;
4549 andrew@dunslane.net 124 : 24 : AH->ReopenPtr = _ReopenArchive;
5340 heikki.linnakangas@i 125 : 24 : AH->PrintTocDataPtr = _PrintTocData;
126 : 24 : AH->ReadExtraTocPtr = _ReadExtraToc;
127 : 24 : AH->WriteExtraTocPtr = _WriteExtraToc;
128 : 24 : AH->PrintExtraTocPtr = _PrintExtraToc;
129 : :
1006 peter@eisentraut.org 130 : 24 : AH->StartLOsPtr = _StartLOs;
131 : 24 : AH->StartLOPtr = _StartLO;
132 : 24 : AH->EndLOPtr = _EndLO;
133 : 24 : AH->EndLOsPtr = _EndLOs;
134 : :
2549 tgl@sss.pgh.pa.us 135 : 24 : AH->PrepParallelRestorePtr = _PrepParallelRestore;
4549 andrew@dunslane.net 136 : 24 : AH->ClonePtr = _Clone;
137 : 24 : AH->DeClonePtr = _DeClone;
138 : :
139 : 24 : AH->WorkerJobRestorePtr = _WorkerJobRestoreDirectory;
140 : 24 : AH->WorkerJobDumpPtr = _WorkerJobDumpDirectory;
141 : :
142 : : /* Set up our private context */
4722 tgl@sss.pgh.pa.us 143 : 24 : ctx = (lclContext *) pg_malloc0(sizeof(lclContext));
282 peter@eisentraut.org 144 : 24 : AH->formatData = ctx;
145 : :
5340 heikki.linnakangas@i 146 : 24 : ctx->dataFH = NULL;
1006 peter@eisentraut.org 147 : 24 : ctx->LOsTocFH = NULL;
148 : :
149 : : /*
150 : : * Now open the TOC file
151 : : */
152 : :
5340 heikki.linnakangas@i 153 [ + - - + ]: 24 : if (!AH->fSpec || strcmp(AH->fSpec, "") == 0)
1247 tgl@sss.pgh.pa.us 154 :UBC 0 : pg_fatal("no output directory specified");
155 : :
5340 heikki.linnakangas@i 156 :CBC 24 : ctx->directory = AH->fSpec;
157 : :
158 [ + + ]: 24 : if (AH->mode == archModeWrite)
159 : : {
160 : : /* we accept an empty existing directory */
149 andrew@dunslane.net 161 : 11 : create_or_open_dir(ctx->directory);
162 : : }
163 : : else
164 : : { /* Read Mode */
165 : : char fname[MAXPGPATH];
166 : : CompressFileHandle *tocFH;
167 : :
4549 168 : 13 : setFilePath(AH, fname, "toc.dat");
169 : :
926 tomas.vondra@postgre 170 : 13 : tocFH = InitDiscoverCompressFileHandle(fname, PG_BINARY_R);
5340 heikki.linnakangas@i 171 [ - + ]: 13 : if (tocFH == NULL)
1247 tgl@sss.pgh.pa.us 172 :UBC 0 : pg_fatal("could not open input file \"%s\": %m", fname);
173 : :
5340 heikki.linnakangas@i 174 :CBC 13 : ctx->dataFH = tocFH;
175 : :
176 : : /*
177 : : * The TOC of a directory format dump shares the format code of the
178 : : * tar format.
179 : : */
180 : 13 : AH->format = archTar;
181 : 13 : ReadHead(AH);
182 : 13 : AH->format = archDirectory;
183 : 13 : ReadToc(AH);
184 : :
185 : : /* Nothing else in the file, so close it again... */
898 tomas.vondra@postgre 186 [ - + ]: 13 : if (!EndCompressFileHandle(tocFH))
1247 tgl@sss.pgh.pa.us 187 :UBC 0 : pg_fatal("could not close TOC file: %m");
5340 heikki.linnakangas@i 188 :CBC 13 : ctx->dataFH = NULL;
189 : : }
190 : 24 : }
191 : :
192 : : /*
193 : : * Called by the Archiver when the dumper creates a new TOC entry.
194 : : *
195 : : * We determine the filename for this entry.
196 : : */
197 : : static void
198 : 2109 : _ArchiveEntry(ArchiveHandle *AH, TocEntry *te)
199 : : {
200 : : lclTocEntry *tctx;
201 : : char fn[MAXPGPATH];
202 : :
4722 tgl@sss.pgh.pa.us 203 : 2109 : tctx = (lclTocEntry *) pg_malloc0(sizeof(lclTocEntry));
2549 204 [ + + ]: 2109 : if (strcmp(te->desc, "BLOBS") == 0)
205 : : {
523 206 : 10 : snprintf(fn, MAXPGPATH, "blobs_%d.toc", te->dumpId);
207 : 10 : tctx->filename = pg_strdup(fn);
208 : : }
2549 209 [ + + ]: 2099 : else if (te->dataDumper)
210 : : {
5340 heikki.linnakangas@i 211 : 215 : snprintf(fn, MAXPGPATH, "%d.dat", te->dumpId);
5034 bruce@momjian.us 212 : 215 : tctx->filename = pg_strdup(fn);
213 : : }
214 : : else
5340 heikki.linnakangas@i 215 : 1884 : tctx->filename = NULL;
216 : :
282 peter@eisentraut.org 217 : 2109 : te->formatData = tctx;
5340 heikki.linnakangas@i 218 : 2109 : }
219 : :
220 : : /*
221 : : * Called by the Archiver to save any extra format-related TOC entry
222 : : * data.
223 : : *
224 : : * Use the Archiver routines to write data - they are non-endian, and
225 : : * maintain other important file information.
226 : : */
227 : : static void
228 : 2109 : _WriteExtraToc(ArchiveHandle *AH, TocEntry *te)
229 : : {
230 : 2109 : lclTocEntry *tctx = (lclTocEntry *) te->formatData;
231 : :
232 : : /*
233 : : * A dumpable object has set tctx->filename, any other object has not.
234 : : * (see _ArchiveEntry).
235 : : */
236 [ + + ]: 2109 : if (tctx->filename)
237 : 225 : WriteStr(AH, tctx->filename);
238 : : else
239 : 1884 : WriteStr(AH, "");
240 : 2109 : }
241 : :
242 : : /*
243 : : * Called by the Archiver to read any extra format-related TOC data.
244 : : *
245 : : * Needs to match the order defined in _WriteExtraToc, and should also
246 : : * use the Archiver input routines.
247 : : */
248 : : static void
249 : 2499 : _ReadExtraToc(ArchiveHandle *AH, TocEntry *te)
250 : : {
251 : 2499 : lclTocEntry *tctx = (lclTocEntry *) te->formatData;
252 : :
253 [ + - ]: 2499 : if (tctx == NULL)
254 : : {
4722 tgl@sss.pgh.pa.us 255 : 2499 : tctx = (lclTocEntry *) pg_malloc0(sizeof(lclTocEntry));
282 peter@eisentraut.org 256 : 2499 : te->formatData = tctx;
257 : : }
258 : :
5340 heikki.linnakangas@i 259 : 2499 : tctx->filename = ReadStr(AH);
260 [ + + ]: 2499 : if (strlen(tctx->filename) == 0)
261 : : {
262 : 2233 : free(tctx->filename);
263 : 2233 : tctx->filename = NULL;
264 : : }
265 : 2499 : }
266 : :
267 : : /*
268 : : * Called by the Archiver when restoring an archive to output a comment
269 : : * that includes useful information about the TOC entry.
270 : : */
271 : : static void
272 : 1979 : _PrintExtraToc(ArchiveHandle *AH, TocEntry *te)
273 : : {
274 : 1979 : lclTocEntry *tctx = (lclTocEntry *) te->formatData;
275 : :
276 [ - + - - ]: 1979 : if (AH->public.verbose && tctx->filename)
5340 heikki.linnakangas@i 277 :UBC 0 : ahprintf(AH, "-- File: %s\n", tctx->filename);
5340 heikki.linnakangas@i 278 :CBC 1979 : }
279 : :
280 : : /*
281 : : * Called by the archiver when saving TABLE DATA (not schema). This routine
282 : : * should save whatever format-specific information is needed to read
283 : : * the archive back.
284 : : *
285 : : * It is called just prior to the dumper's 'DataDumper' routine being called.
286 : : *
287 : : * We create the data file for writing.
288 : : */
289 : : static void
290 : 215 : _StartData(ArchiveHandle *AH, TocEntry *te)
291 : : {
5263 bruce@momjian.us 292 : 215 : lclTocEntry *tctx = (lclTocEntry *) te->formatData;
293 : 215 : lclContext *ctx = (lclContext *) AH->formatData;
294 : : char fname[MAXPGPATH];
295 : :
4549 andrew@dunslane.net 296 : 215 : setFilePath(AH, fname, tctx->filename);
297 : :
926 tomas.vondra@postgre 298 : 215 : ctx->dataFH = InitCompressFileHandle(AH->compression_spec);
299 : :
898 300 [ - + ]: 215 : if (!ctx->dataFH->open_write_func(fname, PG_BINARY_W, ctx->dataFH))
1247 tgl@sss.pgh.pa.us 301 :UBC 0 : pg_fatal("could not open output file \"%s\": %m", fname);
5340 heikki.linnakangas@i 302 :CBC 215 : }
303 : :
304 : : /*
305 : : * Called by archiver when dumper calls WriteData. This routine is
306 : : * called for both LO and table data; it is the responsibility of
307 : : * the format to manage each kind of data using StartLO/StartData.
308 : : *
309 : : * It should only be called from within a DataDumper routine.
310 : : *
311 : : * We write the data to the open data file.
312 : : */
313 : : static void
314 : 24881 : _WriteData(ArchiveHandle *AH, const void *data, size_t dLen)
315 : : {
5263 bruce@momjian.us 316 : 24881 : lclContext *ctx = (lclContext *) AH->formatData;
926 tomas.vondra@postgre 317 : 24881 : CompressFileHandle *CFH = ctx->dataFH;
318 : :
8 dgustafsson@postgres 319 [ + + ]: 24881 : if (dLen <= 0)
320 : 10 : return;
321 : 24871 : CFH->write_func(data, dLen, CFH);
322 : : }
323 : :
324 : : /*
325 : : * Called by the archiver when a dumper's 'DataDumper' routine has
326 : : * finished.
327 : : *
328 : : * We close the data file.
329 : : */
330 : : static void
5340 heikki.linnakangas@i 331 : 215 : _EndData(ArchiveHandle *AH, TocEntry *te)
332 : : {
5263 bruce@momjian.us 333 : 215 : lclContext *ctx = (lclContext *) AH->formatData;
334 : :
335 : : /* Close the file */
898 tomas.vondra@postgre 336 [ - + ]: 215 : if (!EndCompressFileHandle(ctx->dataFH))
1247 tgl@sss.pgh.pa.us 337 :UBC 0 : pg_fatal("could not close data file: %m");
338 : :
5340 heikki.linnakangas@i 339 :CBC 215 : ctx->dataFH = NULL;
340 : 215 : }
341 : :
342 : : /*
343 : : * Print data for a given file (can be a LO as well)
344 : : */
345 : : static void
3524 tgl@sss.pgh.pa.us 346 : 226 : _PrintFileData(ArchiveHandle *AH, char *filename)
347 : : {
348 : : size_t cnt;
349 : : char *buf;
350 : : size_t buflen;
351 : : CompressFileHandle *CFH;
352 : :
5340 heikki.linnakangas@i 353 [ - + ]: 226 : if (!filename)
5340 heikki.linnakangas@i 354 :UBC 0 : return;
355 : :
926 tomas.vondra@postgre 356 :CBC 226 : CFH = InitDiscoverCompressFileHandle(filename, PG_BINARY_R);
357 [ - + ]: 226 : if (!CFH)
1247 tgl@sss.pgh.pa.us 358 :UBC 0 : pg_fatal("could not open input file \"%s\": %m", filename);
359 : :
898 tomas.vondra@postgre 360 :CBC 226 : buflen = DEFAULT_IO_BUFFER_SIZE;
361 : 226 : buf = pg_malloc(buflen);
362 : :
8 dgustafsson@postgres 363 [ + + ]: 493 : while ((cnt = CFH->read_func(buf, buflen, CFH)) > 0)
364 : : {
5340 heikki.linnakangas@i 365 : 267 : ahwrite(buf, 1, cnt, AH);
366 : : }
367 : :
368 : 226 : free(buf);
898 tomas.vondra@postgre 369 [ - + ]: 226 : if (!EndCompressFileHandle(CFH))
1247 tgl@sss.pgh.pa.us 370 :UBC 0 : pg_fatal("could not close data file \"%s\": %m", filename);
371 : : }
372 : :
373 : : /*
374 : : * Print data for a given TOC entry
375 : : */
376 : : static void
3524 tgl@sss.pgh.pa.us 377 :CBC 226 : _PrintTocData(ArchiveHandle *AH, TocEntry *te)
378 : : {
5340 heikki.linnakangas@i 379 : 226 : lclTocEntry *tctx = (lclTocEntry *) te->formatData;
380 : :
381 [ - + ]: 226 : if (!tctx->filename)
5340 heikki.linnakangas@i 382 :UBC 0 : return;
383 : :
5340 heikki.linnakangas@i 384 [ + + ]:CBC 226 : if (strcmp(te->desc, "BLOBS") == 0)
523 tgl@sss.pgh.pa.us 385 : 10 : _LoadLOs(AH, te);
386 : : else
387 : : {
388 : : char fname[MAXPGPATH];
389 : :
4549 andrew@dunslane.net 390 : 216 : setFilePath(AH, fname, tctx->filename);
3524 tgl@sss.pgh.pa.us 391 : 216 : _PrintFileData(AH, fname);
392 : : }
393 : : }
394 : :
395 : : static void
523 396 : 10 : _LoadLOs(ArchiveHandle *AH, TocEntry *te)
397 : : {
398 : : Oid oid;
5263 bruce@momjian.us 399 : 10 : lclContext *ctx = (lclContext *) AH->formatData;
523 tgl@sss.pgh.pa.us 400 : 10 : lclTocEntry *tctx = (lclTocEntry *) te->formatData;
401 : : CompressFileHandle *CFH;
402 : : char tocfname[MAXPGPATH];
403 : : char line[MAXPGPATH];
404 : :
1006 peter@eisentraut.org 405 : 10 : StartRestoreLOs(AH);
406 : :
407 : : /*
408 : : * Note: before archive v16, there was always only one BLOBS TOC entry,
409 : : * now there can be multiple. Furthermore, although the actual filename
410 : : * was always "blobs.toc" before v16, the value of tctx->filename did not
411 : : * match that before commit 548e50976 fixed it. For simplicity we assume
412 : : * it must be "blobs.toc" in all archives before v16.
413 : : */
90 tgl@sss.pgh.pa.us 414 [ - + ]: 10 : if (AH->version < K_VERS_1_16)
90 tgl@sss.pgh.pa.us 415 :UBC 0 : setFilePath(AH, tocfname, "blobs.toc");
416 : : else
90 tgl@sss.pgh.pa.us 417 :CBC 10 : setFilePath(AH, tocfname, tctx->filename);
418 : :
926 tomas.vondra@postgre 419 : 10 : CFH = ctx->LOsTocFH = InitDiscoverCompressFileHandle(tocfname, PG_BINARY_R);
420 : :
1006 peter@eisentraut.org 421 [ - + ]: 10 : if (ctx->LOsTocFH == NULL)
1247 tgl@sss.pgh.pa.us 422 :UBC 0 : pg_fatal("could not open large object TOC file \"%s\" for input: %m",
423 : : tocfname);
424 : :
425 : : /* Read the LOs TOC file line-by-line, and process each LO */
926 tomas.vondra@postgre 426 [ + + ]:CBC 20 : while ((CFH->gets_func(line, MAXPGPATH, CFH)) != NULL)
427 : : {
428 : : char lofname[MAXPGPATH + 1];
429 : : char path[MAXPGPATH];
430 : :
431 : : /* Can't overflow because line and lofname are the same length */
1006 peter@eisentraut.org 432 [ - + ]: 10 : if (sscanf(line, "%u %" CppAsString2(MAXPGPATH) "s\n", &oid, lofname) != 2)
1247 tgl@sss.pgh.pa.us 433 :UBC 0 : pg_fatal("invalid line in large object TOC file \"%s\": \"%s\"",
434 : : tocfname, line);
435 : :
1006 peter@eisentraut.org 436 :CBC 10 : StartRestoreLO(AH, oid, AH->public.ropt->dropSchema);
437 : 10 : snprintf(path, MAXPGPATH, "%s/%s", ctx->directory, lofname);
3524 tgl@sss.pgh.pa.us 438 : 10 : _PrintFileData(AH, path);
1006 peter@eisentraut.org 439 : 10 : EndRestoreLO(AH, oid);
440 : : }
926 tomas.vondra@postgre 441 [ - + ]: 10 : if (!CFH->eof_func(CFH))
1247 tgl@sss.pgh.pa.us 442 :UBC 0 : pg_fatal("error reading large object TOC file \"%s\"",
443 : : tocfname);
444 : :
898 tomas.vondra@postgre 445 [ - + ]:CBC 10 : if (!EndCompressFileHandle(ctx->LOsTocFH))
1247 tgl@sss.pgh.pa.us 446 :UBC 0 : pg_fatal("could not close large object TOC file \"%s\": %m",
447 : : tocfname);
448 : :
1006 peter@eisentraut.org 449 :CBC 10 : ctx->LOsTocFH = NULL;
450 : :
451 : 10 : EndRestoreLOs(AH);
5340 heikki.linnakangas@i 452 : 10 : }
453 : :
454 : :
455 : : /*
456 : : * Write a byte of data to the archive.
457 : : * Called by the archiver to do integer & byte output to the archive.
458 : : * These routines are only used to read & write the headers & TOC.
459 : : */
460 : : static int
461 : 208752 : _WriteByte(ArchiveHandle *AH, const int i)
462 : : {
463 : 208752 : unsigned char c = (unsigned char) i;
464 : 208752 : lclContext *ctx = (lclContext *) AH->formatData;
926 tomas.vondra@postgre 465 : 208752 : CompressFileHandle *CFH = ctx->dataFH;
466 : :
8 dgustafsson@postgres 467 : 208752 : CFH->write_func(&c, 1, CFH);
5340 heikki.linnakangas@i 468 : 208752 : return 1;
469 : : }
470 : :
471 : : /*
472 : : * Read a byte of data from the archive.
473 : : * Called by the archiver to read bytes & integers from the archive.
474 : : * These routines are only used to read & write headers & TOC.
475 : : * EOF should be treated as a fatal error.
476 : : */
477 : : static int
478 : 247306 : _ReadByte(ArchiveHandle *AH)
479 : : {
480 : 247306 : lclContext *ctx = (lclContext *) AH->formatData;
926 tomas.vondra@postgre 481 : 247306 : CompressFileHandle *CFH = ctx->dataFH;
482 : :
483 : 247306 : return CFH->getc_func(CFH);
484 : : }
485 : :
486 : : /*
487 : : * Write a buffer of data to the archive.
488 : : * Called by the archiver to write a block of bytes to the TOC or a data file.
489 : : */
490 : : static void
5340 heikki.linnakangas@i 491 : 23499 : _WriteBuf(ArchiveHandle *AH, const void *buf, size_t len)
492 : : {
493 : 23499 : lclContext *ctx = (lclContext *) AH->formatData;
926 tomas.vondra@postgre 494 : 23499 : CompressFileHandle *CFH = ctx->dataFH;
495 : :
8 dgustafsson@postgres 496 : 23499 : CFH->write_func(buf, len, CFH);
5340 heikki.linnakangas@i 497 : 23499 : }
498 : :
499 : : /*
500 : : * Read a block of bytes from the archive.
501 : : *
502 : : * Called by the archiver to read a block of bytes from the archive
503 : : */
504 : : static void
505 : 27828 : _ReadBuf(ArchiveHandle *AH, void *buf, size_t len)
506 : : {
507 : 27828 : lclContext *ctx = (lclContext *) AH->formatData;
926 tomas.vondra@postgre 508 : 27828 : CompressFileHandle *CFH = ctx->dataFH;
509 : :
510 : : /*
511 : : * We do not expect a short read, so fail if we get one. The read_func
512 : : * already dealt with any outright I/O error.
513 : : */
8 dgustafsson@postgres 514 [ - + ]: 27828 : if (CFH->read_func(buf, len, CFH) != len)
1247 tgl@sss.pgh.pa.us 515 :UBC 0 : pg_fatal("could not read from input file: end of file");
5340 heikki.linnakangas@i 516 :CBC 27828 : }
517 : :
518 : : /*
519 : : * Close the archive.
520 : : *
521 : : * When writing the archive, this is the routine that actually starts
522 : : * the process of saving it to files. No data should be written prior
523 : : * to this point, since the user could sort the TOC after creating it.
524 : : *
525 : : * If an archive is to be written, this routine must call:
526 : : * WriteHead to save the archive header
527 : : * WriteToc to save the TOC entries
528 : : * WriteDataChunks to save all data & LOs.
529 : : */
530 : : static void
3524 tgl@sss.pgh.pa.us 531 : 24 : _CloseArchive(ArchiveHandle *AH)
532 : : {
5340 heikki.linnakangas@i 533 : 24 : lclContext *ctx = (lclContext *) AH->formatData;
534 : :
535 [ + + ]: 24 : if (AH->mode == archModeWrite)
536 : : {
537 : : CompressFileHandle *tocFH;
1009 michael@paquier.xyz 538 : 11 : pg_compress_specification compression_spec = {0};
539 : : char fname[MAXPGPATH];
540 : :
4549 andrew@dunslane.net 541 : 11 : setFilePath(AH, fname, "toc.dat");
542 : :
543 : : /* this will actually fork the processes for a parallel backup */
3524 tgl@sss.pgh.pa.us 544 : 11 : ctx->pstate = ParallelBackupStart(AH);
545 : :
546 : : /* The TOC is always created uncompressed */
1009 michael@paquier.xyz 547 : 11 : compression_spec.algorithm = PG_COMPRESSION_NONE;
926 tomas.vondra@postgre 548 : 11 : tocFH = InitCompressFileHandle(compression_spec);
898 549 [ - + ]: 11 : if (!tocFH->open_write_func(fname, PG_BINARY_W, tocFH))
1247 tgl@sss.pgh.pa.us 550 :UBC 0 : pg_fatal("could not open output file \"%s\": %m", fname);
5340 heikki.linnakangas@i 551 :CBC 11 : ctx->dataFH = tocFH;
552 : :
553 : : /*
554 : : * Write 'tar' in the format field of the toc.dat file. The directory
555 : : * is compatible with 'tar', so there's no point having a different
556 : : * format code for it.
557 : : */
558 : 11 : AH->format = archTar;
559 : 11 : WriteHead(AH);
560 : 11 : AH->format = archDirectory;
561 : 11 : WriteToc(AH);
898 tomas.vondra@postgre 562 [ - + ]: 11 : if (!EndCompressFileHandle(tocFH))
1247 tgl@sss.pgh.pa.us 563 :UBC 0 : pg_fatal("could not close TOC file: %m");
3524 tgl@sss.pgh.pa.us 564 :CBC 11 : WriteDataChunks(AH, ctx->pstate);
565 : :
4549 andrew@dunslane.net 566 : 11 : ParallelBackupEnd(AH, ctx->pstate);
567 : :
568 : : /*
569 : : * In directory mode, there is no need to sync all the entries
570 : : * individually. Just recurse once through all the files generated.
571 : : */
3090 572 [ + + ]: 11 : if (AH->dosync)
731 nathan@postgresql.or 573 : 5 : sync_dir_recurse(ctx->directory, AH->sync_method);
574 : : }
5340 heikki.linnakangas@i 575 : 24 : AH->FH = NULL;
576 : 24 : }
577 : :
578 : : /*
579 : : * Reopen the archive's file handle.
580 : : */
581 : : static void
4549 andrew@dunslane.net 582 : 14 : _ReopenArchive(ArchiveHandle *AH)
583 : : {
584 : : /*
585 : : * Our TOC is in memory, our data files are opened by each child anyway as
586 : : * they are separate. We support reopening the archive by just doing
587 : : * nothing.
588 : : */
589 : 14 : }
590 : :
591 : : /*
592 : : * LO support
593 : : */
594 : :
595 : : /*
596 : : * Called by the archiver when starting to save BLOB DATA (not schema).
597 : : * It is called just prior to the dumper's DataDumper routine.
598 : : *
599 : : * We open the large object TOC file here, so that we can append a line to
600 : : * it for each LO.
601 : : */
602 : : static void
1006 peter@eisentraut.org 603 : 10 : _StartLOs(ArchiveHandle *AH, TocEntry *te)
604 : : {
5263 bruce@momjian.us 605 : 10 : lclContext *ctx = (lclContext *) AH->formatData;
523 tgl@sss.pgh.pa.us 606 : 10 : lclTocEntry *tctx = (lclTocEntry *) te->formatData;
1009 michael@paquier.xyz 607 : 10 : pg_compress_specification compression_spec = {0};
608 : : char fname[MAXPGPATH];
609 : :
523 tgl@sss.pgh.pa.us 610 : 10 : setFilePath(AH, fname, tctx->filename);
611 : :
612 : : /* The LO TOC file is never compressed */
1009 michael@paquier.xyz 613 : 10 : compression_spec.algorithm = PG_COMPRESSION_NONE;
926 tomas.vondra@postgre 614 : 10 : ctx->LOsTocFH = InitCompressFileHandle(compression_spec);
898 615 [ - + ]: 10 : if (!ctx->LOsTocFH->open_write_func(fname, "ab", ctx->LOsTocFH))
1247 tgl@sss.pgh.pa.us 616 :UBC 0 : pg_fatal("could not open output file \"%s\": %m", fname);
5340 heikki.linnakangas@i 617 :CBC 10 : }
618 : :
619 : : /*
620 : : * Called by the archiver when we're about to start dumping a LO.
621 : : *
622 : : * We create a file to write the LO to.
623 : : */
624 : : static void
1006 peter@eisentraut.org 625 : 10 : _StartLO(ArchiveHandle *AH, TocEntry *te, Oid oid)
626 : : {
5263 bruce@momjian.us 627 : 10 : lclContext *ctx = (lclContext *) AH->formatData;
628 : : char fname[MAXPGPATH];
629 : :
5340 heikki.linnakangas@i 630 : 10 : snprintf(fname, MAXPGPATH, "%s/blob_%u.dat", ctx->directory, oid);
631 : :
926 tomas.vondra@postgre 632 : 10 : ctx->dataFH = InitCompressFileHandle(AH->compression_spec);
898 633 [ - + ]: 10 : if (!ctx->dataFH->open_write_func(fname, PG_BINARY_W, ctx->dataFH))
1247 tgl@sss.pgh.pa.us 634 :UBC 0 : pg_fatal("could not open output file \"%s\": %m", fname);
5340 heikki.linnakangas@i 635 :CBC 10 : }
636 : :
637 : : /*
638 : : * Called by the archiver when the dumper is finished writing a LO.
639 : : *
640 : : * We close the LO file and write an entry to the LO TOC file for it.
641 : : */
642 : : static void
1006 peter@eisentraut.org 643 : 10 : _EndLO(ArchiveHandle *AH, TocEntry *te, Oid oid)
644 : : {
5263 bruce@momjian.us 645 : 10 : lclContext *ctx = (lclContext *) AH->formatData;
926 tomas.vondra@postgre 646 : 10 : CompressFileHandle *CFH = ctx->LOsTocFH;
647 : : char buf[50];
648 : : int len;
649 : :
650 : : /* Close the BLOB data file itself */
898 651 [ - + ]: 10 : if (!EndCompressFileHandle(ctx->dataFH))
925 dgustafsson@postgres 652 :UBC 0 : pg_fatal("could not close LO data file: %m");
5340 heikki.linnakangas@i 653 :CBC 10 : ctx->dataFH = NULL;
654 : :
655 : : /* register the LO in blobs_NNN.toc */
656 : 10 : len = snprintf(buf, sizeof(buf), "%u blob_%u.dat\n", oid, oid);
8 dgustafsson@postgres 657 : 10 : CFH->write_func(buf, len, CFH);
5340 heikki.linnakangas@i 658 : 10 : }
659 : :
660 : : /*
661 : : * Called by the archiver when finishing saving BLOB DATA.
662 : : *
663 : : * We close the LOs TOC file.
664 : : */
665 : : static void
1006 peter@eisentraut.org 666 : 10 : _EndLOs(ArchiveHandle *AH, TocEntry *te)
667 : : {
5340 heikki.linnakangas@i 668 : 10 : lclContext *ctx = (lclContext *) AH->formatData;
669 : :
898 tomas.vondra@postgre 670 [ - + ]: 10 : if (!EndCompressFileHandle(ctx->LOsTocFH))
925 dgustafsson@postgres 671 :UBC 0 : pg_fatal("could not close LOs TOC file: %m");
1006 peter@eisentraut.org 672 :CBC 10 : ctx->LOsTocFH = NULL;
5340 heikki.linnakangas@i 673 : 10 : }
674 : :
675 : : /*
676 : : * Gets a relative file name and prepends the output directory, writing the
677 : : * result to buf. The caller needs to make sure that buf is MAXPGPATH bytes
678 : : * big. Can't use a static char[MAXPGPATH] inside the function because we run
679 : : * multithreaded on Windows.
680 : : */
681 : : static void
4549 andrew@dunslane.net 682 : 491 : setFilePath(ArchiveHandle *AH, char *buf, const char *relativeFilename)
683 : : {
5263 bruce@momjian.us 684 : 491 : lclContext *ctx = (lclContext *) AH->formatData;
685 : : char *dname;
686 : :
5340 heikki.linnakangas@i 687 : 491 : dname = ctx->directory;
688 : :
689 [ - + ]: 491 : if (strlen(dname) + 1 + strlen(relativeFilename) + 1 > MAXPGPATH)
1247 tgl@sss.pgh.pa.us 690 :UBC 0 : pg_fatal("file name too long: \"%s\"", dname);
691 : :
5340 heikki.linnakangas@i 692 :CBC 491 : strcpy(buf, dname);
693 : 491 : strcat(buf, "/");
694 : 491 : strcat(buf, relativeFilename);
4549 andrew@dunslane.net 695 : 491 : }
696 : :
697 : : /*
698 : : * Prepare for parallel restore.
699 : : *
700 : : * The main thing that needs to happen here is to fill in TABLE DATA and BLOBS
701 : : * TOC entries' dataLength fields with appropriate values to guide the
702 : : * ordering of restore jobs. The source of said data is format-dependent,
703 : : * as is the exact meaning of the values.
704 : : *
705 : : * A format module might also choose to do other setup here.
706 : : */
707 : : static void
2549 tgl@sss.pgh.pa.us 708 : 4 : _PrepParallelRestore(ArchiveHandle *AH)
709 : : {
710 : : TocEntry *te;
711 : :
712 [ + + ]: 100 : for (te = AH->toc->next; te != AH->toc; te = te->next)
713 : : {
714 : 96 : lclTocEntry *tctx = (lclTocEntry *) te->formatData;
715 : : char fname[MAXPGPATH];
716 : : struct stat st;
717 : :
718 : : /*
719 : : * A dumpable object has set tctx->filename, any other object has not.
720 : : * (see _ArchiveEntry).
721 : : */
722 [ + + ]: 96 : if (tctx->filename == NULL)
723 : 80 : continue;
724 : :
725 : : /* We may ignore items not due to be restored */
198 jdavis@postgresql.or 726 [ - + ]: 16 : if ((te->reqs & (REQ_DATA | REQ_STATS)) == 0)
2549 tgl@sss.pgh.pa.us 727 :UBC 0 : continue;
728 : :
729 : : /*
730 : : * Stat the file and, if successful, put its size in dataLength. When
731 : : * using compression, the physical file size might not be a very good
732 : : * guide to the amount of work involved in restoring the file, but we
733 : : * only need an approximate indicator of that.
734 : : */
2549 tgl@sss.pgh.pa.us 735 :CBC 16 : setFilePath(AH, fname, tctx->filename);
736 : :
737 [ - + ]: 16 : if (stat(fname, &st) == 0)
2549 tgl@sss.pgh.pa.us 738 :UBC 0 : te->dataLength = st.st_size;
926 tomas.vondra@postgre 739 [ + - ]:CBC 16 : else if (AH->compression_spec.algorithm != PG_COMPRESSION_NONE)
740 : : {
741 [ + - ]: 16 : if (AH->compression_spec.algorithm == PG_COMPRESSION_GZIP)
742 : 16 : strlcat(fname, ".gz", sizeof(fname));
926 tomas.vondra@postgre 743 [ # # ]:UBC 0 : else if (AH->compression_spec.algorithm == PG_COMPRESSION_LZ4)
744 : 0 : strlcat(fname, ".lz4", sizeof(fname));
885 745 [ # # ]: 0 : else if (AH->compression_spec.algorithm == PG_COMPRESSION_ZSTD)
746 : 0 : strlcat(fname, ".zst", sizeof(fname));
747 : :
2549 tgl@sss.pgh.pa.us 748 [ + - ]:CBC 16 : if (stat(fname, &st) == 0)
749 : 16 : te->dataLength = st.st_size;
750 : : }
751 : :
752 : : /*
753 : : * If this is a BLOBS entry, what we stat'd was blobs_NNN.toc, which
754 : : * most likely is a lot smaller than the actual blob data. We don't
755 : : * have a cheap way to estimate how much smaller, but fortunately it
756 : : * doesn't matter too much as long as we get the LOs processed
757 : : * reasonably early. Arbitrarily scale up by a factor of 1K.
758 : : */
759 [ - + ]: 16 : if (strcmp(te->desc, "BLOBS") == 0)
2549 tgl@sss.pgh.pa.us 760 :UBC 0 : te->dataLength *= 1024;
761 : : }
2549 tgl@sss.pgh.pa.us 762 :CBC 4 : }
763 : :
764 : : /*
765 : : * Clone format-specific fields during parallel restoration.
766 : : */
767 : : static void
4549 andrew@dunslane.net 768 : 28 : _Clone(ArchiveHandle *AH)
769 : : {
770 : 28 : lclContext *ctx = (lclContext *) AH->formatData;
771 : :
772 : 28 : AH->formatData = (lclContext *) pg_malloc(sizeof(lclContext));
773 : 28 : memcpy(AH->formatData, ctx, sizeof(lclContext));
774 : 28 : ctx = (lclContext *) AH->formatData;
775 : :
776 : : /*
777 : : * TOC-entry-local state isn't an issue because any one TOC entry is
778 : : * touched by just one worker child.
779 : : */
780 : :
781 : : /*
782 : : * We also don't copy the ParallelState pointer (pstate), only the leader
783 : : * process ever writes to it.
784 : : */
785 : 28 : }
786 : :
787 : : static void
788 : 28 : _DeClone(ArchiveHandle *AH)
789 : : {
790 : 28 : lclContext *ctx = (lclContext *) AH->formatData;
791 : :
792 : 28 : free(ctx);
793 : 28 : }
794 : :
795 : : /*
796 : : * This function is executed in the child of a parallel backup for a
797 : : * directory-format archive and dumps the actual data for one TOC entry.
798 : : */
799 : : static int
3524 tgl@sss.pgh.pa.us 800 : 182 : _WorkerJobDumpDirectory(ArchiveHandle *AH, TocEntry *te)
801 : : {
802 : : /*
803 : : * This function returns void. We either fail and die horribly or
804 : : * succeed... A failure will be detected by the parent when the child dies
805 : : * unexpectedly.
806 : : */
807 : 182 : WriteDataChunksForTocEntry(AH, te);
808 : :
3266 809 : 182 : return 0;
810 : : }
811 : :
812 : : /*
813 : : * This function is executed in the child of a parallel restore from a
814 : : * directory-format archive and restores the actual data for one TOC entry.
815 : : */
816 : : static int
817 : 46 : _WorkerJobRestoreDirectory(ArchiveHandle *AH, TocEntry *te)
818 : : {
819 : 46 : return parallel_restore(AH, te);
820 : : }
|