1
0
mirror of https://github.com/postgres/postgres.git synced 2025-05-03 22:24:49 +03:00

Improve pg_upgrade's load_directory() function.

Error out on out-of-memory, rather than returning -1, which the sole
existing caller wasn't checking for anyway.  There doesn't seem to be
any use-case for making the caller check for failure here.

Detect failure return from readdir().

Use a less platform-dependent method of calculating the entrysize.
It's possible, but not yet confirmed, that this explains bug #6733,
in which Mike Wilson reports a pg_upgrade crash that did not occur
in 9.1.  (Note that load_directory is effectively new code in 9.2,
at least on platforms that have scandir().)

Fix up comments, avoid uselessly using two counters, reduce the number
of realloc calls to something sane.
This commit is contained in:
Tom Lane 2012-07-18 01:13:25 -04:00
parent 4abcce8cab
commit 3d929dc7b8
3 changed files with 47 additions and 31 deletions

View File

@ -224,13 +224,12 @@ copy_file(const char *srcfile, const char *dstfile, bool force)
/* /*
* load_directory() * load_directory()
* *
* Returns count of files that meet the selection criteria coded in * Read all the file names in the specified directory, and return them as
* the function pointed to by selector. Creates an array of pointers * an array of "struct dirent" pointers. The array address is returned in
* to dirent structures. Address of array returned in namelist. * *namelist, and the function result is the count of file names.
* *
* Note that the number of dirent structures needed is dynamically * To free the result data, free each namelist array member, then free the
* allocated using realloc. Realloc can be inefficient if invoked a * namelist array itself.
* large number of times.
*/ */
int int
load_directory(const char *dirname, struct dirent *** namelist) load_directory(const char *dirname, struct dirent *** namelist)
@ -238,42 +237,47 @@ load_directory(const char *dirname, struct dirent *** namelist)
DIR *dirdesc; DIR *dirdesc;
struct dirent *direntry; struct dirent *direntry;
int count = 0; int count = 0;
int name_num = 0; int allocsize = 64;
size_t entrysize; size_t entrysize;
*namelist = (struct dirent **)
pg_malloc(allocsize * sizeof(struct dirent *));
if ((dirdesc = opendir(dirname)) == NULL) if ((dirdesc = opendir(dirname)) == NULL)
pg_log(PG_FATAL, "could not open directory \"%s\": %s\n", dirname, getErrorText(errno)); pg_log(PG_FATAL, "could not open directory \"%s\": %s\n",
dirname, getErrorText(errno));
*namelist = NULL; while (errno = 0, (direntry = readdir(dirdesc)) != NULL)
while ((direntry = readdir(dirdesc)) != NULL)
{ {
count++; if (count >= allocsize)
*namelist = (struct dirent **) realloc((void *) (*namelist),
(size_t) ((name_num + 1) * sizeof(struct dirent *)));
if (*namelist == NULL)
{ {
closedir(dirdesc); allocsize *= 2;
return -1; *namelist = (struct dirent **)
pg_realloc(*namelist, allocsize * sizeof(struct dirent *));
} }
entrysize = sizeof(struct dirent) - sizeof(direntry->d_name) + entrysize = offsetof(struct dirent, d_name) +
strlen(direntry->d_name) + 1; strlen(direntry->d_name) + 1;
(*namelist)[name_num] = (struct dirent *) malloc(entrysize); (*namelist)[count] = (struct dirent *) pg_malloc(entrysize);
if ((*namelist)[name_num] == NULL) memcpy((*namelist)[count], direntry, entrysize);
{
closedir(dirdesc); count++;
return -1;
} }
memcpy((*namelist)[name_num], direntry, entrysize); #ifdef WIN32
/*
* This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
* released version
*/
if (GetLastError() == ERROR_NO_MORE_FILES)
errno = 0;
#endif
name_num++; if (errno)
} pg_log(PG_FATAL, "could not read directory \"%s\": %s\n",
dirname, getErrorText(errno));
closedir(dirdesc); closedir(dirdesc);

View File

@ -430,7 +430,8 @@ prep_status(const char *fmt,...)
__attribute__((format(PG_PRINTF_ATTRIBUTE, 1, 2))); __attribute__((format(PG_PRINTF_ATTRIBUTE, 1, 2)));
void check_ok(void); void check_ok(void);
char *pg_strdup(const char *s); char *pg_strdup(const char *s);
void *pg_malloc(int size); void *pg_malloc(size_t size);
void *pg_realloc(void *ptr, size_t size);
void pg_free(void *ptr); void pg_free(void *ptr);
const char *getErrorText(int errNum); const char *getErrorText(int errNum);
unsigned int str2uint(const char *str); unsigned int str2uint(const char *str);

View File

@ -183,7 +183,7 @@ get_user_info(char **user_name)
void * void *
pg_malloc(int n) pg_malloc(size_t n)
{ {
void *p = malloc(n); void *p = malloc(n);
@ -193,6 +193,17 @@ pg_malloc(int n)
return p; return p;
} }
void *
pg_realloc(void *ptr, size_t n)
{
void *p = realloc(ptr, n);
if (p == NULL)
pg_log(PG_FATAL, "%s: out of memory\n", os_info.progname);
return p;
}
void void
pg_free(void *p) pg_free(void *p)