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/>.
|
|
|
|
*/
|
|
|
|
|
2012-11-17 22:01:01 +04:00
|
|
|
#define _FILE_OFFSET_BITS 64
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
|
2011-02-06 07:45:09 +03:00
|
|
|
/* Example program using the highlevel sync interface
|
|
|
|
*/
|
2011-09-01 23:35:54 +04:00
|
|
|
#ifdef WIN32
|
|
|
|
#include "win32_compat.h"
|
|
|
|
#else
|
2012-11-17 22:01:01 +04:00
|
|
|
#include <string.h>
|
2011-09-01 23:35:54 +04:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/statvfs.h>
|
|
|
|
#endif
|
|
|
|
|
2011-08-28 13:24:18 +04:00
|
|
|
|
|
|
|
#if defined(WIN32)
|
|
|
|
#pragma comment(lib, "ws2_32.lib")
|
|
|
|
WSADATA wsaData;
|
|
|
|
#else
|
|
|
|
#include <sys/statvfs.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2011-02-06 07:45:09 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
2012-11-17 22:01:01 +04:00
|
|
|
#include <inttypes.h>
|
2011-02-06 07:45:09 +03:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
2012-07-04 10:53:12 +04:00
|
|
|
#include "libnfs-zdr.h"
|
2011-02-06 07:45:09 +03:00
|
|
|
#include "libnfs.h"
|
2012-11-17 22:01:01 +04:00
|
|
|
#include <rpc/rpc.h> /* for authunix_create() */
|
|
|
|
#include <popt.h>
|
2011-06-19 16:31:34 +04:00
|
|
|
#include "libnfs-raw.h"
|
|
|
|
#include "libnfs-raw-mount.h"
|
2011-02-06 07:45:09 +03:00
|
|
|
|
|
|
|
struct client {
|
|
|
|
char *server;
|
|
|
|
char *export;
|
|
|
|
uint32_t mount_port;
|
|
|
|
int is_finished;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-11-17 22:01:01 +04:00
|
|
|
char buf[3*1024*1024+337];
|
2011-09-12 21:58:23 +04:00
|
|
|
|
2012-11-17 22:01:01 +04:00
|
|
|
void print_usage(void)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Usage: nfsclient-sync [-?|--help] [--usage] <url>\n");
|
2011-09-12 21:58:23 +04:00
|
|
|
}
|
|
|
|
|
2012-11-17 22:01:01 +04:00
|
|
|
void print_help(void)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Usage: nfsclient-sync [OPTION...] <url>\n");
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
fprintf(stderr, "Help options:\n");
|
|
|
|
fprintf(stderr, " -?, --help Show this help message\n");
|
|
|
|
fprintf(stderr, " --usage Display brief usage message\n");
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
fprintf(stderr, "NFS URL format : nfs://<server>/<export-path>\n");
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
fprintf(stderr, "<host> is either of:\n");
|
|
|
|
fprintf(stderr, " \"hostname\" nfs.example\n");
|
|
|
|
fprintf(stderr, " \"ipv4-address\" 10.1.1.27\n");
|
|
|
|
fprintf(stderr, " \"ipv6-address\" [fce0::1]\n");
|
|
|
|
}
|
2011-06-19 18:56:47 +04:00
|
|
|
|
2012-11-17 22:01:01 +04:00
|
|
|
int main(int argc, char *argv[])
|
2011-02-06 07:45:09 +03:00
|
|
|
{
|
2012-11-17 22:01:01 +04:00
|
|
|
struct nfs_context *nfs = NULL;
|
|
|
|
int i, ret, res;
|
2011-11-27 07:49:08 +04:00
|
|
|
uint64_t offset;
|
2011-02-06 07:45:09 +03:00
|
|
|
struct client client;
|
|
|
|
struct stat st;
|
|
|
|
struct nfsfh *nfsfh;
|
|
|
|
struct nfsdir *nfsdir;
|
|
|
|
struct nfsdirent *nfsdirent;
|
|
|
|
struct statvfs svfs;
|
2011-06-19 16:31:34 +04:00
|
|
|
exports export, tmp;
|
2012-11-17 22:01:01 +04:00
|
|
|
int show_help = 0, show_usage = 0;
|
|
|
|
poptContext pc;
|
|
|
|
const char **extra_argv;
|
|
|
|
int extra_argc = 0;
|
|
|
|
const char *url = NULL;
|
|
|
|
char *server = NULL, *path = NULL, *strp;
|
|
|
|
|
|
|
|
struct poptOption popt_options[] = {
|
|
|
|
{ "help", '?', POPT_ARG_NONE, &show_help, 0, "Show this help message", NULL },
|
|
|
|
{ "usage", 0, POPT_ARG_NONE, &show_usage, 0, "Display brief usage message", NULL },
|
|
|
|
POPT_TABLEEND
|
|
|
|
};
|
2011-06-19 16:31:34 +04:00
|
|
|
|
2011-08-28 13:24:18 +04:00
|
|
|
#if defined(WIN32)
|
|
|
|
if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
|
|
|
|
printf("Failed to start Winsock2\n");
|
|
|
|
exit(10);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-11-17 22:01:01 +04:00
|
|
|
pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_POSIXMEHARDER);
|
|
|
|
if ((res = poptGetNextOpt(pc)) < -1) {
|
|
|
|
fprintf(stderr, "Failed to parse option : %s %s\n",
|
|
|
|
poptBadOption(pc, 0), poptStrerror(res));
|
2011-02-06 07:45:09 +03:00
|
|
|
exit(10);
|
|
|
|
}
|
2012-11-17 22:01:01 +04:00
|
|
|
extra_argv = poptGetArgs(pc);
|
|
|
|
if (extra_argv) {
|
|
|
|
url = *extra_argv;
|
|
|
|
extra_argv++;
|
|
|
|
while (extra_argv[extra_argc]) {
|
|
|
|
extra_argc++;
|
|
|
|
}
|
2011-02-06 07:45:09 +03:00
|
|
|
}
|
2012-11-17 22:01:01 +04:00
|
|
|
poptFreeContext(pc);
|
2011-02-06 07:45:09 +03:00
|
|
|
|
2012-11-17 22:01:01 +04:00
|
|
|
if (show_help != 0) {
|
|
|
|
print_help();
|
|
|
|
exit(0);
|
2011-02-06 07:45:09 +03:00
|
|
|
}
|
|
|
|
|
2012-11-17 22:01:01 +04:00
|
|
|
if (show_usage != 0) {
|
|
|
|
print_usage();
|
|
|
|
exit(0);
|
2011-02-06 07:45:09 +03:00
|
|
|
}
|
|
|
|
|
2012-11-17 22:01:01 +04:00
|
|
|
if (url == NULL) {
|
|
|
|
fprintf(stderr, "No URL specified.\n");
|
|
|
|
print_usage();
|
|
|
|
exit(0);
|
2011-02-06 07:45:09 +03:00
|
|
|
}
|
2012-11-17 22:01:01 +04:00
|
|
|
|
|
|
|
if (strncmp(url, "nfs://", 6)) {
|
|
|
|
fprintf(stderr, "Invalid URL specified.\n");
|
|
|
|
print_usage();
|
|
|
|
exit(0);
|
2011-02-06 07:45:09 +03:00
|
|
|
}
|
2012-11-17 22:01:01 +04:00
|
|
|
|
|
|
|
server = strdup(url + 6);
|
|
|
|
if (server == NULL) {
|
|
|
|
fprintf(stderr, "Failed to strdup server string\n");
|
2011-06-25 09:05:50 +04:00
|
|
|
exit(10);
|
|
|
|
}
|
2012-11-17 22:01:01 +04:00
|
|
|
if (server[0] == '/' || server[0] == '\0') {
|
|
|
|
fprintf(stderr, "Invalid server string.\n");
|
|
|
|
free(server);
|
2011-06-25 09:05:50 +04:00
|
|
|
exit(10);
|
|
|
|
}
|
2012-11-17 22:01:01 +04:00
|
|
|
strp = strchr(server, '/');
|
|
|
|
if (strp == NULL) {
|
|
|
|
fprintf(stderr, "Invalid URL specified.\n");
|
|
|
|
print_usage();
|
|
|
|
free(server);
|
|
|
|
exit(0);
|
2011-06-25 09:05:50 +04:00
|
|
|
}
|
2012-11-17 22:01:01 +04:00
|
|
|
path = strdup(strp);
|
|
|
|
if (path == NULL) {
|
|
|
|
fprintf(stderr, "Failed to strdup server string\n");
|
|
|
|
free(server);
|
2011-06-25 09:05:50 +04:00
|
|
|
exit(10);
|
|
|
|
}
|
2012-11-17 22:01:01 +04:00
|
|
|
if (path[0] != '/') {
|
|
|
|
fprintf(stderr, "Invalid path.\n");
|
|
|
|
free(server);
|
|
|
|
free(path);
|
2011-06-25 09:05:50 +04:00
|
|
|
exit(10);
|
|
|
|
}
|
2012-11-17 22:01:01 +04:00
|
|
|
*strp = 0;
|
|
|
|
|
|
|
|
client.server = server;
|
|
|
|
client.export = path;
|
|
|
|
client.is_finished = 0;
|
2011-02-06 07:45:09 +03:00
|
|
|
|
|
|
|
|
2012-11-17 22:01:01 +04:00
|
|
|
nfs = nfs_init_context();
|
|
|
|
if (nfs == NULL) {
|
|
|
|
printf("failed to init context\n");
|
|
|
|
goto finished;
|
2011-02-06 07:45:09 +03:00
|
|
|
}
|
|
|
|
|
2012-11-17 22:01:01 +04:00
|
|
|
ret = nfs_mount(nfs, client.server, client.export);
|
2011-02-06 07:45:09 +03:00
|
|
|
if (ret != 0) {
|
2012-11-17 22:01:01 +04:00
|
|
|
printf("Failed to mount nfs share : %s\n", nfs_get_error(nfs));
|
|
|
|
goto finished;
|
2011-02-06 07:45:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-17 22:01:01 +04:00
|
|
|
ret = nfs_opendir(nfs, "/", &nfsdir);
|
2011-02-06 07:45:09 +03:00
|
|
|
if (ret != 0) {
|
2012-11-17 22:01:01 +04:00
|
|
|
printf("Failed to opendir(\"/\")\n", nfs_get_error(nfs));
|
2011-02-06 07:45:09 +03:00
|
|
|
exit(10);
|
|
|
|
}
|
|
|
|
while((nfsdirent = nfs_readdir(nfs, nfsdir)) != NULL) {
|
2012-11-17 22:01:01 +04:00
|
|
|
char path[1024];
|
|
|
|
|
|
|
|
if (!strcmp(nfsdirent->name, ".") || !strcmp(nfsdirent->name, "..")) {
|
|
|
|
continue;
|
2011-06-25 09:05:50 +04:00
|
|
|
}
|
2012-11-17 22:01:01 +04:00
|
|
|
|
|
|
|
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;
|
2011-06-25 09:05:50 +04:00
|
|
|
}
|
2012-11-17 22:01:01 +04:00
|
|
|
|
|
|
|
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;
|
2011-06-25 09:05:50 +04:00
|
|
|
}
|
2012-11-17 22:01:01 +04:00
|
|
|
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);
|
2011-02-06 07:45:09 +03:00
|
|
|
}
|
|
|
|
nfs_closedir(nfs, nfsdir);
|
|
|
|
|
|
|
|
|
2012-11-17 22:01:01 +04:00
|
|
|
finished:
|
|
|
|
free(server);
|
|
|
|
free(path);
|
|
|
|
if (nfs != NULL) {
|
|
|
|
nfs_destroy_context(nfs);
|
2011-02-06 07:45:09 +03:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2011-06-19 16:31:34 +04:00
|
|
|
|