aboutsummaryrefslogtreecommitdiff
path: root/src/backend/utils/adt/genfile.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2011-02-08 16:08:41 -0500
committerTom Lane <tgl@sss.pgh.pa.us>2011-02-08 16:13:22 -0500
commitd9572c4e3b474031060189050e14ef384b94e001 (patch)
tree07646762f4086b94a69b9fc215734d2bccade5db /src/backend/utils/adt/genfile.c
parent414c5a2ea65cbd38d79ffdf9b1fde7cc75c134e0 (diff)
downloadpostgresql-d9572c4e3b474031060189050e14ef384b94e001.tar.gz
postgresql-d9572c4e3b474031060189050e14ef384b94e001.zip
Core support for "extensions", which are packages of SQL objects.
This patch adds the server infrastructure to support extensions. There is still one significant loose end, namely how to make it play nice with pg_upgrade, so I am not yet committing the changes that would make all the contrib modules depend on this feature. In passing, fix a disturbingly large amount of breakage in AlterObjectNamespace() and callers. Dimitri Fontaine, reviewed by Anssi Kääriäinen, Itagaki Takahiro, Tom Lane, and numerous others
Diffstat (limited to 'src/backend/utils/adt/genfile.c')
-rw-r--r--src/backend/utils/adt/genfile.c63
1 files changed, 45 insertions, 18 deletions
diff --git a/src/backend/utils/adt/genfile.c b/src/backend/utils/adt/genfile.c
index 93bc401c2d1..c3ec98aa5e2 100644
--- a/src/backend/utils/adt/genfile.c
+++ b/src/backend/utils/adt/genfile.c
@@ -82,22 +82,16 @@ convert_and_check_filename(text *arg)
/*
* Read a section of a file, returning it as bytea
*
- * We read the whole of the file when bytes_to_read is nagative.
+ * Caller is responsible for all permissions checking.
+ *
+ * We read the whole of the file when bytes_to_read is negative.
*/
-static bytea *
-read_binary_file(text *filename_t, int64 seek_offset, int64 bytes_to_read)
+bytea *
+read_binary_file(const char *filename, int64 seek_offset, int64 bytes_to_read)
{
bytea *buf;
size_t nbytes;
FILE *file;
- char *filename;
-
- if (!superuser())
- ereport(ERROR,
- (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
- (errmsg("must be superuser to read files"))));
-
- filename = convert_and_check_filename(filename_t);
if (bytes_to_read < 0)
{
@@ -146,7 +140,6 @@ read_binary_file(text *filename_t, int64 seek_offset, int64 bytes_to_read)
SET_VARSIZE(buf, nbytes + VARHDRSZ);
FreeFile(file);
- pfree(filename);
return buf;
}
@@ -156,9 +149,11 @@ read_binary_file(text *filename_t, int64 seek_offset, int64 bytes_to_read)
* in the database encoding.
*/
static text *
-read_text_file(text *filename, int64 seek_offset, int64 bytes_to_read)
+read_text_file(const char *filename, int64 seek_offset, int64 bytes_to_read)
{
- bytea *buf = read_binary_file(filename, seek_offset, bytes_to_read);
+ bytea *buf;
+
+ buf = read_binary_file(filename, seek_offset, bytes_to_read);
/* Make sure the input is valid */
pg_verifymbstr(VARDATA(buf), VARSIZE(buf) - VARHDRSZ, false);
@@ -176,13 +171,21 @@ pg_read_file(PG_FUNCTION_ARGS)
text *filename_t = PG_GETARG_TEXT_P(0);
int64 seek_offset = PG_GETARG_INT64(1);
int64 bytes_to_read = PG_GETARG_INT64(2);
+ char *filename;
+
+ if (!superuser())
+ ereport(ERROR,
+ (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
+ (errmsg("must be superuser to read files"))));
+
+ filename = convert_and_check_filename(filename_t);
if (bytes_to_read < 0)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("requested length cannot be negative")));
- PG_RETURN_TEXT_P(read_text_file(filename_t, seek_offset, bytes_to_read));
+ PG_RETURN_TEXT_P(read_text_file(filename, seek_offset, bytes_to_read));
}
/*
@@ -192,8 +195,16 @@ Datum
pg_read_file_all(PG_FUNCTION_ARGS)
{
text *filename_t = PG_GETARG_TEXT_P(0);
+ char *filename;
+
+ if (!superuser())
+ ereport(ERROR,
+ (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
+ (errmsg("must be superuser to read files"))));
+
+ filename = convert_and_check_filename(filename_t);
- PG_RETURN_TEXT_P(read_text_file(filename_t, 0, -1));
+ PG_RETURN_TEXT_P(read_text_file(filename, 0, -1));
}
/*
@@ -205,13 +216,21 @@ pg_read_binary_file(PG_FUNCTION_ARGS)
text *filename_t = PG_GETARG_TEXT_P(0);
int64 seek_offset = PG_GETARG_INT64(1);
int64 bytes_to_read = PG_GETARG_INT64(2);
+ char *filename;
+
+ if (!superuser())
+ ereport(ERROR,
+ (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
+ (errmsg("must be superuser to read files"))));
+
+ filename = convert_and_check_filename(filename_t);
if (bytes_to_read < 0)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("requested length cannot be negative")));
- PG_RETURN_BYTEA_P(read_binary_file(filename_t, seek_offset, bytes_to_read));
+ PG_RETURN_BYTEA_P(read_binary_file(filename, seek_offset, bytes_to_read));
}
/*
@@ -221,8 +240,16 @@ Datum
pg_read_binary_file_all(PG_FUNCTION_ARGS)
{
text *filename_t = PG_GETARG_TEXT_P(0);
+ char *filename;
+
+ if (!superuser())
+ ereport(ERROR,
+ (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
+ (errmsg("must be superuser to read files"))));
+
+ filename = convert_and_check_filename(filename_t);
- PG_RETURN_BYTEA_P(read_binary_file(filename_t, 0, -1));
+ PG_RETURN_BYTEA_P(read_binary_file(filename, 0, -1));
}
/*