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() */ | |
38 | ||
39 | struct client { | |
40 | char *server; | |
41 | char *export; | |
42 | uint32_t mount_port; | |
43 | int is_finished; | |
44 | }; | |
45 | ||
46 | ||
7d0397cf | 47 | int main(int argc _U_, char *argv[] _U_) |
84004dbf RS |
48 | { |
49 | struct nfs_context *nfs; | |
50 | int i, ret; | |
51 | struct client client; | |
52 | struct stat st; | |
53 | struct nfsfh *nfsfh; | |
54 | struct nfsdir *nfsdir; | |
55 | struct nfsdirent *nfsdirent; | |
56 | client.server = SERVER; | |
57 | client.export = EXPORT; | |
58 | client.is_finished = 0; | |
59 | char buf[16]; | |
60 | off_t offset; | |
61 | struct statvfs svfs; | |
62 | ||
63 | nfs = nfs_init_context(); | |
64 | if (nfs == NULL) { | |
65 | printf("failed to init context\n"); | |
66 | exit(10); | |
67 | } | |
68 | ||
69 | ret = nfs_mount_sync(nfs, client.server, client.export); | |
70 | if (ret != 0) { | |
71 | printf("Failed to mount nfs share : %s\n", nfs_get_error(nfs)); | |
72 | exit(10); | |
73 | } | |
74 | printf("mounted share successfully\n"); | |
75 | ||
76 | ||
77 | ret = nfs_stat_sync(nfs, NFSFILE, &st); | |
78 | if (ret != 0) { | |
79 | printf("Failed to stat(%s) %s\n", NFSFILE, nfs_get_error(nfs)); | |
80 | exit(10); | |
81 | } | |
82 | printf("Mode %04o\n", st.st_mode); | |
83 | printf("Size %d\n", (int)st.st_size); | |
84 | printf("Inode %04o\n", (int)st.st_ino); | |
85 | ||
86 | ret = nfs_open_sync(nfs, NFSFILE, O_RDONLY, &nfsfh); | |
87 | if (ret != 0) { | |
88 | printf("Failed to open(%s) %s\n", NFSFILE, nfs_get_error(nfs)); | |
89 | exit(10); | |
90 | } | |
91 | ||
92 | ret = nfs_read_sync(nfs, nfsfh, 16, buf); | |
93 | if (ret < 0) { | |
94 | printf("Failed to pread(%s) %s\n", NFSFILE, nfs_get_error(nfs)); | |
95 | exit(10); | |
96 | } | |
97 | printf("read %d bytes\n", ret); | |
98 | for (i=0;i<16;i++) { | |
99 | printf("%02x ", buf[i]&0xff); | |
100 | } | |
101 | printf("\n"); | |
102 | ret = nfs_read_sync(nfs, nfsfh, 16, buf); | |
103 | if (ret < 0) { | |
104 | printf("Failed to pread(%s) %s\n", NFSFILE, nfs_get_error(nfs)); | |
105 | exit(10); | |
106 | } | |
107 | printf("read %d bytes\n", ret); | |
108 | for (i=0;i<16;i++) { | |
109 | printf("%02x ", buf[i]&0xff); | |
110 | } | |
111 | printf("\n"); | |
112 | ||
113 | ret = (int)nfs_lseek_sync(nfs, nfsfh, 0, SEEK_CUR, &offset); | |
114 | if (ret < 0) { | |
115 | printf("Failed to lseek(%s) %s\n", NFSFILE, nfs_get_error(nfs)); | |
116 | exit(10); | |
117 | } | |
118 | printf("File position is %d\n", (int)offset); | |
119 | ||
120 | printf("seek to end of file\n"); | |
121 | ret = (int)nfs_lseek_sync(nfs, nfsfh, 0, SEEK_END, &offset); | |
122 | if (ret < 0) { | |
123 | printf("Failed to lseek(%s) %s\n", NFSFILE, nfs_get_error(nfs)); | |
124 | exit(10); | |
125 | } | |
126 | printf("File position is %d\n", (int)offset); | |
127 | ||
128 | ret = nfs_fstat_sync(nfs, nfsfh, &st); | |
129 | if (ret != 0) { | |
130 | printf("Failed to stat(%s) %s\n", NFSFILE, nfs_get_error(nfs)); | |
131 | exit(10); | |
132 | } | |
133 | printf("Mode %04o\n", st.st_mode); | |
134 | printf("Size %d\n", (int)st.st_size); | |
135 | printf("Inode %04o\n", (int)st.st_ino); | |
136 | ||
137 | ||
138 | ret = nfs_close_sync(nfs, nfsfh); | |
139 | if (ret < 0) { | |
7d0397cf | 140 | printf("Failed to close(%s): %s\n", NFSFILE, nfs_get_error(nfs)); |
84004dbf RS |
141 | exit(10); |
142 | } | |
143 | ||
144 | ret = nfs_opendir_sync(nfs, NFSDIR, &nfsdir); | |
145 | if (ret != 0) { | |
146 | printf("Failed to open(%s) %s\n", NFSFILE, nfs_get_error(nfs)); | |
147 | exit(10); | |
148 | } | |
149 | while((nfsdirent = nfs_readdir(nfs, nfsdir)) != NULL) { | |
150 | printf("Inode:%d Name:%s\n", (int)nfsdirent->inode, nfsdirent->name); | |
151 | } | |
152 | nfs_closedir(nfs, nfsdir); | |
153 | ||
154 | ||
155 | ret = nfs_open_sync(nfs, NFSFILEW, O_WRONLY, &nfsfh); | |
156 | if (ret != 0) { | |
157 | printf("Failed to open(%s) %s\n", NFSFILEW, nfs_get_error(nfs)); | |
158 | exit(10); | |
159 | } | |
160 | ret = nfs_pwrite_sync(nfs, nfsfh, 0, 16, buf); | |
161 | if (ret < 0) { | |
162 | printf("Failed to pwrite(%s) %s\n", NFSFILEW, nfs_get_error(nfs)); | |
163 | exit(10); | |
164 | } | |
165 | ret = nfs_fsync_sync(nfs, nfsfh); | |
166 | if (ret < 0) { | |
167 | printf("Failed to fsync(%s) %s\n", NFSFILEW, nfs_get_error(nfs)); | |
168 | exit(10); | |
169 | } | |
170 | ret = nfs_close_sync(nfs, nfsfh); | |
171 | if (ret < 0) { | |
7d0397cf | 172 | printf("Failed to close(%s) %s\n", NFSFILEW, nfs_get_error(nfs)); |
84004dbf RS |
173 | exit(10); |
174 | } | |
175 | ||
176 | ||
177 | ret = nfs_statvfs_sync(nfs, NFSDIR, &svfs); | |
178 | if (ret < 0) { | |
7d0397cf | 179 | printf("Failed to statvfs(%s) %s\n", NFSDIR, nfs_get_error(nfs)); |
84004dbf RS |
180 | exit(10); |
181 | } | |
182 | printf("files %d/%d/%d\n", (int)svfs.f_files, (int)svfs.f_ffree, (int)svfs.f_favail); | |
183 | ||
184 | ||
185 | ret = nfs_access_sync(nfs, NFSFILE, R_OK); | |
186 | if (ret != 0) { | |
187 | printf("Failed to access(%s) %s\n", NFSFILE, nfs_get_error(nfs)); | |
188 | } | |
189 | ||
190 | /* become root */ | |
191 | nfs_set_auth(nfs, authunix_create("Ronnies-Laptop", 0, 0, 0, NULL)); | |
192 | ||
193 | ret = nfs_link_sync(nfs, NFSFILE, NFSFILER); | |
194 | if (ret != 0) { | |
195 | printf("Failed to link(%s) %s\n", NFSFILE, nfs_get_error(nfs)); | |
196 | } | |
197 | ||
198 | ||
199 | nfs_destroy_context(nfs); | |
200 | printf("nfsclient finished\n"); | |
201 | return 0; | |
202 | } |