xref: /dragonfly/sbin/hammer2/cmd_remote.c (revision c127c292)
12910a90cSMatthew Dillon /*
22910a90cSMatthew Dillon  * Copyright (c) 2011-2012 The DragonFly Project.  All rights reserved.
32910a90cSMatthew Dillon  *
42910a90cSMatthew Dillon  * This code is derived from software contributed to The DragonFly Project
52910a90cSMatthew Dillon  * by Matthew Dillon <dillon@dragonflybsd.org>
62910a90cSMatthew Dillon  * by Venkatesh Srinivas <vsrinivas@dragonflybsd.org>
72910a90cSMatthew Dillon  *
82910a90cSMatthew Dillon  * Redistribution and use in source and binary forms, with or without
92910a90cSMatthew Dillon  * modification, are permitted provided that the following conditions
102910a90cSMatthew Dillon  * are met:
112910a90cSMatthew Dillon  *
122910a90cSMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
132910a90cSMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
142910a90cSMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
152910a90cSMatthew Dillon  *    notice, this list of conditions and the following disclaimer in
162910a90cSMatthew Dillon  *    the documentation and/or other materials provided with the
172910a90cSMatthew Dillon  *    distribution.
182910a90cSMatthew Dillon  * 3. Neither the name of The DragonFly Project nor the names of its
192910a90cSMatthew Dillon  *    contributors may be used to endorse or promote products derived
202910a90cSMatthew Dillon  *    from this software without specific, prior written permission.
212910a90cSMatthew Dillon  *
222910a90cSMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
232910a90cSMatthew Dillon  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
242910a90cSMatthew Dillon  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
252910a90cSMatthew Dillon  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
262910a90cSMatthew Dillon  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
272910a90cSMatthew Dillon  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
282910a90cSMatthew Dillon  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
292910a90cSMatthew Dillon  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
302910a90cSMatthew Dillon  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
312910a90cSMatthew Dillon  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
322910a90cSMatthew Dillon  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
332910a90cSMatthew Dillon  * SUCH DAMAGE.
342910a90cSMatthew Dillon  */
352910a90cSMatthew Dillon 
362910a90cSMatthew Dillon #include "hammer2.h"
372910a90cSMatthew Dillon 
382910a90cSMatthew Dillon int
cmd_remote_connect(const char * sel_path,const char * url)392910a90cSMatthew Dillon cmd_remote_connect(const char *sel_path, const char *url)
402910a90cSMatthew Dillon {
412910a90cSMatthew Dillon 	hammer2_ioc_remote_t remote;
422910a90cSMatthew Dillon 	int ecode = 0;
432910a90cSMatthew Dillon 	int fd;
442910a90cSMatthew Dillon 
452910a90cSMatthew Dillon 	if ((fd = hammer2_ioctl_handle(sel_path)) < 0)
462910a90cSMatthew Dillon 		return(1);
472910a90cSMatthew Dillon 	bzero(&remote, sizeof(remote));
482910a90cSMatthew Dillon 	remote.copyid = -1;
492910a90cSMatthew Dillon 	remote.fd = -1;
502910a90cSMatthew Dillon 	if (strlen(url) >= sizeof(remote.copy1.path)) {
512910a90cSMatthew Dillon 		fprintf(stderr, "hammer2: connect: Path too long\n");
522910a90cSMatthew Dillon 		close(fd);
532910a90cSMatthew Dillon 		return(1);
542910a90cSMatthew Dillon 	}
55*c127c292STomohiro Kusumi 	snprintf((char*)remote.copy1.path, sizeof(remote.copy1.path), "%s",
56*c127c292STomohiro Kusumi 		 url);
57ae183399SMatthew Dillon 	if (ioctl(fd, HAMMER2IOC_REMOTE_ADD, &remote) < 0) {
582910a90cSMatthew Dillon 		perror("ioctl");
592910a90cSMatthew Dillon 		ecode = 1;
602910a90cSMatthew Dillon 	}
612910a90cSMatthew Dillon 	close(fd);
62a1b07447SMatthew Dillon 	return ecode;
632910a90cSMatthew Dillon }
642910a90cSMatthew Dillon 
652910a90cSMatthew Dillon int
cmd_remote_disconnect(const char * sel_path,const char * url)662910a90cSMatthew Dillon cmd_remote_disconnect(const char *sel_path, const char *url)
672910a90cSMatthew Dillon {
682910a90cSMatthew Dillon 	hammer2_ioc_remote_t remote;
692910a90cSMatthew Dillon 	int ecode = 0;
702910a90cSMatthew Dillon 	int fd;
712910a90cSMatthew Dillon 
722910a90cSMatthew Dillon 	if ((fd = hammer2_ioctl_handle(sel_path)) < 0)
732910a90cSMatthew Dillon 		return(1);
742910a90cSMatthew Dillon 	bzero(&remote, sizeof(remote));
752910a90cSMatthew Dillon 	remote.copyid = -1;
762910a90cSMatthew Dillon 	remote.fd = -1;
772910a90cSMatthew Dillon 	if (strlen(url) >= sizeof(remote.copy1.path)) {
782910a90cSMatthew Dillon 		fprintf(stderr, "hammer2: disconnect: Path too long\n");
792910a90cSMatthew Dillon 		close(fd);
802910a90cSMatthew Dillon 		return(1);
812910a90cSMatthew Dillon 	}
82*c127c292STomohiro Kusumi 	snprintf((char*)remote.copy1.path, sizeof(remote.copy1.path), "%s",
83*c127c292STomohiro Kusumi 		 url);
84ae183399SMatthew Dillon 	if (ioctl(fd, HAMMER2IOC_REMOTE_DEL, &remote) < 0) {
852910a90cSMatthew Dillon 		perror("ioctl");
862910a90cSMatthew Dillon 		ecode = 1;
872910a90cSMatthew Dillon 	}
882910a90cSMatthew Dillon 	close(fd);
89a1b07447SMatthew Dillon 	return ecode;
902910a90cSMatthew Dillon }
912910a90cSMatthew Dillon 
922910a90cSMatthew Dillon int
cmd_remote_status(const char * sel_path,int all_opt __unused)932910a90cSMatthew Dillon cmd_remote_status(const char *sel_path, int all_opt __unused)
942910a90cSMatthew Dillon {
952910a90cSMatthew Dillon 	hammer2_ioc_remote_t remote;
962910a90cSMatthew Dillon 	int ecode = 0;
972910a90cSMatthew Dillon 	int count = 0;
982910a90cSMatthew Dillon 	int fd;
992910a90cSMatthew Dillon 
1002910a90cSMatthew Dillon 	if ((fd = hammer2_ioctl_handle(sel_path)) < 0)
1012910a90cSMatthew Dillon 		return(1);
1022910a90cSMatthew Dillon 	bzero(&remote, sizeof(remote));
1032910a90cSMatthew Dillon 
1042910a90cSMatthew Dillon 	while ((remote.copyid = remote.nextid) >= 0) {
1051a34728cSMatthew Dillon 		if (ioctl(fd, HAMMER2IOC_REMOTE_SCAN, &remote) < 0) {
1062910a90cSMatthew Dillon 			perror("ioctl");
1072910a90cSMatthew Dillon 			ecode = 1;
1082910a90cSMatthew Dillon 			break;
1092910a90cSMatthew Dillon 		}
1102910a90cSMatthew Dillon 		if (remote.copy1.copyid == 0)
1112910a90cSMatthew Dillon 			continue;
1122910a90cSMatthew Dillon 		if (count == 0)
1132910a90cSMatthew Dillon 			printf("CPYID LABEL           STATUS PATH\n");
1142910a90cSMatthew Dillon 		printf("%5d %-15s %c%c%c.%02x %s\n",
1152910a90cSMatthew Dillon 			remote.copy1.copyid,
1162910a90cSMatthew Dillon 			remote.copy1.label,
1172910a90cSMatthew Dillon 			'-', '-', '-',
1182910a90cSMatthew Dillon 			remote.copy1.priority,
1192910a90cSMatthew Dillon 			remote.copy1.path);
1202910a90cSMatthew Dillon 		++count;
1212910a90cSMatthew Dillon 	}
1222910a90cSMatthew Dillon 	if (count == 0)
1232910a90cSMatthew Dillon 		printf("No linkages found\n");
1242910a90cSMatthew Dillon 	return (ecode);
1252910a90cSMatthew Dillon }
126