我需要创建一个函数来测试文件是否存在postgres.我是用C语言做的,但我遇到了问题.代码是:
#include "postgres.h"
#include <string.h>
#include "fmgr.h"
#include <stdio.h>
#include<sys/stat.h>
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
/* by value */
PG_FUNCTION_INFO_V1(file_exists);
Datum
file_exists (PG_FUNCTION_ARGS)
{
text *fileName= PG_GETARG_TEXT_P(0);
struct stat buf;
int i = stat(fileName, &buf);
/* File found */
if ( i == 0 )
{
PG_RETURN_INT32(1);
}
PG_RETURN_INT32(0);
}
Run Code Online (Sandbox Code Playgroud)
我认为问题在于"stat"函数中的第一个参数,因为fileName是一个文本而且这个函数接收一个char.
这是我第一次编写C代码,所以也许一切都是错的.
如果你仔细阅读标题,你会发现server/c.h:
/* ----------------
* Variable-length datatypes all share the 'struct varlena' header.
*...
*/
struct varlena
{
char vl_len_[4]; /* Do not touch this field directly! */
char vl_dat[1];
};
#define VARHDRSZ ((int32) sizeof(int32))
/*
* These widely-used datatypes are just a varlena header and the data bytes.
* There is no terminating null or anything like that --- the data length is
* always VARSIZE(ptr) - VARHDRSZ.
*/
typedef struct varlena bytea;
typedef struct varlena text;
Run Code Online (Sandbox Code Playgroud)
所以你有text数据类型的定义.请注意评论中相当重要的部分:
没有终止null或类似的东西
这表明你绝对不想把它fileName->data当作C字符串,除非你喜欢segfaults.你需要一种方法来转换为text你可以交给的以空字符结尾的C字符串stat; 有一个功能:text_to_cstring.
text_to_cstring我能找到的唯一文档就是源代码中的这条评论:
/*
* text_to_cstring
*
* Create a palloc'd, null-terminated C string from a text value.
*
* We support being passed a compressed or toasted text value.
* This is a bit bogus since such values shouldn't really be referred to as
* "text *", but it seems useful for robustness. If we didn't handle that
* case here, we'd need another routine that did, anyway.
*/
Run Code Online (Sandbox Code Playgroud)
还有一个使用它的示例:
char *command;
/*...*/
/* Convert given text object to a C string */
command = text_to_cstring(sql);
/*...*/
pfree(command);
Run Code Online (Sandbox Code Playgroud)
你应该可以做这样的事情:
struct stat buf;
char *fileName = text_to_cstring(PG_GETARG_TEXT_P(0));
int i = stat(fileName, &buf);
pfree(fileName);
Run Code Online (Sandbox Code Playgroud)