fix crash in mount/export
[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.truncated"
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 char buf[5*1024*1024];
50
51 int main(int argc _U_, char *argv[] _U_)
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;
63 off_t offset;
64 struct statvfs svfs;
65 exports export, tmp;
66
67 export = mount_getexports(SERVER);
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 }
75
76 mount_free_export_list(export);
77 } else {
78 printf("no exports on server %s\n", SERVER);
79 }
80
81 nfs = nfs_init_context();
82 if (nfs == NULL) {
83 printf("failed to init context\n");
84 exit(10);
85 }
86
87 ret = nfs_mount(nfs, client.server, client.export);
88 if (ret != 0) {
89 printf("Failed to mount nfs share : %s\n", nfs_get_error(nfs));
90 exit(10);
91 }
92 printf("mounted share successfully %s\n", nfs_get_error(nfs));
93
94
95 ret = nfs_stat(nfs, NFSFILE, &st);
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
104 ret = nfs_open(nfs, NFSFILE, O_RDONLY, &nfsfh);
105 if (ret != 0) {
106 printf("Failed to open(%s) %s\n", NFSFILE, nfs_get_error(nfs));
107 exit(10);
108 }
109
110 #if 0
111 ret = nfs_read(nfs, nfsfh, 16, buf);
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");
121 #endif
122 ret = nfs_read(nfs, nfsfh, sizeof(buf), buf);
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
133 ret = (int)nfs_lseek(nfs, nfsfh, 0, SEEK_CUR, &offset);
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");
141 ret = (int)nfs_lseek(nfs, nfsfh, 0, SEEK_END, &offset);
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
148 ret = nfs_fstat(nfs, nfsfh, &st);
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
158 ret = nfs_close(nfs, nfsfh);
159 if (ret < 0) {
160 printf("Failed to close(%s): %s\n", NFSFILE, nfs_get_error(nfs));
161 exit(10);
162 }
163
164 ret = nfs_opendir(nfs, NFSDIR, &nfsdir);
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
175 ret = nfs_open(nfs, NFSFILEW, O_WRONLY, &nfsfh);
176 if (ret != 0) {
177 printf("Failed to open(%s) %s\n", NFSFILEW, nfs_get_error(nfs));
178 exit(10);
179 }
180 ret = nfs_pwrite(nfs, nfsfh, 0, 16, buf);
181 if (ret < 0) {
182 printf("Failed to pwrite(%s) %s\n", NFSFILEW, nfs_get_error(nfs));
183 exit(10);
184 }
185 ret = nfs_fsync(nfs, nfsfh);
186 if (ret < 0) {
187 printf("Failed to fsync(%s) %s\n", NFSFILEW, nfs_get_error(nfs));
188 exit(10);
189 }
190 ret = nfs_close(nfs, nfsfh);
191 if (ret < 0) {
192 printf("Failed to close(%s) %s\n", NFSFILEW, nfs_get_error(nfs));
193 exit(10);
194 }
195
196
197 ret = nfs_statvfs(nfs, NFSDIR, &svfs);
198 if (ret < 0) {
199 printf("Failed to statvfs(%s) %s\n", NFSDIR, nfs_get_error(nfs));
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
205 ret = nfs_access(nfs, NFSFILE, R_OK);
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
213 ret = nfs_link(nfs, NFSFILE, NFSFILER);
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 }
223