summaryrefslogtreecommitdiff
path: root/src/backend/libpq/be-fsstubs.c
diff options
context:
space:
mode:
authorTom Lane2008-03-25 22:42:46 +0000
committerTom Lane2008-03-25 22:42:46 +0000
commit220db7ccd8c88aafea4629f00e8be6f9f073ed00 (patch)
tree772c9dca19736eb9d417cb665a281686c3570b4c /src/backend/libpq/be-fsstubs.c
parentf948197b4055bb0e22e508e9d6966217b7dbea3d (diff)
Simplify and standardize conversions between TEXT datums and ordinary C
strings. This patch introduces four support functions cstring_to_text, cstring_to_text_with_len, text_to_cstring, and text_to_cstring_buffer, and two macros CStringGetTextDatum and TextDatumGetCString. A number of existing macros that provided variants on these themes were removed. Most of the places that need to make such conversions now require just one function or macro call, in place of the multiple notational layers that used to be needed. There are no longer any direct calls of textout or textin, and we got most of the places that were using handmade conversions via memcpy (there may be a few still lurking, though). This commit doesn't make any serious effort to eliminate transient memory leaks caused by detoasting toasted text objects before they reach text_to_cstring. We changed PG_GETARG_TEXT_P to PG_GETARG_TEXT_PP in a few places where it was easy, but much more could be done. Brendan Jurd and Tom Lane
Diffstat (limited to 'src/backend/libpq/be-fsstubs.c')
-rw-r--r--src/backend/libpq/be-fsstubs.c21
1 files changed, 7 insertions, 14 deletions
diff --git a/src/backend/libpq/be-fsstubs.c b/src/backend/libpq/be-fsstubs.c
index e2477a1dd14..a7d746ae58c 100644
--- a/src/backend/libpq/be-fsstubs.c
+++ b/src/backend/libpq/be-fsstubs.c
@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/libpq/be-fsstubs.c,v 1.88 2008/03/22 01:55:14 ishii Exp $
+ * $PostgreSQL: pgsql/src/backend/libpq/be-fsstubs.c,v 1.89 2008/03/25 22:42:43 tgl Exp $
*
* NOTES
* This should be moved to a more appropriate place. It is here
@@ -47,6 +47,7 @@
#include "miscadmin.h"
#include "storage/fd.h"
#include "storage/large_object.h"
+#include "utils/builtins.h"
#include "utils/memutils.h"
@@ -320,7 +321,7 @@ lowrite(PG_FUNCTION_ARGS)
Datum
lo_import(PG_FUNCTION_ARGS)
{
- text *filename = PG_GETARG_TEXT_P(0);
+ text *filename = PG_GETARG_TEXT_PP(0);
PG_RETURN_OID(lo_import_internal(filename, InvalidOid));
}
@@ -332,7 +333,7 @@ lo_import(PG_FUNCTION_ARGS)
Datum
lo_import_with_oid(PG_FUNCTION_ARGS)
{
- text *filename = PG_GETARG_TEXT_P(0);
+ text *filename = PG_GETARG_TEXT_PP(0);
Oid oid = PG_GETARG_OID(1);
PG_RETURN_OID(lo_import_internal(filename, oid));
@@ -362,11 +363,7 @@ lo_import_internal(text *filename, Oid lobjOid)
/*
* open the file to be read in
*/
- nbytes = VARSIZE(filename) - VARHDRSZ;
- if (nbytes >= MAXPGPATH)
- nbytes = MAXPGPATH - 1;
- memcpy(fnamebuf, VARDATA(filename), nbytes);
- fnamebuf[nbytes] = '\0';
+ text_to_cstring_buffer(filename, fnamebuf, sizeof(fnamebuf));
fd = PathNameOpenFile(fnamebuf, O_RDONLY | PG_BINARY, 0666);
if (fd < 0)
ereport(ERROR,
@@ -410,7 +407,7 @@ Datum
lo_export(PG_FUNCTION_ARGS)
{
Oid lobjId = PG_GETARG_OID(0);
- text *filename = PG_GETARG_TEXT_P(1);
+ text *filename = PG_GETARG_TEXT_PP(1);
File fd;
int nbytes,
tmp;
@@ -441,11 +438,7 @@ lo_export(PG_FUNCTION_ARGS)
* 022. This code used to drop it all the way to 0, but creating
* world-writable export files doesn't seem wise.
*/
- nbytes = VARSIZE(filename) - VARHDRSZ;
- if (nbytes >= MAXPGPATH)
- nbytes = MAXPGPATH - 1;
- memcpy(fnamebuf, VARDATA(filename), nbytes);
- fnamebuf[nbytes] = '\0';
+ text_to_cstring_buffer(filename, fnamebuf, sizeof(fnamebuf));
oumask = umask((mode_t) 0022);
fd = PathNameOpenFile(fnamebuf, O_CREAT | O_WRONLY | O_TRUNC | PG_BINARY, 0666);
umask(oumask);