0

lsetxattr() および lgetxattr() 関数をテストする簡単なプログラムを作成しました。このファイルに拡張プロパティを追加して、値を再度取得したかっただけです。しかし、思い通りの結果が得られません。では、これら 2 つの方法を使用する正しい方法は何でしょうか? ありがとう!

#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";

もちろん、他のマウントが拡張属性をサポートしていると仮定します(これは可能性が高いです)。で実行する必要がある場合は、 (tmpfs)/tmpで有効にする方法を理解するためにいくつかのマニュアルを参照する必要があります。/tmp

于 2013-04-11T04:01:14.470 に答える