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-2026, 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
5788 heikki.linnakangas@i 110 :CBC 28 : InitArchiveFmt_Directory(ArchiveHandle *AH)
111 : : {
112 : : lclContext *ctx;
113 : :
114 : : /* Assuming static functions, this can be copied for each format. */
115 : 28 : AH->ArchiveEntryPtr = _ArchiveEntry;
116 : 28 : AH->StartDataPtr = _StartData;
117 : 28 : AH->WriteDataPtr = _WriteData;
118 : 28 : AH->EndDataPtr = _EndData;
119 : 28 : AH->WriteBytePtr = _WriteByte;
120 : 28 : AH->ReadBytePtr = _ReadByte;
121 : 28 : AH->WriteBufPtr = _WriteBuf;
122 : 28 : AH->ReadBufPtr = _ReadBuf;
123 : 28 : AH->ClosePtr = _CloseArchive;
4997 andrew@dunslane.net 124 : 28 : AH->ReopenPtr = _ReopenArchive;
5788 heikki.linnakangas@i 125 : 28 : AH->PrintTocDataPtr = _PrintTocData;
126 : 28 : AH->ReadExtraTocPtr = _ReadExtraToc;
127 : 28 : AH->WriteExtraTocPtr = _WriteExtraToc;
128 : 28 : AH->PrintExtraTocPtr = _PrintExtraToc;
129 : :
1454 peter@eisentraut.org 130 : 28 : AH->StartLOsPtr = _StartLOs;
131 : 28 : AH->StartLOPtr = _StartLO;
132 : 28 : AH->EndLOPtr = _EndLO;
133 : 28 : AH->EndLOsPtr = _EndLOs;
134 : :
2997 tgl@sss.pgh.pa.us 135 : 28 : AH->PrepParallelRestorePtr = _PrepParallelRestore;
4997 andrew@dunslane.net 136 : 28 : AH->ClonePtr = _Clone;
137 : 28 : AH->DeClonePtr = _DeClone;
138 : :
139 : 28 : AH->WorkerJobRestorePtr = _WorkerJobRestoreDirectory;
140 : 28 : AH->WorkerJobDumpPtr = _WorkerJobDumpDirectory;
141 : :
142 : : /* Set up our private context */
288 michael@paquier.xyz 143 : 28 : ctx = pg_malloc0_object(lclContext);
730 peter@eisentraut.org 144 : 28 : AH->formatData = ctx;
145 : :
5788 heikki.linnakangas@i 146 : 28 : ctx->dataFH = NULL;
1454 peter@eisentraut.org 147 : 28 : ctx->LOsTocFH = NULL;
148 : :
149 : : /*
150 : : * Now open the TOC file
151 : : */
152 : :
5788 heikki.linnakangas@i 153 [ + - - + ]: 28 : if (!AH->fSpec || strcmp(AH->fSpec, "") == 0)
1695 tgl@sss.pgh.pa.us 154 :UBC 0 : pg_fatal("no output directory specified");
155 : :
5788 heikki.linnakangas@i 156 :CBC 28 : ctx->directory = AH->fSpec;
157 : :
158 [ + + ]: 28 : if (AH->mode == archModeWrite)
159 : : {
160 : : /* we accept an empty existing directory */
597 andrew@dunslane.net 161 : 12 : create_or_open_dir(ctx->directory);
162 : : }
163 : : else
164 : : { /* Read Mode */
165 : : char fname[MAXPGPATH];
166 : : CompressFileHandle *tocFH;
167 : :
4997 168 : 16 : setFilePath(AH, fname, "toc.dat");
169 : :
1374 tomas.vondra@postgre 170 : 16 : tocFH = InitDiscoverCompressFileHandle(fname, PG_BINARY_R);
5788 heikki.linnakangas@i 171 [ - + ]: 16 : if (tocFH == NULL)
1695 tgl@sss.pgh.pa.us 172 :UBC 0 : pg_fatal("could not open input file \"%s\": %m", fname);
173 : :
5788 heikki.linnakangas@i 174 :CBC 16 : ctx->dataFH = tocFH;
175 : :
176 : : /*
177 : : * The TOC of a directory format dump shares the format code of the
178 : : * tar format.
179 : : */
180 : 16 : AH->format = archTar;
181 : 16 : ReadHead(AH);
182 : 16 : AH->format = archDirectory;
183 : 16 : ReadToc(AH);
184 : :
185 : : /* Nothing else in the file, so close it again... */
1346 tomas.vondra@postgre 186 [ - + ]: 16 : if (!EndCompressFileHandle(tocFH))
1695 tgl@sss.pgh.pa.us 187 :UBC 0 : pg_fatal("could not close TOC file: %m");
5788 heikki.linnakangas@i 188 :CBC 16 : ctx->dataFH = NULL;
189 : : }
190 : 28 : }
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 : 1075 : _ArchiveEntry(ArchiveHandle *AH, TocEntry *te)
199 : : {
200 : : lclTocEntry *tctx;
201 : : char fn[MAXPGPATH];
202 : :
288 michael@paquier.xyz 203 : 1075 : tctx = pg_malloc0_object(lclTocEntry);
2997 tgl@sss.pgh.pa.us 204 [ + + ]: 1075 : if (strcmp(te->desc, "BLOBS") == 0)
205 : : {
971 206 : 6 : snprintf(fn, MAXPGPATH, "blobs_%d.toc", te->dumpId);
207 : 6 : tctx->filename = pg_strdup(fn);
208 : : }
2997 209 [ + + ]: 1069 : else if (te->dataDumper)
210 : : {
5788 heikki.linnakangas@i 211 : 121 : snprintf(fn, MAXPGPATH, "%d.dat", te->dumpId);
5482 bruce@momjian.us 212 : 121 : tctx->filename = pg_strdup(fn);
213 : : }
214 : : else
5788 heikki.linnakangas@i 215 : 948 : tctx->filename = NULL;
216 : :
730 peter@eisentraut.org 217 : 1075 : te->formatData = tctx;
5788 heikki.linnakangas@i 218 : 1075 : }
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 : 1075 : _WriteExtraToc(ArchiveHandle *AH, TocEntry *te)
229 : : {
230 : 1075 : 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 [ + + ]: 1075 : if (tctx->filename)
237 : 127 : WriteStr(AH, tctx->filename);
238 : : else
239 : 948 : WriteStr(AH, "");
240 : 1075 : }
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 : 1531 : _ReadExtraToc(ArchiveHandle *AH, TocEntry *te)
250 : : {
251 : 1531 : lclTocEntry *tctx = (lclTocEntry *) te->formatData;
252 : :
253 [ + - ]: 1531 : if (tctx == NULL)
254 : : {
288 michael@paquier.xyz 255 : 1531 : tctx = pg_malloc0_object(lclTocEntry);
730 peter@eisentraut.org 256 : 1531 : te->formatData = tctx;
257 : : }
258 : :
5788 heikki.linnakangas@i 259 : 1531 : tctx->filename = ReadStr(AH);
260 [ + + ]: 1531 : if (strlen(tctx->filename) == 0)
261 : : {
262 : 1351 : free(tctx->filename);
263 : 1351 : tctx->filename = NULL;
264 : : }
19 nathan@postgresql.or 265 [ - + ]: 180 : else if (first_dir_separator(tctx->filename) != NULL)
19 nathan@postgresql.or 266 :UBC 0 : pg_fatal("invalid file name in archive: \"%s\"", tctx->filename);
5788 heikki.linnakangas@i 267 :CBC 1531 : }
268 : :
269 : : /*
270 : : * Called by the Archiver when restoring an archive to output a comment
271 : : * that includes useful information about the TOC entry.
272 : : */
273 : : static void
274 : 935 : _PrintExtraToc(ArchiveHandle *AH, TocEntry *te)
275 : : {
276 : 935 : lclTocEntry *tctx = (lclTocEntry *) te->formatData;
277 : :
278 [ - + - - ]: 935 : if (AH->public.verbose && tctx->filename)
5788 heikki.linnakangas@i 279 :UBC 0 : ahprintf(AH, "-- File: %s\n", tctx->filename);
5788 heikki.linnakangas@i 280 :CBC 935 : }
281 : :
282 : : /*
283 : : * Called by the archiver when saving TABLE DATA (not schema). This routine
284 : : * should save whatever format-specific information is needed to read
285 : : * the archive back.
286 : : *
287 : : * It is called just prior to the dumper's 'DataDumper' routine being called.
288 : : *
289 : : * We create the data file for writing.
290 : : */
291 : : static void
292 : 121 : _StartData(ArchiveHandle *AH, TocEntry *te)
293 : : {
5711 bruce@momjian.us 294 : 121 : lclTocEntry *tctx = (lclTocEntry *) te->formatData;
295 : 121 : lclContext *ctx = (lclContext *) AH->formatData;
296 : : char fname[MAXPGPATH];
297 : :
4997 andrew@dunslane.net 298 : 121 : setFilePath(AH, fname, tctx->filename);
299 : :
1374 tomas.vondra@postgre 300 : 121 : ctx->dataFH = InitCompressFileHandle(AH->compression_spec);
301 : :
1346 302 [ - + ]: 121 : if (!ctx->dataFH->open_write_func(fname, PG_BINARY_W, ctx->dataFH))
1695 tgl@sss.pgh.pa.us 303 :UBC 0 : pg_fatal("could not open output file \"%s\": %m", fname);
5788 heikki.linnakangas@i 304 :CBC 121 : }
305 : :
306 : : /*
307 : : * Called by archiver when dumper calls WriteData. This routine is
308 : : * called for both LO and table data; it is the responsibility of
309 : : * the format to manage each kind of data using StartLO/StartData.
310 : : *
311 : : * It should only be called from within a DataDumper routine.
312 : : *
313 : : * We write the data to the open data file.
314 : : */
315 : : static void
316 : 24662 : _WriteData(ArchiveHandle *AH, const void *data, size_t dLen)
317 : : {
5711 bruce@momjian.us 318 : 24662 : lclContext *ctx = (lclContext *) AH->formatData;
1374 tomas.vondra@postgre 319 : 24662 : CompressFileHandle *CFH = ctx->dataFH;
320 : :
456 dgustafsson@postgres 321 [ + + ]: 24662 : if (dLen <= 0)
322 : 6 : return;
323 : 24656 : CFH->write_func(data, dLen, CFH);
324 : : }
325 : :
326 : : /*
327 : : * Called by the archiver when a dumper's 'DataDumper' routine has
328 : : * finished.
329 : : *
330 : : * We close the data file.
331 : : */
332 : : static void
5788 heikki.linnakangas@i 333 : 121 : _EndData(ArchiveHandle *AH, TocEntry *te)
334 : : {
5711 bruce@momjian.us 335 : 121 : lclContext *ctx = (lclContext *) AH->formatData;
336 : :
337 : : /* Close the file */
1346 tomas.vondra@postgre 338 [ - + ]: 121 : if (!EndCompressFileHandle(ctx->dataFH))
1695 tgl@sss.pgh.pa.us 339 :UBC 0 : pg_fatal("could not close data file: %m");
340 : :
5788 heikki.linnakangas@i 341 :CBC 121 : ctx->dataFH = NULL;
342 : 121 : }
343 : :
344 : : /*
345 : : * Print data for a given file (can be a LO as well)
346 : : */
347 : : static void
3972 tgl@sss.pgh.pa.us 348 : 134 : _PrintFileData(ArchiveHandle *AH, char *filename)
349 : : {
350 : : size_t cnt;
351 : : char *buf;
352 : : size_t buflen;
353 : : CompressFileHandle *CFH;
354 : :
5788 heikki.linnakangas@i 355 [ - + ]: 134 : if (!filename)
5788 heikki.linnakangas@i 356 :UBC 0 : return;
357 : :
1374 tomas.vondra@postgre 358 :CBC 134 : CFH = InitDiscoverCompressFileHandle(filename, PG_BINARY_R);
359 [ - + ]: 134 : if (!CFH)
1695 tgl@sss.pgh.pa.us 360 :UBC 0 : pg_fatal("could not open input file \"%s\": %m", filename);
361 : :
1346 tomas.vondra@postgre 362 :CBC 134 : buflen = DEFAULT_IO_BUFFER_SIZE;
363 : 134 : buf = pg_malloc(buflen);
364 : :
456 dgustafsson@postgres 365 [ + + ]: 267 : while ((cnt = CFH->read_func(buf, buflen, CFH)) > 0)
366 : : {
5788 heikki.linnakangas@i 367 : 133 : ahwrite(buf, 1, cnt, AH);
368 : : }
369 : :
150 peter@eisentraut.org 370 :GNC 133 : pg_free(buf);
1346 tomas.vondra@postgre 371 [ - + ]:CBC 133 : if (!EndCompressFileHandle(CFH))
1695 tgl@sss.pgh.pa.us 372 :UBC 0 : pg_fatal("could not close data file \"%s\": %m", filename);
373 : : }
374 : :
375 : : /*
376 : : * Print data for a given TOC entry
377 : : */
378 : : static void
3972 tgl@sss.pgh.pa.us 379 :CBC 134 : _PrintTocData(ArchiveHandle *AH, TocEntry *te)
380 : : {
5788 heikki.linnakangas@i 381 : 134 : lclTocEntry *tctx = (lclTocEntry *) te->formatData;
382 : :
383 [ - + ]: 134 : if (!tctx->filename)
5788 heikki.linnakangas@i 384 :UBC 0 : return;
385 : :
5788 heikki.linnakangas@i 386 [ + + ]:CBC 134 : if (strcmp(te->desc, "BLOBS") == 0)
971 tgl@sss.pgh.pa.us 387 : 5 : _LoadLOs(AH, te);
388 : : else
389 : : {
390 : : char fname[MAXPGPATH];
391 : :
4997 andrew@dunslane.net 392 : 129 : setFilePath(AH, fname, tctx->filename);
3972 tgl@sss.pgh.pa.us 393 : 129 : _PrintFileData(AH, fname);
394 : : }
395 : : }
396 : :
397 : : static void
971 398 : 5 : _LoadLOs(ArchiveHandle *AH, TocEntry *te)
399 : : {
400 : : Oid oid;
5711 bruce@momjian.us 401 : 5 : lclContext *ctx = (lclContext *) AH->formatData;
402 : : CompressFileHandle *CFH;
403 : : char tocfname[MAXPGPATH];
404 : : char line[MAXPGPATH];
405 : :
1454 peter@eisentraut.org 406 : 5 : StartRestoreLOs(AH);
407 : :
408 : : /*
409 : : * Note: before archive v16, there was always only one BLOBS TOC entry,
410 : : * now there can be multiple. Furthermore, although the actual filename
411 : : * was always "blobs.toc" before v16, the TOC entry's filename did not
412 : : * match that before commit 548e50976 fixed it. For simplicity we assume
413 : : * it must be "blobs.toc" in all archives before v16.
414 : : *
415 : : * Likewise, the tar format puts the wrong filename into the TOC entry,
416 : : * which matters when reading an extracted tar archive. For simplicity we
417 : : * assume it must be "blobs_<dumpId>.toc" in all newer archives.
418 : : */
538 tgl@sss.pgh.pa.us 419 [ - + ]: 5 : if (AH->version < K_VERS_1_16)
538 tgl@sss.pgh.pa.us 420 :UBC 0 : setFilePath(AH, tocfname, "blobs.toc");
421 : : else
422 : : {
423 : : char tmp[MAXPGPATH];
424 : :
5 nathan@postgresql.or 425 :CBC 5 : snprintf(tmp, MAXPGPATH, "blobs_%d.toc", te->dumpId);
426 : 5 : setFilePath(AH, tocfname, tmp);
427 : : }
428 : :
1374 tomas.vondra@postgre 429 : 5 : CFH = ctx->LOsTocFH = InitDiscoverCompressFileHandle(tocfname, PG_BINARY_R);
430 : :
1454 peter@eisentraut.org 431 [ - + ]: 5 : if (ctx->LOsTocFH == NULL)
1695 tgl@sss.pgh.pa.us 432 :UBC 0 : pg_fatal("could not open large object TOC file \"%s\" for input: %m",
433 : : tocfname);
434 : :
435 : : /* Read the LOs TOC file line-by-line, and process each LO */
1374 tomas.vondra@postgre 436 [ + + ]:CBC 10 : while ((CFH->gets_func(line, MAXPGPATH, CFH)) != NULL)
437 : : {
438 : : char lofname[MAXPGPATH + 1];
439 : : char path[MAXPGPATH];
440 : :
441 : : /* Can't overflow because line and lofname are the same length */
1454 peter@eisentraut.org 442 [ - + ]: 5 : if (sscanf(line, "%u %" CppAsString2(MAXPGPATH) "s\n", &oid, lofname) != 2)
1695 tgl@sss.pgh.pa.us 443 :UBC 0 : pg_fatal("invalid line in large object TOC file \"%s\": \"%s\"",
444 : : tocfname, line);
19 nathan@postgresql.or 445 [ - + ]:CBC 5 : if (first_dir_separator(lofname) != NULL)
19 nathan@postgresql.or 446 :UBC 0 : pg_fatal("invalid file name in archive: \"%s\"", lofname);
447 : :
1454 peter@eisentraut.org 448 :CBC 5 : StartRestoreLO(AH, oid, AH->public.ropt->dropSchema);
449 : 5 : snprintf(path, MAXPGPATH, "%s/%s", ctx->directory, lofname);
3972 tgl@sss.pgh.pa.us 450 : 5 : _PrintFileData(AH, path);
1454 peter@eisentraut.org 451 : 5 : EndRestoreLO(AH, oid);
452 : : }
1374 tomas.vondra@postgre 453 [ - + ]: 5 : if (!CFH->eof_func(CFH))
1695 tgl@sss.pgh.pa.us 454 :UBC 0 : pg_fatal("error reading large object TOC file \"%s\"",
455 : : tocfname);
456 : :
1346 tomas.vondra@postgre 457 [ - + ]:CBC 5 : if (!EndCompressFileHandle(ctx->LOsTocFH))
1695 tgl@sss.pgh.pa.us 458 :UBC 0 : pg_fatal("could not close large object TOC file \"%s\": %m",
459 : : tocfname);
460 : :
1454 peter@eisentraut.org 461 :CBC 5 : ctx->LOsTocFH = NULL;
462 : :
463 : 5 : EndRestoreLOs(AH);
5788 heikki.linnakangas@i 464 : 5 : }
465 : :
466 : :
467 : : /*
468 : : * Write a byte of data to the archive.
469 : : * Called by the archiver to do integer & byte output to the archive.
470 : : * These routines are only used to read & write the headers & TOC.
471 : : */
472 : : static int
473 : 106399 : _WriteByte(ArchiveHandle *AH, const int i)
474 : : {
475 : 106399 : unsigned char c = (unsigned char) i;
476 : 106399 : lclContext *ctx = (lclContext *) AH->formatData;
1374 tomas.vondra@postgre 477 : 106399 : CompressFileHandle *CFH = ctx->dataFH;
478 : :
456 dgustafsson@postgres 479 : 106399 : CFH->write_func(&c, 1, CFH);
5788 heikki.linnakangas@i 480 : 106399 : return 1;
481 : : }
482 : :
483 : : /*
484 : : * Read a byte of data from the archive.
485 : : * Called by the archiver to read bytes & integers from the archive.
486 : : * These routines are only used to read & write headers & TOC.
487 : : * EOF should be treated as a fatal error.
488 : : */
489 : : static int
490 : 151397 : _ReadByte(ArchiveHandle *AH)
491 : : {
492 : 151397 : lclContext *ctx = (lclContext *) AH->formatData;
1374 tomas.vondra@postgre 493 : 151397 : CompressFileHandle *CFH = ctx->dataFH;
494 : :
495 : 151397 : return CFH->getc_func(CFH);
496 : : }
497 : :
498 : : /*
499 : : * Write a buffer of data to the archive.
500 : : * Called by the archiver to write a block of bytes to the TOC or a data file.
501 : : */
502 : : static void
5788 heikki.linnakangas@i 503 : 11920 : _WriteBuf(ArchiveHandle *AH, const void *buf, size_t len)
504 : : {
505 : 11920 : lclContext *ctx = (lclContext *) AH->formatData;
1374 tomas.vondra@postgre 506 : 11920 : CompressFileHandle *CFH = ctx->dataFH;
507 : :
456 dgustafsson@postgres 508 : 11920 : CFH->write_func(buf, len, CFH);
5788 heikki.linnakangas@i 509 : 11920 : }
510 : :
511 : : /*
512 : : * Read a block of bytes from the archive.
513 : : *
514 : : * Called by the archiver to read a block of bytes from the archive
515 : : */
516 : : static void
517 : 16962 : _ReadBuf(ArchiveHandle *AH, void *buf, size_t len)
518 : : {
519 : 16962 : lclContext *ctx = (lclContext *) AH->formatData;
1374 tomas.vondra@postgre 520 : 16962 : CompressFileHandle *CFH = ctx->dataFH;
521 : :
522 : : /*
523 : : * We do not expect a short read, so fail if we get one. The read_func
524 : : * already dealt with any outright I/O error.
525 : : */
456 dgustafsson@postgres 526 [ - + ]: 16962 : if (CFH->read_func(buf, len, CFH) != len)
1695 tgl@sss.pgh.pa.us 527 :UBC 0 : pg_fatal("could not read from input file: end of file");
5788 heikki.linnakangas@i 528 :CBC 16962 : }
529 : :
530 : : /*
531 : : * Close the archive.
532 : : *
533 : : * When writing the archive, this is the routine that actually starts
534 : : * the process of saving it to files. No data should be written prior
535 : : * to this point, since the user could sort the TOC after creating it.
536 : : *
537 : : * If an archive is to be written, this routine must call:
538 : : * WriteHead to save the archive header
539 : : * WriteToc to save the TOC entries
540 : : * WriteDataChunks to save all data & LOs.
541 : : */
542 : : static void
3972 tgl@sss.pgh.pa.us 543 : 27 : _CloseArchive(ArchiveHandle *AH)
544 : : {
5788 heikki.linnakangas@i 545 : 27 : lclContext *ctx = (lclContext *) AH->formatData;
546 : :
547 [ + + ]: 27 : if (AH->mode == archModeWrite)
548 : : {
549 : : CompressFileHandle *tocFH;
1457 michael@paquier.xyz 550 : 12 : pg_compress_specification compression_spec = {0};
551 : : char fname[MAXPGPATH];
552 : :
4997 andrew@dunslane.net 553 : 12 : setFilePath(AH, fname, "toc.dat");
554 : :
555 : : /* this will actually fork the processes for a parallel backup */
3972 tgl@sss.pgh.pa.us 556 : 12 : ctx->pstate = ParallelBackupStart(AH);
557 : :
558 : : /* The TOC is always created uncompressed */
1457 michael@paquier.xyz 559 : 12 : compression_spec.algorithm = PG_COMPRESSION_NONE;
1374 tomas.vondra@postgre 560 : 12 : tocFH = InitCompressFileHandle(compression_spec);
1346 561 [ - + ]: 12 : if (!tocFH->open_write_func(fname, PG_BINARY_W, tocFH))
1695 tgl@sss.pgh.pa.us 562 :UBC 0 : pg_fatal("could not open output file \"%s\": %m", fname);
5788 heikki.linnakangas@i 563 :CBC 12 : ctx->dataFH = tocFH;
564 : :
565 : : /*
566 : : * Write 'tar' in the format field of the toc.dat file. The directory
567 : : * is compatible with 'tar', so there's no point having a different
568 : : * format code for it.
569 : : */
570 : 12 : AH->format = archTar;
571 : 12 : WriteHead(AH);
572 : 12 : AH->format = archDirectory;
573 : 12 : WriteToc(AH);
1346 tomas.vondra@postgre 574 [ - + ]: 12 : if (!EndCompressFileHandle(tocFH))
1695 tgl@sss.pgh.pa.us 575 :UBC 0 : pg_fatal("could not close TOC file: %m");
3972 tgl@sss.pgh.pa.us 576 :CBC 12 : WriteDataChunks(AH, ctx->pstate);
577 : :
4997 andrew@dunslane.net 578 : 12 : ParallelBackupEnd(AH, ctx->pstate);
579 : :
580 : : /*
581 : : * In directory mode, there is no need to sync all the entries
582 : : * individually. Just recurse once through all the files generated.
583 : : */
3538 584 [ + + ]: 12 : if (AH->dosync)
1179 nathan@postgresql.or 585 : 2 : sync_dir_recurse(ctx->directory, AH->sync_method);
586 : : }
5788 heikki.linnakangas@i 587 : 27 : AH->FH = NULL;
588 : 27 : }
589 : :
590 : : /*
591 : : * Reopen the archive's file handle.
592 : : */
593 : : static void
4997 andrew@dunslane.net 594 : 17 : _ReopenArchive(ArchiveHandle *AH)
595 : : {
596 : : /*
597 : : * Our TOC is in memory, our data files are opened by each child anyway as
598 : : * they are separate. We support reopening the archive by just doing
599 : : * nothing.
600 : : */
601 : 17 : }
602 : :
603 : : /*
604 : : * LO support
605 : : */
606 : :
607 : : /*
608 : : * Called by the archiver when starting to save BLOB DATA (not schema).
609 : : * It is called just prior to the dumper's DataDumper routine.
610 : : *
611 : : * We open the large object TOC file here, so that we can append a line to
612 : : * it for each LO.
613 : : */
614 : : static void
1454 peter@eisentraut.org 615 : 6 : _StartLOs(ArchiveHandle *AH, TocEntry *te)
616 : : {
5711 bruce@momjian.us 617 : 6 : lclContext *ctx = (lclContext *) AH->formatData;
971 tgl@sss.pgh.pa.us 618 : 6 : lclTocEntry *tctx = (lclTocEntry *) te->formatData;
1457 michael@paquier.xyz 619 : 6 : pg_compress_specification compression_spec = {0};
620 : : char fname[MAXPGPATH];
621 : :
971 tgl@sss.pgh.pa.us 622 : 6 : setFilePath(AH, fname, tctx->filename);
623 : :
624 : : /* The LO TOC file is never compressed */
1457 michael@paquier.xyz 625 : 6 : compression_spec.algorithm = PG_COMPRESSION_NONE;
1374 tomas.vondra@postgre 626 : 6 : ctx->LOsTocFH = InitCompressFileHandle(compression_spec);
1346 627 [ - + ]: 6 : if (!ctx->LOsTocFH->open_write_func(fname, "ab", ctx->LOsTocFH))
1695 tgl@sss.pgh.pa.us 628 :UBC 0 : pg_fatal("could not open output file \"%s\": %m", fname);
5788 heikki.linnakangas@i 629 :CBC 6 : }
630 : :
631 : : /*
632 : : * Called by the archiver when we're about to start dumping a LO.
633 : : *
634 : : * We create a file to write the LO to.
635 : : */
636 : : static void
1454 peter@eisentraut.org 637 : 6 : _StartLO(ArchiveHandle *AH, TocEntry *te, Oid oid)
638 : : {
5711 bruce@momjian.us 639 : 6 : lclContext *ctx = (lclContext *) AH->formatData;
640 : : char fname[MAXPGPATH];
641 : :
5788 heikki.linnakangas@i 642 : 6 : snprintf(fname, MAXPGPATH, "%s/blob_%u.dat", ctx->directory, oid);
643 : :
1374 tomas.vondra@postgre 644 : 6 : ctx->dataFH = InitCompressFileHandle(AH->compression_spec);
1346 645 [ - + ]: 6 : if (!ctx->dataFH->open_write_func(fname, PG_BINARY_W, ctx->dataFH))
1695 tgl@sss.pgh.pa.us 646 :UBC 0 : pg_fatal("could not open output file \"%s\": %m", fname);
5788 heikki.linnakangas@i 647 :CBC 6 : }
648 : :
649 : : /*
650 : : * Called by the archiver when the dumper is finished writing a LO.
651 : : *
652 : : * We close the LO file and write an entry to the LO TOC file for it.
653 : : */
654 : : static void
1454 peter@eisentraut.org 655 : 6 : _EndLO(ArchiveHandle *AH, TocEntry *te, Oid oid)
656 : : {
5711 bruce@momjian.us 657 : 6 : lclContext *ctx = (lclContext *) AH->formatData;
1374 tomas.vondra@postgre 658 : 6 : CompressFileHandle *CFH = ctx->LOsTocFH;
659 : : char buf[50];
660 : : int len;
661 : :
662 : : /* Close the BLOB data file itself */
1346 663 [ - + ]: 6 : if (!EndCompressFileHandle(ctx->dataFH))
1373 dgustafsson@postgres 664 :UBC 0 : pg_fatal("could not close LO data file: %m");
5788 heikki.linnakangas@i 665 :CBC 6 : ctx->dataFH = NULL;
666 : :
667 : : /* register the LO in blobs_NNN.toc */
668 : 6 : len = snprintf(buf, sizeof(buf), "%u blob_%u.dat\n", oid, oid);
456 dgustafsson@postgres 669 : 6 : CFH->write_func(buf, len, CFH);
5788 heikki.linnakangas@i 670 : 6 : }
671 : :
672 : : /*
673 : : * Called by the archiver when finishing saving BLOB DATA.
674 : : *
675 : : * We close the LOs TOC file.
676 : : */
677 : : static void
1454 peter@eisentraut.org 678 : 6 : _EndLOs(ArchiveHandle *AH, TocEntry *te)
679 : : {
5788 heikki.linnakangas@i 680 : 6 : lclContext *ctx = (lclContext *) AH->formatData;
681 : :
1346 tomas.vondra@postgre 682 [ - + ]: 6 : if (!EndCompressFileHandle(ctx->LOsTocFH))
1373 dgustafsson@postgres 683 :UBC 0 : pg_fatal("could not close LOs TOC file: %m");
1454 peter@eisentraut.org 684 :CBC 6 : ctx->LOsTocFH = NULL;
5788 heikki.linnakangas@i 685 : 6 : }
686 : :
687 : : /*
688 : : * Gets a relative file name and prepends the output directory, writing the
689 : : * result to buf. The caller needs to make sure that buf is MAXPGPATH bytes
690 : : * big. Can't use a static char[MAXPGPATH] inside the function because we run
691 : : * multithreaded on Windows.
692 : : */
693 : : static void
4997 andrew@dunslane.net 694 : 309 : setFilePath(ArchiveHandle *AH, char *buf, const char *relativeFilename)
695 : : {
5711 bruce@momjian.us 696 : 309 : lclContext *ctx = (lclContext *) AH->formatData;
697 : : char *dname;
698 : :
5788 heikki.linnakangas@i 699 : 309 : dname = ctx->directory;
700 : :
701 [ - + ]: 309 : if (strlen(dname) + 1 + strlen(relativeFilename) + 1 > MAXPGPATH)
1695 tgl@sss.pgh.pa.us 702 :UBC 0 : pg_fatal("file name too long: \"%s\"", dname);
703 : :
5788 heikki.linnakangas@i 704 :CBC 309 : strcpy(buf, dname);
705 : 309 : strcat(buf, "/");
706 : 309 : strcat(buf, relativeFilename);
4997 andrew@dunslane.net 707 : 309 : }
708 : :
709 : : /*
710 : : * Prepare for parallel restore.
711 : : *
712 : : * The main thing that needs to happen here is to fill in TABLE DATA and BLOBS
713 : : * TOC entries' dataLength fields with appropriate values to guide the
714 : : * ordering of restore jobs. The source of said data is format-dependent,
715 : : * as is the exact meaning of the values.
716 : : *
717 : : * A format module might also choose to do other setup here.
718 : : */
719 : : static void
2997 tgl@sss.pgh.pa.us 720 : 5 : _PrepParallelRestore(ArchiveHandle *AH)
721 : : {
722 : : TocEntry *te;
723 : :
724 [ + + ]: 124 : for (te = AH->toc->next; te != AH->toc; te = te->next)
725 : : {
726 : 119 : lclTocEntry *tctx = (lclTocEntry *) te->formatData;
727 : : char fname[MAXPGPATH];
728 : : struct stat st;
729 : :
730 : : /*
731 : : * A dumpable object has set tctx->filename, any other object has not.
732 : : * (see _ArchiveEntry).
733 : : */
734 [ + + ]: 119 : if (tctx->filename == NULL)
735 : 99 : continue;
736 : :
737 : : /* We may ignore items not due to be restored */
646 jdavis@postgresql.or 738 [ - + ]: 20 : if ((te->reqs & (REQ_DATA | REQ_STATS)) == 0)
2997 tgl@sss.pgh.pa.us 739 :UBC 0 : continue;
740 : :
741 : : /*
742 : : * Stat the file and, if successful, put its size in dataLength. When
743 : : * using compression, the physical file size might not be a very good
744 : : * guide to the amount of work involved in restoring the file, but we
745 : : * only need an approximate indicator of that.
746 : : *
747 : : * Note that we can't rely on tctx->filename for BLOBS entries (see
748 : : * _LoadLOs()).
749 : : */
5 nathan@postgresql.or 750 [ + - ]:CBC 20 : if (strcmp(te->desc, "BLOBS") != 0)
751 : 20 : setFilePath(AH, fname, tctx->filename);
5 nathan@postgresql.or 752 [ # # ]:UBC 0 : else if (AH->version < K_VERS_1_16)
753 : 0 : setFilePath(AH, fname, "blobs.toc");
754 : : else
755 : : {
756 : : char tmp[MAXPGPATH];
757 : :
758 : 0 : snprintf(tmp, MAXPGPATH, "blobs_%d.toc", te->dumpId);
759 : 0 : setFilePath(AH, fname, tmp);
760 : : }
761 : :
2997 tgl@sss.pgh.pa.us 762 [ - + ]:CBC 20 : if (stat(fname, &st) == 0)
2997 tgl@sss.pgh.pa.us 763 :UBC 0 : te->dataLength = st.st_size;
1374 tomas.vondra@postgre 764 [ + - ]:CBC 20 : else if (AH->compression_spec.algorithm != PG_COMPRESSION_NONE)
765 : : {
766 [ + - ]: 20 : if (AH->compression_spec.algorithm == PG_COMPRESSION_GZIP)
767 : 20 : strlcat(fname, ".gz", sizeof(fname));
1374 tomas.vondra@postgre 768 [ # # ]:UBC 0 : else if (AH->compression_spec.algorithm == PG_COMPRESSION_LZ4)
769 : 0 : strlcat(fname, ".lz4", sizeof(fname));
1333 770 [ # # ]: 0 : else if (AH->compression_spec.algorithm == PG_COMPRESSION_ZSTD)
771 : 0 : strlcat(fname, ".zst", sizeof(fname));
772 : :
2997 tgl@sss.pgh.pa.us 773 [ + - ]:CBC 20 : if (stat(fname, &st) == 0)
774 : 20 : te->dataLength = st.st_size;
775 : : }
776 : :
777 : : /*
778 : : * If this is a BLOBS entry, what we stat'd was blobs_NNN.toc, which
779 : : * most likely is a lot smaller than the actual blob data. We don't
780 : : * have a cheap way to estimate how much smaller, but fortunately it
781 : : * doesn't matter too much as long as we get the LOs processed
782 : : * reasonably early. Arbitrarily scale up by a factor of 1K.
783 : : */
784 [ - + ]: 20 : if (strcmp(te->desc, "BLOBS") == 0)
2997 tgl@sss.pgh.pa.us 785 :UBC 0 : te->dataLength *= 1024;
786 : : }
2997 tgl@sss.pgh.pa.us 787 :CBC 5 : }
788 : :
789 : : /*
790 : : * Clone format-specific fields during parallel restoration.
791 : : */
792 : : static void
4997 andrew@dunslane.net 793 : 28 : _Clone(ArchiveHandle *AH)
794 : : {
795 : 28 : lclContext *ctx = (lclContext *) AH->formatData;
796 : :
288 michael@paquier.xyz 797 : 28 : AH->formatData = pg_malloc_object(lclContext);
4997 andrew@dunslane.net 798 : 28 : memcpy(AH->formatData, ctx, sizeof(lclContext));
799 : 28 : ctx = (lclContext *) AH->formatData;
800 : :
801 : : /*
802 : : * TOC-entry-local state isn't an issue because any one TOC entry is
803 : : * touched by just one worker child.
804 : : */
805 : :
806 : : /*
807 : : * We also don't copy the ParallelState pointer (pstate), only the leader
808 : : * process ever writes to it.
809 : : */
810 : 28 : }
811 : :
812 : : static void
813 : 28 : _DeClone(ArchiveHandle *AH)
814 : : {
815 : 28 : lclContext *ctx = (lclContext *) AH->formatData;
816 : :
817 : 28 : free(ctx);
818 : 28 : }
819 : :
820 : : /*
821 : : * This function is executed in the child of a parallel backup for a
822 : : * directory-format archive and dumps the actual data for one TOC entry.
823 : : */
824 : : static int
3972 tgl@sss.pgh.pa.us 825 : 72 : _WorkerJobDumpDirectory(ArchiveHandle *AH, TocEntry *te)
826 : : {
827 : : /*
828 : : * This function returns void. We either fail and die horribly or
829 : : * succeed... A failure will be detected by the parent when the child dies
830 : : * unexpectedly.
831 : : */
832 : 72 : WriteDataChunksForTocEntry(AH, te);
833 : :
3714 834 : 72 : return 0;
835 : : }
836 : :
837 : : /*
838 : : * This function is executed in the child of a parallel restore from a
839 : : * directory-format archive and restores the actual data for one TOC entry.
840 : : */
841 : : static int
842 : 55 : _WorkerJobRestoreDirectory(ArchiveHandle *AH, TocEntry *te)
843 : : {
844 : 55 : return parallel_restore(AH, te);
845 : : }
|