xref: /haiku/src/add-ons/kernel/file_systems/netfs/netfs_config/netfs_config.cpp (revision 610f99c838cb661ff85377789ffd3ad4ff672a08)
1 // netfs_config.cpp
2 
3 #include <errno.h>
4 #include <fcntl.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <unistd.h>
9 
10 #include <SupportDefs.h>
11 
12 #include "netfs_ioctl.h"
13 
14 // usage
15 static const char* kUsage =
16 "Usage: netfs_config -h | --help\n"
17 "       netfs_config <mount point> -a <server name>\n"
18 "       netfs_config <mount point> -r <server name>\n"
19 "options:\n"
20 "  -a                - adds the supplied server\n"
21 "  -h, --help        - print this text\n"
22 "  -r                - removes the supplied server\n"
23 ;
24 
25 // print_usage
26 static
27 void
28 print_usage(bool error)
29 {
30 	fputs(kUsage, (error ? stderr : stdout));
31 }
32 
33 // add_server
34 static
35 status_t
36 add_server(int fd, const char* serverName)
37 {
38 	netfs_ioctl_add_server params;
39 	if (strlen(serverName) >= sizeof(params.serverName))
40 		return B_BAD_VALUE;
41 	strcpy(params.serverName, serverName);
42 	if (ioctl(fd, NET_FS_IOCTL_ADD_SERVER, &params) < 0)
43 		return errno;
44 	return B_OK;
45 }
46 
47 // remove_server
48 static
49 status_t
50 remove_server(int fd, const char* serverName)
51 {
52 	netfs_ioctl_remove_server params;
53 	if (strlen(serverName) >= sizeof(params.serverName))
54 		return B_BAD_VALUE;
55 	strcpy(params.serverName, serverName);
56 	if (ioctl(fd, NET_FS_IOCTL_REMOVE_SERVER, &params) < 0)
57 		return errno;
58 	return B_OK;
59 }
60 
61 // main
62 int
63 main(int argc, char** argv)
64 {
65 	// parse the arguments
66 	if (argc < 2) {
67 		print_usage(true);
68 		return 1;
69 	}
70 	if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0) {
71 		print_usage(false);
72 		return 0;
73 	}
74 	// add or remove
75 	if (argc != 4) {
76 		print_usage(true);
77 		return 1;
78 	}
79 	const char* mountPoint = argv[1];
80 	bool add = false;
81 	if (strcmp(argv[2], "-a") == 0) {
82 		add = true;
83 	} else if (strcmp(argv[2], "-r") == 0) {
84 		add = false;
85 	} else {
86 		print_usage(true);
87 		return 1;
88 	}
89 	const char* serverName = argv[3];
90 	// open the mount point
91 	int fd = open(mountPoint, O_RDONLY);
92 	if (fd < 0) {
93 		fprintf(stderr, "Opening `%s' failed: %s\n", mountPoint,
94 			strerror(errno));
95 		return 1;
96 	}
97 	// do the ioctl
98 	status_t error = B_OK;
99 	if (add)
100 		error = add_server(fd, serverName);
101 	else
102 		error = remove_server(fd, serverName);
103 	if (error != B_OK)
104 		fprintf(stderr, "Operation failed: %s\n", strerror(error));
105 	// clean up
106 	close(fd);
107 	return (error == B_OK ? 0 : 1);
108 }
109 
110