8
8
* Author: Andreas Pflug <pgadmin@pse-consulting.de>
9
9
*
10
10
* IDENTIFICATION
11
- * $PostgreSQL: pgsql/contrib/adminpack/adminpack.c,v 1.6 2006/10/19 18:32:45 tgl Exp $
11
+ * $PostgreSQL: pgsql/contrib/adminpack/adminpack.c,v 1.7 2006/10/20 00:59:03 tgl Exp $
12
12
*
13
13
*-------------------------------------------------------------------------
14
14
*/
17
17
#include <sys/file.h>
18
18
#include <sys/stat.h>
19
19
#include <unistd.h>
20
- #include <dirent.h>
21
20
22
- #include "miscadmin.h"
23
21
#include "catalog/pg_type.h"
24
22
#include "funcapi.h"
23
+ #include "miscadmin.h"
25
24
#include "postmaster/syslogger.h"
26
25
#include "storage/fd.h"
27
26
#include "utils/datetime.h"
@@ -303,7 +302,7 @@ pg_logdir_ls(PG_FUNCTION_ARGS)
303
302
(errcode (ERRCODE_INSUFFICIENT_PRIVILEGE ),
304
303
(errmsg ("only superuser can list the log directory" ))));
305
304
306
- if (memcmp (Log_filename , "postgresql-%Y-%m-%d_%H%M%S.log" , 30 ) != 0 )
305
+ if (strcmp (Log_filename , "postgresql-%Y-%m-%d_%H%M%S.log" ) != 0 )
307
306
ereport (ERROR ,
308
307
(errcode (ERRCODE_INVALID_PARAMETER_VALUE ),
309
308
(errmsg ("the log_filename parameter must equal 'postgresql-%%Y-%%m-%%d_%%H%%M%%S.log'" ))));
@@ -318,7 +317,7 @@ pg_logdir_ls(PG_FUNCTION_ARGS)
318
317
319
318
fctx = palloc (sizeof (directory_fctx ));
320
319
if (is_absolute_path (Log_directory ))
321
- fctx -> location = Log_directory ;
320
+ fctx -> location = pstrdup ( Log_directory ) ;
322
321
else
323
322
{
324
323
fctx -> location = palloc (strlen (DataDir ) + strlen (Log_directory ) + 2 );
@@ -346,14 +345,11 @@ pg_logdir_ls(PG_FUNCTION_ARGS)
346
345
funcctx = SRF_PERCALL_SETUP ();
347
346
fctx = (directory_fctx * ) funcctx -> user_fctx ;
348
347
349
- if (!fctx -> dirdesc ) /* not a readable directory */
350
- SRF_RETURN_DONE (funcctx );
351
-
352
- while ((de = readdir (fctx -> dirdesc )) != NULL )
348
+ while ((de = ReadDir (fctx -> dirdesc , fctx -> location )) != NULL )
353
349
{
354
350
char * values [2 ];
355
351
HeapTuple tuple ;
356
-
352
+ char timestampbuf [ 32 ];
357
353
char * field [MAXDATEFIELDS ];
358
354
char lowstr [MAXDATELEN + 1 ];
359
355
int dtype ;
@@ -367,25 +363,27 @@ pg_logdir_ls(PG_FUNCTION_ARGS)
367
363
* Default format: postgresql-YYYY-MM-DD_HHMMSS.log
368
364
*/
369
365
if (strlen (de -> d_name ) != 32
370
- || memcmp (de -> d_name , "postgresql-" , 11 )
366
+ || strncmp (de -> d_name , "postgresql-" , 11 ) != 0
371
367
|| de -> d_name [21 ] != '_'
372
- || strcmp (de -> d_name + 28 , ".log" ))
368
+ || strcmp (de -> d_name + 28 , ".log" ) != 0 )
373
369
continue ;
374
370
375
- values [1 ] = palloc (strlen (fctx -> location ) + strlen (de -> d_name ) + 2 );
376
- sprintf (values [1 ], "%s/%s" , fctx -> location , de -> d_name );
377
-
378
- values [0 ] = de -> d_name + 11 ; /* timestamp */
379
- values [0 ][17 ] = 0 ;
371
+ /* extract timestamp portion of filename */
372
+ strcpy (timestampbuf , de -> d_name + 11 );
373
+ timestampbuf [17 ] = '\0' ;
380
374
381
- /* parse and decode expected timestamp */
382
- if (ParseDateTime (values [ 0 ] , lowstr , MAXDATELEN , field , ftype , MAXDATEFIELDS , & nf ))
375
+ /* parse and decode expected timestamp to verify it's OK format */
376
+ if (ParseDateTime (timestampbuf , lowstr , MAXDATELEN , field , ftype , MAXDATEFIELDS , & nf ))
383
377
continue ;
384
378
385
379
if (DecodeDateTime (field , ftype , nf , & dtype , & date , & fsec , & tz ))
386
380
continue ;
387
381
388
- /* Seems the format fits the expected format; feed it into the tuple */
382
+ /* Seems the timestamp is OK; prepare and return tuple */
383
+
384
+ values [0 ] = timestampbuf ;
385
+ values [1 ] = palloc (strlen (fctx -> location ) + strlen (de -> d_name ) + 2 );
386
+ sprintf (values [1 ], "%s/%s" , fctx -> location , de -> d_name );
389
387
390
388
tuple = BuildTupleFromCStrings (funcctx -> attinmeta , values );
391
389
0 commit comments