Commit | Line | Data |
---|---|---|
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 | */ | |
a8a1b858 M |
20 | #ifdef WIN32 |
21 | #include "win32_compat.h" | |
22 | #else | |
23 | #include <fcntl.h> | |
24 | #include <sys/stat.h> | |
25 | #include <sys/statvfs.h> | |
26 | #endif | |
27 | ||
84004dbf RS |
28 | #define SERVER "10.1.1.27" |
29 | #define EXPORT "/VIRTUAL" | |
e4a5ba42 | 30 | #define NFSFILE "/BOOKS/Classics/Dracula.djvu.truncated" |
84004dbf RS |
31 | #define NFSFILER "/BOOKS/Classics/Dracula.djvu.renamed" |
32 | #define NFSFILEW "/BOOKS/Classics/foo" | |
33 | #define NFSDIR "/BOOKS/Classics/" | |
34 | ||
57187d21 | 35 | #define _GNU_SOURCE |
eecdc4f3 RS |
36 | |
37 | #if defined(WIN32) | |
eecdc4f3 RS |
38 | #pragma comment(lib, "ws2_32.lib") |
39 | WSADATA wsaData; | |
40 | #else | |
41 | #include <sys/statvfs.h> | |
42 | #include <unistd.h> | |
43 | #endif | |
44 | ||
84004dbf RS |
45 | #include <stdio.h> |
46 | #include <stdlib.h> | |
47 | #include <stdint.h> | |
48 | #include <sys/types.h> | |
49 | #include <sys/stat.h> | |
84004dbf RS |
50 | #include <fcntl.h> |
51 | #include "libnfs.h" | |
52 | #include <rpc/rpc.h> /* for authunix_create() */ | |
df5af25f RS |
53 | #include "libnfs-raw.h" |
54 | #include "libnfs-raw-mount.h" | |
84004dbf RS |
55 | |
56 | struct client { | |
57 | char *server; | |
58 | char *export; | |
59 | uint32_t mount_port; | |
60 | int is_finished; | |
61 | }; | |
62 | ||
63 | ||
e078d276 | 64 | void PrintServerList() |
65 | { | |
66 | struct nfs_server_list *srvrs; | |
67 | struct nfs_server_list *srv; | |
68 | ||
69 | srvrs = nfs_find_local_servers(); | |
70 | ||
71 | for (srv=srvrs; srv; srv = srv->next) | |
72 | { | |
73 | printf("Found nfs server: %s\n", srv->addr); | |
74 | ||
75 | } | |
76 | free_nfs_srvr_list(srvrs); | |
77 | } | |
78 | ||
57187d21 | 79 | char buf[3*1024*1024+337]; |
cdb19ec1 | 80 | |
7d0397cf | 81 | int main(int argc _U_, char *argv[] _U_) |
84004dbf RS |
82 | { |
83 | struct nfs_context *nfs; | |
84 | int i, ret; | |
183451cf | 85 | uint64_t offset; |
84004dbf RS |
86 | struct client client; |
87 | struct stat st; | |
88 | struct nfsfh *nfsfh; | |
89 | struct nfsdir *nfsdir; | |
90 | struct nfsdirent *nfsdirent; | |
84004dbf | 91 | struct statvfs svfs; |
df5af25f | 92 | exports export, tmp; |
df5af25f | 93 | |
eecdc4f3 RS |
94 | #if defined(WIN32) |
95 | if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) { | |
96 | printf("Failed to start Winsock2\n"); | |
97 | exit(10); | |
98 | } | |
99 | #endif | |
100 | ||
101 | client.server = SERVER; | |
102 | client.export = EXPORT; | |
103 | client.is_finished = 0; | |
e078d276 | 104 | |
105 | PrintServerList(); | |
106 | ||
e210bd2a | 107 | export = mount_getexports(SERVER); |
739df145 RS |
108 | if (export != NULL) { |
109 | printf("exports on server %s\n", SERVER); | |
110 | tmp = export; | |
111 | while (tmp != NULL) { | |
112 | printf("Export: %s\n", tmp->ex_dir); | |
113 | tmp = tmp->ex_next; | |
114 | } | |
df5af25f | 115 | |
739df145 RS |
116 | mount_free_export_list(export); |
117 | } else { | |
118 | printf("no exports on server %s\n", SERVER); | |
119 | } | |
84004dbf RS |
120 | |
121 | nfs = nfs_init_context(); | |
122 | if (nfs == NULL) { | |
123 | printf("failed to init context\n"); | |
124 | exit(10); | |
125 | } | |
126 | ||
e2ba5764 | 127 | ret = nfs_mount(nfs, client.server, client.export); |
84004dbf RS |
128 | if (ret != 0) { |
129 | printf("Failed to mount nfs share : %s\n", nfs_get_error(nfs)); | |
130 | exit(10); | |
131 | } | |
1e8994af | 132 | printf("mounted share successfully %s\n", nfs_get_error(nfs)); |
84004dbf RS |
133 | |
134 | ||
e2ba5764 | 135 | ret = nfs_stat(nfs, NFSFILE, &st); |
84004dbf RS |
136 | if (ret != 0) { |
137 | printf("Failed to stat(%s) %s\n", NFSFILE, nfs_get_error(nfs)); | |
138 | exit(10); | |
139 | } | |
140 | printf("Mode %04o\n", st.st_mode); | |
141 | printf("Size %d\n", (int)st.st_size); | |
142 | printf("Inode %04o\n", (int)st.st_ino); | |
143 | ||
e2ba5764 | 144 | ret = nfs_open(nfs, NFSFILE, O_RDONLY, &nfsfh); |
84004dbf RS |
145 | if (ret != 0) { |
146 | printf("Failed to open(%s) %s\n", NFSFILE, nfs_get_error(nfs)); | |
147 | exit(10); | |
148 | } | |
149 | ||
cdb19ec1 | 150 | #if 0 |
e2ba5764 | 151 | ret = nfs_read(nfs, nfsfh, 16, buf); |
84004dbf RS |
152 | if (ret < 0) { |
153 | printf("Failed to pread(%s) %s\n", NFSFILE, nfs_get_error(nfs)); | |
154 | exit(10); | |
155 | } | |
156 | printf("read %d bytes\n", ret); | |
157 | for (i=0;i<16;i++) { | |
158 | printf("%02x ", buf[i]&0xff); | |
159 | } | |
160 | printf("\n"); | |
cdb19ec1 RS |
161 | #endif |
162 | ret = nfs_read(nfs, nfsfh, sizeof(buf), buf); | |
84004dbf RS |
163 | if (ret < 0) { |
164 | printf("Failed to pread(%s) %s\n", NFSFILE, nfs_get_error(nfs)); | |
165 | exit(10); | |
166 | } | |
167 | printf("read %d bytes\n", ret); | |
168 | for (i=0;i<16;i++) { | |
169 | printf("%02x ", buf[i]&0xff); | |
170 | } | |
171 | printf("\n"); | |
57187d21 RS |
172 | ret = nfs_read(nfs, nfsfh, sizeof(buf), buf); |
173 | if (ret < 0) { | |
174 | printf("Failed to pread(%s) %s\n", NFSFILE, nfs_get_error(nfs)); | |
175 | exit(10); | |
176 | } | |
177 | printf("read %d bytes\n", ret); | |
178 | ret = nfs_read(nfs, nfsfh, sizeof(buf), buf); | |
179 | if (ret < 0) { | |
180 | printf("Failed to pread(%s) %s\n", NFSFILE, nfs_get_error(nfs)); | |
181 | exit(10); | |
182 | } | |
183 | printf("read %d bytes\n", ret); | |
184 | ret = nfs_read(nfs, nfsfh, sizeof(buf), buf); | |
185 | if (ret < 0) { | |
186 | printf("Failed to pread(%s) %s\n", NFSFILE, nfs_get_error(nfs)); | |
187 | exit(10); | |
188 | } | |
189 | printf("read %d bytes\n", ret); | |
190 | ret = nfs_read(nfs, nfsfh, sizeof(buf), buf); | |
191 | if (ret < 0) { | |
192 | printf("Failed to pread(%s) %s\n", NFSFILE, nfs_get_error(nfs)); | |
193 | exit(10); | |
194 | } | |
195 | printf("read %d bytes\n", ret); | |
196 | ret = nfs_read(nfs, nfsfh, sizeof(buf), buf); | |
197 | if (ret < 0) { | |
198 | printf("Failed to pread(%s) %s\n", NFSFILE, nfs_get_error(nfs)); | |
199 | exit(10); | |
200 | } | |
201 | printf("read %d bytes\n", ret); | |
84004dbf | 202 | |
e2ba5764 | 203 | ret = (int)nfs_lseek(nfs, nfsfh, 0, SEEK_CUR, &offset); |
84004dbf RS |
204 | if (ret < 0) { |
205 | printf("Failed to lseek(%s) %s\n", NFSFILE, nfs_get_error(nfs)); | |
206 | exit(10); | |
207 | } | |
208 | printf("File position is %d\n", (int)offset); | |
209 | ||
210 | printf("seek to end of file\n"); | |
e2ba5764 | 211 | ret = (int)nfs_lseek(nfs, nfsfh, 0, SEEK_END, &offset); |
84004dbf RS |
212 | if (ret < 0) { |
213 | printf("Failed to lseek(%s) %s\n", NFSFILE, nfs_get_error(nfs)); | |
214 | exit(10); | |
215 | } | |
216 | printf("File position is %d\n", (int)offset); | |
217 | ||
e2ba5764 | 218 | ret = nfs_fstat(nfs, nfsfh, &st); |
84004dbf RS |
219 | if (ret != 0) { |
220 | printf("Failed to stat(%s) %s\n", NFSFILE, nfs_get_error(nfs)); | |
221 | exit(10); | |
222 | } | |
223 | printf("Mode %04o\n", st.st_mode); | |
224 | printf("Size %d\n", (int)st.st_size); | |
225 | printf("Inode %04o\n", (int)st.st_ino); | |
226 | ||
227 | ||
e2ba5764 | 228 | ret = nfs_close(nfs, nfsfh); |
84004dbf | 229 | if (ret < 0) { |
7d0397cf | 230 | printf("Failed to close(%s): %s\n", NFSFILE, nfs_get_error(nfs)); |
84004dbf RS |
231 | exit(10); |
232 | } | |
233 | ||
e2ba5764 | 234 | ret = nfs_opendir(nfs, NFSDIR, &nfsdir); |
84004dbf RS |
235 | if (ret != 0) { |
236 | printf("Failed to open(%s) %s\n", NFSFILE, nfs_get_error(nfs)); | |
237 | exit(10); | |
238 | } | |
239 | while((nfsdirent = nfs_readdir(nfs, nfsdir)) != NULL) { | |
eecdc4f3 | 240 | char filename[1024]; |
57187d21 | 241 | printf("Inode:%d Name:%s ", (int)nfsdirent->inode, nfsdirent->name); |
dba24f10 | 242 | sprintf(filename, "%s/%s", NFSDIR, nfsdirent->name); |
57187d21 | 243 | ret = nfs_open(nfs, filename, O_RDONLY, &nfsfh); |
57187d21 RS |
244 | if (ret != 0) { |
245 | printf("Failed to open(%s) %s\n", filename, nfs_get_error(nfs)); | |
246 | exit(10); | |
247 | } | |
248 | ret = nfs_read(nfs, nfsfh, sizeof(buf), buf); | |
249 | if (ret < 0) { | |
250 | printf("Error reading file\n"); | |
57187d21 RS |
251 | } |
252 | printf("Read %d bytes\n", ret); | |
253 | ret = nfs_close(nfs, nfsfh); | |
254 | if (ret < 0) { | |
255 | printf("Failed to close(%s): %s\n", NFSFILE, nfs_get_error(nfs)); | |
256 | exit(10); | |
257 | } | |
84004dbf RS |
258 | } |
259 | nfs_closedir(nfs, nfsdir); | |
260 | ||
261 | ||
e2ba5764 | 262 | ret = nfs_open(nfs, NFSFILEW, O_WRONLY, &nfsfh); |
84004dbf RS |
263 | if (ret != 0) { |
264 | printf("Failed to open(%s) %s\n", NFSFILEW, nfs_get_error(nfs)); | |
265 | exit(10); | |
266 | } | |
e2ba5764 | 267 | ret = nfs_pwrite(nfs, nfsfh, 0, 16, buf); |
84004dbf RS |
268 | if (ret < 0) { |
269 | printf("Failed to pwrite(%s) %s\n", NFSFILEW, nfs_get_error(nfs)); | |
270 | exit(10); | |
271 | } | |
e2ba5764 | 272 | ret = nfs_fsync(nfs, nfsfh); |
84004dbf RS |
273 | if (ret < 0) { |
274 | printf("Failed to fsync(%s) %s\n", NFSFILEW, nfs_get_error(nfs)); | |
275 | exit(10); | |
276 | } | |
e2ba5764 | 277 | ret = nfs_close(nfs, nfsfh); |
84004dbf | 278 | if (ret < 0) { |
7d0397cf | 279 | printf("Failed to close(%s) %s\n", NFSFILEW, nfs_get_error(nfs)); |
84004dbf RS |
280 | exit(10); |
281 | } | |
282 | ||
283 | ||
e2ba5764 | 284 | ret = nfs_statvfs(nfs, NFSDIR, &svfs); |
84004dbf | 285 | if (ret < 0) { |
7d0397cf | 286 | printf("Failed to statvfs(%s) %s\n", NFSDIR, nfs_get_error(nfs)); |
84004dbf RS |
287 | exit(10); |
288 | } | |
289 | printf("files %d/%d/%d\n", (int)svfs.f_files, (int)svfs.f_ffree, (int)svfs.f_favail); | |
290 | ||
291 | ||
e2ba5764 | 292 | ret = nfs_access(nfs, NFSFILE, R_OK); |
84004dbf RS |
293 | if (ret != 0) { |
294 | printf("Failed to access(%s) %s\n", NFSFILE, nfs_get_error(nfs)); | |
295 | } | |
296 | ||
297 | /* become root */ | |
298 | nfs_set_auth(nfs, authunix_create("Ronnies-Laptop", 0, 0, 0, NULL)); | |
299 | ||
e2ba5764 | 300 | ret = nfs_link(nfs, NFSFILE, NFSFILER); |
84004dbf RS |
301 | if (ret != 0) { |
302 | printf("Failed to link(%s) %s\n", NFSFILE, nfs_get_error(nfs)); | |
303 | } | |
304 | ||
305 | ||
306 | nfs_destroy_context(nfs); | |
307 | printf("nfsclient finished\n"); | |
308 | return 0; | |
309 | } | |
df5af25f | 310 |