mirror of
https://github.com/postgres/postgres.git
synced 2025-06-11 20:28:21 +03:00
pgindent run for 8.2.
This commit is contained in:
@ -4,11 +4,11 @@
|
||||
*
|
||||
*
|
||||
* Copyright (c) 2002 - 2006, PostgreSQL Global Development Group
|
||||
*
|
||||
*
|
||||
* Author: Andreas Pflug <pgadmin@pse-consulting.de>
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/contrib/adminpack/adminpack.c,v 1.3 2006/07/11 16:35:30 momjian Exp $
|
||||
* $PostgreSQL: pgsql/contrib/adminpack/adminpack.c,v 1.4 2006/10/04 00:29:44 momjian Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@ -35,7 +35,6 @@
|
||||
#ifdef unlink
|
||||
#undef unlink
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
extern DLLIMPORT char *DataDir;
|
||||
@ -44,20 +43,20 @@ extern DLLIMPORT char *Log_filename;
|
||||
|
||||
PG_MODULE_MAGIC;
|
||||
|
||||
Datum pg_file_write(PG_FUNCTION_ARGS);
|
||||
Datum pg_file_rename(PG_FUNCTION_ARGS);
|
||||
Datum pg_file_unlink(PG_FUNCTION_ARGS);
|
||||
Datum pg_logdir_ls(PG_FUNCTION_ARGS);
|
||||
Datum pg_file_write(PG_FUNCTION_ARGS);
|
||||
Datum pg_file_rename(PG_FUNCTION_ARGS);
|
||||
Datum pg_file_unlink(PG_FUNCTION_ARGS);
|
||||
Datum pg_logdir_ls(PG_FUNCTION_ARGS);
|
||||
|
||||
PG_FUNCTION_INFO_V1(pg_file_write);
|
||||
PG_FUNCTION_INFO_V1(pg_file_rename);
|
||||
PG_FUNCTION_INFO_V1(pg_file_unlink);
|
||||
PG_FUNCTION_INFO_V1(pg_logdir_ls);
|
||||
|
||||
typedef struct
|
||||
typedef struct
|
||||
{
|
||||
char *location;
|
||||
DIR *dirdesc;
|
||||
char *location;
|
||||
DIR *dirdesc;
|
||||
} directory_fctx;
|
||||
|
||||
/*-----------------------
|
||||
@ -65,30 +64,31 @@ typedef struct
|
||||
*/
|
||||
|
||||
/*
|
||||
* Return an absolute path. Argument may be absolute or
|
||||
* Return an absolute path. Argument may be absolute or
|
||||
* relative to the DataDir.
|
||||
*/
|
||||
static char *absClusterPath(text *arg, bool logAllowed)
|
||||
static char *
|
||||
absClusterPath(text *arg, bool logAllowed)
|
||||
{
|
||||
char *filename;
|
||||
int len=VARSIZE(arg) - VARHDRSZ;
|
||||
int dlen = strlen(DataDir);
|
||||
char *filename;
|
||||
int len = VARSIZE(arg) - VARHDRSZ;
|
||||
int dlen = strlen(DataDir);
|
||||
|
||||
filename = palloc(len+1);
|
||||
filename = palloc(len + 1);
|
||||
memcpy(filename, VARDATA(arg), len);
|
||||
filename[len] = 0;
|
||||
|
||||
if (strstr(filename, "..") != NULL)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
||||
(errmsg("No .. allowed in filenames"))));
|
||||
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
||||
(errmsg("No .. allowed in filenames"))));
|
||||
|
||||
if (is_absolute_path(filename))
|
||||
{
|
||||
if (logAllowed && !strncmp(filename, Log_directory, strlen(Log_directory)))
|
||||
return filename;
|
||||
if (logAllowed && !strncmp(filename, Log_directory, strlen(Log_directory)))
|
||||
return filename;
|
||||
if (strncmp(filename, DataDir, dlen))
|
||||
ereport(ERROR,
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
||||
(errmsg("Absolute path not allowed"))));
|
||||
|
||||
@ -96,7 +96,8 @@ static char *absClusterPath(text *arg, bool logAllowed)
|
||||
}
|
||||
else
|
||||
{
|
||||
char *absname = palloc(dlen+len+2);
|
||||
char *absname = palloc(dlen + len + 2);
|
||||
|
||||
sprintf(absname, "%s/%s", DataDir, filename);
|
||||
pfree(filename);
|
||||
return absname;
|
||||
@ -111,9 +112,9 @@ static void
|
||||
requireSuperuser(void)
|
||||
{
|
||||
if (!superuser())
|
||||
ereport(ERROR,
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
||||
(errmsg("only superuser may access generic file functions"))));
|
||||
(errmsg("only superuser may access generic file functions"))));
|
||||
}
|
||||
|
||||
|
||||
@ -122,12 +123,13 @@ requireSuperuser(void)
|
||||
* generic file handling functions
|
||||
*/
|
||||
|
||||
Datum pg_file_write(PG_FUNCTION_ARGS)
|
||||
Datum
|
||||
pg_file_write(PG_FUNCTION_ARGS)
|
||||
{
|
||||
FILE *f;
|
||||
char *filename;
|
||||
text *data;
|
||||
int64 count = 0;
|
||||
FILE *f;
|
||||
char *filename;
|
||||
text *data;
|
||||
int64 count = 0;
|
||||
|
||||
requireSuperuser();
|
||||
|
||||
@ -136,16 +138,17 @@ Datum pg_file_write(PG_FUNCTION_ARGS)
|
||||
|
||||
if (PG_ARGISNULL(2) || !PG_GETARG_BOOL(2))
|
||||
{
|
||||
struct stat fst;
|
||||
struct stat fst;
|
||||
|
||||
if (stat(filename, &fst) >= 0)
|
||||
ereport(ERROR,
|
||||
ereport(ERROR,
|
||||
(ERRCODE_DUPLICATE_FILE,
|
||||
errmsg("file %s exists", filename)));
|
||||
|
||||
f = fopen(filename, "wb");
|
||||
f = fopen(filename, "wb");
|
||||
}
|
||||
else
|
||||
f = fopen(filename, "ab");
|
||||
f = fopen(filename, "ab");
|
||||
|
||||
if (!f)
|
||||
{
|
||||
@ -159,7 +162,7 @@ Datum pg_file_write(PG_FUNCTION_ARGS)
|
||||
count = fwrite(VARDATA(data), 1, VARSIZE(data) - VARHDRSZ, f);
|
||||
|
||||
if (count != VARSIZE(data) - VARHDRSZ)
|
||||
ereport(ERROR,
|
||||
ereport(ERROR,
|
||||
(errcode_for_file_access(),
|
||||
errmsg("error writing file %s: %m", filename)));
|
||||
}
|
||||
@ -169,22 +172,25 @@ Datum pg_file_write(PG_FUNCTION_ARGS)
|
||||
}
|
||||
|
||||
|
||||
Datum pg_file_rename(PG_FUNCTION_ARGS)
|
||||
Datum
|
||||
pg_file_rename(PG_FUNCTION_ARGS)
|
||||
{
|
||||
char *fn1, *fn2, *fn3;
|
||||
int rc;
|
||||
char *fn1,
|
||||
*fn2,
|
||||
*fn3;
|
||||
int rc;
|
||||
|
||||
requireSuperuser();
|
||||
|
||||
if (PG_ARGISNULL(0) || PG_ARGISNULL(1))
|
||||
PG_RETURN_NULL();
|
||||
|
||||
fn1=absClusterPath(PG_GETARG_TEXT_P(0), false);
|
||||
fn2=absClusterPath(PG_GETARG_TEXT_P(1), false);
|
||||
fn1 = absClusterPath(PG_GETARG_TEXT_P(0), false);
|
||||
fn2 = absClusterPath(PG_GETARG_TEXT_P(1), false);
|
||||
if (PG_ARGISNULL(2))
|
||||
fn3=0;
|
||||
fn3 = 0;
|
||||
else
|
||||
fn3=absClusterPath(PG_GETARG_TEXT_P(2), false);
|
||||
fn3 = absClusterPath(PG_GETARG_TEXT_P(2), false);
|
||||
|
||||
if (access(fn1, W_OK) < 0)
|
||||
{
|
||||
@ -192,7 +198,7 @@ Datum pg_file_rename(PG_FUNCTION_ARGS)
|
||||
(errcode_for_file_access(),
|
||||
errmsg("file %s not accessible: %m", fn1)));
|
||||
|
||||
PG_RETURN_BOOL(false);
|
||||
PG_RETURN_BOOL(false);
|
||||
}
|
||||
|
||||
if (fn3 && access(fn2, W_OK) < 0)
|
||||
@ -201,7 +207,7 @@ Datum pg_file_rename(PG_FUNCTION_ARGS)
|
||||
(errcode_for_file_access(),
|
||||
errmsg("file %s not accessible: %m", fn2)));
|
||||
|
||||
PG_RETURN_BOOL(false);
|
||||
PG_RETURN_BOOL(false);
|
||||
}
|
||||
|
||||
|
||||
@ -212,10 +218,10 @@ Datum pg_file_rename(PG_FUNCTION_ARGS)
|
||||
(ERRCODE_DUPLICATE_FILE,
|
||||
errmsg("cannot rename to target file %s", fn3 ? fn3 : fn2)));
|
||||
}
|
||||
|
||||
|
||||
if (fn3)
|
||||
{
|
||||
if (rename(fn2, fn3) != 0)
|
||||
if (rename(fn2, fn3) != 0)
|
||||
{
|
||||
ereport(ERROR,
|
||||
(errcode_for_file_access(),
|
||||
@ -231,7 +237,7 @@ Datum pg_file_rename(PG_FUNCTION_ARGS)
|
||||
{
|
||||
ereport(ERROR,
|
||||
(errcode_for_file_access(),
|
||||
errmsg("could not rename %s back to %s: %m", fn3, fn2)));
|
||||
errmsg("could not rename %s back to %s: %m", fn3, fn2)));
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -244,9 +250,9 @@ Datum pg_file_rename(PG_FUNCTION_ARGS)
|
||||
}
|
||||
else if (rename(fn1, fn2) != 0)
|
||||
{
|
||||
ereport(WARNING,
|
||||
(errcode_for_file_access(),
|
||||
errmsg("renaming %s to %s %m", fn1, fn2)));
|
||||
ereport(WARNING,
|
||||
(errcode_for_file_access(),
|
||||
errmsg("renaming %s to %s %m", fn1, fn2)));
|
||||
ereport(ERROR,
|
||||
(errcode_for_file_access(),
|
||||
errmsg("could not rename %s to %s: %m", fn1, fn2)));
|
||||
@ -256,20 +262,21 @@ Datum pg_file_rename(PG_FUNCTION_ARGS)
|
||||
}
|
||||
|
||||
|
||||
Datum pg_file_unlink(PG_FUNCTION_ARGS)
|
||||
Datum
|
||||
pg_file_unlink(PG_FUNCTION_ARGS)
|
||||
{
|
||||
char *filename;
|
||||
char *filename;
|
||||
|
||||
requireSuperuser();
|
||||
|
||||
filename = absClusterPath(PG_GETARG_TEXT_P(0), false);
|
||||
filename = absClusterPath(PG_GETARG_TEXT_P(0), false);
|
||||
|
||||
if (access(filename, W_OK) < 0)
|
||||
{
|
||||
if (errno == ENOENT)
|
||||
PG_RETURN_BOOL(false);
|
||||
if (errno == ENOENT)
|
||||
PG_RETURN_BOOL(false);
|
||||
else
|
||||
ereport(ERROR,
|
||||
ereport(ERROR,
|
||||
(errcode_for_file_access(),
|
||||
errmsg("file %s not accessible: %m", filename)));
|
||||
|
||||
@ -287,17 +294,18 @@ Datum pg_file_unlink(PG_FUNCTION_ARGS)
|
||||
}
|
||||
|
||||
|
||||
Datum pg_logdir_ls(PG_FUNCTION_ARGS)
|
||||
Datum
|
||||
pg_logdir_ls(PG_FUNCTION_ARGS)
|
||||
{
|
||||
FuncCallContext *funcctx;
|
||||
struct dirent *de;
|
||||
directory_fctx *fctx;
|
||||
|
||||
if (!superuser())
|
||||
if (!superuser())
|
||||
ereport(ERROR,
|
||||
(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)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
||||
@ -306,17 +314,17 @@ Datum pg_logdir_ls(PG_FUNCTION_ARGS)
|
||||
if (SRF_IS_FIRSTCALL())
|
||||
{
|
||||
MemoryContext oldcontext;
|
||||
TupleDesc tupdesc;
|
||||
TupleDesc tupdesc;
|
||||
|
||||
funcctx=SRF_FIRSTCALL_INIT();
|
||||
funcctx = SRF_FIRSTCALL_INIT();
|
||||
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
|
||||
|
||||
fctx = palloc(sizeof(directory_fctx));
|
||||
if (is_absolute_path(Log_directory))
|
||||
fctx->location = Log_directory;
|
||||
fctx->location = Log_directory;
|
||||
else
|
||||
{
|
||||
fctx->location = palloc(strlen(DataDir) + strlen(Log_directory) +2);
|
||||
fctx->location = palloc(strlen(DataDir) + strlen(Log_directory) + 2);
|
||||
sprintf(fctx->location, "%s/%s", DataDir, Log_directory);
|
||||
}
|
||||
tupdesc = CreateTemplateTupleDesc(2, false);
|
||||
@ -326,11 +334,11 @@ Datum pg_logdir_ls(PG_FUNCTION_ARGS)
|
||||
TEXTOID, -1, 0);
|
||||
|
||||
funcctx->attinmeta = TupleDescGetAttInMetadata(tupdesc);
|
||||
|
||||
|
||||
fctx->dirdesc = AllocateDir(fctx->location);
|
||||
|
||||
if (!fctx->dirdesc)
|
||||
ereport(ERROR,
|
||||
ereport(ERROR,
|
||||
(errcode_for_file_access(),
|
||||
errmsg("%s is not browsable: %m", fctx->location)));
|
||||
|
||||
@ -338,47 +346,47 @@ Datum pg_logdir_ls(PG_FUNCTION_ARGS)
|
||||
MemoryContextSwitchTo(oldcontext);
|
||||
}
|
||||
|
||||
funcctx=SRF_PERCALL_SETUP();
|
||||
fctx = (directory_fctx*) funcctx->user_fctx;
|
||||
funcctx = SRF_PERCALL_SETUP();
|
||||
fctx = (directory_fctx *) funcctx->user_fctx;
|
||||
|
||||
if (!fctx->dirdesc) /* not a readable directory */
|
||||
if (!fctx->dirdesc) /* not a readable directory */
|
||||
SRF_RETURN_DONE(funcctx);
|
||||
|
||||
while ((de = readdir(fctx->dirdesc)) != NULL)
|
||||
{
|
||||
char *values[2];
|
||||
HeapTuple tuple;
|
||||
|
||||
char *field[MAXDATEFIELDS];
|
||||
char *values[2];
|
||||
HeapTuple tuple;
|
||||
|
||||
char *field[MAXDATEFIELDS];
|
||||
char lowstr[MAXDATELEN + 1];
|
||||
int dtype;
|
||||
int nf, ftype[MAXDATEFIELDS];
|
||||
int dtype;
|
||||
int nf,
|
||||
ftype[MAXDATEFIELDS];
|
||||
fsec_t fsec;
|
||||
int tz = 0;
|
||||
struct pg_tm date;
|
||||
int tz = 0;
|
||||
struct pg_tm date;
|
||||
|
||||
/*
|
||||
* Default format:
|
||||
* postgresql-YYYY-MM-DD_HHMMSS.log
|
||||
* Default format: postgresql-YYYY-MM-DD_HHMMSS.log
|
||||
*/
|
||||
if (strlen(de->d_name) != 32
|
||||
|| memcmp(de->d_name, "postgresql-", 11)
|
||||
|| memcmp(de->d_name, "postgresql-", 11)
|
||||
|| de->d_name[21] != '_'
|
||||
|| strcmp(de->d_name + 28, ".log"))
|
||||
continue;
|
||||
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] = de->d_name + 11; /* timestamp */
|
||||
values[0][17] = 0;
|
||||
|
||||
/* parse and decode expected timestamp */
|
||||
/* parse and decode expected timestamp */
|
||||
if (ParseDateTime(values[0], lowstr, MAXDATELEN, field, ftype, MAXDATEFIELDS, &nf))
|
||||
continue;
|
||||
continue;
|
||||
|
||||
if (DecodeDateTime(field, ftype, nf, &dtype, &date, &fsec, &tz))
|
||||
continue;
|
||||
continue;
|
||||
|
||||
/* Seems the format fits the expected format; feed it into the tuple */
|
||||
|
||||
|
Reference in New Issue
Block a user