0

我编写了一个简单的程序来测试 lsetxattr() 和 lgetxattr() 函数。我只是想向这个文件添加一个扩展属性,然后再次获取该值。但我无法得到预期的结果。那么使用这两种方法的正确方法是什么?谢谢!

#define _GNU_SOURCE
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/xattr.h>
#include <time.h>
#include <unistd.h>

int main()
{
    char *path = "/tmp/abc.txt";
    FILE *file = fopen(path, "w");

    int id = 101;
    if (lsetxattr(path, "user.id", &id, sizeof(int), 0) < 0)
        printf("lsetxattr wrong\n");

    int result;
    if (lgetxattr(path, "user.id", &result, sizeof(int)) != sizeof(int)) {
        printf("lgetxattr wrong\n");
    }
    printf("%d\n", result);
    return 0;
}
4

2 回答 2

1

这可能是因为您的/tmp挂载不支持扩展属性。查看手册页

ENOTSUP
          Extended attributes are not supported by the file system, or are
          disabled, errno is set to ENOTSUP.

您可以通过将路径更改为该挂载之外的路径来验证这一点,例如在当前目录中(当然假设它在该挂载之外):

char *path = "abc.txt";

假设您的其他坐骑当然支持扩展属性(这更有可能)。如果您必须在 上执行此操作/tmp,则必须查看一些手册以了解如何在/tmp(tmpfs) 上启用它。

于 2013-04-11T04:01:14.470 回答
0

看起来像两者lsetxattr()并默认lgetxatter()返回-1

#include <errno.h>
#include <sys/xattr.h>

ssize_t
lgetxattr (const char *__path, const char *__name,
           void *__value, size_t __size)
{
  __set_errno (ENOSYS);
  return -1;
}

stub_warning (lgetxattr)
#include <stub-tag.h>

我在 glibc 的源代码中找到了这个:lgetxattr.clsetxattr.c

于 2017-01-05T07:25:18.603 回答