Skip to content

Commit 33599a6

Browse files
committed
Remove file-level global WalSegSz.
It's better style to pass the value around to just the places that need it. This makes it easier to determine whether the value is always properly initialized before use. Reviewed-by: Amul Sul <sulamul@gmail.com> Discussion: http://postgr.es/m/CAAJ_b94+wObPn-z1VECipnSFhjMJ+R2cpTmKVYLjyQuVn+B5QA@mail.gmail.com
1 parent 851f664 commit 33599a6

1 file changed

Lines changed: 19 additions & 18 deletions

File tree

src/bin/pg_waldump/pg_waldump.c

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939

4040
static const char *progname;
4141

42-
static int WalSegSz;
4342
static volatile sig_atomic_t time_to_stop = false;
4443

4544
static const RelFileLocator emptyRelFileLocator = {0, 0, 0};
@@ -203,11 +202,11 @@ open_file_in_directory(const char *directory, const char *fname)
203202
/*
204203
* Try to find fname in the given directory. Returns true if it is found,
205204
* false otherwise. If fname is NULL, search the complete directory for any
206-
* file with a valid WAL file name. If file is successfully opened, set the
207-
* wal segment size.
205+
* file with a valid WAL file name. If file is successfully opened, set
206+
* *WaSegSz to the WAL segment size.
208207
*/
209208
static bool
210-
search_directory(const char *directory, const char *fname)
209+
search_directory(const char *directory, const char *fname, int *WalSegSz)
211210
{
212211
int fd = -1;
213212
DIR *xldir;
@@ -249,17 +248,17 @@ search_directory(const char *directory, const char *fname)
249248
{
250249
XLogLongPageHeader longhdr = (XLogLongPageHeader) buf.data;
251250

252-
WalSegSz = longhdr->xlp_seg_size;
253-
254-
if (!IsValidWalSegSize(WalSegSz))
251+
if (!IsValidWalSegSize(longhdr->xlp_seg_size))
255252
{
256253
pg_log_error(ngettext("invalid WAL segment size in WAL file \"%s\" (%d byte)",
257254
"invalid WAL segment size in WAL file \"%s\" (%d bytes)",
258-
WalSegSz),
259-
fname, WalSegSz);
255+
longhdr->xlp_seg_size),
256+
fname, longhdr->xlp_seg_size);
260257
pg_log_error_detail("The WAL segment size must be a power of two between 1 MB and 1 GB.");
261258
exit(1);
262259
}
260+
261+
*WalSegSz = longhdr->xlp_seg_size;
263262
}
264263
else if (r < 0)
265264
pg_fatal("could not read file \"%s\": %m",
@@ -286,40 +285,41 @@ search_directory(const char *directory, const char *fname)
286285
* XLOGDIR /
287286
* $PGDATA / XLOGDIR /
288287
*
289-
* The valid target directory is returned.
288+
* The valid target directory is returned, and *WalSegSz is set to the
289+
* size of the WAL segment found in that directory.
290290
*/
291291
static char *
292-
identify_target_directory(char *directory, char *fname)
292+
identify_target_directory(char *directory, char *fname, int *WalSegSz)
293293
{
294294
char fpath[MAXPGPATH];
295295

296296
if (directory != NULL)
297297
{
298-
if (search_directory(directory, fname))
298+
if (search_directory(directory, fname, WalSegSz))
299299
return pg_strdup(directory);
300300

301301
/* directory / XLOGDIR */
302302
snprintf(fpath, MAXPGPATH, "%s/%s", directory, XLOGDIR);
303-
if (search_directory(fpath, fname))
303+
if (search_directory(fpath, fname, WalSegSz))
304304
return pg_strdup(fpath);
305305
}
306306
else
307307
{
308308
const char *datadir;
309309

310310
/* current directory */
311-
if (search_directory(".", fname))
311+
if (search_directory(".", fname, WalSegSz))
312312
return pg_strdup(".");
313313
/* XLOGDIR */
314-
if (search_directory(XLOGDIR, fname))
314+
if (search_directory(XLOGDIR, fname, WalSegSz))
315315
return pg_strdup(XLOGDIR);
316316

317317
datadir = getenv("PGDATA");
318318
/* $PGDATA / XLOGDIR */
319319
if (datadir != NULL)
320320
{
321321
snprintf(fpath, MAXPGPATH, "%s/%s", datadir, XLOGDIR);
322-
if (search_directory(fpath, fname))
322+
if (search_directory(fpath, fname, WalSegSz))
323323
return pg_strdup(fpath);
324324
}
325325
}
@@ -801,6 +801,7 @@ main(int argc, char **argv)
801801
XLogRecPtr first_record;
802802
char *waldir = NULL;
803803
char *errormsg;
804+
int WalSegSz;
804805

805806
static struct option long_options[] = {
806807
{"bkp-details", no_argument, NULL, 'b'},
@@ -1127,7 +1128,7 @@ main(int argc, char **argv)
11271128
pg_fatal("could not open directory \"%s\": %m", waldir);
11281129
}
11291130

1130-
waldir = identify_target_directory(waldir, fname);
1131+
waldir = identify_target_directory(waldir, fname, &WalSegSz);
11311132
fd = open_file_in_directory(waldir, fname);
11321133
if (fd < 0)
11331134
pg_fatal("could not open file \"%s\"", fname);
@@ -1189,7 +1190,7 @@ main(int argc, char **argv)
11891190
}
11901191
}
11911192
else
1192-
waldir = identify_target_directory(waldir, NULL);
1193+
waldir = identify_target_directory(waldir, NULL, &WalSegSz);
11931194

11941195
/* we don't know what to print */
11951196
if (!XLogRecPtrIsValid(private.startptr))

0 commit comments

Comments
 (0)