Redo the buffer handling for input buffers and make sure we only read one PDU at...
[deb_libnfs.git] / examples / nfsclient-sync.c
CommitLineData
84004dbf
RS
1/*
2 Copyright (C) by Ronnie Sahlberg <ronniesahlberg@gmail.com> 2010
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/* Example program using the highlevel sync interface
19 */
20
21#define SERVER "10.1.1.27"
22#define EXPORT "/VIRTUAL"
23#define NFSFILE "/BOOKS/Classics/Dracula.djvu"
24#define NFSFILER "/BOOKS/Classics/Dracula.djvu.renamed"
25#define NFSFILEW "/BOOKS/Classics/foo"
26#define NFSDIR "/BOOKS/Classics/"
27
28#include <stdio.h>
29#include <stdlib.h>
30#include <stdint.h>
31#include <sys/types.h>
32#include <sys/stat.h>
33#include <sys/statvfs.h>
34#include <unistd.h>
35#include <fcntl.h>
36#include "libnfs.h"
37#include <rpc/rpc.h> /* for authunix_create() */
df5af25f
RS
38#include "libnfs-raw.h"
39#include "libnfs-raw-mount.h"
84004dbf
RS
40
41struct client {
42 char *server;
43 char *export;
44 uint32_t mount_port;
45 int is_finished;
46};
47
48
cdb19ec1
RS
49char buf[2*1024*1024];
50
7d0397cf 51int main(int argc _U_, char *argv[] _U_)
84004dbf
RS
52{
53 struct nfs_context *nfs;
54 int i, ret;
55 struct client client;
56 struct stat st;
57 struct nfsfh *nfsfh;
58 struct nfsdir *nfsdir;
59 struct nfsdirent *nfsdirent;
60 client.server = SERVER;
61 client.export = EXPORT;
62 client.is_finished = 0;
84004dbf
RS
63 off_t offset;
64 struct statvfs svfs;
df5af25f 65 exports export, tmp;
df5af25f
RS
66
67 printf("exports on server %s\n", SERVER);
e210bd2a 68 export = mount_getexports(SERVER);
df5af25f
RS
69 tmp = export;
70 while (tmp != NULL) {
71 printf("Export: %s\n", tmp->ex_dir);
72 tmp = tmp->ex_next;
73 }
74 mount_free_export_list(export);
df5af25f 75
84004dbf
RS
76
77 nfs = nfs_init_context();
78 if (nfs == NULL) {
79 printf("failed to init context\n");
80 exit(10);
81 }
82
e2ba5764 83 ret = nfs_mount(nfs, client.server, client.export);
84004dbf
RS
84 if (ret != 0) {
85 printf("Failed to mount nfs share : %s\n", nfs_get_error(nfs));
86 exit(10);
87 }
88 printf("mounted share successfully\n");
89
90
e2ba5764 91 ret = nfs_stat(nfs, NFSFILE, &st);
84004dbf
RS
92 if (ret != 0) {
93 printf("Failed to stat(%s) %s\n", NFSFILE, nfs_get_error(nfs));
94 exit(10);
95 }
96 printf("Mode %04o\n", st.st_mode);
97 printf("Size %d\n", (int)st.st_size);
98 printf("Inode %04o\n", (int)st.st_ino);
99
e2ba5764 100 ret = nfs_open(nfs, NFSFILE, O_RDONLY, &nfsfh);
84004dbf
RS
101 if (ret != 0) {
102 printf("Failed to open(%s) %s\n", NFSFILE, nfs_get_error(nfs));
103 exit(10);
104 }
105
cdb19ec1 106#if 0
e2ba5764 107 ret = nfs_read(nfs, nfsfh, 16, buf);
84004dbf
RS
108 if (ret < 0) {
109 printf("Failed to pread(%s) %s\n", NFSFILE, nfs_get_error(nfs));
110 exit(10);
111 }
112 printf("read %d bytes\n", ret);
113 for (i=0;i<16;i++) {
114 printf("%02x ", buf[i]&0xff);
115 }
116 printf("\n");
cdb19ec1
RS
117#endif
118 ret = nfs_read(nfs, nfsfh, sizeof(buf), buf);
84004dbf
RS
119 if (ret < 0) {
120 printf("Failed to pread(%s) %s\n", NFSFILE, nfs_get_error(nfs));
121 exit(10);
122 }
123 printf("read %d bytes\n", ret);
124 for (i=0;i<16;i++) {
125 printf("%02x ", buf[i]&0xff);
126 }
127 printf("\n");
128
e2ba5764 129 ret = (int)nfs_lseek(nfs, nfsfh, 0, SEEK_CUR, &offset);
84004dbf
RS
130 if (ret < 0) {
131 printf("Failed to lseek(%s) %s\n", NFSFILE, nfs_get_error(nfs));
132 exit(10);
133 }
134 printf("File position is %d\n", (int)offset);
135
136 printf("seek to end of file\n");
e2ba5764 137 ret = (int)nfs_lseek(nfs, nfsfh, 0, SEEK_END, &offset);
84004dbf
RS
138 if (ret < 0) {
139 printf("Failed to lseek(%s) %s\n", NFSFILE, nfs_get_error(nfs));
140 exit(10);
141 }
142 printf("File position is %d\n", (int)offset);
143
e2ba5764 144 ret = nfs_fstat(nfs, nfsfh, &st);
84004dbf
RS
145 if (ret != 0) {
146 printf("Failed to stat(%s) %s\n", NFSFILE, nfs_get_error(nfs));
147 exit(10);
148 }
149 printf("Mode %04o\n", st.st_mode);
150 printf("Size %d\n", (int)st.st_size);
151 printf("Inode %04o\n", (int)st.st_ino);
152
153
e2ba5764 154 ret = nfs_close(nfs, nfsfh);
84004dbf 155 if (ret < 0) {
7d0397cf 156 printf("Failed to close(%s): %s\n", NFSFILE, nfs_get_error(nfs));
84004dbf
RS
157 exit(10);
158 }
159
e2ba5764 160 ret = nfs_opendir(nfs, NFSDIR, &nfsdir);
84004dbf
RS
161 if (ret != 0) {
162 printf("Failed to open(%s) %s\n", NFSFILE, nfs_get_error(nfs));
163 exit(10);
164 }
165 while((nfsdirent = nfs_readdir(nfs, nfsdir)) != NULL) {
166 printf("Inode:%d Name:%s\n", (int)nfsdirent->inode, nfsdirent->name);
167 }
168 nfs_closedir(nfs, nfsdir);
169
170
e2ba5764 171 ret = nfs_open(nfs, NFSFILEW, O_WRONLY, &nfsfh);
84004dbf
RS
172 if (ret != 0) {
173 printf("Failed to open(%s) %s\n", NFSFILEW, nfs_get_error(nfs));
174 exit(10);
175 }
e2ba5764 176 ret = nfs_pwrite(nfs, nfsfh, 0, 16, buf);
84004dbf
RS
177 if (ret < 0) {
178 printf("Failed to pwrite(%s) %s\n", NFSFILEW, nfs_get_error(nfs));
179 exit(10);
180 }
e2ba5764 181 ret = nfs_fsync(nfs, nfsfh);
84004dbf
RS
182 if (ret < 0) {
183 printf("Failed to fsync(%s) %s\n", NFSFILEW, nfs_get_error(nfs));
184 exit(10);
185 }
e2ba5764 186 ret = nfs_close(nfs, nfsfh);
84004dbf 187 if (ret < 0) {
7d0397cf 188 printf("Failed to close(%s) %s\n", NFSFILEW, nfs_get_error(nfs));
84004dbf
RS
189 exit(10);
190 }
191
192
e2ba5764 193 ret = nfs_statvfs(nfs, NFSDIR, &svfs);
84004dbf 194 if (ret < 0) {
7d0397cf 195 printf("Failed to statvfs(%s) %s\n", NFSDIR, nfs_get_error(nfs));
84004dbf
RS
196 exit(10);
197 }
198 printf("files %d/%d/%d\n", (int)svfs.f_files, (int)svfs.f_ffree, (int)svfs.f_favail);
199
200
e2ba5764 201 ret = nfs_access(nfs, NFSFILE, R_OK);
84004dbf
RS
202 if (ret != 0) {
203 printf("Failed to access(%s) %s\n", NFSFILE, nfs_get_error(nfs));
204 }
205
206 /* become root */
207 nfs_set_auth(nfs, authunix_create("Ronnies-Laptop", 0, 0, 0, NULL));
208
e2ba5764 209 ret = nfs_link(nfs, NFSFILE, NFSFILER);
84004dbf
RS
210 if (ret != 0) {
211 printf("Failed to link(%s) %s\n", NFSFILE, nfs_get_error(nfs));
212 }
213
214
215 nfs_destroy_context(nfs);
216 printf("nfsclient finished\n");
217 return 0;
218}
df5af25f 219