PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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

 PG_MODULE_MAGIC_EXT (.name="basic_archive",.version=PG_VERSION)
 
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

static chararchive_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 85 of file basic_archive.c.

86{
88}
static const ArchiveModuleCallbacks basic_archive_callbacks

References basic_archive_callbacks.

◆ _PG_init()

void _PG_init ( void  )

Definition at line 65 of file basic_archive.c.

66{
67 DefineCustomStringVariable("basic_archive.archive_directory",
68 "Archive file destination directory.",
69 NULL,
71 "",
73 0,
75
76 MarkGUCPrefixReserved("basic_archive");
77}
static char * archive_directory
static bool check_archive_directory(char **newval, void **extra, GucSource source)
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:5123
void MarkGUCPrefixReserved(const char *className)
Definition guc.c:5180
@ PGC_SIGHUP
Definition guc.h:75
static int fb(int x)

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

◆ basic_archive_configured()

static bool basic_archive_configured ( ArchiveModuleState state)
static

Definition at line 125 of file basic_archive.c.

126{
127 if (archive_directory != NULL && archive_directory[0] != '\0')
128 return true;
129
130 arch_module_check_errdetail("%s is not set.",
131 "basic_archive.archive_directory");
132 return false;
133}
#define arch_module_check_errdetail

References arch_module_check_errdetail, archive_directory, and fb().

◆ basic_archive_file()

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

Definition at line 141 of file basic_archive.c.

142{
144 char temp[MAXPGPATH + 256];
145 struct stat st;
146 struct timeval tv;
147 uint64 epoch; /* milliseconds */
148
150 (errmsg("archiving \"%s\" via basic_archive", file)));
151
153
154 /*
155 * First, check if the file has already been archived. If it already
156 * exists and has the same contents as the file we're trying to archive,
157 * we can return success (after ensuring the file is persisted to disk).
158 * This scenario is possible if the server crashed after archiving the
159 * file but before renaming its .ready file to .done.
160 *
161 * If the archive file already exists but has different contents,
162 * something might be wrong, so we just fail.
163 */
164 if (stat(destination, &st) == 0)
165 {
166 if (compare_files(path, destination))
167 {
169 (errmsg("archive file \"%s\" already exists with identical contents",
170 destination)));
171
172 fsync_fname(destination, false);
174
175 return true;
176 }
177
179 (errmsg("archive file \"%s\" already exists", destination)));
180 }
181 else if (errno != ENOENT)
184 errmsg("could not stat file \"%s\": %m", destination)));
185
186 /*
187 * Pick a sufficiently unique name for the temporary file so that a
188 * collision is unlikely. This helps avoid problems in case a temporary
189 * file was left around after a crash or another server happens to be
190 * archiving to the same directory.
191 */
192 gettimeofday(&tv, NULL);
193 if (pg_mul_u64_overflow((uint64) 1000, (uint64) tv.tv_sec, &epoch) ||
194 pg_add_u64_overflow(epoch, (uint64) (tv.tv_usec / 1000), &epoch))
195 elog(ERROR, "could not generate temporary file name for archiving");
196
197 snprintf(temp, sizeof(temp), "%s/%s.%s.%d." UINT64_FORMAT,
198 archive_directory, "archtemp", file, MyProcPid, epoch);
199
200 /*
201 * Copy the file to its temporary destination. Note that this will fail
202 * if temp already exists.
203 */
204 copy_file(path, temp);
205
206 /*
207 * Sync the temporary file to disk and move it to its final destination.
208 * Note that this will overwrite any existing file, but this is only
209 * possible if someone else created the file since the stat() above.
210 */
212
214 (errmsg("archived \"%s\" via basic_archive", file)));
215
216 return true;
217}
static bool compare_files(const char *file1, const char *file2)
#define UINT64_FORMAT
Definition c.h:637
uint64_t uint64
Definition c.h:619
void copy_file(const char *fromfile, const char *tofile)
Definition copydir.c:134
int errcode_for_file_access(void)
Definition elog.c:897
#define DEBUG3
Definition elog.h:28
#define DEBUG1
Definition elog.h:30
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226
#define ereport(elevel,...)
Definition elog.h:150
int durable_rename(const char *oldfile, const char *newfile, int elevel)
Definition fd.c:783
void fsync_fname(const char *fname, bool isdir)
Definition fd.c:757
int MyProcPid
Definition globals.c:47
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
static char * errmsg
#define MAXPGPATH
#define snprintf
Definition port.h:260
#define stat
Definition win32_port.h:74
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, fb(), 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 96 of file basic_archive.c.

97{
98 /*
99 * The default value is an empty string, so we have to accept that value.
100 * Our check_configured callback also checks for this and prevents
101 * archiving from proceeding if it is still empty.
102 */
103 if (*newval == NULL || (*newval)[0] == '\0')
104 return true;
105
106 /*
107 * Make sure the file paths won't be too long. The docs indicate that the
108 * file names to be archived can be up to 64 characters long.
109 */
110 if (strlen(*newval) + 64 + 2 >= MAXPGPATH)
111 {
112 GUC_check_errdetail("Archive directory too long.");
113 return false;
114 }
115
116 return true;
117}
#define newval
#define GUC_check_errdetail
Definition guc.h:507

References fb(), GUC_check_errdetail, MAXPGPATH, and newval.

Referenced by _PG_init().

◆ compare_files()

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

Definition at line 225 of file basic_archive.c.

226{
227#define CMP_BUF_SIZE (4096)
228 char buf1[CMP_BUF_SIZE];
229 char buf2[CMP_BUF_SIZE];
230 int fd1;
231 int fd2;
232 bool ret = true;
233
235 if (fd1 < 0)
238 errmsg("could not open file \"%s\": %m", file1)));
239
241 if (fd2 < 0)
244 errmsg("could not open file \"%s\": %m", file2)));
245
246 for (;;)
247 {
248 int nbytes = 0;
249 int buf1_len = 0;
250 int buf2_len = 0;
251
252 while (buf1_len < CMP_BUF_SIZE)
253 {
254 nbytes = read(fd1, buf1 + buf1_len, CMP_BUF_SIZE - buf1_len);
255 if (nbytes < 0)
258 errmsg("could not read file \"%s\": %m", file1)));
259 else if (nbytes == 0)
260 break;
261
262 buf1_len += nbytes;
263 }
264
265 while (buf2_len < CMP_BUF_SIZE)
266 {
267 nbytes = read(fd2, buf2 + buf2_len, CMP_BUF_SIZE - buf2_len);
268 if (nbytes < 0)
271 errmsg("could not read file \"%s\": %m", file2)));
272 else if (nbytes == 0)
273 break;
274
275 buf2_len += nbytes;
276 }
277
278 if (buf1_len != buf2_len || memcmp(buf1, buf2, buf1_len) != 0)
279 {
280 ret = false;
281 break;
282 }
283 else if (buf1_len == 0)
284 break;
285 }
286
287 if (CloseTransientFile(fd1) != 0)
290 errmsg("could not close file \"%s\": %m", file1)));
291
292 if (CloseTransientFile(fd2) != 0)
295 errmsg("could not close file \"%s\": %m", file2)));
296
297 return ret;
298}
#define CMP_BUF_SIZE
#define PG_BINARY
Definition c.h:1376
int CloseTransientFile(int fd)
Definition fd.c:2855
int OpenTransientFile(const char *fileName, int fileFlags)
Definition fd.c:2678
#define read(a, b, c)
Definition win32.h:13

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

Referenced by basic_archive_file().

◆ PG_MODULE_MAGIC_EXT()

PG_MODULE_MAGIC_EXT ( name = "basic_archive",
version = PG_VERSION 
)

Variable Documentation

◆ archive_directory

char* archive_directory = NULL
static

Definition at line 45 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 52 of file basic_archive.c.

52 {
53 .startup_cb = NULL,
54 .check_configured_cb = basic_archive_configured,
55 .archive_file_cb = basic_archive_file,
56 .shutdown_cb = NULL
57};

Referenced by _PG_archive_module_init().