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

Commit 7feedda

Browse files
committed
Marginal code cleanups in pg_logdir_ls: use ReadDir not readdir,
and avoid scribbling on its result (might be safe but why risk it)
1 parent 33af087 commit 7feedda

File tree

1 file changed

+18
-20
lines changed

1 file changed

+18
-20
lines changed

contrib/adminpack/adminpack.c

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Author: Andreas Pflug <pgadmin@pse-consulting.de>
99
*
1010
* 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 $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -17,11 +17,10 @@
1717
#include <sys/file.h>
1818
#include <sys/stat.h>
1919
#include <unistd.h>
20-
#include <dirent.h>
2120

22-
#include "miscadmin.h"
2321
#include "catalog/pg_type.h"
2422
#include "funcapi.h"
23+
#include "miscadmin.h"
2524
#include "postmaster/syslogger.h"
2625
#include "storage/fd.h"
2726
#include "utils/datetime.h"
@@ -303,7 +302,7 @@ pg_logdir_ls(PG_FUNCTION_ARGS)
303302
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
304303
(errmsg("only superuser can list the log directory"))));
305304

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)
307306
ereport(ERROR,
308307
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
309308
(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)
318317

319318
fctx = palloc(sizeof(directory_fctx));
320319
if (is_absolute_path(Log_directory))
321-
fctx->location = Log_directory;
320+
fctx->location = pstrdup(Log_directory);
322321
else
323322
{
324323
fctx->location = palloc(strlen(DataDir) + strlen(Log_directory) + 2);
@@ -346,14 +345,11 @@ pg_logdir_ls(PG_FUNCTION_ARGS)
346345
funcctx = SRF_PERCALL_SETUP();
347346
fctx = (directory_fctx *) funcctx->user_fctx;
348347

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)
353349
{
354350
char *values[2];
355351
HeapTuple tuple;
356-
352+
char timestampbuf[32];
357353
char *field[MAXDATEFIELDS];
358354
char lowstr[MAXDATELEN + 1];
359355
int dtype;
@@ -367,25 +363,27 @@ pg_logdir_ls(PG_FUNCTION_ARGS)
367363
* Default format: postgresql-YYYY-MM-DD_HHMMSS.log
368364
*/
369365
if (strlen(de->d_name) != 32
370-
|| memcmp(de->d_name, "postgresql-", 11)
366+
|| strncmp(de->d_name, "postgresql-", 11) != 0
371367
|| de->d_name[21] != '_'
372-
|| strcmp(de->d_name + 28, ".log"))
368+
|| strcmp(de->d_name + 28, ".log") != 0)
373369
continue;
374370

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';
380374

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))
383377
continue;
384378

385379
if (DecodeDateTime(field, ftype, nf, &dtype, &date, &fsec, &tz))
386380
continue;
387381

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);
389387

390388
tuple = BuildTupleFromCStrings(funcctx->attinmeta, values);
391389

0 commit comments

Comments
 (0)