Win32 changes, include files we need when compiling under win32
[deb_libnfs.git] / lib / libnfs-sync.c
index e964c144b17fc23a164a5913792bd43450d8f447..3d3692b403ebcf18c1d117b890f3984bb1b50cd9 100644 (file)
  * High level api to nfs filesystems
  */
 
+#if defined (WIN32)
+#include <winsock2.h>
+#define DllExport
+#else
+#include <strings.h>
+#include <unistd.h>
+#include <sys/statvfs.h>
+#include <poll.h>
+#include <sys/ioctl.h>
+#include <netdb.h>
+#include <sys/socket.h>
+#include <net/if.h>
+#endif
+
+#ifdef HAVE_CONFIG_H
 #include "config.h"
+#endif
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <strings.h>
 #include <sys/types.h>
 #include <sys/stat.h>
-#include <sys/statvfs.h>
-#include <sys/ioctl.h>
-#include <unistd.h>
 #include <fcntl.h>
 #include <errno.h>
-#include <poll.h>
-#include <sys/socket.h>
-#include <net/if.h>
-#include <netdb.h>
 #include "libnfs.h"
 #include "libnfs-raw.h"
 #include "libnfs-raw-mount.h"
@@ -53,13 +62,10 @@ static void wait_for_reply(struct rpc_context *rpc, struct sync_cb_data *cb_data
 {
        struct pollfd pfd;
 
-       for (;;) {
-               if (cb_data->is_finished) {
-                       break;
-               }
+       while (!cb_data->is_finished) {
+
                pfd.fd = rpc_get_fd(rpc);
                pfd.events = rpc_which_events(rpc);
-
                if (poll(&pfd, 1, -1) < 0) {
                        rpc_set_error(rpc, "Poll failed");
                        cb_data->status = -EIO;
@@ -70,6 +76,44 @@ static void wait_for_reply(struct rpc_context *rpc, struct sync_cb_data *cb_data
                        cb_data->status = -EIO;
                        break;
                }
+               if (rpc_get_fd(rpc) == -1) {
+                       rpc_set_error(rpc, "Socket closed\n");
+                       break;
+               }
+       }
+}
+
+static void wait_for_nfs_reply(struct nfs_context *nfs, struct sync_cb_data *cb_data)
+{
+       struct pollfd pfd;
+
+       while (!cb_data->is_finished) {
+
+               pfd.fd = nfs_get_fd(nfs);
+               pfd.events = nfs_which_events(nfs);
+               if (poll(&pfd, 1, -1) < 0) {
+                       nfs_set_error(nfs, "Poll failed");
+                       cb_data->status = -EIO;
+                       break;
+               }
+               if (nfs_service(nfs, pfd.revents) < 0) {
+                       nfs_set_error(nfs, "nfs_service failed");
+                       cb_data->status = -EIO;
+                       break;
+               }
+               if (nfs_get_fd(nfs) == -1) {
+                       char *server = strdup(nfs_get_server(nfs));
+                       char *export = strdup(nfs_get_export(nfs));
+
+                       if (nfs_mount(nfs, server, export) != 0) {
+                               nfs_set_error(nfs, "Failed to reconnect to nfs server %s", nfs_get_error(nfs));
+                               free(server);
+                               free(export);
+                               break;
+                       }
+                       free(server);
+                       free(export);
+               }
        }
 }
 
@@ -105,7 +149,7 @@ int nfs_mount(struct nfs_context *nfs, const char *server, const char *export)
                return -1;
        }
 
-       wait_for_reply(nfs_get_rpc_context(nfs), &cb_data);
+       wait_for_nfs_reply(nfs, &cb_data);
 
        return cb_data.status;
 }
@@ -141,7 +185,7 @@ int nfs_stat(struct nfs_context *nfs, const char *path, struct stat *st)
                return -1;
        }
 
-       wait_for_reply(nfs_get_rpc_context(nfs), &cb_data);
+       wait_for_nfs_reply(nfs, &cb_data);
 
        return cb_data.status;
 }
@@ -182,7 +226,7 @@ int nfs_open(struct nfs_context *nfs, const char *path, int mode, struct nfsfh *
                return -1;
        }
 
-       wait_for_reply(nfs_get_rpc_context(nfs), &cb_data);
+       wait_for_nfs_reply(nfs, &cb_data);
 
        return cb_data.status;
 }
@@ -221,7 +265,7 @@ int nfs_pread(struct nfs_context *nfs, struct nfsfh *nfsfh, off_t offset, size_t
                return -1;
        }
 
-       wait_for_reply(nfs_get_rpc_context(nfs), &cb_data);
+       wait_for_nfs_reply(nfs, &cb_data);
 
        return cb_data.status;
 }
@@ -260,7 +304,7 @@ int nfs_close(struct nfs_context *nfs, struct nfsfh *nfsfh)
                return -1;
        }
 
-       wait_for_reply(nfs_get_rpc_context(nfs), &cb_data);
+       wait_for_nfs_reply(nfs, &cb_data);
 
        return cb_data.status;
 }
@@ -283,7 +327,7 @@ int nfs_fstat(struct nfs_context *nfs, struct nfsfh *nfsfh, struct stat *st)
                return -1;
        }
 
-       wait_for_reply(nfs_get_rpc_context(nfs), &cb_data);
+       wait_for_nfs_reply(nfs, &cb_data);
 
        return cb_data.status;
 }
@@ -315,7 +359,7 @@ int nfs_pwrite(struct nfs_context *nfs, struct nfsfh *nfsfh, off_t offset, size_
                return -1;
        }
 
-       wait_for_reply(nfs_get_rpc_context(nfs), &cb_data);
+       wait_for_nfs_reply(nfs, &cb_data);
 
        return cb_data.status;
 }
@@ -355,7 +399,7 @@ int nfs_fsync(struct nfs_context *nfs, struct nfsfh *nfsfh)
                return -1;
        }
 
-       wait_for_reply(nfs_get_rpc_context(nfs), &cb_data);
+       wait_for_nfs_reply(nfs, &cb_data);
 
        return cb_data.status;
 }
@@ -389,7 +433,7 @@ int nfs_ftruncate(struct nfs_context *nfs, struct nfsfh *nfsfh, off_t length)
                return -1;
        }
 
-       wait_for_reply(nfs_get_rpc_context(nfs), &cb_data);
+       wait_for_nfs_reply(nfs, &cb_data);
 
        return cb_data.status;
 }
@@ -422,7 +466,7 @@ int nfs_truncate(struct nfs_context *nfs, const char *path, off_t length)
                return -1;
        }
 
-       wait_for_reply(nfs_get_rpc_context(nfs), &cb_data);
+       wait_for_nfs_reply(nfs, &cb_data);
 
        return cb_data.status;
 }
@@ -457,7 +501,7 @@ int nfs_mkdir(struct nfs_context *nfs, const char *path)
                return -1;
        }
 
-       wait_for_reply(nfs_get_rpc_context(nfs), &cb_data);
+       wait_for_nfs_reply(nfs, &cb_data);
 
        return cb_data.status;
 }
@@ -492,7 +536,7 @@ int nfs_rmdir(struct nfs_context *nfs, const char *path)
                return -1;
        }
 
-       wait_for_reply(nfs_get_rpc_context(nfs), &cb_data);
+       wait_for_nfs_reply(nfs, &cb_data);
 
        return cb_data.status;
 }
@@ -532,7 +576,7 @@ int nfs_creat(struct nfs_context *nfs, const char *path, int mode, struct nfsfh
                return -1;
        }
 
-       wait_for_reply(nfs_get_rpc_context(nfs), &cb_data);
+       wait_for_nfs_reply(nfs, &cb_data);
 
        return cb_data.status;
 }
@@ -567,7 +611,7 @@ int nfs_unlink(struct nfs_context *nfs, const char *path)
                return -1;
        }
 
-       wait_for_reply(nfs_get_rpc_context(nfs), &cb_data);
+       wait_for_nfs_reply(nfs, &cb_data);
 
        return cb_data.status;
 }
@@ -607,7 +651,7 @@ int nfs_opendir(struct nfs_context *nfs, const char *path, struct nfsdir **nfsdi
                return -1;
        }
 
-       wait_for_reply(nfs_get_rpc_context(nfs), &cb_data);
+       wait_for_nfs_reply(nfs, &cb_data);
 
        return cb_data.status;
 }
@@ -645,7 +689,7 @@ int nfs_lseek(struct nfs_context *nfs, struct nfsfh *nfsfh, off_t offset, int wh
                return -1;
        }
 
-       wait_for_reply(nfs_get_rpc_context(nfs), &cb_data);
+       wait_for_nfs_reply(nfs, &cb_data);
 
        return cb_data.status;
 }
@@ -682,7 +726,7 @@ int nfs_statvfs(struct nfs_context *nfs, const char *path, struct statvfs *svfs)
                return -1;
        }
 
-       wait_for_reply(nfs_get_rpc_context(nfs), &cb_data);
+       wait_for_nfs_reply(nfs, &cb_data);
 
        return cb_data.status;
 }
@@ -728,7 +772,7 @@ int nfs_readlink(struct nfs_context *nfs, const char *path, char *buf, int bufsi
                return -1;
        }
 
-       wait_for_reply(nfs_get_rpc_context(nfs), &cb_data);
+       wait_for_nfs_reply(nfs, &cb_data);
 
        return cb_data.status;
 }
@@ -762,7 +806,7 @@ int nfs_chmod(struct nfs_context *nfs, const char *path, int mode)
                return -1;
        }
 
-       wait_for_reply(nfs_get_rpc_context(nfs), &cb_data);
+       wait_for_nfs_reply(nfs, &cb_data);
 
        return cb_data.status;
 }
@@ -797,7 +841,7 @@ int nfs_fchmod(struct nfs_context *nfs, struct nfsfh *nfsfh, int mode)
                return -1;
        }
 
-       wait_for_reply(nfs_get_rpc_context(nfs), &cb_data);
+       wait_for_nfs_reply(nfs, &cb_data);
 
        return cb_data.status;
 }
@@ -832,7 +876,7 @@ int nfs_chown(struct nfs_context *nfs, const char *path, int uid, int gid)
                return -1;
        }
 
-       wait_for_reply(nfs_get_rpc_context(nfs), &cb_data);
+       wait_for_nfs_reply(nfs, &cb_data);
 
        return cb_data.status;
 }
@@ -864,7 +908,7 @@ int nfs_fchown(struct nfs_context *nfs, struct nfsfh *nfsfh, int uid, int gid)
                return -1;
        }
 
-       wait_for_reply(nfs_get_rpc_context(nfs), &cb_data);
+       wait_for_nfs_reply(nfs, &cb_data);
 
        return cb_data.status;
 }
@@ -898,7 +942,7 @@ int nfs_utimes(struct nfs_context *nfs, const char *path, struct timeval *times)
                return -1;
        }
 
-       wait_for_reply(nfs_get_rpc_context(nfs), &cb_data);
+       wait_for_nfs_reply(nfs, &cb_data);
 
        return cb_data.status;
 }
@@ -932,7 +976,7 @@ int nfs_utime(struct nfs_context *nfs, const char *path, struct utimbuf *times)
                return -1;
        }
 
-       wait_for_reply(nfs_get_rpc_context(nfs), &cb_data);
+       wait_for_nfs_reply(nfs, &cb_data);
 
        return cb_data.status;
 }
@@ -967,7 +1011,7 @@ int nfs_access(struct nfs_context *nfs, const char *path, int mode)
                return -1;
        }
 
-       wait_for_reply(nfs_get_rpc_context(nfs), &cb_data);
+       wait_for_nfs_reply(nfs, &cb_data);
 
        return cb_data.status;
 }
@@ -1001,7 +1045,7 @@ int nfs_symlink(struct nfs_context *nfs, const char *oldpath, const char *newpat
                return -1;
        }
 
-       wait_for_reply(nfs_get_rpc_context(nfs), &cb_data);
+       wait_for_nfs_reply(nfs, &cb_data);
 
        return cb_data.status;
 }
@@ -1035,7 +1079,7 @@ int nfs_rename(struct nfs_context *nfs, const char *oldpath, const char *newpath
                return -1;
        }
 
-       wait_for_reply(nfs_get_rpc_context(nfs), &cb_data);
+       wait_for_nfs_reply(nfs, &cb_data);
 
        return cb_data.status;
 }
@@ -1069,7 +1113,7 @@ int nfs_link(struct nfs_context *nfs, const char *oldpath, const char *newpath)
                return -1;
        }
 
-       wait_for_reply(nfs_get_rpc_context(nfs), &cb_data);
+       wait_for_nfs_reply(nfs, &cb_data);
 
        return cb_data.status;
 }
@@ -1136,7 +1180,7 @@ void mount_free_export_list(struct exportnode *exports)
 
 
 
-
+#if !defined(WIN32)
 void free_nfs_srvr_list(struct nfs_server_list *srv)
 {
        while (srv != NULL) {
@@ -1184,7 +1228,7 @@ void callit_cb(struct rpc_context *rpc, int status, void *data _U_, void *privat
        /* check for dupes */
        for (srvr = srv_data->srvrs; srvr; srvr = srvr->next) {
                if (!strcmp(hostdd, srvr->addr)) {
-                       return 0;
+                       return;
                }
        }
 
@@ -1342,3 +1386,4 @@ struct nfs_server_list *nfs_find_local_servers(void)
 
        return data.srvrs;
 }
+#endif