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_chmod <url> <cwd> <path> <mode(octal)>"
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 mode;
47 
48 	if (argc != 5) {
49 		usage();
50 	}
51 
52         mode = strtol(argv[4], NULL, 8);
53 
54 	nfs = nfs_init_context();
55 	if (nfs == NULL) {
56 		printf("failed to init context\n");
57 		exit(1);
58 	}
59 
60 	url = nfs_parse_url_full(nfs, argv[1]);
61 	if (url == NULL) {
62 		fprintf(stderr, "%s\n", nfs_get_error(nfs));
63 		exit(1);
64 	}
65 
66 	if (nfs_mount(nfs, url->server, url->path) != 0) {
67  		fprintf(stderr, "Failed to mount nfs share : %s\n",
68 			nfs_get_error(nfs));
69 		ret = 1;
70 		goto finished;
71 	}
72 
73 	if (nfs_chdir(nfs, argv[2]) != 0) {
74  		fprintf(stderr, "Failed to chdir to \"%s\" : %s\n",
75 			argv[2], nfs_get_error(nfs));
76 		ret = 1;
77 		goto finished;
78 	}
79 
80 	if (nfs_chmod(nfs, argv[3], mode)) {
81  		fprintf(stderr, "Failed to chmod(): %s\n",
82 			nfs_get_error(nfs));
83 		ret = 1;
84 		goto finished;
85 	}
86 
87 finished:
88 	nfs_destroy_url(url);
89 	nfs_destroy_context(nfs);
90 
91 	return ret;
92 }
93