Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit e191a69

Browse files
committed
pg_upgrade: Don't overwrite existing files.
For historical reasons, copyFile and rewriteVisibilityMap took a force argument which was always passed as true, meaning that any existing file should be overwritten. However, it seems much safer to instead fail if a file we need to write already exists. While we're at it, remove the "force" argument altogether, since it was never passed as anything other than true (and now we would never pass it as anything other than false, if we kept it). Noted by Andres Freund during post-commit review of the patch that added rewriteVisibilityMap, commit 7087166, but this also changes the behavior when copying files without rewriting them. Patch by Masahiko Sawada.
1 parent 932b97a commit e191a69

File tree

3 files changed

+13
-14
lines changed

3 files changed

+13
-14
lines changed

src/bin/pg_upgrade/file.c

+8-8
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323

2424
#ifndef WIN32
25-
static int copy_file(const char *fromfile, const char *tofile, bool force);
25+
static int copy_file(const char *fromfile, const char *tofile);
2626
#else
2727
static int win32_pghardlink(const char *src, const char *dst);
2828
#endif
@@ -34,12 +34,12 @@ static int win32_pghardlink(const char *src, const char *dst);
3434
* Copies a relation file from src to dst.
3535
*/
3636
const char *
37-
copyFile(const char *src, const char *dst, bool force)
37+
copyFile(const char *src, const char *dst)
3838
{
3939
#ifndef WIN32
40-
if (copy_file(src, dst, force) == -1)
40+
if (copy_file(src, dst) == -1)
4141
#else
42-
if (CopyFile(src, dst, !force) == 0)
42+
if (CopyFile(src, dst, true) == 0)
4343
#endif
4444
return getErrorText();
4545
else
@@ -68,7 +68,7 @@ linkFile(const char *src, const char *dst)
6868

6969
#ifndef WIN32
7070
static int
71-
copy_file(const char *srcfile, const char *dstfile, bool force)
71+
copy_file(const char *srcfile, const char *dstfile)
7272
{
7373
#define COPY_BUF_SIZE (50 * BLCKSZ)
7474

@@ -87,7 +87,7 @@ copy_file(const char *srcfile, const char *dstfile, bool force)
8787
if ((src_fd = open(srcfile, O_RDONLY, 0)) < 0)
8888
return -1;
8989

90-
if ((dest_fd = open(dstfile, O_RDWR | O_CREAT | (force ? 0 : O_EXCL), S_IRUSR | S_IWUSR)) < 0)
90+
if ((dest_fd = open(dstfile, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR)) < 0)
9191
{
9292
save_errno = errno;
9393

@@ -159,7 +159,7 @@ copy_file(const char *srcfile, const char *dstfile, bool force)
159159
* VACUUM.
160160
*/
161161
const char *
162-
rewriteVisibilityMap(const char *fromfile, const char *tofile, bool force)
162+
rewriteVisibilityMap(const char *fromfile, const char *tofile)
163163
{
164164
int src_fd = 0;
165165
int dst_fd = 0;
@@ -186,7 +186,7 @@ rewriteVisibilityMap(const char *fromfile, const char *tofile, bool force)
186186
return getErrorText();
187187
}
188188

189-
if ((dst_fd = open(tofile, O_RDWR | O_CREAT | (force ? 0 : O_EXCL), S_IRUSR | S_IWUSR)) < 0)
189+
if ((dst_fd = open(tofile, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR)) < 0)
190190
{
191191
close(src_fd);
192192
return getErrorText();

src/bin/pg_upgrade/pg_upgrade.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -367,10 +367,9 @@ bool pid_lock_file_exists(const char *datadir);
367367

368368
/* file.c */
369369

370-
const char *copyFile(const char *src, const char *dst, bool force);
370+
const char *copyFile(const char *src, const char *dst);
371371
const char *linkFile(const char *src, const char *dst);
372-
const char *rewriteVisibilityMap(const char *fromfile, const char *tofile,
373-
bool force);
372+
const char *rewriteVisibilityMap(const char *fromfile, const char *tofile);
374373

375374
void check_hard_link(void);
376375
FILE *fopen_priv(const char *path, const char *mode);

src/bin/pg_upgrade/relfilenode.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,9 @@ transfer_relfile(FileNameMap *map, const char *type_suffix, bool vm_must_add_fro
248248

249249
/* Rewrite visibility map if needed */
250250
if (vm_must_add_frozenbit && (strcmp(type_suffix, "_vm") == 0))
251-
msg = rewriteVisibilityMap(old_file, new_file, true);
251+
msg = rewriteVisibilityMap(old_file, new_file);
252252
else
253-
msg = copyFile(old_file, new_file, true);
253+
msg = copyFile(old_file, new_file);
254254

255255
if (msg)
256256
pg_fatal("error while copying relation \"%s.%s\" (\"%s\" to \"%s\"): %s\n",
@@ -262,7 +262,7 @@ transfer_relfile(FileNameMap *map, const char *type_suffix, bool vm_must_add_fro
262262

263263
/* Rewrite visibility map if needed */
264264
if (vm_must_add_frozenbit && (strcmp(type_suffix, "_vm") == 0))
265-
msg = rewriteVisibilityMap(old_file, new_file, true);
265+
msg = rewriteVisibilityMap(old_file, new_file);
266266
else
267267
msg = linkFile(old_file, new_file);
268268

0 commit comments

Comments
 (0)