glusterfs
1/*
2Copyright (c) 2018 Red Hat, Inc. <http://www.redhat.com>
3This file is part of GlusterFS.
4
5This file is licensed to you under your choice of the GNU Lesser
6General Public License, version 3 or any later version (LGPLv3 or
7later), or the GNU General Public License, version 2 (GPLv2), in all
8cases 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
20static void21cleanup(glfs_t *fs)22{
23if (!fs)24return;25#if 026/* glfs fini path is still racy and crashing the program. Since27* 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. */
31glfs_fini (fs);32#endif33}
34
35int
36main(int argc, char **argv)37{
38glfs_t *fs = NULL;39int ret = -1;40char *volname = NULL;41char *logfilepath = NULL;42char *path_src = NULL;43char *path_dst = NULL;44glfs_fd_t *glfd_in = NULL;45glfs_fd_t *glfd_out = NULL;46char *volfile_server = NULL;47
48struct stat stbuf = {490,50};51struct glfs_stat stat_src = {520,53};54struct glfs_stat prestat_dst = {550,56};57struct glfs_stat poststat_dst = {580,59};60size_t len;61
62if (argc < 6) {63printf("%s <volume> <log file path> <source> <destination>", argv[0]);64ret = -1;65goto out;66}67
68volfile_server = argv[1];69volname = argv[2];70logfilepath = argv[3];71path_src = argv[4];72path_dst = argv[5];73
74if (path_src[0] != '/') {75fprintf(stderr, "source path %s is not absolute", path_src);76errno = EINVAL;77goto out;78}79
80if (path_dst[0] != '/') {81fprintf(stderr, "destination path %s is not absolute", path_dst);82errno = EINVAL;83goto out;84}85
86fs = glfs_new(volname);87if (!fs) {88ret = -errno;89fprintf(stderr, "Not able to initialize volume '%s'", volname);90goto out;91}92
93ret = glfs_set_volfile_server(fs, "tcp", volfile_server, 24007);94if (ret < 0) {95ret = -errno;96fprintf(stderr,97"Failed to set the volfile server, "98"%s",99strerror(errno));100goto out;101}102
103ret = glfs_set_logging(fs, logfilepath, 7);104if (ret < 0) {105ret = -errno;106fprintf(stderr,107"Failed to set the log file path, "108"%s",109strerror(errno));110goto out;111}112
113ret = glfs_init(fs);114if (ret < 0) {115ret = -errno;116if (errno == ENOENT) {117fprintf(stderr, "Volume %s does not exist", volname);118} else {119fprintf(stderr,120"%s: Not able to fetch "121"volfile from glusterd",122volname);123}124goto out;125}126
127glfd_in = glfs_open(fs, path_src, O_RDONLY | O_NONBLOCK);128if (!glfd_in) {129ret = -errno;130goto out;131} else {132printf("OPEN_SRC: opening %s is success\n", path_src);133}134
135glfd_out = glfs_creat(fs, path_dst, O_RDWR, 0644);136if (!glfd_out) {137fprintf(stderr,138"FAILED_DST_OPEN: failed to "139"open (create) %s (%s)\n",140path_dst, strerror(errno));141ret = -errno;142goto out;143} else {144printf("OPEN_DST: opening %s is success\n", path_dst);145}146
147ret = glfs_fstat(glfd_in, &stbuf);148if (ret < 0) {149ret = -errno;150goto out;151} else {152printf("FSTAT_SRC: fstat on %s is success\n", path_dst);153}154
155len = stbuf.st_size;156
157do {158ret = glfs_copy_file_range(glfd_in, NULL, glfd_out, NULL, len, 0,159&stat_src, &prestat_dst, &poststat_dst);160if (ret == -1) {161fprintf(stderr, "copy_file_range failed with %s\n",162strerror(errno));163ret = -errno;164break;165} else {166printf("copy_file_range successful\n");167len -= ret;168}169} while (len > 0);170
171out:172if (glfd_in)173glfs_close(glfd_in);174if (glfd_out)175glfs_close(glfd_out);176
177cleanup(fs);178
179return ret;180}
181