Net-LibNFS

 view release on metacpan or  search on metacpan

libnfs/examples/nfs-io.c  view on Meta::CPAN

	struct nfs_url *url = NULL;
	fattr4_acl acl4;
	fattr3_acl acl3;
	int i;

#ifdef WIN32
	if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
		printf("Failed to start Winsock2\n");
		exit(10);
	}
#endif

#ifdef AROS
	aros_init_socket();
#endif

	if (argc < 3) {
		fprintf(stderr, "No URL specified.\n");
		goto finished;
	}

	nfs = nfs_init_context();
	if (nfs == NULL) {
		printf("failed to init context\n");
		goto finished;
	}

	url = nfs_parse_url_full(nfs, argv[argc - 1]);
	if (url == NULL) {
		fprintf(stderr, "%s\n", nfs_get_error(nfs));
		goto finished;
	}

	if (nfs_mount(nfs, url->server, url->path) != 0) {
 		fprintf(stderr, "Failed to mount nfs share : %s\n", nfs_get_error(nfs));
		goto finished;
	}

	if (!strncmp(argv[1], "creat", 5)) {
		ret = nfs_creat(nfs, url->file, 0600, &nfsfh);
	} else if (!strncmp(argv[1], "unlink", 6)) {
		ret = nfs_unlink(nfs, url->file);
	} else if (!strncmp(argv[1], "mkdir", 5)) {
		ret = nfs_mkdir(nfs, url->file);
	} else if (!strncmp(argv[1], "rmdir", 5)) {
		ret = nfs_rmdir(nfs, url->file);
	} else if (!strncmp(argv[1], "trunc", 5)) {
		ret = nfs_truncate(nfs, url->file, 0);
	} else if (!strncmp(argv[1], "touch", 5)) {
		struct timeval times[2];
		gettimeofday(&times[0], NULL);
		gettimeofday(&times[1], NULL);
		ret = nfs_utimes(nfs, url->file, times);
	} else if (!strncmp(argv[1], "chmod", 5)) {
		if (argc < 4) {
			fprintf(stderr, "Invalid arguments for chmod");
			goto finished;
		}
		int mode = strtol(argv[2], NULL, 8);
		ret = nfs_chmod(nfs, url->file, mode);
	} else if (!strncmp(argv[1], "chown", 5)) {
		if (argc < 5) {
			fprintf(stderr, "Invalid arguments for chown");
			goto finished;
		}
		int uid = strtol(argv[2], NULL, 10);
		int gid = strtol(argv[3], NULL, 10);
		ret = nfs_chown(nfs, url->file, uid, gid);
	} else if (!strncmp(argv[1], "stat", 4)) {
		struct nfs_stat_64 st;
		ret = nfs_stat64(nfs, url->file, &st);
		if (!ret) {
			switch (st.nfs_mode & S_IFMT) {
	#ifndef WIN32
			case S_IFLNK:
				printf("l");
				break;
	#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.nfs_mode & S_IRUSR)],
			       "-w"[!!(st.nfs_mode & S_IWUSR)],
			       "-xSs"[  !!(st.nfs_mode & S_IXUSR) +
				      2*!!(st.nfs_mode & S_ISUID)]
			       );
			printf("%c%c%c",
			       "-r"[!!(st.nfs_mode & S_IRGRP)],
			       "-w"[!!(st.nfs_mode & S_IWGRP)],
			       "-xSs"[  !!(st.nfs_mode & S_IXGRP) +
				      2*!!(st.nfs_mode & S_ISGID)]
			);
			printf("%c%c%c",
			       "-r"[!!(st.nfs_mode & S_IROTH)],
			       "-w"[!!(st.nfs_mode & S_IWOTH)],
			       "-xTt"[  !!(st.nfs_mode & S_IXOTH) +
				      2*!!(st.nfs_mode & S_ISVTX)]
			);
			printf(" %2d", (int)st.nfs_nlink);
			printf(" %5d", (int)st.nfs_uid);
			printf(" %5d", (int)st.nfs_gid);
			printf(" size: %12" PRId64, st.nfs_size);
			printf(" mtime: %lu %lu", st.nfs_mtime, st.nfs_mtime_nsec);
			printf("\n");
		}
	} else if (!strncmp(argv[1], "acl", 3)) {
		ret = nfs_open(nfs, url->file, 0600, &nfsfh);
		if (ret != 0) {
			printf("failed to open %s. %s\n", url->file, nfs_get_error(nfs));
			goto finished;
		}

		printf("ACL version:%d\n", nfs_get_version(nfs));
		
		if (nfs_get_version(nfs) == NFS_V3) {
			printf("Get v3 ACL\n");
			memset(&acl3, 0, sizeof(fattr3_acl));



( run in 0.963 second using v1.01-cache-2.11-cpan-71847e10f99 )