summaryrefslogtreecommitdiffstats
path: root/tests/basic/metadisp/fxattr.c
blob: e552057778a1279c4d83d3789745311a769b15b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#include <sys/xattr.h>

static char MY_XATTR[] = "user.fxtest";
static char *PROGRAM;
#define CONSUME(v)                                                             \
    do {                                                                       \
        if (!argc) {                                                           \
            fprintf(stderr, "missing argument\n");                             \
            return EXIT_FAILURE;                                               \
        }                                                                      \
        v = argv[0];                                                           \
        ++argv;                                                                \
        --argc;                                                                \
    } while (0)

static int
do_get(int argc, char **argv, int fd)
{
    char *value;
    int ret;
    char buf[1024];

    CONSUME(value);

    ret = fgetxattr(fd, MY_XATTR, buf, sizeof(buf));
    if (ret == (-1)) {
        perror("fgetxattr");
        return EXIT_FAILURE;
    }

    if (strncmp(buf, value, ret) != 0) {
        fprintf(stderr, "data mismatch\n");
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}

static int
do_set(int argc, char **argv, int fd)
{
    char *value;
    int ret;

    CONSUME(value);

    ret = fsetxattr(fd, MY_XATTR, value, strlen(value), 0);
    if (ret == (-1)) {
        perror("fsetxattr");
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}

static int
do_remove(int argc, char **argv, int fd)
{
    int ret;

    ret = fremovexattr(fd, MY_XATTR);
    if (ret == (-1)) {
        perror("femovexattr");
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}

int
main(int argc, char **argv)
{
    int fd;
    char *path;
    char *cmd;

    CONSUME(PROGRAM);
    CONSUME(path);
    CONSUME(cmd);

    fd = open(path, O_RDWR);
    if (fd == (-1)) {
        perror("open");
        return EXIT_FAILURE;
    }

    if (strcmp(cmd, "get") == 0) {
        return do_get(argc, argv, fd);
    }

    if (strcmp(cmd, "set") == 0) {
        return do_set(argc, argv, fd);
    }

    if (strcmp(cmd, "remove") == 0) {
        return do_remove(argc, argv, fd);
    }

    return EXIT_SUCCESS;
}