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