--- /dev/null
+/*-------------------------------------------------------------------------\r
+ *\r
+ * admin81.c\r
+ *\r
+ *\r
+ * Copyright (c) 2002 - 2006, PostgreSQL Global Development Group\r
+ * \r
+ * Author: Andreas Pflug <pgadmin@pse-consulting.de>\r
+ *\r
+ * IDENTIFICATION\r
+ * $PostgreSQL: pgsql/contrib/adminpack/adminpack.c,v 1.1 2006/05/30 12:07:31 momjian Exp $\r
+ *\r
+ *-------------------------------------------------------------------------\r
+ */\r
+#include "postgres.h"\r
+\r
+#include <sys/file.h>\r
+#include <sys/stat.h>\r
+#include <unistd.h>\r
+#include <dirent.h>\r
+\r
+#include "miscadmin.h"\r
+#include "storage/fd.h"\r
+#include "catalog/pg_type.h"\r
+#include "funcapi.h"\r
+#include "utils/datetime.h"\r
+\r
+\r
+#ifdef WIN32\r
+\r
+#ifdef rename\r
+#undef rename\r
+#endif\r
+\r
+#ifdef unlink\r
+#undef unlink\r
+#endif\r
+\r
+#endif\r
+\r
+extern DLLIMPORT char *DataDir;\r
+extern DLLIMPORT char *Log_directory;\r
+extern DLLIMPORT char *Log_filename;\r
+\r
+Datum pg_file_write(PG_FUNCTION_ARGS);\r
+Datum pg_file_rename(PG_FUNCTION_ARGS);\r
+Datum pg_file_unlink(PG_FUNCTION_ARGS);\r
+Datum pg_logdir_ls(PG_FUNCTION_ARGS);\r
+\r
+PG_FUNCTION_INFO_V1(pg_file_write);\r
+PG_FUNCTION_INFO_V1(pg_file_rename);\r
+PG_FUNCTION_INFO_V1(pg_file_unlink);\r
+PG_FUNCTION_INFO_V1(pg_logdir_ls);\r
+\r
+typedef struct \r
+{\r
+ char *location;\r
+ DIR *dirdesc;\r
+} directory_fctx;\r
+\r
+/*-----------------------\r
+ * some helper functions\r
+ */\r
+\r
+/*\r
+ * Return an absolute path. Argument may be absolute or \r
+ * relative to the DataDir.\r
+ */\r
+static char *absClusterPath(text *arg, bool logAllowed)\r
+{\r
+ char *filename;\r
+ int len=VARSIZE(arg) - VARHDRSZ;\r
+ int dlen = strlen(DataDir);\r
+\r
+ filename = palloc(len+1);\r
+ memcpy(filename, VARDATA(arg), len);\r
+ filename[len] = 0;\r
+\r
+ if (strstr(filename, "..") != NULL)\r
+ ereport(ERROR,\r
+ (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),\r
+ (errmsg("No .. allowed in filenames"))));\r
+ \r
+ if (is_absolute_path(filename))\r
+ {\r
+ if (logAllowed && !strncmp(filename, Log_directory, strlen(Log_directory)))\r
+ return filename;\r
+ if (strncmp(filename, DataDir, dlen))\r
+ ereport(ERROR,\r
+ (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),\r
+ (errmsg("Absolute path not allowed"))));\r
+\r
+ return filename;\r
+ }\r
+ else\r
+ {\r
+ char *absname = palloc(dlen+len+2);\r
+ sprintf(absname, "%s/%s", DataDir, filename);\r
+ pfree(filename);\r
+ return absname;\r
+ }\r
+}\r
+\r
+\r
+/*\r
+ * check for superuser, bark if not.\r
+ */\r
+static void\r
+requireSuperuser(void)\r
+{\r
+ if (!superuser())\r
+ ereport(ERROR,\r
+ (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),\r
+ (errmsg("only superuser may access generic file functions"))));\r
+}\r
+\r
+\r
+\r
+/* ------------------------------------\r
+ * generic file handling functions\r
+ */\r
+\r
+Datum pg_file_write(PG_FUNCTION_ARGS)\r
+{\r
+ FILE *f;\r
+ char *filename;\r
+ text *data;\r
+ int64 count = 0;\r
+\r
+ requireSuperuser();\r
+\r
+ filename = absClusterPath(PG_GETARG_TEXT_P(0), false);\r
+ data = PG_GETARG_TEXT_P(1);\r
+\r
+ if (PG_ARGISNULL(2) || !PG_GETARG_BOOL(2))\r
+ {\r
+ struct stat fst;\r
+ if (stat(filename, &fst) >= 0)\r
+ ereport(ERROR,\r
+ (ERRCODE_DUPLICATE_FILE,\r
+ errmsg("file %s exists", filename)));\r
+\r
+ f = fopen(filename, "wb");\r
+ }\r
+ else\r
+ f = fopen(filename, "ab");\r
+\r
+ if (!f)\r
+ {\r
+ ereport(ERROR,\r
+ (errcode_for_file_access(),\r
+ errmsg("could open file %s for writing: %m", filename)));\r
+ }\r
+\r
+ if (VARSIZE(data) != 0)\r
+ {\r
+ count = fwrite(VARDATA(data), 1, VARSIZE(data) - VARHDRSZ, f);\r
+\r
+ if (count != VARSIZE(data) - VARHDRSZ)\r
+ ereport(ERROR,\r
+ (errcode_for_file_access(),\r
+ errmsg("error writing file %s: %m", filename)));\r
+ }\r
+ fclose(f);\r
+\r
+ PG_RETURN_INT64(count);\r
+}\r
+\r
+\r
+Datum pg_file_rename(PG_FUNCTION_ARGS)\r
+{\r
+ char *fn1, *fn2, *fn3;\r
+ int rc;\r
+\r
+ requireSuperuser();\r
+\r
+ if (PG_ARGISNULL(0) || PG_ARGISNULL(1))\r
+ PG_RETURN_NULL();\r
+\r
+ fn1=absClusterPath(PG_GETARG_TEXT_P(0), false);\r
+ fn2=absClusterPath(PG_GETARG_TEXT_P(1), false);\r
+ if (PG_ARGISNULL(2))\r
+ fn3=0;\r
+ else\r
+ fn3=absClusterPath(PG_GETARG_TEXT_P(2), false);\r
+\r
+ if (access(fn1, W_OK) < 0)\r
+ {\r
+ ereport(WARNING,\r
+ (errcode_for_file_access(),\r
+ errmsg("file %s not accessible: %m", fn1)));\r
+\r
+ PG_RETURN_BOOL(false);\r
+ }\r
+\r
+ if (fn3 && access(fn2, W_OK) < 0)\r
+ {\r
+ ereport(WARNING,\r
+ (errcode_for_file_access(),\r
+ errmsg("file %s not accessible: %m", fn2)));\r
+\r
+ PG_RETURN_BOOL(false);\r
+ }\r
+\r
+\r
+ rc = access(fn3 ? fn3 : fn2, 2);\r
+ if (rc >= 0 || errno != ENOENT)\r
+ {\r
+ ereport(ERROR,\r
+ (ERRCODE_DUPLICATE_FILE,\r
+ errmsg("cannot rename to target file %s", fn3 ? fn3 : fn2)));\r
+ }\r
+ \r
+ if (fn3)\r
+ {\r
+ if (rename(fn2, fn3) != 0)\r
+ {\r
+ ereport(ERROR,\r
+ (errcode_for_file_access(),\r
+ errmsg("could not rename %s to %s: %m", fn2, fn3)));\r
+ }\r
+ if (rename(fn1, fn2) != 0)\r
+ {\r
+ ereport(WARNING,\r
+ (errcode_for_file_access(),\r
+ errmsg("could not rename %s to %s: %m", fn1, fn2)));\r
+\r
+ if (rename(fn3, fn2) != 0)\r
+ {\r
+ ereport(ERROR,\r
+ (errcode_for_file_access(),\r
+ errmsg("could not rename %s back to %s: %m", fn3, fn2)));\r
+ }\r
+ else\r
+ {\r
+ ereport(ERROR,\r
+ (ERRCODE_UNDEFINED_FILE,\r
+ errmsg("renaming %s to %s was reverted", fn2, fn3)));\r
+\r
+ }\r
+ }\r
+ }\r
+ else if (rename(fn1, fn2) != 0)\r
+ {\r
+ ereport(WARNING,\r
+ (errcode_for_file_access(),\r
+ errmsg("renaming %s to %s %m", fn1, fn2)));\r
+ ereport(ERROR,\r
+ (errcode_for_file_access(),\r
+ errmsg("could not rename %s to %s: %m", fn1, fn2)));\r
+ }\r
+\r
+ PG_RETURN_BOOL(true);\r
+}\r
+\r
+\r
+Datum pg_file_unlink(PG_FUNCTION_ARGS)\r
+{\r
+ char *filename;\r
+\r
+ requireSuperuser();\r
+\r
+ filename = absClusterPath(PG_GETARG_TEXT_P(0), false);\r
+\r
+ if (access(filename, W_OK) < 0)\r
+ {\r
+ if (errno == ENOENT)\r
+ PG_RETURN_BOOL(false);\r
+ else\r
+ ereport(ERROR,\r
+ (errcode_for_file_access(),\r
+ errmsg("file %s not accessible: %m", filename)));\r
+\r
+ }\r
+\r
+ if (unlink(filename) < 0)\r
+ {\r
+ ereport(WARNING,\r
+ (errcode_for_file_access(),\r
+ errmsg("could not unlink file %s: %m", filename)));\r
+\r
+ PG_RETURN_BOOL(false);\r
+ }\r
+ PG_RETURN_BOOL(true);\r
+}\r
+\r
+\r
+Datum pg_logdir_ls(PG_FUNCTION_ARGS)\r
+{\r
+ FuncCallContext *funcctx;\r
+ struct dirent *de;\r
+ directory_fctx *fctx;\r
+\r
+ if (!superuser()) \r
+ ereport(ERROR,\r
+ (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),\r
+ (errmsg("only superuser can list the log directory"))));\r
+ \r
+ if (memcmp(Log_filename, "postgresql-%Y-%m-%d_%H%M%S.log", 30) != 0)\r
+ ereport(ERROR,\r
+ (errcode(ERRCODE_INVALID_PARAMETER_VALUE),\r
+ (errmsg("the log_filename parameter must equal 'postgresql-%%Y-%%m-%%d_%%H%%M%%S.log'"))));\r
+\r
+ if (SRF_IS_FIRSTCALL())\r
+ {\r
+ MemoryContext oldcontext;\r
+ TupleDesc tupdesc;\r
+\r
+ funcctx=SRF_FIRSTCALL_INIT();\r
+ oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);\r
+\r
+ fctx = palloc(sizeof(directory_fctx));\r
+ if (is_absolute_path(Log_directory))\r
+ fctx->location = Log_directory;\r
+ else\r
+ {\r
+ fctx->location = palloc(strlen(DataDir) + strlen(Log_directory) +2);\r
+ sprintf(fctx->location, "%s/%s", DataDir, Log_directory);\r
+ }\r
+ tupdesc = CreateTemplateTupleDesc(2, false);\r
+ TupleDescInitEntry(tupdesc, (AttrNumber) 1, "starttime",\r
+ TIMESTAMPOID, -1, 0);\r
+ TupleDescInitEntry(tupdesc, (AttrNumber) 2, "filename",\r
+ TEXTOID, -1, 0);\r
+\r
+ funcctx->attinmeta = TupleDescGetAttInMetadata(tupdesc);\r
+ \r
+ fctx->dirdesc = AllocateDir(fctx->location);\r
+\r
+ if (!fctx->dirdesc)\r
+ ereport(ERROR,\r
+ (errcode_for_file_access(),\r
+ errmsg("%s is not browsable: %m", fctx->location)));\r
+\r
+ funcctx->user_fctx = fctx;\r
+ MemoryContextSwitchTo(oldcontext);\r
+ }\r
+\r
+ funcctx=SRF_PERCALL_SETUP();\r
+ fctx = (directory_fctx*) funcctx->user_fctx;\r
+\r
+ if (!fctx->dirdesc) /* not a readable directory */\r
+ SRF_RETURN_DONE(funcctx);\r
+\r
+ while ((de = readdir(fctx->dirdesc)) != NULL)\r
+ {\r
+ char *values[2];\r
+ HeapTuple tuple;\r
+ \r
+ char *field[MAXDATEFIELDS];\r
+ char lowstr[MAXDATELEN + 1];\r
+ int dtype;\r
+ int nf, ftype[MAXDATEFIELDS];\r
+ fsec_t fsec;\r
+ int tz = 0;\r
+ struct pg_tm date;\r
+\r
+ /*\r
+ * Default format:\r
+ * postgresql-YYYY-MM-DD_HHMMSS.log\r
+ */\r
+ if (strlen(de->d_name) != 32\r
+ || memcmp(de->d_name, "postgresql-", 11)\r
+ || de->d_name[21] != '_'\r
+ || strcmp(de->d_name + 28, ".log"))\r
+ continue;\r
+\r
+ values[1] = palloc(strlen(fctx->location) + strlen(de->d_name) + 2);\r
+ sprintf(values[1], "%s/%s", fctx->location, de->d_name);\r
+\r
+ values[0] = de->d_name + 11; /* timestamp */\r
+ values[0][17] = 0;\r
+\r
+ /* parse and decode expected timestamp */\r
+ if (ParseDateTime(values[0], lowstr, MAXDATELEN, field, ftype, MAXDATEFIELDS, &nf))\r
+ continue;\r
+\r
+ if (DecodeDateTime(field, ftype, nf, &dtype, &date, &fsec, &tz))\r
+ continue;\r
+\r
+ /* Seems the format fits the expected format; feed it into the tuple */\r
+\r
+ tuple = BuildTupleFromCStrings(funcctx->attinmeta, values);\r
+\r
+ SRF_RETURN_NEXT(funcctx, HeapTupleGetDatum(tuple));\r
+ }\r
+\r
+ FreeDir(fctx->dirdesc);\r
+ SRF_RETURN_DONE(funcctx);\r
+}\r