summaryrefslogtreecommitdiffstats
path: root/tests/basic/gfapi/glfs-copy-file-range.c
blob: 756c38d21ecc4a5687aac4513f13733514eaacfc (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
 Copyright (c) 2018 Red Hat, Inc. <http://www.redhat.com>
 This file is part of GlusterFS.

 This file is licensed to you under your choice of the GNU Lesser
 General Public License, version 3 or any later version (LGPLv3 or
 later), or the GNU General Public License, version 2 (GPLv2), in all
 cases as published by the Free Software Foundation.
*/

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <glusterfs/api/glfs.h>
#include <glusterfs/api/glfs-handles.h>
#include <string.h>
#include <time.h>
#include <libgen.h>

static void
cleanup(glfs_t *fs)
{
    if (!fs)
        return;
#if 0
        /* glfs fini path is still racy and crashing the program. Since
         * this program any way has to die, we are not going to call fini
         * in the released versions. i.e. final builds. For all
         * internal testing lets enable this so that glfs_fini code
         * path becomes stable. */
        glfs_fini (fs);
#endif
}

int
main(int argc, char **argv)
{
    glfs_t *fs = NULL;
    int ret = -1;
    char *volname = NULL;
    char *logfilepath = NULL;
    char *path_src = NULL;
    char *path_dst = NULL;
    glfs_fd_t *glfd_in = NULL;
    glfs_fd_t *glfd_out = NULL;
    char *volfile_server = NULL;

    struct stat stbuf = {
        0,
    };
    struct stat prestat_dst = {
        0,
    };
    struct stat poststat_dst = {
        0,
    };
    size_t len;

    if (argc < 6) {
        printf("%s <volume> <log file path> <source> <destination>", argv[0]);
        ret = -1;
        goto out;
    }

    volfile_server = argv[1];
    volname = argv[2];
    logfilepath = argv[3];
    path_src = argv[4];
    path_dst = argv[5];

    if (path_src[0] != '/') {
        fprintf(stderr, "source path %s is not absolute", path_src);
        errno = EINVAL;
        goto out;
    }

    if (path_dst[0] != '/') {
        fprintf(stderr, "destination path %s is not absolute", path_dst);
        errno = EINVAL;
        goto out;
    }

    fs = glfs_new(volname);
    if (!fs) {
        ret = -errno;
        fprintf(stderr, "Not able to initialize volume '%s'", volname);
        goto out;
    }

    ret = glfs_set_volfile_server(fs, "tcp", volfile_server, 24007);
    if (ret < 0) {
        ret = -errno;
        fprintf(stderr,
                "Failed to set the volfile server, "
                "%s",
                strerror(errno));
        goto out;
    }

    ret = glfs_set_logging(fs, logfilepath, 7);
    if (ret < 0) {
        ret = -errno;
        fprintf(stderr,
                "Failed to set the log file path, "
                "%s",
                strerror(errno));
        goto out;
    }

    ret = glfs_init(fs);
    if (ret < 0) {
        ret = -errno;
        if (errno == ENOENT) {
            fprintf(stderr, "Volume %s does not exist", volname);
        } else {
            fprintf(stderr,
                    "%s: Not able to fetch "
                    "volfile from glusterd",
                    volname);
        }
        goto out;
    }

    glfd_in = glfs_open(fs, path_src, O_RDONLY | O_NONBLOCK);
    if (!glfd_in) {
        ret = -errno;
        goto out;
    } else {
        printf("OPEN_SRC: opening %s is success\n", path_src);
    }

    glfd_out = glfs_creat(fs, path_dst, O_RDWR, 0644);
    if (!glfd_out) {
        fprintf(stderr,
                "FAILED_DST_OPEN: failed to "
                "open (create) %s (%s)\n",
                path_dst, strerror(errno));
        ret = -errno;
        goto out;
    } else {
        printf("OPEN_DST: opening %s is success\n", path_dst);
    }

    ret = glfs_fstat(glfd_in, &stbuf);
    if (ret < 0) {
        ret = -errno;
        goto out;
    } else {
        printf("FSTAT_SRC: fstat on %s is success\n", path_dst);
    }

    len = stbuf.st_size;

    do {
        ret = glfs_copy_file_range(glfd_in, NULL, glfd_out, NULL, len, 0,
                                   &stbuf, &prestat_dst, &poststat_dst);
        if (ret == -1) {
            fprintf(stderr, "copy_file_range failed with %s\n",
                    strerror(errno));
            ret = -errno;
            break;
        } else {
            printf("copy_file_range successful\n");
            len -= ret;
        }
    } while (len > 0);

out:
    if (glfd_in)
        glfs_close(glfd_in);
    if (glfd_out)
        glfs_close(glfd_out);

    cleanup(fs);

    return ret;
}