types: remove the [u_]quad type and replace with [u]int64
[deb_libnfs.git] / examples / nfs-cp.c
1 /*
2 Copyright (C) by Ronnie Sahlberg <ronniesahlberg@gmail.com> 2013
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #define _FILE_OFFSET_BITS 64
19 #define _GNU_SOURCE
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #ifdef AROS
26 #include "aros_compat.h"
27 #endif
28
29
30 #ifdef WIN32
31 #include "win32_compat.h"
32 #pragma comment(lib, "ws2_32.lib")
33 WSADATA wsaData;
34 #else
35 #include <sys/stat.h>
36 #include <string.h>
37 #endif
38
39 #ifdef HAVE_POLL_H
40 #include <poll.h>
41 #endif
42
43 #ifdef HAVE_UNISTD_H
44 #include <unistd.h>
45 #endif
46
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <stdint.h>
50 #include <sys/types.h>
51 #include <fcntl.h>
52 #include "libnfs.h"
53 #include "libnfs-raw.h"
54 #include "libnfs-raw-mount.h"
55
56 struct file_context {
57 int is_nfs;
58 int fd;
59 struct nfs_context *nfs;
60 struct nfsfh *nfsfh;
61 struct nfs_url *url;
62 };
63
64 void usage(void)
65 {
66 fprintf(stderr, "Usage: nfs-cp <src> <dst>\n");
67 fprintf(stderr, "<src>,<dst> can either be a local file or "
68 "an nfs URL.\n");
69 exit(0);
70 }
71
72 static void
73 free_file_context(struct file_context *file_context)
74 {
75 if (file_context->fd != -1) {
76 close(file_context->fd);
77 }
78 if (file_context->nfsfh != NULL) {
79 nfs_close(file_context->nfs, file_context->nfsfh);
80 }
81 if (file_context->nfs != NULL) {
82 nfs_destroy_context(file_context->nfs);
83 }
84 nfs_destroy_url(file_context->url);
85 free(file_context);
86 }
87
88 static int
89 fstat_file(struct file_context *fc, struct stat *st)
90 {
91 if (fc->is_nfs == 0) {
92 return fstat(fc->fd, st);
93 } else {
94 return nfs_fstat(fc->nfs, fc->nfsfh, st);
95 }
96 }
97
98 static int64_t
99 file_pread(struct file_context *fc, char *buf, int64_t count, uint64_t off)
100 {
101 if (fc->is_nfs == 0) {
102 lseek(fc->fd, off, SEEK_SET);
103 return read(fc->fd, buf, count);
104 } else {
105 return nfs_pread(fc->nfs, fc->nfsfh, off, count, buf);
106 }
107 }
108
109 static int64_t
110 file_pwrite(struct file_context *fc, char *buf, int64_t count, uint64_t off)
111 {
112 if (fc->is_nfs == 0) {
113 lseek(fc->fd, off, SEEK_SET);
114 return write(fc->fd, buf, count);
115 } else {
116 return nfs_pwrite(fc->nfs, fc->nfsfh, off, count, buf);
117 }
118 }
119
120 static struct file_context *
121 open_file(const char *url, int flags)
122 {
123 struct file_context *file_context;
124
125 file_context = malloc(sizeof(struct file_context));
126 if (file_context == NULL) {
127 fprintf(stderr, "Failed to malloc file_context\n");
128 return NULL;
129 }
130 file_context->is_nfs = 0;
131 file_context->fd = -1;
132 file_context->nfs = NULL;
133 file_context->nfsfh = NULL;
134 file_context->url = NULL;
135
136 if (strncmp(url, "nfs://", 6)) {
137 file_context->is_nfs = 0;
138 file_context->fd = open(url, flags, 0660);
139 if (file_context->fd == -1) {
140 fprintf(stderr, "Failed to open %s\n", url);
141 free_file_context(file_context);
142 return NULL;
143 }
144 return file_context;
145 }
146
147 file_context->is_nfs = 1;
148
149 file_context->nfs = nfs_init_context();
150 if (file_context->nfs == NULL) {
151 fprintf(stderr, "failed to init context\n");
152 free_file_context(file_context);
153 return NULL;
154 }
155
156 file_context->url = nfs_parse_url_full(file_context->nfs, url);
157 if (file_context->url == NULL) {
158 fprintf(stderr, "%s\n", nfs_get_error(file_context->nfs));
159 free_file_context(file_context);
160 return NULL;
161 }
162
163 if (nfs_mount(file_context->nfs, file_context->url->server,
164 file_context->url->path) != 0) {
165 fprintf(stderr, "Failed to mount nfs share : %s\n",
166 nfs_get_error(file_context->nfs));
167 free_file_context(file_context);
168 return NULL;
169 }
170
171 if (flags == O_RDONLY) {
172 if (nfs_open(file_context->nfs, file_context->url->file, flags,
173 &file_context->nfsfh) != 0) {
174 fprintf(stderr, "Failed to open file %s: %s\n",
175 file_context->url->file,
176 nfs_get_error(file_context->nfs));
177 free_file_context(file_context);
178 return NULL;
179 }
180 } else {
181 if (nfs_creat(file_context->nfs, file_context->url->file, 0660,
182 &file_context->nfsfh) != 0) {
183 fprintf(stderr, "Failed to creat file %s: %s\n",
184 file_context->url->file,
185 nfs_get_error(file_context->nfs));
186 free_file_context(file_context);
187 return NULL;
188 }
189 }
190 return file_context;
191 }
192
193 #define BUFSIZE 1024*1024
194 static char buf[BUFSIZE];
195
196 int main(int argc, char *argv[])
197 {
198 int ret;
199 struct stat st;
200 struct file_context *src;
201 struct file_context *dst;
202 uint64_t off;
203 int64_t count;
204
205 #ifdef WIN32
206 if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
207 printf("Failed to start Winsock2\n");
208 return 10;
209 }
210 #endif
211
212 #ifdef AROS
213 aros_init_socket();
214 #endif
215
216 if (argc != 3) {
217 usage();
218 }
219
220 src = open_file(argv[1], O_RDONLY);
221 if (src == NULL) {
222 fprintf(stderr, "Failed to open %s\n", argv[1]);
223 return 10;
224 }
225
226 dst = open_file(argv[2], O_WRONLY|O_CREAT|O_TRUNC);
227 if (dst == NULL) {
228 fprintf(stderr, "Failed to open %s\n", argv[2]);
229 free_file_context(src);
230 return 10;
231 }
232
233 if (fstat_file(src, &st) != 0) {
234 fprintf(stderr, "Failed to fstat source file\n");
235 free_file_context(src);
236 free_file_context(dst);
237 return 10;
238 }
239
240 off = 0;
241 while (off < st.st_size) {
242 count = st.st_size - off;
243 if (count > BUFSIZE) {
244 count = BUFSIZE;
245 }
246 count = file_pread(src, buf, count, off);
247 if (count < 0) {
248 fprintf(stderr, "Failed to read from source file\n");
249 free_file_context(src);
250 free_file_context(dst);
251 return 10;
252 }
253 count = file_pwrite(dst, buf, count, off);
254 if (count < 0) {
255 fprintf(stderr, "Failed to write to dest file\n");
256 free_file_context(src);
257 free_file_context(dst);
258 return 10;
259 }
260
261 off += count;
262 }
263 printf("copied %d bytes\n", (int)off);
264
265 free_file_context(src);
266 free_file_context(dst);
267
268 return 0;
269 }