2011-02-06 07:45:09 +03:00
|
|
|
/*
|
|
|
|
Copyright (C) by Ronnie Sahlberg <ronniesahlberg@gmail.com> 2010
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Example program using the highlevel async interface.
|
|
|
|
*/
|
2013-04-14 21:11:48 +04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2011-09-01 23:35:54 +04:00
|
|
|
#ifdef WIN32
|
2018-04-16 22:38:38 +03:00
|
|
|
#include <win32/win32_compat.h>
|
2016-04-28 05:32:16 +03:00
|
|
|
#pragma comment(lib, "ws2_32.lib")
|
|
|
|
WSADATA wsaData;
|
2011-09-01 23:35:54 +04:00
|
|
|
#else
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#endif
|
|
|
|
|
2013-04-14 21:11:48 +04:00
|
|
|
#ifdef HAVE_POLL_H
|
|
|
|
#include <poll.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2011-02-06 07:45:09 +03:00
|
|
|
#define SERVER "10.1.1.27"
|
2011-06-19 02:43:28 +04:00
|
|
|
#define EXPORT "/VIRTUAL"
|
2011-02-06 07:45:09 +03:00
|
|
|
#define NFSFILE "/BOOKS/Classics/Dracula.djvu"
|
|
|
|
#define NFSDIR "/BOOKS/Classics/"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <sys/types.h>
|
2013-04-14 21:11:48 +04:00
|
|
|
#include <fcntl.h>
|
2011-02-06 07:45:09 +03:00
|
|
|
#include "libnfs.h"
|
2011-06-19 08:54:17 +04:00
|
|
|
#include "libnfs-raw.h"
|
|
|
|
#include "libnfs-raw-mount.h"
|
|
|
|
|
|
|
|
struct rpc_context *mount_context;
|
2011-02-06 07:45:09 +03:00
|
|
|
|
|
|
|
struct client {
|
|
|
|
char *server;
|
|
|
|
char *export;
|
|
|
|
uint32_t mount_port;
|
|
|
|
struct nfsfh *nfsfh;
|
|
|
|
int is_finished;
|
|
|
|
};
|
|
|
|
|
2011-06-19 08:54:17 +04:00
|
|
|
void mount_export_cb(struct rpc_context *mount_context, int status, void *data, void *private_data)
|
|
|
|
{
|
|
|
|
struct client *client = private_data;
|
|
|
|
exports export = *(exports *)data;
|
|
|
|
|
|
|
|
if (status < 0) {
|
|
|
|
printf("MOUNT/EXPORT failed with \"%s\"\n", rpc_get_error(mount_context));
|
|
|
|
exit(10);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Got exports list from server %s\n", client->server);
|
|
|
|
while (export != NULL) {
|
|
|
|
printf("Export: %s\n", export->ex_dir);
|
|
|
|
export = export->ex_next;
|
|
|
|
}
|
|
|
|
|
|
|
|
mount_context = NULL;
|
|
|
|
|
|
|
|
client->is_finished = 1;
|
|
|
|
}
|
|
|
|
|
2011-02-06 07:45:09 +03:00
|
|
|
void nfs_opendir_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
|
|
|
|
{
|
|
|
|
struct client *client = private_data;
|
|
|
|
struct nfsdir *nfsdir = data;
|
|
|
|
struct nfsdirent *nfsdirent;
|
|
|
|
|
|
|
|
if (status < 0) {
|
|
|
|
printf("opendir failed with \"%s\"\n", (char *)data);
|
|
|
|
exit(10);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("opendir successful\n");
|
|
|
|
while((nfsdirent = nfs_readdir(nfs, nfsdir)) != NULL) {
|
|
|
|
printf("Inode:%d Name:%s\n", (int)nfsdirent->inode, nfsdirent->name);
|
|
|
|
}
|
|
|
|
nfs_closedir(nfs, nfsdir);
|
|
|
|
|
2011-06-19 08:54:17 +04:00
|
|
|
mount_context = rpc_init_context();
|
|
|
|
if (mount_getexports_async(mount_context, client->server, mount_export_cb, client) != 0) {
|
|
|
|
printf("Failed to start MOUNT/EXPORT\n");
|
|
|
|
exit(10);
|
|
|
|
}
|
2011-02-06 07:45:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void nfs_close_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
|
|
|
|
{
|
|
|
|
struct client *client = private_data;
|
|
|
|
|
|
|
|
if (status < 0) {
|
|
|
|
printf("close failed with \"%s\"\n", (char *)data);
|
|
|
|
exit(10);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("close successful\n");
|
|
|
|
printf("call opendir(%s)\n", NFSDIR);
|
|
|
|
if (nfs_opendir_async(nfs, NFSDIR, nfs_opendir_cb, client) != 0) {
|
|
|
|
printf("Failed to start async nfs close\n");
|
|
|
|
exit(10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-24 17:38:17 +03:00
|
|
|
void nfs_fstat64_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
|
2011-02-06 07:45:09 +03:00
|
|
|
{
|
|
|
|
struct client *client = private_data;
|
2015-05-24 17:38:17 +03:00
|
|
|
struct nfs_stat_64 *st;
|
2011-02-06 07:45:09 +03:00
|
|
|
|
|
|
|
if (status < 0) {
|
|
|
|
printf("fstat call failed with \"%s\"\n", (char *)data);
|
|
|
|
exit(10);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Got reply from server for fstat(%s).\n", NFSFILE);
|
2015-05-24 17:38:17 +03:00
|
|
|
st = (struct nfs_stat_64 *)data;
|
|
|
|
printf("Mode %04o\n", (int)st->nfs_mode);
|
|
|
|
printf("Size %d\n", (int)st->nfs_size);
|
|
|
|
printf("Inode %04o\n", (int)st->nfs_ino);
|
2011-02-06 07:45:09 +03:00
|
|
|
|
|
|
|
printf("Close file\n");
|
|
|
|
if (nfs_close_async(nfs, client->nfsfh, nfs_close_cb, client) != 0) {
|
|
|
|
printf("Failed to start async nfs close\n");
|
|
|
|
exit(10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void nfs_read_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
|
|
|
|
{
|
|
|
|
struct client *client = private_data;
|
|
|
|
char *read_data;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (status < 0) {
|
|
|
|
printf("read failed with \"%s\"\n", (char *)data);
|
|
|
|
exit(10);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("read successful with %d bytes of data\n", status);
|
|
|
|
read_data = data;
|
|
|
|
for (i=0;i<16;i++) {
|
|
|
|
printf("%02x ", read_data[i]&0xff);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
printf("Fstat file :%s\n", NFSFILE);
|
2015-05-24 17:38:17 +03:00
|
|
|
if (nfs_fstat64_async(nfs, client->nfsfh, nfs_fstat64_cb, client) != 0) {
|
2011-02-06 07:45:09 +03:00
|
|
|
printf("Failed to start async nfs fstat\n");
|
|
|
|
exit(10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void nfs_open_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
|
|
|
|
{
|
|
|
|
struct client *client = private_data;
|
|
|
|
struct nfsfh *nfsfh;
|
|
|
|
|
|
|
|
if (status < 0) {
|
|
|
|
printf("open call failed with \"%s\"\n", (char *)data);
|
|
|
|
exit(10);
|
|
|
|
}
|
|
|
|
|
|
|
|
nfsfh = data;
|
|
|
|
client->nfsfh = nfsfh;
|
|
|
|
printf("Got reply from server for open(%s). Handle:%p\n", NFSFILE, data);
|
2011-06-19 02:43:28 +04:00
|
|
|
printf("Read first 64 bytes\n");
|
|
|
|
if (nfs_pread_async(nfs, nfsfh, 0, 64, nfs_read_cb, client) != 0) {
|
2011-02-06 07:45:09 +03:00
|
|
|
printf("Failed to start async nfs open\n");
|
|
|
|
exit(10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-24 17:38:17 +03:00
|
|
|
void nfs_stat64_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
|
2011-02-06 07:45:09 +03:00
|
|
|
{
|
|
|
|
struct client *client = private_data;
|
2015-05-24 17:38:17 +03:00
|
|
|
struct nfs_stat_64 *st;
|
2011-02-06 07:45:09 +03:00
|
|
|
|
|
|
|
if (status < 0) {
|
|
|
|
printf("stat call failed with \"%s\"\n", (char *)data);
|
|
|
|
exit(10);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Got reply from server for stat(%s).\n", NFSFILE);
|
2015-06-19 14:49:17 +03:00
|
|
|
st = (struct nfs_stat_64 *)data;
|
|
|
|
printf("Mode %04o\n", (unsigned int) st->nfs_mode);
|
2015-05-24 17:38:17 +03:00
|
|
|
printf("Size %d\n", (int)st->nfs_size);
|
|
|
|
printf("Inode %04o\n", (int)st->nfs_ino);
|
2011-02-06 07:45:09 +03:00
|
|
|
|
|
|
|
printf("Open file for reading :%s\n", NFSFILE);
|
|
|
|
if (nfs_open_async(nfs, NFSFILE, O_RDONLY, nfs_open_cb, client) != 0) {
|
|
|
|
printf("Failed to start async nfs open\n");
|
|
|
|
exit(10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void nfs_mount_cb(int status, struct nfs_context *nfs, void *data, void *private_data)
|
|
|
|
{
|
|
|
|
struct client *client = private_data;
|
|
|
|
|
|
|
|
if (status < 0) {
|
|
|
|
printf("mount/mnt call failed with \"%s\"\n", (char *)data);
|
|
|
|
exit(10);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Got reply from server for MOUNT/MNT procedure.\n");
|
|
|
|
printf("Stat file :%s\n", NFSFILE);
|
2015-05-24 17:38:17 +03:00
|
|
|
if (nfs_stat64_async(nfs, NFSFILE, nfs_stat64_cb, client) != 0) {
|
2011-02-06 07:45:09 +03:00
|
|
|
printf("Failed to start async nfs stat\n");
|
|
|
|
exit(10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-05-31 18:12:55 +04:00
|
|
|
int main(int argc _U_, char *argv[] _U_)
|
2011-02-06 07:45:09 +03:00
|
|
|
{
|
|
|
|
struct nfs_context *nfs;
|
|
|
|
int ret;
|
|
|
|
struct client client;
|
2011-06-19 08:54:17 +04:00
|
|
|
struct pollfd pfds[2]; /* nfs:0 mount:1 */
|
2011-02-06 07:45:09 +03:00
|
|
|
|
2016-04-28 05:32:16 +03:00
|
|
|
#ifdef WIN32
|
|
|
|
if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
|
|
|
|
printf("Failed to start Winsock2\n");
|
|
|
|
exit(10);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-02-06 07:45:09 +03:00
|
|
|
client.server = SERVER;
|
|
|
|
client.export = EXPORT;
|
|
|
|
client.is_finished = 0;
|
|
|
|
|
|
|
|
nfs = nfs_init_context();
|
|
|
|
if (nfs == NULL) {
|
|
|
|
printf("failed to init context\n");
|
|
|
|
exit(10);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = nfs_mount_async(nfs, client.server, client.export, nfs_mount_cb, &client);
|
|
|
|
if (ret != 0) {
|
|
|
|
printf("Failed to start async nfs mount\n");
|
|
|
|
exit(10);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;;) {
|
2011-06-19 08:54:17 +04:00
|
|
|
int num_fds;
|
|
|
|
|
|
|
|
pfds[0].fd = nfs_get_fd(nfs);
|
|
|
|
pfds[0].events = nfs_which_events(nfs);
|
|
|
|
num_fds = 1;
|
2011-02-06 07:45:09 +03:00
|
|
|
|
2011-06-19 08:54:17 +04:00
|
|
|
if (mount_context != 0 && rpc_get_fd(mount_context) != -1) {
|
|
|
|
pfds[1].fd = rpc_get_fd(mount_context);
|
|
|
|
pfds[1].events = rpc_which_events(mount_context);
|
|
|
|
num_fds = 2;
|
|
|
|
}
|
|
|
|
if (poll(&pfds[0], 2, -1) < 0) {
|
2011-02-06 07:45:09 +03:00
|
|
|
printf("Poll failed");
|
|
|
|
exit(10);
|
|
|
|
}
|
2011-06-19 08:54:17 +04:00
|
|
|
if (mount_context != NULL) {
|
|
|
|
if (rpc_service(mount_context, pfds[1].revents) < 0) {
|
|
|
|
printf("rpc_service failed\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (nfs_service(nfs, pfds[0].revents) < 0) {
|
2011-02-06 07:45:09 +03:00
|
|
|
printf("nfs_service failed\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (client.is_finished) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nfs_destroy_context(nfs);
|
2011-06-19 08:54:17 +04:00
|
|
|
if (mount_context != NULL) {
|
|
|
|
rpc_destroy_context(mount_context);
|
|
|
|
mount_context = NULL;
|
|
|
|
}
|
2011-02-06 07:45:09 +03:00
|
|
|
printf("nfsclient finished\n");
|
|
|
|
return 0;
|
|
|
|
}
|