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" | |
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() */ | |
df5af25f RS |
38 | #include "libnfs-raw.h" |
39 | #include "libnfs-raw-mount.h" | |
84004dbf RS |
40 | |
41 | struct client { | |
42 | char *server; | |
43 | char *export; | |
44 | uint32_t mount_port; | |
45 | int is_finished; | |
46 | }; | |
47 | ||
48 | ||
7d0397cf | 49 | int main(int argc _U_, char *argv[] _U_) |
84004dbf RS |
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; | |
df5af25f RS |
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 | ||
84004dbf RS |
79 | |
80 | nfs = nfs_init_context(); | |
81 | if (nfs == NULL) { | |
82 | printf("failed to init context\n"); | |
83 | exit(10); | |
84 | } | |
85 | ||
e2ba5764 | 86 | ret = nfs_mount(nfs, client.server, client.export); |
84004dbf RS |
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 | ||
e2ba5764 | 94 | ret = nfs_stat(nfs, NFSFILE, &st); |
84004dbf RS |
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 | ||
e2ba5764 | 103 | ret = nfs_open(nfs, NFSFILE, O_RDONLY, &nfsfh); |
84004dbf RS |
104 | if (ret != 0) { |
105 | printf("Failed to open(%s) %s\n", NFSFILE, nfs_get_error(nfs)); | |
106 | exit(10); | |
107 | } | |
108 | ||
e2ba5764 | 109 | ret = nfs_read(nfs, nfsfh, 16, buf); |
84004dbf RS |
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"); | |
e2ba5764 | 119 | ret = nfs_read(nfs, nfsfh, 16, buf); |
84004dbf RS |
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 | ||
e2ba5764 | 130 | ret = (int)nfs_lseek(nfs, nfsfh, 0, SEEK_CUR, &offset); |
84004dbf RS |
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"); | |
e2ba5764 | 138 | ret = (int)nfs_lseek(nfs, nfsfh, 0, SEEK_END, &offset); |
84004dbf RS |
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 | ||
e2ba5764 | 145 | ret = nfs_fstat(nfs, nfsfh, &st); |
84004dbf RS |
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 | ||
e2ba5764 | 155 | ret = nfs_close(nfs, nfsfh); |
84004dbf | 156 | if (ret < 0) { |
7d0397cf | 157 | printf("Failed to close(%s): %s\n", NFSFILE, nfs_get_error(nfs)); |
84004dbf RS |
158 | exit(10); |
159 | } | |
160 | ||
e2ba5764 | 161 | ret = nfs_opendir(nfs, NFSDIR, &nfsdir); |
84004dbf RS |
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 | ||
e2ba5764 | 172 | ret = nfs_open(nfs, NFSFILEW, O_WRONLY, &nfsfh); |
84004dbf RS |
173 | if (ret != 0) { |
174 | printf("Failed to open(%s) %s\n", NFSFILEW, nfs_get_error(nfs)); | |
175 | exit(10); | |
176 | } | |
e2ba5764 | 177 | ret = nfs_pwrite(nfs, nfsfh, 0, 16, buf); |
84004dbf RS |
178 | if (ret < 0) { |
179 | printf("Failed to pwrite(%s) %s\n", NFSFILEW, nfs_get_error(nfs)); | |
180 | exit(10); | |
181 | } | |
e2ba5764 | 182 | ret = nfs_fsync(nfs, nfsfh); |
84004dbf RS |
183 | if (ret < 0) { |
184 | printf("Failed to fsync(%s) %s\n", NFSFILEW, nfs_get_error(nfs)); | |
185 | exit(10); | |
186 | } | |
e2ba5764 | 187 | ret = nfs_close(nfs, nfsfh); |
84004dbf | 188 | if (ret < 0) { |
7d0397cf | 189 | printf("Failed to close(%s) %s\n", NFSFILEW, nfs_get_error(nfs)); |
84004dbf RS |
190 | exit(10); |
191 | } | |
192 | ||
193 | ||
e2ba5764 | 194 | ret = nfs_statvfs(nfs, NFSDIR, &svfs); |
84004dbf | 195 | if (ret < 0) { |
7d0397cf | 196 | printf("Failed to statvfs(%s) %s\n", NFSDIR, nfs_get_error(nfs)); |
84004dbf RS |
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 | ||
e2ba5764 | 202 | ret = nfs_access(nfs, NFSFILE, R_OK); |
84004dbf RS |
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 | ||
e2ba5764 | 210 | ret = nfs_link(nfs, NFSFILE, NFSFILER); |
84004dbf RS |
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 | } | |
df5af25f | 220 |