add a sync function to get the export list from a server and add an example of how...
[deb_libnfs.git] / examples / nfsclient-sync.c
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() */
38 #include "libnfs-raw.h"
39 #include "libnfs-raw-mount.h"
40
41 struct client {
42 char *server;
43 char *export;
44 uint32_t mount_port;
45 int is_finished;
46 };
47
48
49 int main(int argc _U_, char *argv[] _U_)
50 {
51 struct nfs_context *nfs;
52 int i, ret;
53 struct client client;
54 struct stat st;
55 struct nfsfh *nfsfh;
56 struct nfsdir *nfsdir;
57 struct nfsdirent *nfsdirent;
58 client.server = SERVER;
59 client.export = EXPORT;
60 client.is_finished = 0;
61 char buf[16];
62 off_t offset;
63 struct statvfs svfs;
64 exports export, tmp;
65 struct rpc_context *mount_context;
66
67 printf("exports on server %s\n", SERVER);
68 mount_context = rpc_init_context();
69 export = mount_getexports(mount_context, SERVER);
70 tmp = export;
71 while (tmp != NULL) {
72 printf("Export: %s\n", tmp->ex_dir);
73 tmp = tmp->ex_next;
74 }
75 mount_free_export_list(export);
76 rpc_destroy_context(mount_context);
77
78
79
80 nfs = nfs_init_context();
81 if (nfs == NULL) {
82 printf("failed to init context\n");
83 exit(10);
84 }
85
86 ret = nfs_mount(nfs, client.server, client.export);
87 if (ret != 0) {
88 printf("Failed to mount nfs share : %s\n", nfs_get_error(nfs));
89 exit(10);
90 }
91 printf("mounted share successfully\n");
92
93
94 ret = nfs_stat(nfs, NFSFILE, &st);
95 if (ret != 0) {
96 printf("Failed to stat(%s) %s\n", NFSFILE, nfs_get_error(nfs));
97 exit(10);
98 }
99 printf("Mode %04o\n", st.st_mode);
100 printf("Size %d\n", (int)st.st_size);
101 printf("Inode %04o\n", (int)st.st_ino);
102
103 ret = nfs_open(nfs, NFSFILE, O_RDONLY, &nfsfh);
104 if (ret != 0) {
105 printf("Failed to open(%s) %s\n", NFSFILE, nfs_get_error(nfs));
106 exit(10);
107 }
108
109 ret = nfs_read(nfs, nfsfh, 16, buf);
110 if (ret < 0) {
111 printf("Failed to pread(%s) %s\n", NFSFILE, nfs_get_error(nfs));
112 exit(10);
113 }
114 printf("read %d bytes\n", ret);
115 for (i=0;i<16;i++) {
116 printf("%02x ", buf[i]&0xff);
117 }
118 printf("\n");
119 ret = nfs_read(nfs, nfsfh, 16, buf);
120 if (ret < 0) {
121 printf("Failed to pread(%s) %s\n", NFSFILE, nfs_get_error(nfs));
122 exit(10);
123 }
124 printf("read %d bytes\n", ret);
125 for (i=0;i<16;i++) {
126 printf("%02x ", buf[i]&0xff);
127 }
128 printf("\n");
129
130 ret = (int)nfs_lseek(nfs, nfsfh, 0, SEEK_CUR, &offset);
131 if (ret < 0) {
132 printf("Failed to lseek(%s) %s\n", NFSFILE, nfs_get_error(nfs));
133 exit(10);
134 }
135 printf("File position is %d\n", (int)offset);
136
137 printf("seek to end of file\n");
138 ret = (int)nfs_lseek(nfs, nfsfh, 0, SEEK_END, &offset);
139 if (ret < 0) {
140 printf("Failed to lseek(%s) %s\n", NFSFILE, nfs_get_error(nfs));
141 exit(10);
142 }
143 printf("File position is %d\n", (int)offset);
144
145 ret = nfs_fstat(nfs, nfsfh, &st);
146 if (ret != 0) {
147 printf("Failed to stat(%s) %s\n", NFSFILE, nfs_get_error(nfs));
148 exit(10);
149 }
150 printf("Mode %04o\n", st.st_mode);
151 printf("Size %d\n", (int)st.st_size);
152 printf("Inode %04o\n", (int)st.st_ino);
153
154
155 ret = nfs_close(nfs, nfsfh);
156 if (ret < 0) {
157 printf("Failed to close(%s): %s\n", NFSFILE, nfs_get_error(nfs));
158 exit(10);
159 }
160
161 ret = nfs_opendir(nfs, NFSDIR, &nfsdir);
162 if (ret != 0) {
163 printf("Failed to open(%s) %s\n", NFSFILE, nfs_get_error(nfs));
164 exit(10);
165 }
166 while((nfsdirent = nfs_readdir(nfs, nfsdir)) != NULL) {
167 printf("Inode:%d Name:%s\n", (int)nfsdirent->inode, nfsdirent->name);
168 }
169 nfs_closedir(nfs, nfsdir);
170
171
172 ret = nfs_open(nfs, NFSFILEW, O_WRONLY, &nfsfh);
173 if (ret != 0) {
174 printf("Failed to open(%s) %s\n", NFSFILEW, nfs_get_error(nfs));
175 exit(10);
176 }
177 ret = nfs_pwrite(nfs, nfsfh, 0, 16, buf);
178 if (ret < 0) {
179 printf("Failed to pwrite(%s) %s\n", NFSFILEW, nfs_get_error(nfs));
180 exit(10);
181 }
182 ret = nfs_fsync(nfs, nfsfh);
183 if (ret < 0) {
184 printf("Failed to fsync(%s) %s\n", NFSFILEW, nfs_get_error(nfs));
185 exit(10);
186 }
187 ret = nfs_close(nfs, nfsfh);
188 if (ret < 0) {
189 printf("Failed to close(%s) %s\n", NFSFILEW, nfs_get_error(nfs));
190 exit(10);
191 }
192
193
194 ret = nfs_statvfs(nfs, NFSDIR, &svfs);
195 if (ret < 0) {
196 printf("Failed to statvfs(%s) %s\n", NFSDIR, nfs_get_error(nfs));
197 exit(10);
198 }
199 printf("files %d/%d/%d\n", (int)svfs.f_files, (int)svfs.f_ffree, (int)svfs.f_favail);
200
201
202 ret = nfs_access(nfs, NFSFILE, R_OK);
203 if (ret != 0) {
204 printf("Failed to access(%s) %s\n", NFSFILE, nfs_get_error(nfs));
205 }
206
207 /* become root */
208 nfs_set_auth(nfs, authunix_create("Ronnies-Laptop", 0, 0, 0, NULL));
209
210 ret = nfs_link(nfs, NFSFILE, NFSFILER);
211 if (ret != 0) {
212 printf("Failed to link(%s) %s\n", NFSFILE, nfs_get_error(nfs));
213 }
214
215
216 nfs_destroy_context(nfs);
217 printf("nfsclient finished\n");
218 return 0;
219 }
220