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() */
47 int main(int argc _U_
, char *argv
[] _U_
)
49 struct nfs_context
*nfs
;
54 struct nfsdir
*nfsdir
;
55 struct nfsdirent
*nfsdirent
;
56 client
.server
= SERVER
;
57 client
.export
= EXPORT
;
58 client
.is_finished
= 0;
63 nfs
= nfs_init_context();
65 printf("failed to init context\n");
69 ret
= nfs_mount_sync(nfs
, client
.server
, client
.export
);
71 printf("Failed to mount nfs share : %s\n", nfs_get_error(nfs
));
74 printf("mounted share successfully\n");
77 ret
= nfs_stat_sync(nfs
, NFSFILE
, &st
);
79 printf("Failed to stat(%s) %s\n", NFSFILE
, nfs_get_error(nfs
));
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
);
86 ret
= nfs_open_sync(nfs
, NFSFILE
, O_RDONLY
, &nfsfh
);
88 printf("Failed to open(%s) %s\n", NFSFILE
, nfs_get_error(nfs
));
92 ret
= nfs_read_sync(nfs
, nfsfh
, 16, buf
);
94 printf("Failed to pread(%s) %s\n", NFSFILE
, nfs_get_error(nfs
));
97 printf("read %d bytes\n", ret
);
99 printf("%02x ", buf
[i
]&0xff);
102 ret
= nfs_read_sync(nfs
, nfsfh
, 16, buf
);
104 printf("Failed to pread(%s) %s\n", NFSFILE
, nfs_get_error(nfs
));
107 printf("read %d bytes\n", ret
);
109 printf("%02x ", buf
[i
]&0xff);
113 ret
= (int)nfs_lseek_sync(nfs
, nfsfh
, 0, SEEK_CUR
, &offset
);
115 printf("Failed to lseek(%s) %s\n", NFSFILE
, nfs_get_error(nfs
));
118 printf("File position is %d\n", (int)offset
);
120 printf("seek to end of file\n");
121 ret
= (int)nfs_lseek_sync(nfs
, nfsfh
, 0, SEEK_END
, &offset
);
123 printf("Failed to lseek(%s) %s\n", NFSFILE
, nfs_get_error(nfs
));
126 printf("File position is %d\n", (int)offset
);
128 ret
= nfs_fstat_sync(nfs
, nfsfh
, &st
);
130 printf("Failed to stat(%s) %s\n", NFSFILE
, nfs_get_error(nfs
));
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
);
138 ret
= nfs_close_sync(nfs
, nfsfh
);
140 printf("Failed to close(%s): %s\n", NFSFILE
, nfs_get_error(nfs
));
144 ret
= nfs_opendir_sync(nfs
, NFSDIR
, &nfsdir
);
146 printf("Failed to open(%s) %s\n", NFSFILE
, nfs_get_error(nfs
));
149 while((nfsdirent
= nfs_readdir(nfs
, nfsdir
)) != NULL
) {
150 printf("Inode:%d Name:%s\n", (int)nfsdirent
->inode
, nfsdirent
->name
);
152 nfs_closedir(nfs
, nfsdir
);
155 ret
= nfs_open_sync(nfs
, NFSFILEW
, O_WRONLY
, &nfsfh
);
157 printf("Failed to open(%s) %s\n", NFSFILEW
, nfs_get_error(nfs
));
160 ret
= nfs_pwrite_sync(nfs
, nfsfh
, 0, 16, buf
);
162 printf("Failed to pwrite(%s) %s\n", NFSFILEW
, nfs_get_error(nfs
));
165 ret
= nfs_fsync_sync(nfs
, nfsfh
);
167 printf("Failed to fsync(%s) %s\n", NFSFILEW
, nfs_get_error(nfs
));
170 ret
= nfs_close_sync(nfs
, nfsfh
);
172 printf("Failed to close(%s) %s\n", NFSFILEW
, nfs_get_error(nfs
));
177 ret
= nfs_statvfs_sync(nfs
, NFSDIR
, &svfs
);
179 printf("Failed to statvfs(%s) %s\n", NFSDIR
, nfs_get_error(nfs
));
182 printf("files %d/%d/%d\n", (int)svfs
.f_files
, (int)svfs
.f_ffree
, (int)svfs
.f_favail
);
185 ret
= nfs_access_sync(nfs
, NFSFILE
, R_OK
);
187 printf("Failed to access(%s) %s\n", NFSFILE
, nfs_get_error(nfs
));
191 nfs_set_auth(nfs
, authunix_create("Ronnies-Laptop", 0, 0, 0, NULL
));
193 ret
= nfs_link_sync(nfs
, NFSFILE
, NFSFILER
);
195 printf("Failed to link(%s) %s\n", NFSFILE
, nfs_get_error(nfs
));
199 nfs_destroy_context(nfs
);
200 printf("nfsclient finished\n");