summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Lane2006-10-20 00:59:03 +0000
committerTom Lane2006-10-20 00:59:03 +0000
commit9bf1c1ea219a4d75f167edd7c7e6a172b211a5a1 (patch)
tree61ac32b6a8d5533f3f68883b3e1f66d07da50b44
parent07408cdbaa699a5e5447f5a36403d5b2ae223a38 (diff)
Marginal code cleanups in pg_logdir_ls: use ReadDir not readdir,
and avoid scribbling on its result (might be safe but why risk it)
-rw-r--r--contrib/adminpack/adminpack.c36
1 files changed, 17 insertions, 19 deletions
diff --git a/contrib/adminpack/adminpack.c b/contrib/adminpack/adminpack.c
index 0a202b456e..99bf1a3eee 100644
--- a/contrib/adminpack/adminpack.c
+++ b/contrib/adminpack/adminpack.c
@@ -17,11 +17,10 @@
#include <sys/file.h>
#include <sys/stat.h>
#include <unistd.h>
-#include <dirent.h>
-#include "miscadmin.h"
#include "catalog/pg_type.h"
#include "funcapi.h"
+#include "miscadmin.h"
#include "postmaster/syslogger.h"
#include "storage/fd.h"
#include "utils/datetime.h"
@@ -303,7 +302,7 @@ pg_logdir_ls(PG_FUNCTION_ARGS)
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
(errmsg("only superuser can list the log directory"))));
- if (memcmp(Log_filename, "postgresql-%Y-%m-%d_%H%M%S.log", 30) != 0)
+ if (strcmp(Log_filename, "postgresql-%Y-%m-%d_%H%M%S.log") != 0)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
(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)
fctx = palloc(sizeof(directory_fctx));
if (is_absolute_path(Log_directory))
- fctx->location = Log_directory;
+ fctx->location = pstrdup(Log_directory);
else
{
fctx->location = palloc(strlen(DataDir) + strlen(Log_directory) + 2);
@@ -346,14 +345,11 @@ pg_logdir_ls(PG_FUNCTION_ARGS)
funcctx = SRF_PERCALL_SETUP();
fctx = (directory_fctx *) funcctx->user_fctx;
- if (!fctx->dirdesc) /* not a readable directory */
- SRF_RETURN_DONE(funcctx);
-
- while ((de = readdir(fctx->dirdesc)) != NULL)
+ while ((de = ReadDir(fctx->dirdesc, fctx->location)) != NULL)
{
char *values[2];
HeapTuple tuple;
-
+ char timestampbuf[32];
char *field[MAXDATEFIELDS];
char lowstr[MAXDATELEN + 1];
int dtype;
@@ -367,25 +363,27 @@ pg_logdir_ls(PG_FUNCTION_ARGS)
* Default format: postgresql-YYYY-MM-DD_HHMMSS.log
*/
if (strlen(de->d_name) != 32
- || memcmp(de->d_name, "postgresql-", 11)
+ || strncmp(de->d_name, "postgresql-", 11) != 0
|| de->d_name[21] != '_'
- || strcmp(de->d_name + 28, ".log"))
+ || strcmp(de->d_name + 28, ".log") != 0)
continue;
- values[1] = palloc(strlen(fctx->location) + strlen(de->d_name) + 2);
- sprintf(values[1], "%s/%s", fctx->location, de->d_name);
-
- values[0] = de->d_name + 11; /* timestamp */
- values[0][17] = 0;
+ /* extract timestamp portion of filename */
+ strcpy(timestampbuf, de->d_name + 11);
+ timestampbuf[17] = '\0';
- /* parse and decode expected timestamp */
- if (ParseDateTime(values[0], lowstr, MAXDATELEN, field, ftype, MAXDATEFIELDS, &nf))
+ /* parse and decode expected timestamp to verify it's OK format */
+ if (ParseDateTime(timestampbuf, lowstr, MAXDATELEN, field, ftype, MAXDATEFIELDS, &nf))
continue;
if (DecodeDateTime(field, ftype, nf, &dtype, &date, &fsec, &tz))
continue;
- /* Seems the format fits the expected format; feed it into the tuple */
+ /* Seems the timestamp is OK; prepare and return tuple */
+
+ values[0] = timestampbuf;
+ values[1] = palloc(strlen(fctx->location) + strlen(de->d_name) + 2);
+ sprintf(values[1], "%s/%s", fctx->location, de->d_name);
tuple = BuildTupleFromCStrings(funcctx->attinmeta, values);