Thread: File access problem access(), stat()

File access problem access(), stat()

From
vishal saberwal
Date:
hi all,

I have read through the fmgr's readme but havent been able to solve my problem with accessing the file.
I gave the permissions: [root@localhost /]# chmod 777-r /ResourceFS"  and have made sure the file i am trying to access does exist.

I am not sure where i am going wrong.

/*
gcc -Wall -I /root/postgresql-7.4.5/src/include -shared -Wl,-soname,resource_test.so.1 -o resource_test.so resource_test.c
*/
                                                                                                                                                            
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "/usr/include/pgsql/server/postgres.h"
#include "/usr/include/pgsql/server/fmgr.h"
#include "/usr/include/pgsql/server/libpq/pqformat.h"
                                                                                                                                                            
Datum file_check(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(file_check);
 
Datum file_check(PG_FUNCTION_ARGS)
{
        char    *filecmd = (char*) PG_GETARG_VARCHAR_P(0);

    int filelength=0;
        struct stat stat_buf;

        if(!access(filecmd,R_OK))
        {
                stat(filecmd, &stat_buf );
                filelength=stat_buf.st_size;
        }
    return filelength;
}

xy_db=# create or replace function file_check(varchar) returns varchar as
xy_db-# '/root/resource_test.so','file_check' language 'c';
CREATE FUNCTION
k2_db=# select file_check('/ResourceFS/IconFS/Alarm/My.gif');
server closed the connection unexpectedly
        This probably means the server terminated abnormally
        before or while processing the request.
The connection to the server was lost. Attempting reset: Failed.
!> \q

thanks,
vish

Re: File access problem access(), stat()

From
Tom Lane
Date:
vishal saberwal <vishalsaberwal@gmail.com> writes:
> xy_db=# create or replace function file_check(varchar) returns varchar as
> xy_db-# '/root/resource_test.so','file_check' language 'c';
> CREATE FUNCTION
> k2_db=# select file_check('/ResourceFS/IconFS/Alarm/My.gif');
> server closed the connection unexpectedly

This is not a file permissions problem ... more likely, your C function
is wrong and is dumping core.  Getting out your debugger would be a good
response.

>         char    *filecmd = (char*) PG_GETARG_VARCHAR_P(0);

However, I can point you in the right direction: you can't just cast a
varchar * to char *, they are not remotely the same thing.

            regards, tom lane

Re: File access problem access(), stat()

From
vishal saberwal
Date:
i am sorry for misleading subject line ...

thanks for your tip.

Perhaps fmgr has more in it that i need to understand and study so as to convert text/varchar to char* .

thanks,
vish

On 12/12/05, Tom Lane <tgl@sss.pgh.pa.us> wrote:
vishal saberwal <vishalsaberwal@gmail.com> writes:
> xy_db=# create or replace function file_check(varchar) returns varchar as
> xy_db-# '/root/resource_test.so','file_check' language 'c';
> CREATE FUNCTION
> k2_db=# select file_check('/ResourceFS/IconFS/Alarm/My.gif');
> server closed the connection unexpectedly

This is not a file permissions problem ... more likely, your C function
is wrong and is dumping core.  Getting out your debugger would be a good
response.

>         char    *filecmd = (char*) PG_GETARG_VARCHAR_P(0);

However, I can point you in the right direction: you can't just cast a
varchar * to char *, they are not remotely the same thing.

                        regards, tom lane

Re: File access problem access(), stat()

From
vishal saberwal
Date:
hi,
It was a stupid error on my part.
I was declaring the function to return varchar when i was actually returning integer. Also, i looked at varchar.c which was a great help.
It had a function that shows how to convert varchar to cstring.

Thanks tom,
vish

On 12/12/05, vishal saberwal <vishalsaberwal@gmail.com> wrote:
i am sorry for misleading subject line ...

thanks for your tip.

Perhaps fmgr has more in it that i need to understand and study so as to convert text/varchar to char* .

thanks,
vish


On 12/12/05, Tom Lane < tgl@sss.pgh.pa.us> wrote:
vishal saberwal <vishalsaberwal@gmail.com> writes:
> xy_db=# create or replace function file_check(varchar) returns varchar as
> xy_db-# '/root/resource_test.so','file_check' language 'c';
> CREATE FUNCTION
> k2_db=# select file_check('/ResourceFS/IconFS/Alarm/My.gif');
> server closed the connection unexpectedly

This is not a file permissions problem ... more likely, your C function
is wrong and is dumping core.  Getting out your debugger would be a good
response.

>         char    *filecmd = (char*) PG_GETARG_VARCHAR_P(0);

However, I can point you in the right direction: you can't just cast a
varchar * to char *, they are not remotely the same thing.

                        regards, tom lane