44 * execution functions
55 *
66 * Copyright (c) 2010, PostgreSQL Global Development Group
7- * $PostgreSQL: pgsql/contrib/pg_upgrade/exec.c,v 1.8 2010/07/06 19:18 :55 momjian Exp $
7+ * $PostgreSQL: pgsql/contrib/pg_upgrade/exec.c,v 1.9 2010/07/13 18:09 :55 momjian Exp $
88 */
99
1010#include "pg_upgrade.h"
1313#include <grp.h>
1414
1515
16- static void checkBinDir (migratorContext * ctx , ClusterInfo * cluster );
16+ static void check_data_dir (migratorContext * ctx , const char * pg_data );
17+ static void check_bin_dir (migratorContext * ctx , ClusterInfo * cluster );
1718static int check_exec (migratorContext * ctx , const char * dir , const char * cmdName );
1819static const char * validate_exec (const char * path );
19- static int check_data_dir (migratorContext * ctx , const char * pg_data );
2020
2121
2222/*
@@ -55,6 +55,34 @@ exec_prog(migratorContext *ctx, bool throw_error, const char *fmt,...)
5555}
5656
5757
58+ /*
59+ * is_server_running()
60+ *
61+ * checks whether postmaster on the given data directory is running or not.
62+ * The check is performed by looking for the existence of postmaster.pid file.
63+ */
64+ bool
65+ is_server_running (migratorContext * ctx , const char * datadir )
66+ {
67+ char path [MAXPGPATH ];
68+ int fd ;
69+
70+ snprintf (path , sizeof (path ), "%s/postmaster.pid" , datadir );
71+
72+ if ((fd = open (path , O_RDONLY , 0 )) < 0 )
73+ {
74+ if (errno != ENOENT )
75+ pg_log (ctx , PG_FATAL , "\ncould not open file \"%s\" for reading\n" ,
76+ path );
77+
78+ return false;
79+ }
80+
81+ close (fd );
82+ return true;
83+ }
84+
85+
5886/*
5987 * verify_directories()
6088 *
6795verify_directories (migratorContext * ctx )
6896{
6997 prep_status (ctx , "Checking old data directory (%s)" , ctx -> old .pgdata );
70- if (check_data_dir (ctx , ctx -> old .pgdata ) != 0 )
71- pg_log (ctx , PG_FATAL , "Failed\n" );
72- checkBinDir (ctx , & ctx -> old );
98+ check_data_dir (ctx , ctx -> old .pgdata );
99+ check_ok (ctx );
100+
101+ prep_status (ctx , "Checking old bin directory (%s)" , ctx -> old .bindir );
102+ check_bin_dir (ctx , & ctx -> old );
73103 check_ok (ctx );
74104
75105 prep_status (ctx , "Checking new data directory (%s)" , ctx -> new .pgdata );
76- if (check_data_dir (ctx , ctx -> new .pgdata ) != 0 )
77- pg_log (ctx , PG_FATAL , "Failed\n" );
78- checkBinDir (ctx , & ctx -> new );
106+ check_data_dir (ctx , ctx -> new .pgdata );
107+ check_ok (ctx );
108+
109+ prep_status (ctx , "Checking new bin directory (%s)" , ctx -> new .bindir );
110+ check_bin_dir (ctx , & ctx -> new );
79111 check_ok (ctx );
80112}
81113
82114
83115/*
84- * checkBinDir()
116+ * check_data_dir()
117+ *
118+ * This function validates the given cluster directory - we search for a
119+ * small set of subdirectories that we expect to find in a valid $PGDATA
120+ * directory. If any of the subdirectories are missing (or secured against
121+ * us) we display an error message and exit()
122+ *
123+ */
124+ static void
125+ check_data_dir (migratorContext * ctx , const char * pg_data )
126+ {
127+ char subDirName [MAXPGPATH ];
128+ int subdirnum ;
129+ const char * requiredSubdirs [] = {"base" , "global" , "pg_clog" ,
130+ "pg_multixact" , "pg_subtrans" , "pg_tblspc" , "pg_twophase" ,
131+ "pg_xlog" };
132+
133+ for (subdirnum = 0 ;
134+ subdirnum < sizeof (requiredSubdirs ) / sizeof (requiredSubdirs [0 ]);
135+ ++ subdirnum )
136+ {
137+ struct stat statBuf ;
138+
139+ snprintf (subDirName , sizeof (subDirName ), "%s/%s" , pg_data ,
140+ requiredSubdirs [subdirnum ]);
141+
142+ if (stat (subDirName , & statBuf ) != 0 )
143+ report_status (ctx , PG_FATAL , "check for %s failed: %s" ,
144+ requiredSubdirs [subdirnum ], getErrorText (errno ));
145+ else if (!S_ISDIR (statBuf .st_mode ))
146+ report_status (ctx , PG_FATAL , "%s is not a directory" ,
147+ requiredSubdirs [subdirnum ]);
148+ }
149+ }
150+
151+
152+ /*
153+ * check_bin_dir()
85154 *
86155 * This function searches for the executables that we expect to find
87156 * in the binaries directory. If we find that a required executable
88157 * is missing (or secured against us), we display an error message and
89158 * exit().
90159 */
91160static void
92- checkBinDir (migratorContext * ctx , ClusterInfo * cluster )
161+ check_bin_dir (migratorContext * ctx , ClusterInfo * cluster )
93162{
94163 check_exec (ctx , cluster -> bindir , "postgres" );
95164 check_exec (ctx , cluster -> bindir , "psql" );
@@ -98,34 +167,6 @@ checkBinDir(migratorContext *ctx, ClusterInfo *cluster)
98167}
99168
100169
101- /*
102- * is_server_running()
103- *
104- * checks whether postmaster on the given data directory is running or not.
105- * The check is performed by looking for the existence of postmaster.pid file.
106- */
107- bool
108- is_server_running (migratorContext * ctx , const char * datadir )
109- {
110- char path [MAXPGPATH ];
111- int fd ;
112-
113- snprintf (path , sizeof (path ), "%s/postmaster.pid" , datadir );
114-
115- if ((fd = open (path , O_RDONLY , 0 )) < 0 )
116- {
117- if (errno != ENOENT )
118- pg_log (ctx , PG_FATAL , "\ncould not open file \"%s\" for reading\n" ,
119- path );
120-
121- return false;
122- }
123-
124- close (fd );
125- return true;
126- }
127-
128-
129170/*
130171 * check_exec()
131172 *
@@ -264,50 +305,3 @@ validate_exec(const char *path)
264305 return NULL ;
265306#endif
266307}
267-
268-
269- /*
270- * check_data_dir()
271- *
272- * This function validates the given cluster directory - we search for a
273- * small set of subdirectories that we expect to find in a valid $PGDATA
274- * directory. If any of the subdirectories are missing (or secured against
275- * us) we display an error message and exit()
276- *
277- */
278- static int
279- check_data_dir (migratorContext * ctx , const char * pg_data )
280- {
281- char subDirName [MAXPGPATH ];
282- const char * requiredSubdirs [] = {"base" , "global" , "pg_clog" ,
283- "pg_multixact" , "pg_subtrans" ,
284- "pg_tblspc" , "pg_twophase" , "pg_xlog" };
285- bool fail = false;
286- int subdirnum ;
287-
288- for (subdirnum = 0 ; subdirnum < sizeof (requiredSubdirs ) / sizeof (requiredSubdirs [0 ]); ++ subdirnum )
289- {
290- struct stat statBuf ;
291-
292- snprintf (subDirName , sizeof (subDirName ), "%s/%s" , pg_data ,
293- requiredSubdirs [subdirnum ]);
294-
295- if ((stat (subDirName , & statBuf )) != 0 )
296- {
297- report_status (ctx , PG_WARNING , "check for %s warning: %s" ,
298- requiredSubdirs [subdirnum ], getErrorText (errno ));
299- fail = true;
300- }
301- else
302- {
303- if (!S_ISDIR (statBuf .st_mode ))
304- {
305- report_status (ctx , PG_WARNING , "%s is not a directory" ,
306- requiredSubdirs [subdirnum ]);
307- fail = true;
308- }
309- }
310- }
311-
312- return (fail ) ? -1 : 0 ;
313- }
0 commit comments