1 /* -*-  mode:c; tab-width:8; c-basic-offset:8; indent-tabs-mode:nil;  -*- */
2 /*
3    Copyright (C) by Ronnie Sahlberg <ronniesahlberg@gmail.com> 2017
4 
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9 
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #define _FILE_OFFSET_BITS 64
20 #define _GNU_SOURCE
21 
22 #include <fcntl.h>
23 #include <inttypes.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <stdint.h>
27 #include <string.h>
28 #include <sys/statvfs.h>
29 #include <sys/types.h>
30 
31 #include "libnfs.h"
32 
usage(void)33 void usage(void)
34 {
35 	fprintf(stderr, "Usage: prog_statvfs <url> <cwd> <path>\n");
36 	exit(1);
37 }
38 
main(int argc,char * argv[])39 int main(int argc, char *argv[])
40 {
41 	struct nfs_context *nfs = NULL;
42 	struct nfs_url *url = NULL;
43 	struct nfs_statvfs_64 svfs;
44 	int ret = 0;
45 
46 	if (argc != 4) {
47 		usage();
48 	}
49 
50 	nfs = nfs_init_context();
51 	if (nfs == NULL) {
52 		printf("failed to init context\n");
53 		exit(1);
54 	}
55 
56 	nfs_set_timeout(nfs, 300);
57 
58 	url = nfs_parse_url_full(nfs, argv[1]);
59 	if (url == NULL) {
60 		fprintf(stderr, "%s\n", nfs_get_error(nfs));
61 		exit(1);
62 	}
63 
64 	if (nfs_mount(nfs, url->server, url->path) != 0) {
65  		fprintf(stderr, "Failed to mount nfs share : %s\n",
66 			nfs_get_error(nfs));
67 		ret = 1;
68 		goto finished;
69 	}
70 
71 	if (nfs_chdir(nfs, argv[2]) != 0) {
72  		fprintf(stderr, "Failed to chdir to \"%s\" : %s\n",
73 			argv[2], nfs_get_error(nfs));
74 		ret = 1;
75 		goto finished;
76 	}
77 
78 	if (nfs_statvfs64(nfs, argv[3], &svfs)) {
79 		fprintf(stderr, "statvfs64 failed : %s\n",
80 			nfs_get_error(nfs));
81 		ret = 1;
82 		goto finished;
83 	}
84 
85 	printf("bsize:%" PRIu64 "\n", svfs.f_bsize);
86 	printf("frsize:%" PRIu64 "\n", svfs.f_frsize);
87 	printf("blocks:%" PRIu64 "\n", svfs.f_blocks);
88 	printf("bfree:%" PRIu64 "\n", svfs.f_bfree);
89 	printf("bavail:%" PRIu64 "\n", svfs.f_bavail);
90 	printf("files:%" PRIu64 "\n", svfs.f_files);
91 	printf("ffree:%" PRIu64 "\n", svfs.f_ffree);
92 	printf("favail:%" PRIu64 "\n", svfs.f_favail);
93 	printf("fsid:%" PRIu64 "\n", svfs.f_fsid);
94 	printf("namemax:%" PRIu64 "\n", svfs.f_namemax);
95 
96 finished:
97 	nfs_destroy_url(url);
98 	nfs_destroy_context(nfs);
99 
100 	return ret;
101 }
102