ZDR: remove dependency on zdr.h from the examples and nfs-ls
[deb_libnfs.git] / examples / nfsclient-sync.c
index 2f3839580a250d2325ae8ad18ab8a15aa3449bf5..0985acdbf61c233e36f147d0b4d42a15b4b32b30 100644 (file)
    along with this program; if not, see <http://www.gnu.org/licenses/>.
 */
 
+#define _FILE_OFFSET_BITS 64
+#define _GNU_SOURCE
+
 /* Example program using the highlevel sync interface
  */
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#ifdef AROS
+#include "aros_compat.h"
+#endif
+#ifdef WIN32
+#include "win32_compat.h"
+#pragma comment(lib, "ws2_32.lib")
+WSADATA wsaData;
+#define PRId64 "ll"
+#else
+#include <inttypes.h>
+#include <string.h>
+#include <sys/stat.h>
+#ifndef AROS
+#include <sys/statvfs.h>
+#endif
+#endif
 
-#define SERVER "10.1.1.27"
-#define EXPORT "/VIRTUAL"
-#define NFSFILE "/BOOKS/Classics/Dracula.djvu"
-#define NFSFILER "/BOOKS/Classics/Dracula.djvu.renamed"
-#define NFSFILEW "/BOOKS/Classics/foo"
-#define NFSDIR "/BOOKS/Classics/"
+#ifdef HAVE_UNISTD_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() */
+#include "libnfs-raw.h"
+#include "libnfs-raw-mount.h"
 
 struct client {
        char *server;
@@ -44,159 +64,169 @@ struct client {
 };
 
 
+char buf[3*1024*1024+337];
+
+void print_usage(void)
+{
+       fprintf(stderr, "Usage: nfsclient-sync [-?|--help] [--usage] <url>\n");
+}
+
 int main(int argc, char *argv[])
 {
-       struct nfs_context *nfs;
-       int i, ret;
+       struct nfs_context *nfs = NULL;
+       int i, ret, res;
+       uint64_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;
-       char buf[16];
-       off_t offset;
        struct statvfs svfs;
+       exports export, tmp;
+       const char *url = NULL;
+       char *server = NULL, *path = NULL, *strp;
 
-       nfs = nfs_init_context();
-       if (nfs == NULL) {
-               printf("failed to init context\n");
+#ifdef WIN32
+       if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
+               printf("Failed to start Winsock2\n");
                exit(10);
        }
+#endif
 
-       ret = nfs_mount_sync(nfs, client.server, client.export);
-       if (ret != 0) {
-               printf("Failed to mount nfs share : %s\n", nfs_get_error(nfs));
-               exit(10);
-       }
-       printf("mounted share successfully\n");
+#ifdef AROS
+       aros_init_socket();
+#endif
 
+       url = argv[1];
 
-       ret = nfs_stat_sync(nfs, NFSFILE, &st);
-       if (ret != 0) {
-               printf("Failed to stat(%s) %s\n", NFSFILE, nfs_get_error(nfs));
-               exit(10);
+       if (url == NULL) {
+               fprintf(stderr, "No URL specified.\n");
+               print_usage();
+               exit(0);
        }
-       printf("Mode %04o\n", st.st_mode);
-       printf("Size %d\n", (int)st.st_size);
-       printf("Inode %04o\n", (int)st.st_ino);
 
-       ret = nfs_open_sync(nfs, NFSFILE, O_RDONLY, &nfsfh);
-       if (ret != 0) {
-               printf("Failed to open(%s) %s\n", NFSFILE, nfs_get_error(nfs));
-               exit(10);
+       if (strncmp(url, "nfs://", 6)) {
+               fprintf(stderr, "Invalid URL specified.\n");
+               print_usage();
+               exit(0);
        }
 
-       ret = nfs_read_sync(nfs, nfsfh, 16, buf);
-       if (ret < 0) {
-               printf("Failed to pread(%s) %s\n", NFSFILE, nfs_get_error(nfs));
+       server = strdup(url + 6);
+       if (server == NULL) {
+               fprintf(stderr, "Failed to strdup server string\n");
                exit(10);
        }
-       printf("read %d bytes\n", ret);
-       for (i=0;i<16;i++) {
-               printf("%02x ", buf[i]&0xff);
-       }
-       printf("\n");
-       ret = nfs_read_sync(nfs, nfsfh, 16, buf);
-       if (ret < 0) {
-               printf("Failed to pread(%s) %s\n", NFSFILE, nfs_get_error(nfs));
+       if (server[0] == '/' || server[0] == '\0') {
+               fprintf(stderr, "Invalid server string.\n");
+               free(server);
                exit(10);
        }
-       printf("read %d bytes\n", ret);
-       for (i=0;i<16;i++) {
-               printf("%02x ", buf[i]&0xff);
+       strp = strchr(server, '/');
+       if (strp == NULL) {
+               fprintf(stderr, "Invalid URL specified.\n");
+               print_usage();
+               free(server);
+               exit(0);
        }
-       printf("\n");
-
-       ret = (int)nfs_lseek_sync(nfs, nfsfh, 0, SEEK_CUR, &offset);
-       if (ret < 0) {
-               printf("Failed to lseek(%s) %s\n", NFSFILE, nfs_get_error(nfs));
+       path = strdup(strp);
+       if (path == NULL) {
+               fprintf(stderr, "Failed to strdup server string\n");
+               free(server);
                exit(10);
        }
-       printf("File position is %d\n", (int)offset);
-
-       printf("seek to end of file\n");
-       ret = (int)nfs_lseek_sync(nfs, nfsfh, 0, SEEK_END, &offset);
-       if (ret < 0) {
-               printf("Failed to lseek(%s) %s\n", NFSFILE, nfs_get_error(nfs));
-               exit(10);
-       }
-       printf("File position is %d\n", (int)offset);
-
-       ret = nfs_fstat_sync(nfs, nfsfh, &st);
-       if (ret != 0) {
-               printf("Failed to stat(%s) %s\n", NFSFILE, nfs_get_error(nfs));
+       if (path[0] != '/') {
+               fprintf(stderr, "Invalid path.\n");
+               free(server);
+               free(path);
                exit(10);
        }
-       printf("Mode %04o\n", st.st_mode);
-       printf("Size %d\n", (int)st.st_size);
-       printf("Inode %04o\n", (int)st.st_ino);
+       *strp = 0;
+       
+       client.server = server;
+       client.export = path;
+       client.is_finished = 0;
 
 
-       ret = nfs_close_sync(nfs, nfsfh);
-       if (ret < 0) {
-               printf("Failed to close(%s)\n", NFSFILE, nfs_get_error(nfs));
-               exit(10);
+       nfs = nfs_init_context();
+       if (nfs == NULL) {
+               printf("failed to init context\n");
+               goto finished;
        }
 
-       ret = nfs_opendir_sync(nfs, NFSDIR, &nfsdir);
+       ret = nfs_mount(nfs, client.server, client.export);
        if (ret != 0) {
-               printf("Failed to open(%s) %s\n", NFSFILE, nfs_get_error(nfs));
-               exit(10);
-       }
-       while((nfsdirent = nfs_readdir(nfs, nfsdir)) != NULL) {
-               printf("Inode:%d Name:%s\n", (int)nfsdirent->inode, nfsdirent->name);
+               printf("Failed to mount nfs share : %s\n", nfs_get_error(nfs));
+               goto finished;
        }
-       nfs_closedir(nfs, nfsdir);
 
 
-       ret = nfs_open_sync(nfs, NFSFILEW, O_WRONLY, &nfsfh);
+       ret = nfs_opendir(nfs, "/", &nfsdir);
        if (ret != 0) {
-               printf("Failed to open(%s) %s\n", NFSFILEW, nfs_get_error(nfs));
-               exit(10);
-       }
-       ret = nfs_pwrite_sync(nfs, nfsfh, 0, 16, buf);
-       if (ret < 0) {
-               printf("Failed to pwrite(%s) %s\n", NFSFILEW, nfs_get_error(nfs));
+               printf("Failed to opendir(\"/\") %s\n", nfs_get_error(nfs));
                exit(10);
        }
-       ret = nfs_fsync_sync(nfs, nfsfh);
-       if (ret < 0) {
-               printf("Failed to fsync(%s) %s\n", NFSFILEW, nfs_get_error(nfs));
-               exit(10);
-       }
-       ret = nfs_close_sync(nfs, nfsfh);
-       if (ret < 0) {
-               printf("Failed to close(%s)\n", NFSFILEW, nfs_get_error(nfs));
-               exit(10);
-       }
-
-
-       ret = nfs_statvfs_sync(nfs, NFSDIR, &svfs);
-       if (ret < 0) {
-               printf("Failed to statvfs(%s)\n", NFSDIR, nfs_get_error(nfs));
-               exit(10);
-       }
-       printf("files %d/%d/%d\n", (int)svfs.f_files, (int)svfs.f_ffree, (int)svfs.f_favail);
-
-
-       ret = nfs_access_sync(nfs, NFSFILE, R_OK);
-       if (ret != 0) {
-               printf("Failed to access(%s) %s\n", NFSFILE, nfs_get_error(nfs));
+       while((nfsdirent = nfs_readdir(nfs, nfsdir)) != NULL) {
+               char path[1024];
+
+               if (!strcmp(nfsdirent->name, ".") || !strcmp(nfsdirent->name, "..")) {
+                       continue;
+               }
+
+               sprintf(path, "%s/%s", "/", nfsdirent->name);
+               ret = nfs_stat(nfs, path, &st);
+               if (ret != 0) {
+                       fprintf(stderr, "Failed to stat(%s) %s\n", path, nfs_get_error(nfs));
+                       continue;
+               }
+
+               switch (st.st_mode & S_IFMT) {
+#ifndef WIN32
+               case S_IFLNK:
+#endif
+               case S_IFREG:
+                       printf("-");
+                       break;
+               case S_IFDIR:
+                       printf("d");
+                       break;
+               case S_IFCHR:
+                       printf("c");
+                       break;
+               case S_IFBLK:
+                       printf("b");
+                       break;
+               }
+               printf("%c%c%c",
+                       "-r"[!!(st.st_mode & S_IRUSR)],
+                       "-w"[!!(st.st_mode & S_IWUSR)],
+                       "-x"[!!(st.st_mode & S_IXUSR)]
+               );
+               printf("%c%c%c",
+                       "-r"[!!(st.st_mode & S_IRGRP)],
+                       "-w"[!!(st.st_mode & S_IWGRP)],
+                       "-x"[!!(st.st_mode & S_IXGRP)]
+               );
+               printf("%c%c%c",
+                       "-r"[!!(st.st_mode & S_IROTH)],
+                       "-w"[!!(st.st_mode & S_IWOTH)],
+                       "-x"[!!(st.st_mode & S_IXOTH)]
+               );
+               printf(" %2d", (int)st.st_nlink);
+               printf(" %5d", st.st_uid);
+               printf(" %5d", st.st_gid);
+               printf(" %12" PRId64, st.st_size);
+
+               printf(" %s\n", nfsdirent->name);
        }
+       nfs_closedir(nfs, nfsdir);
 
-       /* become root */
-       nfs_set_auth(nfs, authunix_create("Ronnies-Laptop", 0, 0, 0, NULL));
 
-       ret = nfs_link_sync(nfs, NFSFILE, NFSFILER);
-       if (ret != 0) {
-               printf("Failed to link(%s) %s\n", NFSFILE, nfs_get_error(nfs));
+finished:
+       free(server);
+       free(path);
+       if (nfs != NULL) {              
+               nfs_destroy_context(nfs);
        }
-
-
-       nfs_destroy_context(nfs);
-       printf("nfsclient finished\n");
        return 0;
 }
+