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

Commit 4da8fc0

Browse files
committed
Simplify pg_upgrade's handling when returning directory listings.
Backpatch to 9.2.
1 parent af026b5 commit 4da8fc0

File tree

3 files changed

+17
-26
lines changed

3 files changed

+17
-26
lines changed

contrib/pg_upgrade/file.c

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -225,23 +225,21 @@ copy_file(const char *srcfile, const char *dstfile, bool force)
225225
* load_directory()
226226
*
227227
* Read all the file names in the specified directory, and return them as
228-
* an array of "struct dirent" pointers. The array address is returned in
228+
* an array of "char *" pointers. The array address is returned in
229229
* *namelist, and the function result is the count of file names.
230230
*
231-
* To free the result data, free each namelist array member, then free the
231+
* To free the result data, free each (char *) array member, then free the
232232
* namelist array itself.
233233
*/
234234
int
235-
load_directory(const char *dirname, struct dirent *** namelist)
235+
load_directory(const char *dirname, char ***namelist)
236236
{
237237
DIR *dirdesc;
238238
struct dirent *direntry;
239239
int count = 0;
240-
int allocsize = 64;
241-
size_t entrysize;
240+
int allocsize = 64; /* initial array size */
242241

243-
*namelist = (struct dirent **)
244-
pg_malloc(allocsize * sizeof(struct dirent *));
242+
*namelist = (char **) pg_malloc(allocsize * sizeof(char *));
245243

246244
if ((dirdesc = opendir(dirname)) == NULL)
247245
pg_log(PG_FATAL, "could not open directory \"%s\": %s\n",
@@ -252,18 +250,11 @@ load_directory(const char *dirname, struct dirent *** namelist)
252250
if (count >= allocsize)
253251
{
254252
allocsize *= 2;
255-
*namelist = (struct dirent **)
256-
pg_realloc(*namelist, allocsize * sizeof(struct dirent *));
253+
*namelist = (char **)
254+
pg_realloc(*namelist, allocsize * sizeof(char *));
257255
}
258256

259-
entrysize = offsetof(struct dirent, d_name) +
260-
strlen(direntry->d_name) + 1;
261-
262-
(*namelist)[count] = (struct dirent *) pg_malloc(entrysize);
263-
264-
memcpy((*namelist)[count], direntry, entrysize);
265-
266-
count++;
257+
(*namelist)[count++] = pg_strdup(direntry->d_name);
267258
}
268259

269260
#ifdef WIN32

contrib/pg_upgrade/pg_upgrade.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ const char *setupPageConverter(pageCnvCtx **result);
356356
typedef void *pageCnvCtx;
357357
#endif
358358

359-
int load_directory(const char *dirname, struct dirent *** namelist);
359+
int load_directory(const char *dirname, char ***namelist);
360360
const char *copyAndUpdateFile(pageCnvCtx *pageConverter, const char *src,
361361
const char *dst, bool force);
362362
const char *linkAndUpdateFile(pageCnvCtx *pageConverter, const char *src,

contrib/pg_upgrade/relfilenode.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ transfer_single_new_db(pageCnvCtx *pageConverter,
133133
{
134134
char old_dir[MAXPGPATH];
135135
char file_pattern[MAXPGPATH];
136-
struct dirent **namelist = NULL;
136+
char **namelist = NULL;
137137
int numFiles = 0;
138138
int mapnum;
139139
int fileno;
@@ -192,21 +192,21 @@ transfer_single_new_db(pageCnvCtx *pageConverter,
192192

193193
for (fileno = 0; fileno < numFiles; fileno++)
194194
{
195-
char *vm_offset = strstr(namelist[fileno]->d_name, "_vm");
195+
char *vm_offset = strstr(namelist[fileno], "_vm");
196196
bool is_vm_file = false;
197197

198198
/* Is a visibility map file? (name ends with _vm) */
199199
if (vm_offset && strlen(vm_offset) == strlen("_vm"))
200200
is_vm_file = true;
201201

202-
if (strncmp(namelist[fileno]->d_name, file_pattern,
202+
if (strncmp(namelist[fileno], file_pattern,
203203
strlen(file_pattern)) == 0 &&
204204
(!is_vm_file || !vm_crashsafe_change))
205205
{
206206
snprintf(old_file, sizeof(old_file), "%s/%s", maps[mapnum].old_dir,
207-
namelist[fileno]->d_name);
207+
namelist[fileno]);
208208
snprintf(new_file, sizeof(new_file), "%s/%u%s", maps[mapnum].new_dir,
209-
maps[mapnum].new_relfilenode, strchr(namelist[fileno]->d_name, '_'));
209+
maps[mapnum].new_relfilenode, strchr(namelist[fileno], '_'));
210210

211211
unlink(new_file);
212212
transfer_relfile(pageConverter, old_file, new_file,
@@ -227,13 +227,13 @@ transfer_single_new_db(pageCnvCtx *pageConverter,
227227

228228
for (fileno = 0; fileno < numFiles; fileno++)
229229
{
230-
if (strncmp(namelist[fileno]->d_name, file_pattern,
230+
if (strncmp(namelist[fileno], file_pattern,
231231
strlen(file_pattern)) == 0)
232232
{
233233
snprintf(old_file, sizeof(old_file), "%s/%s", maps[mapnum].old_dir,
234-
namelist[fileno]->d_name);
234+
namelist[fileno]);
235235
snprintf(new_file, sizeof(new_file), "%s/%u%s", maps[mapnum].new_dir,
236-
maps[mapnum].new_relfilenode, strchr(namelist[fileno]->d_name, '.'));
236+
maps[mapnum].new_relfilenode, strchr(namelist[fileno], '.'));
237237

238238
unlink(new_file);
239239
transfer_relfile(pageConverter, old_file, new_file,

0 commit comments

Comments
 (0)