2

我需要创建一个函数来测试 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);

}

我认为问题出在“stat”函数中的第一个参数上,因为 fileName 是一个文本,而这个函数接收一个字符。

这是我第一次编写 C 代码,所以可能一切都错了。

4

2 回答 2

5

如果您深入研究标题,您会在以下位置找到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;

所以这是你对text数据类型的定义。请注意评论中这个相当重要的部分:

没有终止 null 或类似的东西

这表明您绝对不想将fileName->data其视为 C 字符串,除非您喜欢 segfaults。您需要一种将 a 转换为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.
 */

还有一个使用它的例子

char *command;
/*...*/

/* Convert given text object to a C string */
command = text_to_cstring(sql);

/*...*/
pfree(command);

你应该能够做这样的事情:

struct stat buf;
char *fileName = text_to_cstring(PG_GETARG_TEXT_P(0)); 
int i = stat(fileName, &buf);
pfree(fileName);
于 2012-10-13T00:13:46.453 回答
1

我遇到过同样的问题。text_to_cstring 对我不起作用,然后我意识到问题是我没有包含 text_to_cstring func 的头文件。那是多么愚蠢。所以我不得不在 /usr/include/postgresql/your_version/server/utils/ 文件夹中搜索,直到我最终找到它,然后一切正常。尝试

char *filename = text_to_cstring(PG_GETARG_TEXT_PP(0));

但不要忘记包含第一个 builtins.h

#include "utils/builtins.h"
于 2015-01-08T11:32:46.587 回答