X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=examples%2Fnfsclient-sync.c;h=4bd2c0b942714b26d4c72ee8305842ecdec1b5f2;hb=2142af5d472ae35afca3b6b2c0da26974e8b11b3;hp=4f1d2bc4e2f7997cff514e0089189346efd6c876;hpb=584108f43e97072c631fdea54ab73a51f195d5c6;p=deb_libnfs.git
diff --git a/examples/nfsclient-sync.c b/examples/nfsclient-sync.c
index 4f1d2bc..4bd2c0b 100644
--- a/examples/nfsclient-sync.c
+++ b/examples/nfsclient-sync.c
@@ -15,41 +15,38 @@
along with this program; if not, see .
*/
+#define _FILE_OFFSET_BITS 64
+#define _GNU_SOURCE
+
/* Example program using the highlevel sync interface
*/
#ifdef WIN32
#include "win32_compat.h"
+#pragma comment(lib, "ws2_32.lib")
+WSADATA wsaData;
#else
+#include
#include
#include
+#include
+#ifndef AROS
#include
#endif
-
-#define SERVER "10.1.1.27"
-#define EXPORT "/VIRTUAL"
-#define NFSFILE "/BOOKS/Classics/Dracula.djvu.truncated"
-#define NFSFILER "/BOOKS/Classics/Dracula.djvu.renamed"
-#define NFSFILEW "/BOOKS/Classics/foo"
-#define NFSDIR "/BOOKS/Classics/"
-
-#define _GNU_SOURCE
-
-#if defined(WIN32)
-#pragma comment(lib, "ws2_32.lib")
-WSADATA wsaData;
-#else
-#include
-#include
#endif
+#ifdef AROS
+#include "aros_compat.h"
+#endif
+
#include
#include
#include
+#include
#include
#include
#include
+#include "libnfs-zdr.h"
#include "libnfs.h"
-#include /* for authunix_create() */
#include "libnfs-raw.h"
#include "libnfs-raw-mount.h"
@@ -61,28 +58,18 @@ struct client {
};
-void PrintServerList()
-{
- struct nfs_server_list *srvrs;
- struct nfs_server_list *srv;
-
- srvrs = nfs_find_local_servers();
-
- for (srv=srvrs; srv; srv = srv->next)
- {
- printf("Found nfs server: %s\n", srv->addr);
+char buf[3*1024*1024+337];
- }
- free_nfs_srvr_list(srvrs);
+void print_usage(void)
+{
+ fprintf(stderr, "Usage: nfsclient-sync [-?|--help] [--usage] \n");
}
-char buf[3*1024*1024+337];
-
-int main(int argc _U_, char *argv[] _U_)
+int main(int argc, char *argv[])
{
- struct nfs_context *nfs;
- int i, ret;
- off_t offset;
+ struct nfs_context *nfs = NULL;
+ int i, ret, res;
+ uint64_t offset;
struct client client;
struct stat st;
struct nfsfh *nfsfh;
@@ -90,221 +77,148 @@ int main(int argc _U_, char *argv[] _U_)
struct nfsdirent *nfsdirent;
struct statvfs svfs;
exports export, tmp;
+ const char *url = NULL;
+ char *server = NULL, *path = NULL, *strp;
-#if defined(WIN32)
+#ifdef 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;
-
- PrintServerList();
-
- export = mount_getexports(SERVER);
- if (export != NULL) {
- printf("exports on server %s\n", SERVER);
- tmp = export;
- while (tmp != NULL) {
- printf("Export: %s\n", tmp->ex_dir);
- tmp = tmp->ex_next;
- }
-
- mount_free_export_list(export);
- } else {
- printf("no exports on server %s\n", SERVER);
- }
-
- nfs = nfs_init_context();
- if (nfs == NULL) {
- printf("failed to init context\n");
- exit(10);
- }
-
- ret = nfs_mount(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 %s\n", nfs_get_error(nfs));
+#ifdef AROS
+ aros_init_socket();
+#endif
+ url = argv[1];
- ret = nfs_stat(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(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);
}
-#if 0
- ret = nfs_read(nfs, nfsfh, 16, buf);
- if (ret < 0) {
- printf("Failed to pread(%s) %s\n", NFSFILE, nfs_get_error(nfs));
- exit(10);
- }
- printf("read %d bytes\n", ret);
- for (i=0;i<16;i++) {
- printf("%02x ", buf[i]&0xff);
- }
- printf("\n");
-#endif
- 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);
- for (i=0;i<16;i++) {
- 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));
+ server = strdup(url + 6);
+ if (server == NULL) {
+ fprintf(stderr, "Failed to strdup server string\n");
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));
+ if (server[0] == '/' || server[0] == '\0') {
+ fprintf(stderr, "Invalid server string.\n");
+ free(server);
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);
+ strp = strchr(server, '/');
+ if (strp == NULL) {
+ fprintf(stderr, "Invalid URL specified.\n");
+ print_usage();
+ free(server);
+ exit(0);
}
- 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));
+ path = strdup(strp);
+ if (path == NULL) {
+ fprintf(stderr, "Failed to strdup server string\n");
+ free(server);
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));
+ if (path[0] != '/') {
+ fprintf(stderr, "Invalid path.\n");
+ free(server);
+ free(path);
exit(10);
}
- printf("read %d bytes\n", ret);
+ *strp = 0;
+
+ client.server = server;
+ client.export = path;
+ client.is_finished = 0;
- ret = (int)nfs_lseek(nfs, nfsfh, 0, SEEK_CUR, &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);
- printf("seek to end of file\n");
- ret = (int)nfs_lseek(nfs, nfsfh, 0, SEEK_END, &offset);
- if (ret < 0) {
- printf("Failed to lseek(%s) %s\n", NFSFILE, nfs_get_error(nfs));
- exit(10);
+ nfs = nfs_init_context();
+ if (nfs == NULL) {
+ printf("failed to init context\n");
+ goto finished;
}
- printf("File position is %d\n", (int)offset);
- ret = nfs_fstat(nfs, nfsfh, &st);
+ ret = nfs_mount(nfs, client.server, client.export);
if (ret != 0) {
- printf("Failed to stat(%s) %s\n", NFSFILE, nfs_get_error(nfs));
- exit(10);
+ printf("Failed to mount nfs share : %s\n", nfs_get_error(nfs));
+ goto finished;
}
- 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_close(nfs, nfsfh);
- if (ret < 0) {
- printf("Failed to close(%s): %s\n", NFSFILE, nfs_get_error(nfs));
- exit(10);
- }
- ret = nfs_opendir(nfs, NFSDIR, &nfsdir);
+ ret = nfs_opendir(nfs, "/", &nfsdir);
if (ret != 0) {
- printf("Failed to open(%s) %s\n", NFSFILE, nfs_get_error(nfs));
+ printf("Failed to opendir(\"/\")\n", nfs_get_error(nfs));
exit(10);
}
while((nfsdirent = nfs_readdir(nfs, nfsdir)) != NULL) {
- 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);
+ char path[1024];
+
+ if (!strcmp(nfsdirent->name, ".") || !strcmp(nfsdirent->name, "..")) {
+ continue;
}
- ret = nfs_read(nfs, nfsfh, sizeof(buf), buf);
- if (ret < 0) {
- printf("Error reading file\n");
+
+ snprintf(path, 1024, "%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;
}
- 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);
+
+ switch (st.st_mode & S_IFMT) {
+ case S_IFLNK:
+ 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", 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);
- ret = nfs_open(nfs, NFSFILEW, O_WRONLY, &nfsfh);
- if (ret != 0) {
- printf("Failed to open(%s) %s\n", NFSFILEW, nfs_get_error(nfs));
- exit(10);
- }
- ret = nfs_pwrite(nfs, nfsfh, 0, 16, buf);
- if (ret < 0) {
- printf("Failed to pwrite(%s) %s\n", NFSFILEW, nfs_get_error(nfs));
- exit(10);
- }
- ret = nfs_fsync(nfs, nfsfh);
- if (ret < 0) {
- printf("Failed to fsync(%s) %s\n", NFSFILEW, nfs_get_error(nfs));
- exit(10);
- }
- ret = nfs_close(nfs, nfsfh);
- if (ret < 0) {
- printf("Failed to close(%s) %s\n", NFSFILEW, nfs_get_error(nfs));
- exit(10);
+finished:
+ free(server);
+ free(path);
+ if (nfs != NULL) {
+ nfs_destroy_context(nfs);
}
-
-
- ret = nfs_statvfs(nfs, NFSDIR, &svfs);
- if (ret < 0) {
- printf("Failed to statvfs(%s) %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(nfs, NFSFILE, R_OK);
- if (ret != 0) {
- printf("Failed to access(%s) %s\n", NFSFILE, nfs_get_error(nfs));
- }
-
- /* become root */
- nfs_set_auth(nfs, authunix_create("Ronnies-Laptop", 0, 0, 0, NULL));
-
- ret = nfs_link(nfs, NFSFILE, NFSFILER);
- if (ret != 0) {
- printf("Failed to link(%s) %s\n", NFSFILE, nfs_get_error(nfs));
- }
-
-
- nfs_destroy_context(nfs);
- printf("nfsclient finished\n");
return 0;
}