Skip to content

Commit 5b36e8f

Browse files
committed
Change struct tablespaceinfo's oid member from 'char *' to 'Oid'
This shouldn't change behavior except in the unusual case where there are file in the tablespace directory that have entirely numeric names but are nevertheless not possible names for a tablespace directory, either because their names have leading zeroes that shouldn't be there, or the value is actually zero, or because the value is too large to represent as an OID. In those cases, the directory would previously have made it into the list of tablespaceinfo objects and no longer will. Thus, base backups will now ignore such directories, instead of treating them as legitimate tablespace directories. Similarly, if entries for such tablespaces occur in a tablespace_map file, they will now be rejected as erroneous, instead of being honored. This is infrastructure for future work that wants to be able to know the tablespace of each relation that is part of a backup *as an OID*. By strengthening the up-front validation, we don't have to worry about weird cases later, and can more easily avoid repeated string->integer conversions. Patch by me, reviewed by David Steele. Discussion: http://postgr.es/m/CA+TgmoZNVeBzoqDL8xvr-nkaepq815jtDR4nJzPew7=3iEuM1g@mail.gmail.com
1 parent 5c47c65 commit 5b36e8f

File tree

7 files changed

+49
-29
lines changed

7 files changed

+49
-29
lines changed

src/backend/access/transam/xlog.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8579,9 +8579,22 @@ do_pg_backup_start(const char *backupidstr, bool fast, List **tablespaces,
85798579
char *relpath = NULL;
85808580
char *s;
85818581
PGFileType de_type;
8582+
char *badp;
8583+
Oid tsoid;
85828584

8583-
/* Skip anything that doesn't look like a tablespace */
8584-
if (strspn(de->d_name, "0123456789") != strlen(de->d_name))
8585+
/*
8586+
* Try to parse the directory name as an unsigned integer.
8587+
*
8588+
* Tablespace directories should be positive integers that can be
8589+
* represented in 32 bits, with no leading zeroes or trailing
8590+
* garbage. If we come across a name that doesn't meet those
8591+
* criteria, skip it.
8592+
*/
8593+
if (de->d_name[0] < '1' || de->d_name[1] > '9')
8594+
continue;
8595+
errno = 0;
8596+
tsoid = strtoul(de->d_name, &badp, 10);
8597+
if (*badp != '\0' || errno == EINVAL || errno == ERANGE)
85858598
continue;
85868599

85878600
snprintf(fullpath, sizeof(fullpath), "pg_tblspc/%s", de->d_name);
@@ -8656,7 +8669,7 @@ do_pg_backup_start(const char *backupidstr, bool fast, List **tablespaces,
86568669
}
86578670

86588671
ti = palloc(sizeof(tablespaceinfo));
8659-
ti->oid = pstrdup(de->d_name);
8672+
ti->oid = tsoid;
86608673
ti->path = pstrdup(linkpath);
86618674
ti->rpath = relpath;
86628675
ti->size = -1;

src/backend/access/transam/xlogrecovery.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ InitWalRecovery(ControlFileData *ControlFile, bool *wasShutdown_ptr,
678678
tablespaceinfo *ti = lfirst(lc);
679679
char *linkloc;
680680

681-
linkloc = psprintf("pg_tblspc/%s", ti->oid);
681+
linkloc = psprintf("pg_tblspc/%u", ti->oid);
682682

683683
/*
684684
* Remove the existing symlink if any and Create the symlink
@@ -692,7 +692,6 @@ InitWalRecovery(ControlFileData *ControlFile, bool *wasShutdown_ptr,
692692
errmsg("could not create symbolic link \"%s\": %m",
693693
linkloc)));
694694

695-
pfree(ti->oid);
696695
pfree(ti->path);
697696
pfree(ti);
698697
}
@@ -1341,6 +1340,8 @@ read_tablespace_map(List **tablespaces)
13411340
{
13421341
if (!was_backslash && (ch == '\n' || ch == '\r'))
13431342
{
1343+
char *endp;
1344+
13441345
if (i == 0)
13451346
continue; /* \r immediately followed by \n */
13461347

@@ -1360,7 +1361,12 @@ read_tablespace_map(List **tablespaces)
13601361
str[n++] = '\0';
13611362

13621363
ti = palloc0(sizeof(tablespaceinfo));
1363-
ti->oid = pstrdup(str);
1364+
errno = 0;
1365+
ti->oid = strtoul(str, &endp, 10);
1366+
if (*endp != '\0' || errno == EINVAL || errno == ERANGE)
1367+
ereport(FATAL,
1368+
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1369+
errmsg("invalid data in file \"%s\"", TABLESPACE_MAP)));
13641370
ti->path = pstrdup(str + n);
13651371
*tablespaces = lappend(*tablespaces, ti);
13661372

src/backend/backup/backup_manifest.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ FreeBackupManifest(backup_manifest_info *manifest)
9797
* Add an entry to the backup manifest for a file.
9898
*/
9999
void
100-
AddFileToBackupManifest(backup_manifest_info *manifest, const char *spcoid,
100+
AddFileToBackupManifest(backup_manifest_info *manifest, Oid spcoid,
101101
const char *pathname, size_t size, pg_time_t mtime,
102102
pg_checksum_context *checksum_ctx)
103103
{
@@ -114,9 +114,9 @@ AddFileToBackupManifest(backup_manifest_info *manifest, const char *spcoid,
114114
* pathname relative to the data directory (ignoring the intermediate
115115
* symlink traversal).
116116
*/
117-
if (spcoid != NULL)
117+
if (OidIsValid(spcoid))
118118
{
119-
snprintf(pathbuf, sizeof(pathbuf), "pg_tblspc/%s/%s", spcoid,
119+
snprintf(pathbuf, sizeof(pathbuf), "pg_tblspc/%u/%s", spcoid,
120120
pathname);
121121
pathname = pathbuf;
122122
}

src/backend/backup/basebackup.c

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,15 @@ typedef struct
7575
pg_checksum_type manifest_checksum_type;
7676
} basebackup_options;
7777

78-
static int64 sendTablespace(bbsink *sink, char *path, char *spcoid, bool sizeonly,
78+
static int64 sendTablespace(bbsink *sink, char *path, Oid spcoid, bool sizeonly,
7979
struct backup_manifest_info *manifest);
8080
static int64 sendDir(bbsink *sink, const char *path, int basepathlen, bool sizeonly,
8181
List *tablespaces, bool sendtblspclinks,
82-
backup_manifest_info *manifest, const char *spcoid);
82+
backup_manifest_info *manifest, Oid spcoid);
8383
static bool sendFile(bbsink *sink, const char *readfilename, const char *tarfilename,
84-
struct stat *statbuf, bool missing_ok, Oid dboid,
85-
backup_manifest_info *manifest, const char *spcoid);
84+
struct stat *statbuf, bool missing_ok,
85+
Oid dboid, Oid spcoid,
86+
backup_manifest_info *manifest);
8687
static off_t read_file_data_into_buffer(bbsink *sink,
8788
const char *readfilename, int fd,
8889
off_t offset, size_t length,
@@ -305,7 +306,7 @@ perform_base_backup(basebackup_options *opt, bbsink *sink)
305306

306307
if (tmp->path == NULL)
307308
tmp->size = sendDir(sink, ".", 1, true, state.tablespaces,
308-
true, NULL, NULL);
309+
true, NULL, InvalidOid);
309310
else
310311
tmp->size = sendTablespace(sink, tmp->path, tmp->oid, true,
311312
NULL);
@@ -346,7 +347,7 @@ perform_base_backup(basebackup_options *opt, bbsink *sink)
346347

347348
/* Then the bulk of the files... */
348349
sendDir(sink, ".", 1, false, state.tablespaces,
349-
sendtblspclinks, &manifest, NULL);
350+
sendtblspclinks, &manifest, InvalidOid);
350351

351352
/* ... and pg_control after everything else. */
352353
if (lstat(XLOG_CONTROL_FILE, &statbuf) != 0)
@@ -355,11 +356,11 @@ perform_base_backup(basebackup_options *opt, bbsink *sink)
355356
errmsg("could not stat file \"%s\": %m",
356357
XLOG_CONTROL_FILE)));
357358
sendFile(sink, XLOG_CONTROL_FILE, XLOG_CONTROL_FILE, &statbuf,
358-
false, InvalidOid, &manifest, NULL);
359+
false, InvalidOid, InvalidOid, &manifest);
359360
}
360361
else
361362
{
362-
char *archive_name = psprintf("%s.tar", ti->oid);
363+
char *archive_name = psprintf("%u.tar", ti->oid);
363364

364365
bbsink_begin_archive(sink, archive_name);
365366

@@ -623,8 +624,8 @@ perform_base_backup(basebackup_options *opt, bbsink *sink)
623624
(errcode_for_file_access(),
624625
errmsg("could not stat file \"%s\": %m", pathbuf)));
625626

626-
sendFile(sink, pathbuf, pathbuf, &statbuf, false, InvalidOid,
627-
&manifest, NULL);
627+
sendFile(sink, pathbuf, pathbuf, &statbuf, false,
628+
InvalidOid, InvalidOid, &manifest);
628629

629630
/* unconditionally mark file as archived */
630631
StatusFilePath(pathbuf, fname, ".done");
@@ -1087,7 +1088,7 @@ sendFileWithContent(bbsink *sink, const char *filename, const char *content,
10871088

10881089
_tarWritePadding(sink, len);
10891090

1090-
AddFileToBackupManifest(manifest, NULL, filename, len,
1091+
AddFileToBackupManifest(manifest, InvalidOid, filename, len,
10911092
(pg_time_t) statbuf.st_mtime, &checksum_ctx);
10921093
}
10931094

@@ -1099,7 +1100,7 @@ sendFileWithContent(bbsink *sink, const char *filename, const char *content,
10991100
* Only used to send auxiliary tablespaces, not PGDATA.
11001101
*/
11011102
static int64
1102-
sendTablespace(bbsink *sink, char *path, char *spcoid, bool sizeonly,
1103+
sendTablespace(bbsink *sink, char *path, Oid spcoid, bool sizeonly,
11031104
backup_manifest_info *manifest)
11041105
{
11051106
int64 size;
@@ -1154,7 +1155,7 @@ sendTablespace(bbsink *sink, char *path, char *spcoid, bool sizeonly,
11541155
static int64
11551156
sendDir(bbsink *sink, const char *path, int basepathlen, bool sizeonly,
11561157
List *tablespaces, bool sendtblspclinks, backup_manifest_info *manifest,
1157-
const char *spcoid)
1158+
Oid spcoid)
11581159
{
11591160
DIR *dir;
11601161
struct dirent *de;
@@ -1416,8 +1417,8 @@ sendDir(bbsink *sink, const char *path, int basepathlen, bool sizeonly,
14161417

14171418
if (!sizeonly)
14181419
sent = sendFile(sink, pathbuf, pathbuf + basepathlen + 1, &statbuf,
1419-
true, isDbDir ? atooid(lastDir + 1) : InvalidOid,
1420-
manifest, spcoid);
1420+
true, isDbDir ? atooid(lastDir + 1) : InvalidOid, spcoid,
1421+
manifest);
14211422

14221423
if (sent || sizeonly)
14231424
{
@@ -1486,8 +1487,8 @@ is_checksummed_file(const char *fullpath, const char *filename)
14861487
*/
14871488
static bool
14881489
sendFile(bbsink *sink, const char *readfilename, const char *tarfilename,
1489-
struct stat *statbuf, bool missing_ok, Oid dboid,
1490-
backup_manifest_info *manifest, const char *spcoid)
1490+
struct stat *statbuf, bool missing_ok, Oid dboid, Oid spcoid,
1491+
backup_manifest_info *manifest)
14911492
{
14921493
int fd;
14931494
BlockNumber blkno = 0;

src/backend/backup/basebackup_copy.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ SendTablespaceList(List *tablespaces)
407407
}
408408
else
409409
{
410-
values[0] = ObjectIdGetDatum(strtoul(ti->oid, NULL, 10));
410+
values[0] = ObjectIdGetDatum(ti->oid);
411411
values[1] = CStringGetTextDatum(ti->path);
412412
}
413413
if (ti->size >= 0)

src/include/backup/backup_manifest.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ extern void InitializeBackupManifest(backup_manifest_info *manifest,
3939
backup_manifest_option want_manifest,
4040
pg_checksum_type manifest_checksum_type);
4141
extern void AddFileToBackupManifest(backup_manifest_info *manifest,
42-
const char *spcoid,
42+
Oid spcoid,
4343
const char *pathname, size_t size,
4444
pg_time_t mtime,
4545
pg_checksum_context *checksum_ctx);

src/include/backup/basebackup.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
*/
2828
typedef struct
2929
{
30-
char *oid; /* tablespace's OID, as a decimal string */
30+
Oid oid; /* tablespace's OID */
3131
char *path; /* full path to tablespace's directory */
3232
char *rpath; /* relative path if it's within PGDATA, else
3333
* NULL */

0 commit comments

Comments
 (0)