11/*
2- * $PostgreSQL: pgsql/contrib/pg_archivecleanup/pg_archivecleanup.c,v 1.2 2010/06/17 17:31:27 tgl Exp $
2+ * $PostgreSQL: pgsql/contrib/pg_archivecleanup/pg_archivecleanup.c,v 1.3 2010/07/06 19:18:55 momjian Exp $
33 *
44 * pg_archivecleanup.c
55 *
@@ -40,8 +40,9 @@ booldebug = false;/* are we debugging? */
4040char * archiveLocation ;/* where to find the archive? */
4141char * restartWALFileName ;/* the file from which we can restart restore */
4242char WALFilePath [MAXPGPATH ];/* the file path including archive */
43- char exclusiveCleanupFileName [MAXPGPATH ];/* the oldest file we want to
44- * remain in archive */
43+ char exclusiveCleanupFileName [MAXPGPATH ];/* the oldest file we
44+ * want to remain in
45+ * archive */
4546
4647
4748/* =====================================================================
@@ -68,14 +69,14 @@ charexclusiveCleanupFileName[MAXPGPATH];/* the oldest file we want to
6869/*
6970 *Initialize allows customized commands into the archive cleanup program.
7071 *
71- * You may wish to add code to check for tape libraries, etc..
72+ * You may wish to add code to check for tape libraries, etc..
7273 */
7374static void
7475Initialize (void )
7576{
7677/*
77- * This code assumes that archiveLocation is a directory, so we use
78- *stat to test if it's accessible.
78+ * This code assumes that archiveLocation is a directory, so we use stat
79+ * to test if it's accessible.
7980 */
8081struct stat stat_buf ;
8182
@@ -100,22 +101,21 @@ CleanupPriorWALFiles(void)
100101while ((xlde = readdir (xldir ))!= NULL )
101102{
102103/*
103- * We ignore the timeline part of the XLOG segment identifiers
104- *in deciding whether a segment is still needed. This
105- *ensures that we won't prematurely remove a segment from a
106- *parent timeline. We could probably be a little more
107- *proactive about removing segments of non-parent timelines,
108- *but that would be a whole lot more complicated.
104+ * We ignore the timeline part of the XLOG segment identifiers in
105+ * deciding whether a segment is still needed.This ensures that
106+ * we won't prematurely remove a segment from a parent timeline.
107+ * We could probably be a little more proactive about removing
108+ * segments of non-parent timelines, but that would be a whole lot
109+ * more complicated.
109110 *
110- * We use the alphanumeric sorting property of the filenames
111- * to decide which ones are earlier than the
112- * exclusiveCleanupFileName file. Note that this means files
113- * are not removed in the order they were originally written,
114- * in case this worries you.
111+ * We use the alphanumeric sorting property of the filenames to
112+ * decide which ones are earlier than the exclusiveCleanupFileName
113+ * file. Note that this means files are not removed in the order
114+ * they were originally written, in case this worries you.
115115 */
116116if (strlen (xlde -> d_name )== XLOG_DATA_FNAME_LEN &&
117- strspn (xlde -> d_name ,"0123456789ABCDEF" )== XLOG_DATA_FNAME_LEN &&
118- strcmp (xlde -> d_name + 8 ,exclusiveCleanupFileName + 8 )< 0 )
117+ strspn (xlde -> d_name ,"0123456789ABCDEF" )== XLOG_DATA_FNAME_LEN &&
118+ strcmp (xlde -> d_name + 8 ,exclusiveCleanupFileName + 8 )< 0 )
119119{
120120#ifdef WIN32
121121snprintf (WALFilePath ,MAXPGPATH ,"%s\\%s" ,archiveLocation ,xlde -> d_name );
@@ -152,13 +152,13 @@ CleanupPriorWALFiles(void)
152152static void
153153SetWALFileNameForCleanup (void )
154154{
155- bool fnameOK = false;
155+ bool fnameOK = false;
156156
157157/*
158- * If restartWALFileName is a WAL file name then just use it directly.
159- *If restartWALFileName is a .backup filename, make sure we use
160- *the prefix of the filename, otherwise we will remove wrong files
161- *since 000000010000000000000010.00000020.backup is after
158+ * If restartWALFileName is a WAL file name then just use it directly. If
159+ * restartWALFileName is a .backup filename, make sure we use the prefix
160+ * of the filename, otherwise we will remove wrong files since
161+ * 000000010000000000000010.00000020.backup is after
162162 * 000000010000000000000010.
163163 */
164164if (strlen (restartWALFileName )== XLOG_DATA_FNAME_LEN &&
@@ -169,17 +169,20 @@ SetWALFileNameForCleanup(void)
169169}
170170else if (strlen (restartWALFileName )== XLOG_BACKUP_FNAME_LEN )
171171{
172- int args ;
172+ int args ;
173173uint32 tli = 1 ,
174174log = 0 ,
175175seg = 0 ,
176176offset = 0 ;
177+
177178args = sscanf (restartWALFileName ,"%08X%08X%08X.%08X.backup" ,& tli ,& log ,& seg ,& offset );
178179if (args == 4 )
179180{
180181fnameOK = true;
182+
181183/*
182- * Use just the prefix of the filename, ignore everything after first period
184+ * Use just the prefix of the filename, ignore everything after
185+ * first period
183186 */
184187XLogFileName (exclusiveCleanupFileName ,tli ,log ,seg );
185188}
@@ -205,12 +208,12 @@ usage(void)
205208printf ("Usage:\n" );
206209printf (" %s [OPTION]... ARCHIVELOCATION OLDESTKEPTWALFILE\n" ,progname );
207210printf ("\n"
208- "for use as an archive_cleanup_command in the recovery.conf when standby_mode = on:\n"
211+ "for use as an archive_cleanup_command in the recovery.conf when standby_mode = on:\n"
209212" archive_cleanup_command = 'pg_archivecleanup [OPTION]... ARCHIVELOCATION %%r'\n"
210213"e.g.\n"
211214" archive_cleanup_command = 'pg_archivecleanup /mnt/server/archiverdir %%r'\n" );
212215printf ("\n"
213- "or for use as a standalone archive cleaner:\n"
216+ "or for use as a standalone archive cleaner:\n"
214217"e.g.\n"
215218" pg_archivecleanup /mnt/server/archiverdir 000000010000000000000010.00000020.backup\n" );
216219printf ("\nOptions:\n" );
@@ -258,9 +261,10 @@ main(int argc, char **argv)
258261
259262/*
260263 * We will go to the archiveLocation to check restartWALFileName.
261- * restartWALFileName may not exist anymore, which would not be an error, so
262- * we separate the archiveLocation and restartWALFileName so we can check
263- * separately whether archiveLocation exists, if not that is an error
264+ * restartWALFileName may not exist anymore, which would not be an error,
265+ * so we separate the archiveLocation and restartWALFileName so we can
266+ * check separately whether archiveLocation exists, if not that is an
267+ * error
264268 */
265269if (optind < argc )
266270{