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

Commit ba7c597

Browse files
committed
Move pgfnames() from libpgport to libpgcommon
It requires pstrdup() from libpgcommon.
1 parent cab5dc5 commit ba7c597

File tree

5 files changed

+112
-93
lines changed

5 files changed

+112
-93
lines changed

src/bin/initdb/nls.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# src/bin/initdb/nls.mk
22
CATALOG_NAME = initdb
33
AVAIL_LANGUAGES = cs de es fr it ja pl pt_BR ru zh_CN
4-
GETTEXT_FILES = findtimezone.c initdb.c ../../common/exec.c ../../common/fe_memutils.c ../../common/wait_error.c ../../port/dirmod.c
4+
GETTEXT_FILES = findtimezone.c initdb.c ../../common/exec.c ../../common/fe_memutils.c ../../common/pgfnames.c ../../common/wait_error.c ../../port/dirmod.c
55
GETTEXT_TRIGGERS = simple_prompt

src/common/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ include $(top_builddir)/src/Makefile.global
2323
override CPPFLAGS := -DFRONTEND $(CPPFLAGS)
2424
LIBS += $(PTHREAD_LIBS)
2525

26-
OBJS_COMMON = exec.o relpath.o wait_error.o
26+
OBJS_COMMON = exec.o pgfnames.o relpath.o wait_error.o
2727

2828
OBJS_FRONTEND = $(OBJS_COMMON) fe_memutils.o
2929

src/common/pgfnames.c

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*-------------------------------------------------------------------------
2+
*
3+
* pgfnames.c
4+
* directory handling functions
5+
*
6+
* Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
7+
* Portions Copyright (c) 1994, Regents of the University of California
8+
*
9+
* IDENTIFICATION
10+
* src/common/pgfnames.c
11+
*
12+
*-------------------------------------------------------------------------
13+
*/
14+
15+
#ifndef FRONTEND
16+
#include "postgres.h"
17+
#else
18+
#include "postgres_fe.h"
19+
#endif
20+
21+
#include <dirent.h>
22+
23+
/*
24+
* pgfnames
25+
*
26+
* return a list of the names of objects in the argument directory. Caller
27+
* must call pgfnames_cleanup later to free the memory allocated by this
28+
* function.
29+
*/
30+
char **
31+
pgfnames(const char *path)
32+
{
33+
DIR *dir;
34+
struct dirent *file;
35+
char **filenames;
36+
int numnames = 0;
37+
int fnsize = 200; /* enough for many small dbs */
38+
39+
dir = opendir(path);
40+
if (dir == NULL)
41+
{
42+
#ifndef FRONTEND
43+
elog(WARNING, "could not open directory \"%s\": %m", path);
44+
#else
45+
fprintf(stderr, _("could not open directory \"%s\": %s\n"),
46+
path, strerror(errno));
47+
#endif
48+
return NULL;
49+
}
50+
51+
filenames = (char **) palloc(fnsize * sizeof(char *));
52+
53+
errno = 0;
54+
while ((file = readdir(dir)) != NULL)
55+
{
56+
if (strcmp(file->d_name, ".") != 0 && strcmp(file->d_name, "..") != 0)
57+
{
58+
if (numnames + 1 >= fnsize)
59+
{
60+
fnsize *= 2;
61+
filenames = (char **) repalloc(filenames,
62+
fnsize * sizeof(char *));
63+
}
64+
filenames[numnames++] = pstrdup(file->d_name);
65+
}
66+
errno = 0;
67+
}
68+
#ifdef WIN32
69+
70+
/*
71+
* This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
72+
* released version
73+
*/
74+
if (GetLastError() == ERROR_NO_MORE_FILES)
75+
errno = 0;
76+
#endif
77+
if (errno)
78+
{
79+
#ifndef FRONTEND
80+
elog(WARNING, "could not read directory \"%s\": %m", path);
81+
#else
82+
fprintf(stderr, _("could not read directory \"%s\": %s\n"),
83+
path, strerror(errno));
84+
#endif
85+
}
86+
87+
filenames[numnames] = NULL;
88+
89+
closedir(dir);
90+
91+
return filenames;
92+
}
93+
94+
95+
/*
96+
* pgfnames_cleanup
97+
*
98+
* deallocate memory used for filenames
99+
*/
100+
void
101+
pgfnames_cleanup(char **filenames)
102+
{
103+
char **fn;
104+
105+
for (fn = filenames; *fn; fn++)
106+
pfree(*fn);
107+
108+
pfree(filenames);
109+
}

src/port/dirmod.c

Lines changed: 0 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
#endif
2929

3030
#include <unistd.h>
31-
#include <dirent.h>
3231
#include <sys/stat.h>
3332

3433
#if defined(WIN32) || defined(__CYGWIN__)
@@ -352,95 +351,6 @@ pgwin32_is_junction(char *path)
352351
#endif /* defined(WIN32) && !defined(__CYGWIN__) */
353352

354353

355-
/*
356-
* pgfnames
357-
*
358-
* return a list of the names of objects in the argument directory. Caller
359-
* must call pgfnames_cleanup later to free the memory allocated by this
360-
* function.
361-
*/
362-
char **
363-
pgfnames(const char *path)
364-
{
365-
DIR *dir;
366-
struct dirent *file;
367-
char **filenames;
368-
int numnames = 0;
369-
int fnsize = 200; /* enough for many small dbs */
370-
371-
dir = opendir(path);
372-
if (dir == NULL)
373-
{
374-
#ifndef FRONTEND
375-
elog(WARNING, "could not open directory \"%s\": %m", path);
376-
#else
377-
fprintf(stderr, _("could not open directory \"%s\": %s\n"),
378-
path, strerror(errno));
379-
#endif
380-
return NULL;
381-
}
382-
383-
filenames = (char **) palloc(fnsize * sizeof(char *));
384-
385-
errno = 0;
386-
while ((file = readdir(dir)) != NULL)
387-
{
388-
if (strcmp(file->d_name, ".") != 0 && strcmp(file->d_name, "..") != 0)
389-
{
390-
if (numnames + 1 >= fnsize)
391-
{
392-
fnsize *= 2;
393-
filenames = (char **) repalloc(filenames,
394-
fnsize * sizeof(char *));
395-
}
396-
filenames[numnames++] = pstrdup(file->d_name);
397-
}
398-
errno = 0;
399-
}
400-
#ifdef WIN32
401-
402-
/*
403-
* This fix is in mingw cvs (runtime/mingwex/dirent.c rev 1.4), but not in
404-
* released version
405-
*/
406-
if (GetLastError() == ERROR_NO_MORE_FILES)
407-
errno = 0;
408-
#endif
409-
if (errno)
410-
{
411-
#ifndef FRONTEND
412-
elog(WARNING, "could not read directory \"%s\": %m", path);
413-
#else
414-
fprintf(stderr, _("could not read directory \"%s\": %s\n"),
415-
path, strerror(errno));
416-
#endif
417-
}
418-
419-
filenames[numnames] = NULL;
420-
421-
closedir(dir);
422-
423-
return filenames;
424-
}
425-
426-
427-
/*
428-
* pgfnames_cleanup
429-
*
430-
* deallocate memory used for filenames
431-
*/
432-
void
433-
pgfnames_cleanup(char **filenames)
434-
{
435-
char **fn;
436-
437-
for (fn = filenames; *fn; fn++)
438-
pfree(*fn);
439-
440-
pfree(filenames);
441-
}
442-
443-
444354
/*
445355
* rmtree
446356
*

src/tools/msvc/Mkvcbuild.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ sub mkvcbuild
7474
win32error.c win32setlocale.c);
7575

7676
our @pgcommonallfiles = qw(
77-
exec.c relpath.c wait_error.c);
77+
exec.c pgfnames.c relpath.c wait_error.c);
7878

7979
our @pgcommonfrontendfiles = (@pgcommonallfiles, qw(fe_memutils.c));
8080

0 commit comments

Comments
 (0)