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