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