Win32 changes, include files we need when compiling under win32
[deb_libnfs.git] / examples / nfsclient-sync.c
index a5af3ce73d19c40db030337eb33a8c05ad94b998..00e4c88042eba4a3e50553acb8e79460eb11c03d 100644 (file)
 #define NFSFILEW "/BOOKS/Classics/foo"
 #define NFSDIR "/BOOKS/Classics/"
 
+#define _GNU_SOURCE
+
+#if defined(WIN32)
+#include <winsock2.h>
+typedef int off_t;
+#pragma comment(lib, "ws2_32.lib")
+WSADATA wsaData;
+#else
+#include <sys/statvfs.h>
+#include <unistd.h>
+#endif
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdint.h>
 #include <sys/types.h>
 #include <sys/stat.h>
-#include <sys/statvfs.h>
-#include <unistd.h>
 #include <fcntl.h>
 #include "libnfs.h"
 #include <rpc/rpc.h>            /* for authunix_create() */
@@ -46,24 +56,31 @@ struct client {
 };
 
 
-char buf[5*1024*1024];
+char buf[3*1024*1024+337];
 
 int main(int argc _U_, char *argv[] _U_)
 {
        struct nfs_context *nfs;
        int i, ret;
+       off_t offset;
        struct client client;
        struct stat st;
        struct nfsfh  *nfsfh;
        struct nfsdir *nfsdir;
        struct nfsdirent *nfsdirent;
-       client.server = SERVER;
-       client.export = EXPORT;
-       client.is_finished = 0;
-       off_t offset;
        struct statvfs svfs;
        exports export, tmp;
 
+#if defined(WIN32)
+       if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
+               printf("Failed to start Winsock2\n");
+               exit(10);
+       }
+#endif
+
+       client.server = SERVER;
+       client.export = EXPORT;
+       client.is_finished = 0;
        export = mount_getexports(SERVER);
        if (export != NULL) {
                printf("exports on server %s\n", SERVER);
@@ -129,6 +146,36 @@ int main(int argc _U_, char *argv[] _U_)
                printf("%02x ", buf[i]&0xff);
        }
        printf("\n");
+       ret = nfs_read(nfs, nfsfh, sizeof(buf), buf);
+       if (ret < 0) {
+               printf("Failed to pread(%s) %s\n", NFSFILE, nfs_get_error(nfs));
+               exit(10);
+       }
+       printf("read %d bytes\n", ret);
+       ret = nfs_read(nfs, nfsfh, sizeof(buf), buf);
+       if (ret < 0) {
+               printf("Failed to pread(%s) %s\n", NFSFILE, nfs_get_error(nfs));
+               exit(10);
+       }
+       printf("read %d bytes\n", ret);
+       ret = nfs_read(nfs, nfsfh, sizeof(buf), buf);
+       if (ret < 0) {
+               printf("Failed to pread(%s) %s\n", NFSFILE, nfs_get_error(nfs));
+               exit(10);
+       }
+       printf("read %d bytes\n", ret);
+       ret = nfs_read(nfs, nfsfh, sizeof(buf), buf);
+       if (ret < 0) {
+               printf("Failed to pread(%s) %s\n", NFSFILE, nfs_get_error(nfs));
+               exit(10);
+       }
+       printf("read %d bytes\n", ret);
+       ret = nfs_read(nfs, nfsfh, sizeof(buf), buf);
+       if (ret < 0) {
+               printf("Failed to pread(%s) %s\n", NFSFILE, nfs_get_error(nfs));
+               exit(10);
+       }
+       printf("read %d bytes\n", ret);
 
        ret = (int)nfs_lseek(nfs, nfsfh, 0, SEEK_CUR, &offset);
        if (ret < 0) {
@@ -167,7 +214,25 @@ int main(int argc _U_, char *argv[] _U_)
                exit(10);
        }
        while((nfsdirent = nfs_readdir(nfs, nfsdir)) != NULL) {
-               printf("Inode:%d Name:%s\n", (int)nfsdirent->inode, nfsdirent->name);
+         char filename[1024];
+               printf("Inode:%d Name:%s ", (int)nfsdirent->inode, nfsdirent->name);
+               sprintf(&filename, "%s/%s", NFSDIR, nfsdirent->name);
+               ret = nfs_open(nfs, filename, O_RDONLY, &nfsfh);
+               if (ret != 0) {
+                       printf("Failed to open(%s) %s\n", filename, nfs_get_error(nfs));
+                       exit(10);
+               }
+               ret = nfs_read(nfs, nfsfh, sizeof(buf), buf);
+               if (ret < 0) {
+                       printf("Error reading file\n");
+                       exit(10);
+               }
+               printf("Read %d bytes\n", ret);
+               ret = nfs_close(nfs, nfsfh);
+               if (ret < 0) {
+                       printf("Failed to close(%s): %s\n", NFSFILE, nfs_get_error(nfs));
+                       exit(10);
+               }
        }
        nfs_closedir(nfs, nfsdir);