2 Copyright (C) by Ronnie Sahlberg <ronniesahlberg@gmail.com> 2010
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.
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.
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/>.
18 /* Example program using the highlevel sync interface
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/"
31 #include <sys/types.h>
33 #include <sys/statvfs.h>
37 #include <rpc/rpc.h> /* for authunix_create() */
38 #include "libnfs-raw.h"
39 #include "libnfs-raw-mount.h"
49 int main(int argc _U_
, char *argv
[] _U_
)
51 struct nfs_context
*nfs
;
56 struct nfsdir
*nfsdir
;
57 struct nfsdirent
*nfsdirent
;
58 client
.server
= SERVER
;
59 client
.export
= EXPORT
;
60 client
.is_finished
= 0;
65 struct rpc_context
*mount_context
;
67 printf("exports on server %s\n", SERVER
);
68 mount_context
= rpc_init_context();
69 export
= mount_getexports(mount_context
, SERVER
);
72 printf("Export: %s\n", tmp
->ex_dir
);
75 mount_free_export_list(export
);
76 rpc_destroy_context(mount_context
);
80 nfs
= nfs_init_context();
82 printf("failed to init context\n");
86 ret
= nfs_mount(nfs
, client
.server
, client
.export
);
88 printf("Failed to mount nfs share : %s\n", nfs_get_error(nfs
));
91 printf("mounted share successfully\n");
94 ret
= nfs_stat(nfs
, NFSFILE
, &st
);
96 printf("Failed to stat(%s) %s\n", NFSFILE
, nfs_get_error(nfs
));
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
);
103 ret
= nfs_open(nfs
, NFSFILE
, O_RDONLY
, &nfsfh
);
105 printf("Failed to open(%s) %s\n", NFSFILE
, nfs_get_error(nfs
));
109 ret
= nfs_read(nfs
, nfsfh
, 16, buf
);
111 printf("Failed to pread(%s) %s\n", NFSFILE
, nfs_get_error(nfs
));
114 printf("read %d bytes\n", ret
);
116 printf("%02x ", buf
[i
]&0xff);
119 ret
= nfs_read(nfs
, nfsfh
, 16, buf
);
121 printf("Failed to pread(%s) %s\n", NFSFILE
, nfs_get_error(nfs
));
124 printf("read %d bytes\n", ret
);
126 printf("%02x ", buf
[i
]&0xff);
130 ret
= (int)nfs_lseek(nfs
, nfsfh
, 0, SEEK_CUR
, &offset
);
132 printf("Failed to lseek(%s) %s\n", NFSFILE
, nfs_get_error(nfs
));
135 printf("File position is %d\n", (int)offset
);
137 printf("seek to end of file\n");
138 ret
= (int)nfs_lseek(nfs
, nfsfh
, 0, SEEK_END
, &offset
);
140 printf("Failed to lseek(%s) %s\n", NFSFILE
, nfs_get_error(nfs
));
143 printf("File position is %d\n", (int)offset
);
145 ret
= nfs_fstat(nfs
, nfsfh
, &st
);
147 printf("Failed to stat(%s) %s\n", NFSFILE
, nfs_get_error(nfs
));
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
);
155 ret
= nfs_close(nfs
, nfsfh
);
157 printf("Failed to close(%s): %s\n", NFSFILE
, nfs_get_error(nfs
));
161 ret
= nfs_opendir(nfs
, NFSDIR
, &nfsdir
);
163 printf("Failed to open(%s) %s\n", NFSFILE
, nfs_get_error(nfs
));
166 while((nfsdirent
= nfs_readdir(nfs
, nfsdir
)) != NULL
) {
167 printf("Inode:%d Name:%s\n", (int)nfsdirent
->inode
, nfsdirent
->name
);
169 nfs_closedir(nfs
, nfsdir
);
172 ret
= nfs_open(nfs
, NFSFILEW
, O_WRONLY
, &nfsfh
);
174 printf("Failed to open(%s) %s\n", NFSFILEW
, nfs_get_error(nfs
));
177 ret
= nfs_pwrite(nfs
, nfsfh
, 0, 16, buf
);
179 printf("Failed to pwrite(%s) %s\n", NFSFILEW
, nfs_get_error(nfs
));
182 ret
= nfs_fsync(nfs
, nfsfh
);
184 printf("Failed to fsync(%s) %s\n", NFSFILEW
, nfs_get_error(nfs
));
187 ret
= nfs_close(nfs
, nfsfh
);
189 printf("Failed to close(%s) %s\n", NFSFILEW
, nfs_get_error(nfs
));
194 ret
= nfs_statvfs(nfs
, NFSDIR
, &svfs
);
196 printf("Failed to statvfs(%s) %s\n", NFSDIR
, nfs_get_error(nfs
));
199 printf("files %d/%d/%d\n", (int)svfs
.f_files
, (int)svfs
.f_ffree
, (int)svfs
.f_favail
);
202 ret
= nfs_access(nfs
, NFSFILE
, R_OK
);
204 printf("Failed to access(%s) %s\n", NFSFILE
, nfs_get_error(nfs
));
208 nfs_set_auth(nfs
, authunix_create("Ronnies-Laptop", 0, 0, 0, NULL
));
210 ret
= nfs_link(nfs
, NFSFILE
, NFSFILER
);
212 printf("Failed to link(%s) %s\n", NFSFILE
, nfs_get_error(nfs
));
216 nfs_destroy_context(nfs
);
217 printf("nfsclient finished\n");