summaryrefslogtreecommitdiffstats
path: root/tests/basic/ec/ec-fast-fgetxattr.c
blob: bf98215186190775c614a5f7b14bff65caa7eb0b (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <limits.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <glusterfs/api/glfs.h>
#include <glusterfs/api/glfs-handles.h>

int cbk_complete = 0;
ssize_t cbk_ret_val = 0;
int
fill_iov(struct iovec *iov, char fillchar, int count)
{
    int ret = -1;

    iov->iov_base = malloc(count + 1);
    if (iov->iov_base == NULL) {
        return ret;
    } else {
        iov->iov_len = count;
        ret = 0;
    }
    memset(iov->iov_base, fillchar, count);
    memset(iov->iov_base + count, '\0', 1);

    return ret;
}

void
write_async_cbk(glfs_fd_t *fd, ssize_t ret, struct stat *prestat,
                struct stat *poststat, void *cookie)
{
    if (ret < 0) {
        fprintf(stderr, "glfs_write failed");
    }
    cbk_ret_val = ret;
    cbk_complete = 1;
}

int
write_async(glfs_t *fs, glfs_fd_t *glfd, int char_count)
{
    ssize_t ret = -1;
    int flags = O_RDWR;
    struct iovec iov = {0};

    ret = fill_iov(&iov, 'a', char_count);
    if (ret) {
        fprintf(stderr, "failed to create iov");
        goto out;
    }

    ret = glfs_pwritev_async(glfd, &iov, 1, 0, flags, write_async_cbk, NULL);
out:
    if (ret < 0) {
        fprintf(stderr, "glfs_pwritev async failed");
    }
    return ret;
}

int
main(int argc, char *argv[])
{
    glfs_t *fs = NULL;
    glfs_fd_t *fd = NULL;
    int ret = 1;
    char buf[1024] = {0};

    if (argc != 4) {
        fprintf(stderr, "Syntax: %s <host> <volname> <file>\n", argv[0]);
        return 1;
    }

    fs = glfs_new(argv[2]);
    if (!fs) {
        fprintf(stderr, "glfs_new: returned NULL\n");
        return 1;
    }

    ret = glfs_set_volfile_server(fs, "tcp", argv[1], 24007);
    if (ret != 0) {
        fprintf(stderr, "glfs_set_volfile_server: returned %d\n", ret);
        goto out;
    }
    ret = glfs_set_logging(fs, "/tmp/ec-fgetxattr.log", 7);
    if (ret != 0) {
        fprintf(stderr, "glfs_set_logging: returned %d\n", ret);
        goto out;
    }
    ret = glfs_init(fs);
    if (ret != 0) {
        fprintf(stderr, "glfs_init: returned %d\n", ret);
        goto out;
    }

    fd = glfs_open(fs, argv[3], O_RDWR | O_TRUNC);
    if (fd == NULL) {
        fprintf(stderr, "glfs_open: returned NULL\n");
        goto out;
    }

    ret = write_async(fs, fd, 16);
    if (ret) {
        fprintf(stderr, "write_async failed\n");
    }

    sleep(1);
    ret = glfs_fgetxattr(fd, "trusted.glusterfs.abc", buf, sizeof buf);
    while (cbk_complete != 1) {
        /* ret will be -ve as xattr doesn't exist, and fgetxattr should
         * return waaaayyy before writev */
        ret = 0;
        sleep(1);
    }
    if (cbk_ret_val < 0) {
        fprintf(stderr, "cbk_ret_val is -ve\n");
        ret = -1;
    }
    glfs_close(fd);

out:
    unlink("/tmp/ec-fgetxattr.log");
    glfs_fini(fs);

    return ret;
}