|
Home > Archive > PostgreSQL Discussion > December 2005 > C Function Problem for bytea output
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
C Function Problem for bytea output
|
|
| vishal saberwal 2005-12-28, 8:24 pm |
| hi all,
I am trying to read a file from the File System.
The function compiles fine, the function is created fine in postgres but the
output is NULL/BLANK.
The file does exists and has chmod 777 permission.
PG_FUNCTION_INFO_V1(
file_export);
Datum file_export(PG_FUNCT
ION_ARGS)
{
char *FilePath = PG_GETARG_CSTRING(0)
;
int32 FileLength = PG_GETARG_INT32(1);
FILE *fptr;
bytea *imdata;
char *rp;
imdata = (bytea *) palloc(FileLength+VA
RHDRSZ);
VARATT_SIZEP(imdata)
= FileLength+VARHDRSZ;
rp = VARDATA(imdata);
if ((fptr = fopen(FilePath, "r")) == NULL)
PG_RETURN_NULL();
while (!feof(fptr))
{
fgets(rp,1024,fptr);
rp+=1024;
}
fclose(fptr);
PG_RETURN_BYTEA_P(im
data);
}
select * from file_export('/ResourceFS/Alarm_Arm.gif',1219);
file_export
-------------
(1 row)
I believe its something in the function but i can't spot the error.
thanks,
vish
| |
| Tom Lane 2005-12-28, 8:24 pm |
| vishal saberwal < vishalsaberwal@gmail
.com> writes:
> I believe its something in the function but i can't spot the error.
Well, that sure looks like a NULL result; have you considered the
possibility that the fopen is failing?
BTW, that code will happily clobber memory that doesn't
belong to it if the passed FileLength is wrong.
regards, tom lane
---------------------------(end of broadcast)---------------------------
TIP 2: Don't 'kill -9' the postmaster
| |
| vishal saberwal 2005-12-28, 8:24 pm |
| thanks tom,
yes it was fopen problem ... the problem was solved when i did chown
postgres ...
i should have seen that ...
thanks,
vish
On 12/28/05, Tom Lane <tgl@sss.pgh.pa.us> wrote:
>
> vishal saberwal < vishalsaberwal@gmail
.com> writes:
>
> Well, that sure looks like a NULL result; have you considered the
> possibility that the fopen is failing?
>
> BTW, that code will happily clobber memory that doesn't
> belong to it if the passed FileLength is wrong.
>
> regards, tom lane
>
|
|
|
|
|