PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
basic_archive.c File Reference
#include "postgres.h"
#include <sys/stat.h>
#include <sys/time.h>
#include <unistd.h>
#include "archive/archive_module.h"
#include "common/int.h"
#include "miscadmin.h"
#include "storage/copydir.h"
#include "storage/fd.h"
#include "utils/guc.h"
Include dependency graph for basic_archive.c:

Go to the source code of this file.

Macros

#define CMP_BUF_SIZE   (4096)
 

Functions

static bool basic_archive_configured (ArchiveModuleState *state)
 
static bool basic_archive_file (ArchiveModuleState *state, const char *file, const char *path)
 
static bool check_archive_directory (char **newval, void **extra, GucSource source)
 
static bool compare_files (const char *file1, const char *file2)
 
void _PG_init (void)
 
const ArchiveModuleCallbacks_PG_archive_module_init (void)
 

Variables

 PG_MODULE_MAGIC
 
static char * archive_directory = NULL
 
static const ArchiveModuleCallbacks basic_archive_callbacks
 

Macro Definition Documentation

◆ CMP_BUF_SIZE

#define CMP_BUF_SIZE   (4096)

Function Documentation

◆ _PG_archive_module_init()

const ArchiveModuleCallbacks * _PG_archive_module_init ( void  )

Definition at line 82 of file basic_archive.c.

83{
85}
static const ArchiveModuleCallbacks basic_archive_callbacks
Definition: basic_archive.c:49

References basic_archive_callbacks.

◆ _PG_init()

void _PG_init ( void  )

Definition at line 62 of file basic_archive.c.

63{
64 DefineCustomStringVariable("basic_archive.archive_directory",
65 gettext_noop("Archive file destination directory."),
66 NULL,
68 "",
70 0,
71 check_archive_directory, NULL, NULL);
72
73 MarkGUCPrefixReserved("basic_archive");
74}
static char * archive_directory
Definition: basic_archive.c:42
static bool check_archive_directory(char **newval, void **extra, GucSource source)
Definition: basic_archive.c:93
#define gettext_noop(x)
Definition: c.h:1150
void DefineCustomStringVariable(const char *name, const char *short_desc, const char *long_desc, char **valueAddr, const char *bootValue, GucContext context, int flags, GucStringCheckHook check_hook, GucStringAssignHook assign_hook, GucShowHook show_hook)
Definition: guc.c:5218
void MarkGUCPrefixReserved(const char *className)
Definition: guc.c:5279
@ PGC_SIGHUP
Definition: guc.h:71

References archive_directory, check_archive_directory(), DefineCustomStringVariable(), gettext_noop, MarkGUCPrefixReserved(), and PGC_SIGHUP.

◆ basic_archive_configured()

static bool basic_archive_configured ( ArchiveModuleState state)
static

Definition at line 135 of file basic_archive.c.

136{
137 if (archive_directory != NULL && archive_directory[0] != '\0')
138 return true;
139
140 arch_module_check_errdetail("%s is not set.",
141 "basic_archive.archive_directory");
142 return false;
143}
#define arch_module_check_errdetail

References arch_module_check_errdetail, and archive_directory.

◆ basic_archive_file()

static bool basic_archive_file ( ArchiveModuleState state,
const char *  file,
const char *  path 
)
static

Definition at line 151 of file basic_archive.c.

152{
153 char destination[MAXPGPATH];
154 char temp[MAXPGPATH + 256];
155 struct stat st;
156 struct timeval tv;
157 uint64 epoch; /* milliseconds */
158
160 (errmsg("archiving \"%s\" via basic_archive", file)));
161
162 snprintf(destination, MAXPGPATH, "%s/%s", archive_directory, file);
163
164 /*
165 * First, check if the file has already been archived. If it already
166 * exists and has the same contents as the file we're trying to archive,
167 * we can return success (after ensuring the file is persisted to disk).
168 * This scenario is possible if the server crashed after archiving the
169 * file but before renaming its .ready file to .done.
170 *
171 * If the archive file already exists but has different contents,
172 * something might be wrong, so we just fail.
173 */
174 if (stat(destination, &st) == 0)
175 {
176 if (compare_files(path, destination))
177 {
179 (errmsg("archive file \"%s\" already exists with identical contents",
180 destination)));
181
182 fsync_fname(destination, false);
184
185 return true;
186 }
187
189 (errmsg("archive file \"%s\" already exists", destination)));
190 }
191 else if (errno != ENOENT)
194 errmsg("could not stat file \"%s\": %m", destination)));
195
196 /*
197 * Pick a sufficiently unique name for the temporary file so that a
198 * collision is unlikely. This helps avoid problems in case a temporary
199 * file was left around after a crash or another server happens to be
200 * archiving to the same directory.
201 */
202 gettimeofday(&tv, NULL);
203 if (pg_mul_u64_overflow((uint64) 1000, (uint64) tv.tv_sec, &epoch) ||
204 pg_add_u64_overflow(epoch, (uint64) (tv.tv_usec / 1000), &epoch))
205 elog(ERROR, "could not generate temporary file name for archiving");
206
207 snprintf(temp, sizeof(temp), "%s/%s.%s.%d." UINT64_FORMAT,
208 archive_directory, "archtemp", file, MyProcPid, epoch);
209
210 /*
211 * Copy the file to its temporary destination. Note that this will fail
212 * if temp already exists.
213 */
214 copy_file(path, temp);
215
216 /*
217 * Sync the temporary file to disk and move it to its final destination.
218 * Note that this will overwrite any existing file, but this is only
219 * possible if someone else created the file since the stat() above.
220 */
221 (void) durable_rename(temp, destination, ERROR);
222
224 (errmsg("archived \"%s\" via basic_archive", file)));
225
226 return true;
227}
static bool compare_files(const char *file1, const char *file2)
#define UINT64_FORMAT
Definition: c.h:504
uint64_t uint64
Definition: c.h:486
void copy_file(const char *fromfile, const char *tofile)
Definition: copydir.c:117
int errcode_for_file_access(void)
Definition: elog.c:876
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define DEBUG3
Definition: elog.h:28
#define DEBUG1
Definition: elog.h:30
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
#define ereport(elevel,...)
Definition: elog.h:149
int durable_rename(const char *oldfile, const char *newfile, int elevel)
Definition: fd.c:781
void fsync_fname(const char *fname, bool isdir)
Definition: fd.c:755
int MyProcPid
Definition: globals.c:46
static bool pg_add_u64_overflow(uint64 a, uint64 b, uint64 *result)
Definition: int.h:514
static bool pg_mul_u64_overflow(uint64 a, uint64 b, uint64 *result)
Definition: int.h:548
#define MAXPGPATH
#define snprintf
Definition: port.h:238
#define stat
Definition: win32_port.h:284
static const unsigned __int64 epoch
int gettimeofday(struct timeval *tp, void *tzp)

References archive_directory, compare_files(), copy_file(), DEBUG1, DEBUG3, durable_rename(), elog, epoch, ereport, errcode_for_file_access(), errmsg(), ERROR, fsync_fname(), gettimeofday(), MAXPGPATH, MyProcPid, pg_add_u64_overflow(), pg_mul_u64_overflow(), snprintf, stat, and UINT64_FORMAT.

◆ check_archive_directory()

static bool check_archive_directory ( char **  newval,
void **  extra,
GucSource  source 
)
static

Definition at line 93 of file basic_archive.c.

94{
95 struct stat st;
96
97 /*
98 * The default value is an empty string, so we have to accept that value.
99 * Our check_configured callback also checks for this and prevents
100 * archiving from proceeding if it is still empty.
101 */
102 if (*newval == NULL || *newval[0] == '\0')
103 return true;
104
105 /*
106 * Make sure the file paths won't be too long. The docs indicate that the
107 * file names to be archived can be up to 64 characters long.
108 */
109 if (strlen(*newval) + 64 + 2 >= MAXPGPATH)
110 {
111 GUC_check_errdetail("Archive directory too long.");
112 return false;
113 }
114
115 /*
116 * Do a basic sanity check that the specified archive directory exists. It
117 * could be removed at some point in the future, so we still need to be
118 * prepared for it not to exist in the actual archiving logic.
119 */
120 if (stat(*newval, &st) != 0 || !S_ISDIR(st.st_mode))
121 {
122 GUC_check_errdetail("Specified archive directory does not exist.");
123 return false;
124 }
125
126 return true;
127}
#define newval
#define GUC_check_errdetail
Definition: guc.h:476
#define S_ISDIR(m)
Definition: win32_port.h:325

References GUC_check_errdetail, MAXPGPATH, newval, S_ISDIR, stat::st_mode, and stat.

Referenced by _PG_init().

◆ compare_files()

static bool compare_files ( const char *  file1,
const char *  file2 
)
static

Definition at line 235 of file basic_archive.c.

236{
237#define CMP_BUF_SIZE (4096)
238 char buf1[CMP_BUF_SIZE];
239 char buf2[CMP_BUF_SIZE];
240 int fd1;
241 int fd2;
242 bool ret = true;
243
244 fd1 = OpenTransientFile(file1, O_RDONLY | PG_BINARY);
245 if (fd1 < 0)
248 errmsg("could not open file \"%s\": %m", file1)));
249
250 fd2 = OpenTransientFile(file2, O_RDONLY | PG_BINARY);
251 if (fd2 < 0)
254 errmsg("could not open file \"%s\": %m", file2)));
255
256 for (;;)
257 {
258 int nbytes = 0;
259 int buf1_len = 0;
260 int buf2_len = 0;
261
262 while (buf1_len < CMP_BUF_SIZE)
263 {
264 nbytes = read(fd1, buf1 + buf1_len, CMP_BUF_SIZE - buf1_len);
265 if (nbytes < 0)
268 errmsg("could not read file \"%s\": %m", file1)));
269 else if (nbytes == 0)
270 break;
271
272 buf1_len += nbytes;
273 }
274
275 while (buf2_len < CMP_BUF_SIZE)
276 {
277 nbytes = read(fd2, buf2 + buf2_len, CMP_BUF_SIZE - buf2_len);
278 if (nbytes < 0)
281 errmsg("could not read file \"%s\": %m", file2)));
282 else if (nbytes == 0)
283 break;
284
285 buf2_len += nbytes;
286 }
287
288 if (buf1_len != buf2_len || memcmp(buf1, buf2, buf1_len) != 0)
289 {
290 ret = false;
291 break;
292 }
293 else if (buf1_len == 0)
294 break;
295 }
296
297 if (CloseTransientFile(fd1) != 0)
300 errmsg("could not close file \"%s\": %m", file1)));
301
302 if (CloseTransientFile(fd2) != 0)
305 errmsg("could not close file \"%s\": %m", file2)));
306
307 return ret;
308}
#define CMP_BUF_SIZE
#define PG_BINARY
Definition: c.h:1227
int CloseTransientFile(int fd)
Definition: fd.c:2831
int OpenTransientFile(const char *fileName, int fileFlags)
Definition: fd.c:2655
#define read(a, b, c)
Definition: win32.h:13

References CloseTransientFile(), CMP_BUF_SIZE, ereport, errcode_for_file_access(), errmsg(), ERROR, OpenTransientFile(), PG_BINARY, and read.

Referenced by basic_archive_file().

Variable Documentation

◆ archive_directory

char* archive_directory = NULL
static

Definition at line 42 of file basic_archive.c.

Referenced by _PG_init(), basic_archive_configured(), and basic_archive_file().

◆ basic_archive_callbacks

const ArchiveModuleCallbacks basic_archive_callbacks
static
Initial value:
= {
.startup_cb = NULL,
.check_configured_cb = basic_archive_configured,
.archive_file_cb = basic_archive_file,
.shutdown_cb = NULL
}
static bool basic_archive_file(ArchiveModuleState *state, const char *file, const char *path)
static bool basic_archive_configured(ArchiveModuleState *state)

Definition at line 49 of file basic_archive.c.

Referenced by _PG_archive_module_init().

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 40 of file basic_archive.c.