summaryrefslogtreecommitdiff
path: root/usr/src/cmd/fs.d/hyprlofs/hlcfg/hlcfg.c
blob: 16e8e32b1ce034d6a3c2bf90d156bf76c8af5884 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2012, Joyent, Inc.  All rights reserved.
 */

/*
 * This is a simple test program to exercise the hyprlofs ioctls.  This is
 * not designed as a full featured CLI and only does minimal error checking
 * and reporting.
 */
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <libgen.h>
#include <strings.h>
#include <sys/errno.h>
#include <sys/fs/hyprlofs.h>

extern int errno;

char *usage =	"usage: <fs path> add [<file name> <alias>]+\n"
		"       <fs path> addl [<file name>]+\n"
		"       <fs path> rm [<alias>]+\n"
		"       <fs path> clear"
		"       <fs path> get";

typedef enum {
	CMD_ADD,
	CMD_RM,
	CMD_CLR,
	CMD_ADDL,
	CMD_GET
} cmd_t;

static int
get_entries(int fd)
{
	int err;
	int i;
	hyprlofs_curr_entries_t e;
	hyprlofs_curr_entry_t *ep;

	e.hce_cnt = 0;
	e.hce_entries = NULL;

	err = ioctl(fd, HYPRLOFS_GET_ENTRIES, &e);
	if (err != 0 && errno != E2BIG) {
		perror("ioctl");
		return (1);
	}

	if (err == 0) {
		(void) printf("success, but no entries\n");
		return (0);
	}

	/*
	 * E2BIG is what we expect when there are existing mappings
	 * since the current cnt is still returned in that case.
	 */
	(void) printf("cnt: %d\n", e.hce_cnt);

	/* alloc array and call again, then print array */
	if ((ep = (hyprlofs_curr_entry_t *)
	    malloc(sizeof (hyprlofs_curr_entry_t) * e.hce_cnt)) == NULL) {
		(void) fprintf(stderr, "out of memory\n");
		exit(1);
	}

	e.hce_entries = ep;
	errno = 0;
	if (ioctl(fd, HYPRLOFS_GET_ENTRIES, &e) != 0) {
		/*
		 * Not handling an increase here. We would need to free and
		 * start over to do that, but ok for a test program.
		 */
		perror("ioctl");
		free(ep);
		return (1);
	}
	for (i = 0; i < e.hce_cnt; i++)
		(void) printf("%s %s\n", ep[i].hce_path, ep[i].hce_name);

	free(ep);
	return (0);
}

int
main(int argc, char **argv)
{
	int i, ap;
	cmd_t cmd;
	int cnt = 0;
	int fd;
	int rv = 0;
	hyprlofs_entry_t *e = NULL;
	hyprlofs_entries_t ents;

	if (argc < 3) {
		(void) fprintf(stderr, "%s\n", usage);
		exit(1);
	}

	if ((fd = open(argv[1], O_RDONLY)) < 0) {
		perror("can't open hyprlofs mount");
		exit(1);
	}

	if (strcmp(argv[2], "add") == 0) {
		cmd = CMD_ADD;
	} else if (strcmp(argv[2], "rm") == 0) {
		cmd = CMD_RM;
	} else if (strcmp(argv[2], "clear") == 0) {
		cmd = CMD_CLR;
	} else if (strcmp(argv[2], "addl") == 0) {
		cmd = CMD_ADDL;
	} else if (strcmp(argv[2], "get") == 0) {
		cmd = CMD_GET;
	} else {
		(void) fprintf(stderr, "%s\n", usage);
		exit(1);
	}

	/* Count up the number of parameters. The arg format varies w/ cmd */
	switch (cmd) {
	case CMD_ADD:
		for (i = 3; i < argc; i++) {
			/* argv[i] is the file path */

			/* The next arg is the alias */
			if (++i >= argc) {
				(void) fprintf(stderr, "missing alias for %s\n",
				    argv[i - 1]);
				exit(1);
			}

			cnt++;
		}
		break;
	case CMD_ADDL:
		cnt = argc - 3;
		break;
	case CMD_RM:
		cnt = argc - 3;
		break;
	case CMD_CLR:	/*FALLTHRU*/
	case CMD_GET:
		if (argc > 3) {
			(void) fprintf(stderr, "%s\n", usage);
			exit(1);
		}
		break;
	}

	if (cnt > 0) {
		if ((e = (hyprlofs_entry_t *)malloc(sizeof (hyprlofs_entry_t) *
		    cnt)) == NULL) {
			(void) fprintf(stderr, "out of memory\n");
			exit(1);
		}
	}

	/*
	 * Format up the args.
	 * We only setup the path member for the add cmd.
	 * We won't run this loop for the clear cmd.
	 * The addl command is special since we use basename to get the alias.
	 */
	for (i = 0, ap = 3; i < cnt; i++, ap++) {
		if (cmd == CMD_ADDL) {
			e[i].hle_path = argv[ap];
			e[i].hle_plen = strlen(e[i].hle_path);

			e[i].hle_name = basename(argv[ap]);
			e[i].hle_nlen = strlen(e[i].hle_name);

			continue;
		}

		if (cmd == CMD_ADD) {
			e[i].hle_path = argv[ap++];
			e[i].hle_plen = strlen(e[i].hle_path);
		}

		e[i].hle_name = argv[ap];
		e[i].hle_nlen = strlen(e[i].hle_name);
	}

	ents.hle_entries = e;
	ents.hle_len = cnt;

	switch (cmd) {
	case CMD_ADD:	/*FALLTHRU*/
	case CMD_ADDL:
		if (ioctl(fd, HYPRLOFS_ADD_ENTRIES, &ents) < 0)  {
			perror("ioctl");
			rv = 1;
		}
		break;
	case CMD_RM:
		if (ioctl(fd, HYPRLOFS_RM_ENTRIES, &ents) < 0)  {
			perror("ioctl");
			rv = 1;
		}
		break;
	case CMD_CLR:
		if (ioctl(fd, HYPRLOFS_RM_ALL) < 0)  {
			perror("ioctl");
			rv = 1;
		}
		break;
	case CMD_GET:
		rv = get_entries(fd);
		break;
	}

	(void) close(fd);
	if (cnt > 0)
		free(e);
	return (rv);
}