Add two more examples: nfs-ls and nfs-cp
[deb_libnfs.git] / examples / nfs-cp.c
CommitLineData
a6bc1707
RS
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#else
33#include <sys/stat.h>
34#include <string.h>
35#endif
36
37#ifdef HAVE_POLL_H
38#include <poll.h>
39#endif
40
41#ifdef HAVE_UNISTD_H
42#include <unistd.h>
43#endif
44
45#include <stdio.h>
46#include <stdlib.h>
47#include <stdint.h>
48#include <sys/types.h>
49#include <fcntl.h>
50#include "libnfs-zdr.h"
51#include "libnfs.h"
52#include "libnfs-raw.h"
53#include "libnfs-raw-mount.h"
54
55struct file_context {
56 int is_nfs;
57 int fd;
58 struct nfs_context *nfs;
59 struct nfsfh *nfsfh;
60};
61
62void usage(void)
63{
64 fprintf(stderr, "Usage: nfs-cp <src> <dst>\n");
65 fprintf(stderr, "<src>,<dst> can either be a local file or "
66 "an nfs URL.\n");
67 exit(0);
68}
69
70static void
71free_file_context(struct file_context *file_context)
72{
73 if (file_context->fd != -1) {
74 close(file_context->fd);
75 }
76 if (file_context->nfsfh != NULL) {
77 nfs_close(file_context->nfs, file_context->nfsfh);
78 }
79 if (file_context->nfs != NULL) {
80 nfs_destroy_context(file_context->nfs);
81 }
82 free(file_context);
83}
84
85static int
86fstat_file(struct file_context *fc, struct stat *st)
87{
88 if (fc->is_nfs == 0) {
89 return fstat(fc->fd, st);
90 } else {
91 return nfs_fstat(fc->nfs, fc->nfsfh, st);
92 }
93}
94
95static int64_t
96file_pread(struct file_context *fc, char *buf, int64_t count, uint64_t off)
97{
98 if (fc->is_nfs == 0) {
99 return pread(fc->fd, buf, count, off);
100 } else {
101 return nfs_pread(fc->nfs, fc->nfsfh, off, count, buf);
102 }
103}
104
105static int64_t
106file_pwrite(struct file_context *fc, char *buf, int64_t count, uint64_t off)
107{
108 if (fc->is_nfs == 0) {
109 return pwrite(fc->fd, buf, count, off);
110 } else {
111 return nfs_pwrite(fc->nfs, fc->nfsfh, off, count, buf);
112 }
113}
114
115static struct file_context *
116open_file(const char *url, int flags)
117{
118 struct file_context *file_context;
119 char *server = NULL, *path = NULL, *file = NULL, *strp;
120
121 file_context = malloc(sizeof(struct file_context));
122 if (file_context == NULL) {
123 fprintf(stderr, "Failed to malloc file_context\n");
124 return NULL;
125 }
126 file_context->is_nfs = 0;
127 file_context->fd = -1;
128 file_context->nfs = NULL;
129 file_context->nfsfh = NULL;
130
131
132 if (strncmp(url, "nfs://", 6)) {
133 file_context->is_nfs = 0;
134 file_context->fd = open(url, flags, 0660);
135 if (file_context->fd == -1) {
136 fprintf(stderr, "Failed to open %s\n", url);
137 free_file_context(file_context);
138 return NULL;
139 }
140 return file_context;
141 }
142
143 file_context->is_nfs = 1;
144
145 file_context->nfs = nfs_init_context();
146 if (file_context->nfs == NULL) {
147 fprintf(stderr, "failed to init context\n");
148 free_file_context(file_context);
149 return NULL;
150 }
151
152 server = strdup(url + 6);
153 if (server == NULL) {
154 fprintf(stderr, "Failed to strdup server string\n");
155 free_file_context(file_context);
156 return NULL;
157 }
158 if (server[0] == '/' || server[0] == '\0') {
159 fprintf(stderr, "Invalid server string.\n");
160 free(server);
161 free_file_context(file_context);
162 return NULL;
163 }
164 strp = strchr(server, '/');
165 path = strdup(strp);
166 *strp = 0;
167 if (path == NULL) {
168 fprintf(stderr, "Invalid URL specified.\n");
169 free(server);
170 free_file_context(file_context);
171 return NULL;
172 }
173
174 strp = strrchr(path, '/');
175 if (strp == NULL) {
176 fprintf(stderr, "Invalid URL specified.\n");
177 free(path);
178 free(server);
179 free_file_context(file_context);
180 return NULL;
181 }
182 file = strdup(strp);
183 *strp = 0;
184
185 if (nfs_mount(file_context->nfs, server, path) != 0) {
186 fprintf(stderr, "Failed to mount nfs share : %s\n",
187 nfs_get_error(file_context->nfs));
188 free(file);
189 free(path);
190 free(server);
191 free_file_context(file_context);
192 return NULL;
193 }
194
195 if (flags == O_RDONLY) {
196 if (nfs_open(file_context->nfs, file, flags,
197 &file_context->nfsfh) != 0) {
198 fprintf(stderr, "Failed to open file : %s\n",
199 nfs_get_error(file_context->nfs));
200 free(file);
201 free(path);
202 free(server);
203 free_file_context(file_context);
204 return NULL;
205 }
206 } else {
207 if (nfs_creat(file_context->nfs, file, 0660,
208 &file_context->nfsfh) != 0) {
209 fprintf(stderr, "Failed to creat file %s %s\n", file,
210 nfs_get_error(file_context->nfs));
211 free(file);
212 free(path);
213 free(server);
214 free_file_context(file_context);
215 return NULL;
216 }
217 }
218
219 free(file);
220 free(path);
221 free(server);
222
223 return file_context;
224}
225
226#define BUFSIZE 1024*1024
227static char buf[BUFSIZE];
228
229int main(int argc, char *argv[])
230{
231 int ret;
232 struct stat st;
233 struct file_context *src;
234 struct file_context *dst;
235 uint64_t off;
236 int64_t count;
237
238#ifdef WIN32
239 if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
240 printf("Failed to start Winsock2\n");
241 return 10;
242 }
243#endif
244
245#ifdef AROS
246 aros_init_socket();
247#endif
248
249 if (argc != 3) {
250 usage();
251 }
252
253 src = open_file(argv[1], O_RDONLY);
254 if (src == NULL) {
255 fprintf(stderr, "Failed to open %s\n", argv[1]);
256 return 10;
257 }
258
259 dst = open_file(argv[2], O_WRONLY|O_CREAT|O_TRUNC);
260 if (dst == NULL) {
261 fprintf(stderr, "Failed to open %s\n", argv[2]);
262 free_file_context(src);
263 return 10;
264 }
265
266 if (fstat_file(src, &st) != 0) {
267 fprintf(stderr, "Failed to fstat source file\n");
268 free_file_context(src);
269 free_file_context(dst);
270 return 10;
271 }
272
273 off = 0;
274 while (off < st.st_size) {
275 count = st.st_size - off;
276 if (count > BUFSIZE) {
277 count = BUFSIZE;
278 }
279 count = file_pread(src, buf, count, off);
280 if (count < 0) {
281 fprintf(stderr, "Failed to read from source file\n");
282 free_file_context(src);
283 free_file_context(dst);
284 return 10;
285 }
286 count = file_pwrite(dst, buf, count, off);
287 if (count < 0) {
288 fprintf(stderr, "Failed to write to dest file\n");
289 free_file_context(src);
290 free_file_context(dst);
291 return 10;
292 }
293
294 off += count;
295 }
296 printf("copied %d bytes\n", (int)count);
297
298 free_file_context(src);
299 free_file_context(dst);
300
301 return 0;
302}