add RPC definition for the portmapper/callit function
[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"
e4a5ba42 23#define NFSFILE "/BOOKS/Classics/Dracula.djvu.truncated"
84004dbf
RS
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
e4a5ba42 49char buf[5*1024*1024];
cdb19ec1 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 66
e210bd2a 67 export = mount_getexports(SERVER);
739df145
RS
68 if (export != NULL) {
69 printf("exports on server %s\n", SERVER);
70 tmp = export;
71 while (tmp != NULL) {
72 printf("Export: %s\n", tmp->ex_dir);
73 tmp = tmp->ex_next;
74 }
df5af25f 75
739df145
RS
76 mount_free_export_list(export);
77 } else {
78 printf("no exports on server %s\n", SERVER);
79 }
84004dbf
RS
80
81 nfs = nfs_init_context();
82 if (nfs == NULL) {
83 printf("failed to init context\n");
84 exit(10);
85 }
86
e2ba5764 87 ret = nfs_mount(nfs, client.server, client.export);
84004dbf
RS
88 if (ret != 0) {
89 printf("Failed to mount nfs share : %s\n", nfs_get_error(nfs));
90 exit(10);
91 }
1e8994af 92 printf("mounted share successfully %s\n", nfs_get_error(nfs));
84004dbf
RS
93
94
e2ba5764 95 ret = nfs_stat(nfs, NFSFILE, &st);
84004dbf
RS
96 if (ret != 0) {
97 printf("Failed to stat(%s) %s\n", NFSFILE, nfs_get_error(nfs));
98 exit(10);
99 }
100 printf("Mode %04o\n", st.st_mode);
101 printf("Size %d\n", (int)st.st_size);
102 printf("Inode %04o\n", (int)st.st_ino);
103
e2ba5764 104 ret = nfs_open(nfs, NFSFILE, O_RDONLY, &nfsfh);
84004dbf
RS
105 if (ret != 0) {
106 printf("Failed to open(%s) %s\n", NFSFILE, nfs_get_error(nfs));
107 exit(10);
108 }
109
cdb19ec1 110#if 0
e2ba5764 111 ret = nfs_read(nfs, nfsfh, 16, buf);
84004dbf
RS
112 if (ret < 0) {
113 printf("Failed to pread(%s) %s\n", NFSFILE, nfs_get_error(nfs));
114 exit(10);
115 }
116 printf("read %d bytes\n", ret);
117 for (i=0;i<16;i++) {
118 printf("%02x ", buf[i]&0xff);
119 }
120 printf("\n");
cdb19ec1
RS
121#endif
122 ret = nfs_read(nfs, nfsfh, sizeof(buf), buf);
84004dbf
RS
123 if (ret < 0) {
124 printf("Failed to pread(%s) %s\n", NFSFILE, nfs_get_error(nfs));
125 exit(10);
126 }
127 printf("read %d bytes\n", ret);
128 for (i=0;i<16;i++) {
129 printf("%02x ", buf[i]&0xff);
130 }
131 printf("\n");
132
e2ba5764 133 ret = (int)nfs_lseek(nfs, nfsfh, 0, SEEK_CUR, &offset);
84004dbf
RS
134 if (ret < 0) {
135 printf("Failed to lseek(%s) %s\n", NFSFILE, nfs_get_error(nfs));
136 exit(10);
137 }
138 printf("File position is %d\n", (int)offset);
139
140 printf("seek to end of file\n");
e2ba5764 141 ret = (int)nfs_lseek(nfs, nfsfh, 0, SEEK_END, &offset);
84004dbf
RS
142 if (ret < 0) {
143 printf("Failed to lseek(%s) %s\n", NFSFILE, nfs_get_error(nfs));
144 exit(10);
145 }
146 printf("File position is %d\n", (int)offset);
147
e2ba5764 148 ret = nfs_fstat(nfs, nfsfh, &st);
84004dbf
RS
149 if (ret != 0) {
150 printf("Failed to stat(%s) %s\n", NFSFILE, nfs_get_error(nfs));
151 exit(10);
152 }
153 printf("Mode %04o\n", st.st_mode);
154 printf("Size %d\n", (int)st.st_size);
155 printf("Inode %04o\n", (int)st.st_ino);
156
157
e2ba5764 158 ret = nfs_close(nfs, nfsfh);
84004dbf 159 if (ret < 0) {
7d0397cf 160 printf("Failed to close(%s): %s\n", NFSFILE, nfs_get_error(nfs));
84004dbf
RS
161 exit(10);
162 }
163
e2ba5764 164 ret = nfs_opendir(nfs, NFSDIR, &nfsdir);
84004dbf
RS
165 if (ret != 0) {
166 printf("Failed to open(%s) %s\n", NFSFILE, nfs_get_error(nfs));
167 exit(10);
168 }
169 while((nfsdirent = nfs_readdir(nfs, nfsdir)) != NULL) {
170 printf("Inode:%d Name:%s\n", (int)nfsdirent->inode, nfsdirent->name);
171 }
172 nfs_closedir(nfs, nfsdir);
173
174
e2ba5764 175 ret = nfs_open(nfs, NFSFILEW, O_WRONLY, &nfsfh);
84004dbf
RS
176 if (ret != 0) {
177 printf("Failed to open(%s) %s\n", NFSFILEW, nfs_get_error(nfs));
178 exit(10);
179 }
e2ba5764 180 ret = nfs_pwrite(nfs, nfsfh, 0, 16, buf);
84004dbf
RS
181 if (ret < 0) {
182 printf("Failed to pwrite(%s) %s\n", NFSFILEW, nfs_get_error(nfs));
183 exit(10);
184 }
e2ba5764 185 ret = nfs_fsync(nfs, nfsfh);
84004dbf
RS
186 if (ret < 0) {
187 printf("Failed to fsync(%s) %s\n", NFSFILEW, nfs_get_error(nfs));
188 exit(10);
189 }
e2ba5764 190 ret = nfs_close(nfs, nfsfh);
84004dbf 191 if (ret < 0) {
7d0397cf 192 printf("Failed to close(%s) %s\n", NFSFILEW, nfs_get_error(nfs));
84004dbf
RS
193 exit(10);
194 }
195
196
e2ba5764 197 ret = nfs_statvfs(nfs, NFSDIR, &svfs);
84004dbf 198 if (ret < 0) {
7d0397cf 199 printf("Failed to statvfs(%s) %s\n", NFSDIR, nfs_get_error(nfs));
84004dbf
RS
200 exit(10);
201 }
202 printf("files %d/%d/%d\n", (int)svfs.f_files, (int)svfs.f_ffree, (int)svfs.f_favail);
203
204
e2ba5764 205 ret = nfs_access(nfs, NFSFILE, R_OK);
84004dbf
RS
206 if (ret != 0) {
207 printf("Failed to access(%s) %s\n", NFSFILE, nfs_get_error(nfs));
208 }
209
210 /* become root */
211 nfs_set_auth(nfs, authunix_create("Ronnies-Laptop", 0, 0, 0, NULL));
212
e2ba5764 213 ret = nfs_link(nfs, NFSFILE, NFSFILER);
84004dbf
RS
214 if (ret != 0) {
215 printf("Failed to link(%s) %s\n", NFSFILE, nfs_get_error(nfs));
216 }
217
218
219 nfs_destroy_context(nfs);
220 printf("nfsclient finished\n");
221 return 0;
222}
df5af25f 223