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

Commit 2e6bc4b

Browse files
committed
Move rmtree() from libpgport to libpgcommon
It requires pgfnames() from libpgcommon.
1 parent ba7c597 commit 2e6bc4b

File tree

5 files changed

+134
-113
lines changed

5 files changed

+134
-113
lines changed

src/bin/initdb/nls.mk

+1-1
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/pgfnames.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/rmtree.c ../../common/wait_error.c ../../port/dirmod.c
55
GETTEXT_TRIGGERS = simple_prompt

src/common/Makefile

+1-1
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 pgfnames.o relpath.o wait_error.o
26+
OBJS_COMMON = exec.o pgfnames.o relpath.o rmtree.o wait_error.o
2727

2828
OBJS_FRONTEND = $(OBJS_COMMON) fe_memutils.o
2929

src/common/rmtree.c

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*-------------------------------------------------------------------------
2+
*
3+
* rmtree.c
4+
*
5+
* Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
6+
* Portions Copyright (c) 1994, Regents of the University of California
7+
*
8+
* IDENTIFICATION
9+
* src/common/rmtree.c
10+
*
11+
*-------------------------------------------------------------------------
12+
*/
13+
14+
#ifndef FRONTEND
15+
#include "postgres.h"
16+
#else
17+
#include "postgres_fe.h"
18+
#endif
19+
20+
#include <unistd.h>
21+
#include <sys/stat.h>
22+
23+
24+
/*
25+
* rmtree
26+
*
27+
* Delete a directory tree recursively.
28+
* Assumes path points to a valid directory.
29+
* Deletes everything under path.
30+
* If rmtopdir is true deletes the directory too.
31+
* Returns true if successful, false if there was any problem.
32+
* (The details of the problem are reported already, so caller
33+
* doesn't really have to say anything more, but most do.)
34+
*/
35+
bool
36+
rmtree(const char *path, bool rmtopdir)
37+
{
38+
bool result = true;
39+
char pathbuf[MAXPGPATH];
40+
char **filenames;
41+
char **filename;
42+
struct stat statbuf;
43+
44+
/*
45+
* we copy all the names out of the directory before we start modifying
46+
* it.
47+
*/
48+
filenames = pgfnames(path);
49+
50+
if (filenames == NULL)
51+
return false;
52+
53+
/* now we have the names we can start removing things */
54+
for (filename = filenames; *filename; filename++)
55+
{
56+
snprintf(pathbuf, MAXPGPATH, "%s/%s", path, *filename);
57+
58+
/*
59+
* It's ok if the file is not there anymore; we were just about to
60+
* delete it anyway.
61+
*
62+
* This is not an academic possibility. One scenario where this
63+
* happens is when bgwriter has a pending unlink request for a file in
64+
* a database that's being dropped. In dropdb(), we call
65+
* ForgetDatabaseFsyncRequests() to flush out any such pending unlink
66+
* requests, but because that's asynchronous, it's not guaranteed that
67+
* the bgwriter receives the message in time.
68+
*/
69+
if (lstat(pathbuf, &statbuf) != 0)
70+
{
71+
if (errno != ENOENT)
72+
{
73+
#ifndef FRONTEND
74+
elog(WARNING, "could not stat file or directory \"%s\": %m",
75+
pathbuf);
76+
#else
77+
fprintf(stderr, _("could not stat file or directory \"%s\": %s\n"),
78+
pathbuf, strerror(errno));
79+
#endif
80+
result = false;
81+
}
82+
continue;
83+
}
84+
85+
if (S_ISDIR(statbuf.st_mode))
86+
{
87+
/* call ourselves recursively for a directory */
88+
if (!rmtree(pathbuf, true))
89+
{
90+
/* we already reported the error */
91+
result = false;
92+
}
93+
}
94+
else
95+
{
96+
if (unlink(pathbuf) != 0)
97+
{
98+
if (errno != ENOENT)
99+
{
100+
#ifndef FRONTEND
101+
elog(WARNING, "could not remove file or directory \"%s\": %m",
102+
pathbuf);
103+
#else
104+
fprintf(stderr, _("could not remove file or directory \"%s\": %s\n"),
105+
pathbuf, strerror(errno));
106+
#endif
107+
result = false;
108+
}
109+
}
110+
}
111+
}
112+
113+
if (rmtopdir)
114+
{
115+
if (rmdir(path) != 0)
116+
{
117+
#ifndef FRONTEND
118+
elog(WARNING, "could not remove file or directory \"%s\": %m",
119+
path);
120+
#else
121+
fprintf(stderr, _("could not remove file or directory \"%s\": %s\n"),
122+
path, strerror(errno));
123+
#endif
124+
result = false;
125+
}
126+
}
127+
128+
pgfnames_cleanup(filenames);
129+
130+
return result;
131+
}

src/port/dirmod.c

-110
Original file line numberDiff line numberDiff line change
@@ -351,116 +351,6 @@ pgwin32_is_junction(char *path)
351351
#endif /* defined(WIN32) && !defined(__CYGWIN__) */
352352

353353

354-
/*
355-
* rmtree
356-
*
357-
* Delete a directory tree recursively.
358-
* Assumes path points to a valid directory.
359-
* Deletes everything under path.
360-
* If rmtopdir is true deletes the directory too.
361-
* Returns true if successful, false if there was any problem.
362-
* (The details of the problem are reported already, so caller
363-
* doesn't really have to say anything more, but most do.)
364-
*/
365-
bool
366-
rmtree(const char *path, bool rmtopdir)
367-
{
368-
bool result = true;
369-
char pathbuf[MAXPGPATH];
370-
char **filenames;
371-
char **filename;
372-
struct stat statbuf;
373-
374-
/*
375-
* we copy all the names out of the directory before we start modifying
376-
* it.
377-
*/
378-
filenames = pgfnames(path);
379-
380-
if (filenames == NULL)
381-
return false;
382-
383-
/* now we have the names we can start removing things */
384-
for (filename = filenames; *filename; filename++)
385-
{
386-
snprintf(pathbuf, MAXPGPATH, "%s/%s", path, *filename);
387-
388-
/*
389-
* It's ok if the file is not there anymore; we were just about to
390-
* delete it anyway.
391-
*
392-
* This is not an academic possibility. One scenario where this
393-
* happens is when bgwriter has a pending unlink request for a file in
394-
* a database that's being dropped. In dropdb(), we call
395-
* ForgetDatabaseFsyncRequests() to flush out any such pending unlink
396-
* requests, but because that's asynchronous, it's not guaranteed that
397-
* the bgwriter receives the message in time.
398-
*/
399-
if (lstat(pathbuf, &statbuf) != 0)
400-
{
401-
if (errno != ENOENT)
402-
{
403-
#ifndef FRONTEND
404-
elog(WARNING, "could not stat file or directory \"%s\": %m",
405-
pathbuf);
406-
#else
407-
fprintf(stderr, _("could not stat file or directory \"%s\": %s\n"),
408-
pathbuf, strerror(errno));
409-
#endif
410-
result = false;
411-
}
412-
continue;
413-
}
414-
415-
if (S_ISDIR(statbuf.st_mode))
416-
{
417-
/* call ourselves recursively for a directory */
418-
if (!rmtree(pathbuf, true))
419-
{
420-
/* we already reported the error */
421-
result = false;
422-
}
423-
}
424-
else
425-
{
426-
if (unlink(pathbuf) != 0)
427-
{
428-
if (errno != ENOENT)
429-
{
430-
#ifndef FRONTEND
431-
elog(WARNING, "could not remove file or directory \"%s\": %m",
432-
pathbuf);
433-
#else
434-
fprintf(stderr, _("could not remove file or directory \"%s\": %s\n"),
435-
pathbuf, strerror(errno));
436-
#endif
437-
result = false;
438-
}
439-
}
440-
}
441-
}
442-
443-
if (rmtopdir)
444-
{
445-
if (rmdir(path) != 0)
446-
{
447-
#ifndef FRONTEND
448-
elog(WARNING, "could not remove file or directory \"%s\": %m",
449-
path);
450-
#else
451-
fprintf(stderr, _("could not remove file or directory \"%s\": %s\n"),
452-
path, strerror(errno));
453-
#endif
454-
result = false;
455-
}
456-
}
457-
458-
pgfnames_cleanup(filenames);
459-
460-
return result;
461-
}
462-
463-
464354
#if defined(WIN32) && !defined(__CYGWIN__)
465355

466356
#undef stat

src/tools/msvc/Mkvcbuild.pm

+1-1
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 pgfnames.c relpath.c wait_error.c);
77+
exec.c pgfnames.c relpath.c rmtree.c wait_error.c);
7878

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

0 commit comments

Comments
 (0)