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