2 Copyright (C) by Ronnie Sahlberg <ronniesahlberg@gmail.com> 2010
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.
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.
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/>.
18 /* Example program using the highlevel sync interface
21 #define SERVER "10.1.1.27"
22 #define EXPORT "/VIRTUAL"
23 #define NFSFILE "/BOOKS/Classics/Dracula.djvu.truncated"
24 #define NFSFILER "/BOOKS/Classics/Dracula.djvu.renamed"
25 #define NFSFILEW "/BOOKS/Classics/foo"
26 #define NFSDIR "/BOOKS/Classics/"
33 #pragma comment(lib, "ws2_32.lib")
36 #include <sys/statvfs.h>
43 #include <sys/types.h>
47 #include <rpc/rpc.h> /* for authunix_create() */
48 #include "libnfs-raw.h"
49 #include "libnfs-raw-mount.h"
59 char buf
[3*1024*1024+337];
61 int main(int argc _U_
, char *argv
[] _U_
)
63 struct nfs_context
*nfs
;
69 struct nfsdir
*nfsdir
;
70 struct nfsdirent
*nfsdirent
;
75 if (WSAStartup(MAKEWORD(2,2), &wsaData
) != 0) {
76 printf("Failed to start Winsock2\n");
81 client
.server
= SERVER
;
82 client
.export
= EXPORT
;
83 client
.is_finished
= 0;
84 export
= mount_getexports(SERVER
);
86 printf("exports on server %s\n", SERVER
);
89 printf("Export: %s\n", tmp
->ex_dir
);
93 mount_free_export_list(export
);
95 printf("no exports on server %s\n", SERVER
);
98 nfs
= nfs_init_context();
100 printf("failed to init context\n");
104 ret
= nfs_mount(nfs
, client
.server
, client
.export
);
106 printf("Failed to mount nfs share : %s\n", nfs_get_error(nfs
));
109 printf("mounted share successfully %s\n", nfs_get_error(nfs
));
112 ret
= nfs_stat(nfs
, NFSFILE
, &st
);
114 printf("Failed to stat(%s) %s\n", NFSFILE
, nfs_get_error(nfs
));
117 printf("Mode %04o\n", st
.st_mode
);
118 printf("Size %d\n", (int)st
.st_size
);
119 printf("Inode %04o\n", (int)st
.st_ino
);
121 ret
= nfs_open(nfs
, NFSFILE
, O_RDONLY
, &nfsfh
);
123 printf("Failed to open(%s) %s\n", NFSFILE
, nfs_get_error(nfs
));
128 ret
= nfs_read(nfs
, nfsfh
, 16, buf
);
130 printf("Failed to pread(%s) %s\n", NFSFILE
, nfs_get_error(nfs
));
133 printf("read %d bytes\n", ret
);
135 printf("%02x ", buf
[i
]&0xff);
139 ret
= nfs_read(nfs
, nfsfh
, sizeof(buf
), buf
);
141 printf("Failed to pread(%s) %s\n", NFSFILE
, nfs_get_error(nfs
));
144 printf("read %d bytes\n", ret
);
146 printf("%02x ", buf
[i
]&0xff);
149 ret
= nfs_read(nfs
, nfsfh
, sizeof(buf
), buf
);
151 printf("Failed to pread(%s) %s\n", NFSFILE
, nfs_get_error(nfs
));
154 printf("read %d bytes\n", ret
);
155 ret
= nfs_read(nfs
, nfsfh
, sizeof(buf
), buf
);
157 printf("Failed to pread(%s) %s\n", NFSFILE
, nfs_get_error(nfs
));
160 printf("read %d bytes\n", ret
);
161 ret
= nfs_read(nfs
, nfsfh
, sizeof(buf
), buf
);
163 printf("Failed to pread(%s) %s\n", NFSFILE
, nfs_get_error(nfs
));
166 printf("read %d bytes\n", ret
);
167 ret
= nfs_read(nfs
, nfsfh
, sizeof(buf
), buf
);
169 printf("Failed to pread(%s) %s\n", NFSFILE
, nfs_get_error(nfs
));
172 printf("read %d bytes\n", ret
);
173 ret
= nfs_read(nfs
, nfsfh
, sizeof(buf
), buf
);
175 printf("Failed to pread(%s) %s\n", NFSFILE
, nfs_get_error(nfs
));
178 printf("read %d bytes\n", ret
);
180 ret
= (int)nfs_lseek(nfs
, nfsfh
, 0, SEEK_CUR
, &offset
);
182 printf("Failed to lseek(%s) %s\n", NFSFILE
, nfs_get_error(nfs
));
185 printf("File position is %d\n", (int)offset
);
187 printf("seek to end of file\n");
188 ret
= (int)nfs_lseek(nfs
, nfsfh
, 0, SEEK_END
, &offset
);
190 printf("Failed to lseek(%s) %s\n", NFSFILE
, nfs_get_error(nfs
));
193 printf("File position is %d\n", (int)offset
);
195 ret
= nfs_fstat(nfs
, nfsfh
, &st
);
197 printf("Failed to stat(%s) %s\n", NFSFILE
, nfs_get_error(nfs
));
200 printf("Mode %04o\n", st
.st_mode
);
201 printf("Size %d\n", (int)st
.st_size
);
202 printf("Inode %04o\n", (int)st
.st_ino
);
205 ret
= nfs_close(nfs
, nfsfh
);
207 printf("Failed to close(%s): %s\n", NFSFILE
, nfs_get_error(nfs
));
211 ret
= nfs_opendir(nfs
, NFSDIR
, &nfsdir
);
213 printf("Failed to open(%s) %s\n", NFSFILE
, nfs_get_error(nfs
));
216 while((nfsdirent
= nfs_readdir(nfs
, nfsdir
)) != NULL
) {
218 printf("Inode:%d Name:%s ", (int)nfsdirent
->inode
, nfsdirent
->name
);
219 sprintf(&filename
, "%s/%s", NFSDIR
, nfsdirent
->name
);
220 ret
= nfs_open(nfs
, filename
, O_RDONLY
, &nfsfh
);
222 printf("Failed to open(%s) %s\n", filename
, nfs_get_error(nfs
));
225 ret
= nfs_read(nfs
, nfsfh
, sizeof(buf
), buf
);
227 printf("Error reading file\n");
230 printf("Read %d bytes\n", ret
);
231 ret
= nfs_close(nfs
, nfsfh
);
233 printf("Failed to close(%s): %s\n", NFSFILE
, nfs_get_error(nfs
));
237 nfs_closedir(nfs
, nfsdir
);
240 ret
= nfs_open(nfs
, NFSFILEW
, O_WRONLY
, &nfsfh
);
242 printf("Failed to open(%s) %s\n", NFSFILEW
, nfs_get_error(nfs
));
245 ret
= nfs_pwrite(nfs
, nfsfh
, 0, 16, buf
);
247 printf("Failed to pwrite(%s) %s\n", NFSFILEW
, nfs_get_error(nfs
));
250 ret
= nfs_fsync(nfs
, nfsfh
);
252 printf("Failed to fsync(%s) %s\n", NFSFILEW
, nfs_get_error(nfs
));
255 ret
= nfs_close(nfs
, nfsfh
);
257 printf("Failed to close(%s) %s\n", NFSFILEW
, nfs_get_error(nfs
));
262 ret
= nfs_statvfs(nfs
, NFSDIR
, &svfs
);
264 printf("Failed to statvfs(%s) %s\n", NFSDIR
, nfs_get_error(nfs
));
267 printf("files %d/%d/%d\n", (int)svfs
.f_files
, (int)svfs
.f_ffree
, (int)svfs
.f_favail
);
270 ret
= nfs_access(nfs
, NFSFILE
, R_OK
);
272 printf("Failed to access(%s) %s\n", NFSFILE
, nfs_get_error(nfs
));
276 nfs_set_auth(nfs
, authunix_create("Ronnies-Laptop", 0, 0, 0, NULL
));
278 ret
= nfs_link(nfs
, NFSFILE
, NFSFILER
);
280 printf("Failed to link(%s) %s\n", NFSFILE
, nfs_get_error(nfs
));
284 nfs_destroy_context(nfs
);
285 printf("nfsclient finished\n");