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/stat.h>
29 #include <sys/types.h>
30 #include <unistd.h>
31 
32 #include "libnfs.h"
33 
usage(void)34 void usage(void)
35 {
36 	fprintf(stderr, "Usage: prog_chown <url> <cwd> <path> <uid> <gid>"
37                 "\n");
38 	exit(1);
39 }
40 
main(int argc,char * argv[])41 int main(int argc, char *argv[])
42 {
43 	struct nfs_context *nfs = NULL;
44 	struct nfs_url *url = NULL;
45 	int ret = 0;
46         int uid, gid;
47 
48 	if (argc != 6) {
49 		usage();
50 	}
51 
52         uid = strtol(argv[4], NULL, 10);
53         gid = strtol(argv[5], NULL, 10);
54 
55 	nfs = nfs_init_context();
56 	if (nfs == NULL) {
57 		printf("failed to init context\n");
58 		exit(1);
59 	}
60 
61 	url = nfs_parse_url_full(nfs, argv[1]);
62 	if (url == NULL) {
63 		fprintf(stderr, "%s\n", nfs_get_error(nfs));
64 		exit(1);
65 	}
66 
67 	if (nfs_mount(nfs, url->server, url->path) != 0) {
68  		fprintf(stderr, "Failed to mount nfs share : %s\n",
69 			nfs_get_error(nfs));
70 		ret = 1;
71 		goto finished;
72 	}
73 
74 	if (nfs_chdir(nfs, argv[2]) != 0) {
75  		fprintf(stderr, "Failed to chdir to \"%s\" : %s\n",
76 			argv[2], nfs_get_error(nfs));
77 		ret = 1;
78 		goto finished;
79 	}
80 
81 	if (nfs_chown(nfs, argv[3], uid, gid)) {
82  		fprintf(stderr, "Failed to chown(): %s\n",
83 			nfs_get_error(nfs));
84 		ret = 1;
85 		goto finished;
86 	}
87 
88 finished:
89 	nfs_destroy_url(url);
90 	nfs_destroy_context(nfs);
91 
92 	return ret;
93 }
94