glusterfs

Форк
0
/
glfs-copy-file-range.c 
180 строк · 4.3 Кб
1
/*
2
 Copyright (c) 2018 Red Hat, Inc. <http://www.redhat.com>
3
 This file is part of GlusterFS.
4

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

11
#include <stdio.h>
12
#include <stdlib.h>
13
#include <errno.h>
14
#include <glusterfs/api/glfs.h>
15
#include <glusterfs/api/glfs-handles.h>
16
#include <string.h>
17
#include <time.h>
18
#include <libgen.h>
19

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

35
int
36
main(int argc, char **argv)
37
{
38
    glfs_t *fs = NULL;
39
    int ret = -1;
40
    char *volname = NULL;
41
    char *logfilepath = NULL;
42
    char *path_src = NULL;
43
    char *path_dst = NULL;
44
    glfs_fd_t *glfd_in = NULL;
45
    glfs_fd_t *glfd_out = NULL;
46
    char *volfile_server = NULL;
47

48
    struct stat stbuf = {
49
        0,
50
    };
51
    struct glfs_stat stat_src = {
52
        0,
53
    };
54
    struct glfs_stat prestat_dst = {
55
        0,
56
    };
57
    struct glfs_stat poststat_dst = {
58
        0,
59
    };
60
    size_t len;
61

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

68
    volfile_server = argv[1];
69
    volname = argv[2];
70
    logfilepath = argv[3];
71
    path_src = argv[4];
72
    path_dst = argv[5];
73

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

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

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

93
    ret = glfs_set_volfile_server(fs, "tcp", volfile_server, 24007);
94
    if (ret < 0) {
95
        ret = -errno;
96
        fprintf(stderr,
97
                "Failed to set the volfile server, "
98
                "%s",
99
                strerror(errno));
100
        goto out;
101
    }
102

103
    ret = glfs_set_logging(fs, logfilepath, 7);
104
    if (ret < 0) {
105
        ret = -errno;
106
        fprintf(stderr,
107
                "Failed to set the log file path, "
108
                "%s",
109
                strerror(errno));
110
        goto out;
111
    }
112

113
    ret = glfs_init(fs);
114
    if (ret < 0) {
115
        ret = -errno;
116
        if (errno == ENOENT) {
117
            fprintf(stderr, "Volume %s does not exist", volname);
118
        } else {
119
            fprintf(stderr,
120
                    "%s: Not able to fetch "
121
                    "volfile from glusterd",
122
                    volname);
123
        }
124
        goto out;
125
    }
126

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

135
    glfd_out = glfs_creat(fs, path_dst, O_RDWR, 0644);
136
    if (!glfd_out) {
137
        fprintf(stderr,
138
                "FAILED_DST_OPEN: failed to "
139
                "open (create) %s (%s)\n",
140
                path_dst, strerror(errno));
141
        ret = -errno;
142
        goto out;
143
    } else {
144
        printf("OPEN_DST: opening %s is success\n", path_dst);
145
    }
146

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

155
    len = stbuf.st_size;
156

157
    do {
158
        ret = glfs_copy_file_range(glfd_in, NULL, glfd_out, NULL, len, 0,
159
                                   &stat_src, &prestat_dst, &poststat_dst);
160
        if (ret == -1) {
161
            fprintf(stderr, "copy_file_range failed with %s\n",
162
                    strerror(errno));
163
            ret = -errno;
164
            break;
165
        } else {
166
            printf("copy_file_range successful\n");
167
            len -= ret;
168
        }
169
    } while (len > 0);
170

171
out:
172
    if (glfd_in)
173
        glfs_close(glfd_in);
174
    if (glfd_out)
175
        glfs_close(glfd_out);
176

177
    cleanup(fs);
178

179
    return ret;
180
}
181

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.