Skip to content

Commit 3864afa

Browse files
committed
Clean up some copied-and-pasted code in pg_upgrade.
1. Don't reimplement S_ISDIR() and S_ISREG() badly. 2. Don't reimplement access() badly. This code appears to have been copied from ancient versions of the corresponding backend routines, and not patched to incorporate subsequent fixes (see my commits of 2008-03-31 and 2010-01-14 respectively). It might be a good idea to change it to just *call* those routines, but for now I'll just transpose these fixes over.
1 parent 1319002 commit 3864afa

File tree

2 files changed

+7
-66
lines changed

2 files changed

+7
-66
lines changed

contrib/pg_upgrade/exec.c

Lines changed: 5 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include "pg_upgrade.h"
1111

1212
#include <fcntl.h>
13-
#include <grp.h>
13+
#include <unistd.h>
1414

1515

1616
static void check_data_dir(const char *pg_data);
@@ -206,17 +206,9 @@ validate_exec(const char *path)
206206
{
207207
struct stat buf;
208208

209-
#ifndef WIN32
210-
uid_t euid;
211-
struct group *gp;
212-
struct passwd *pwp;
213-
int in_grp = 0;
214-
#else
215-
char path_exe[MAXPGPATH + sizeof(EXE_EXT) - 1];
216-
#endif
217-
218209
#ifdef WIN32
219210
/* Win32 requires a .exe suffix for stat() */
211+
char path_exe[MAXPGPATH + sizeof(EXE_EXT) - 1];
220212

221213
if (strlen(path) >= strlen(EXE_EXT) &&
222214
pg_strcasecmp(path + strlen(path) - strlen(EXE_EXT), EXE_EXT) != 0)
@@ -233,68 +225,17 @@ validate_exec(const char *path)
233225
if (stat(path, &buf) < 0)
234226
return getErrorText(errno);
235227

236-
if ((buf.st_mode & S_IFMT) != S_IFREG)
228+
if (!S_ISREG(buf.st_mode))
237229
return "not an executable file";
238230

239-
/*
240-
* Ensure that we are using an authorized executable.
241-
*/
242-
243231
/*
244232
* Ensure that the file is both executable and readable (required for
245233
* dynamic loading).
246234
*/
247235
#ifndef WIN32
248-
euid = geteuid();
249-
250-
/* If owned by us, just check owner bits */
251-
if (euid == buf.st_uid)
252-
{
253-
if ((buf.st_mode & S_IRUSR) == 0)
254-
return "can't read file (permission denied)";
255-
if ((buf.st_mode & S_IXUSR) == 0)
256-
return "can't execute (permission denied)";
257-
return NULL;
258-
}
259-
260-
/* OK, check group bits */
261-
pwp = getpwuid(euid); /* not thread-safe */
262-
263-
if (pwp)
264-
{
265-
if (pwp->pw_gid == buf.st_gid) /* my primary group? */
266-
++in_grp;
267-
else if (pwp->pw_name &&
268-
(gp = getgrgid(buf.st_gid)) != NULL &&
269-
/* not thread-safe */ gp->gr_mem != NULL)
270-
{
271-
/* try list of member groups */
272-
int i;
273-
274-
for (i = 0; gp->gr_mem[i]; ++i)
275-
{
276-
if (!strcmp(gp->gr_mem[i], pwp->pw_name))
277-
{
278-
++in_grp;
279-
break;
280-
}
281-
}
282-
}
283-
284-
if (in_grp)
285-
{
286-
if ((buf.st_mode & S_IRGRP) == 0)
287-
return "can't read file (permission denied)";
288-
if ((buf.st_mode & S_IXGRP) == 0)
289-
return "can't execute (permission denied)";
290-
return NULL;
291-
}
292-
}
293-
294-
/* Check "other" bits */
295-
if ((buf.st_mode & S_IROTH) == 0)
236+
if (access(path, R_OK) != 0)
296237
return "can't read file (permission denied)";
297-
if ((buf.st_mode & S_IXOTH) == 0)
238+
if (access(path, X_OK) != 0)
298239
return "can't execute (permission denied)";
299240
return NULL;
300241
#else

contrib/pg_upgrade/file.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,13 +440,13 @@ copy_dir(const char *src, const char *dst, bool force)
440440
return -1;
441441
}
442442

443-
if (fst.st_mode & S_IFDIR)
443+
if (S_ISDIR(fst.st_mode))
444444
{
445445
/* recurse to handle subdirectories */
446446
if (force)
447447
copy_dir(src_file, dest_file, true);
448448
}
449-
else if (fst.st_mode & S_IFREG)
449+
else if (S_ISREG(fst.st_mode))
450450
{
451451
if ((copy_file(src_file, dest_file, 1)) == -1)
452452
{

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy