summaryrefslogtreecommitdiff
path: root/draft/fuse-examples/fioclient.c
blob: 5f05525f25ced1405c54862992b756e584521a62 (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
/*
  FUSE fioclient: FUSE ioctl example client
  Copyright (C) 2008       SUSE Linux Products GmbH
  Copyright (C) 2008       Tejun Heo <teheo@suse.de>

  This program can be distributed under the terms of the GNU GPL.
  See the file COPYING.

  gcc -Wall fioclient.c -o fioclient
*/

#include <sys/types.h>
#include <sys/fcntl.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
#include "fioc.h"

const char *usage =
"Usage: fioclient FIOC_FILE COMMAND\n"
"\n"
"COMMANDS\n"
"  s [SIZE]     : get size if SIZE is omitted, set size otherwise\n"
"  r SIZE [OFF] : read SIZE bytes @ OFF (dfl 0) and output to stdout\n"
"  w SIZE [OFF] : write SIZE bytes @ OFF (dfl 0) from stdin\n"
"\n";

static int do_rw(int fd, int is_read, size_t size, off_t offset,
		 size_t *prev_size, size_t *new_size)
{
	struct fioc_rw_arg arg = { .offset = offset };
	ssize_t ret;

	arg.buf = calloc(1, size);
	if (!arg.buf) {
		fprintf(stderr, "failed to allocated %zu bytes\n", size);
		return -1;
	}

	if (is_read) {
		arg.size = size;
		ret = ioctl(fd, FIOC_READ, &arg);
		if (ret >= 0)
			fwrite(arg.buf, 1, ret, stdout);
	} else {
		arg.size = fread(arg.buf, 1, size, stdin);
		fprintf(stderr, "Writing %zu bytes\n", arg.size);
		ret = ioctl(fd, FIOC_WRITE, &arg);
	}

	if (ret >= 0) {
		*prev_size = arg.prev_size;
		*new_size = arg.new_size;
	} else
		perror("ioctl");

	free(arg.buf);
	return ret;
}

int main(int argc, char **argv)
{
	size_t param[2] = { };
	size_t size, prev_size = 0, new_size = 0;
	char cmd;
	int fd, i, rc;

	if (argc < 3)
		goto usage;

	fd = open(argv[1], O_RDWR);
	if (fd < 0) {
		perror("open");
		return 1;
	}

	cmd = tolower(argv[2][0]);
	argc -= 3;
	argv += 3;

	for (i = 0; i < argc; i++) {
		char *endp;
		param[i] = strtoul(argv[i], &endp, 0);
		if (endp == argv[i] || *endp != '\0')
			goto usage;
	}

	switch (cmd) {
	case 's':
		if (!argc) {
			if (ioctl(fd, FIOC_GET_SIZE, &size)) {
				perror("ioctl");
				return 1;
			}
			printf("%zu\n", size);
		} else {
			size = param[0];
			if (ioctl(fd, FIOC_SET_SIZE, &size)) {
				perror("ioctl");
				return 1;
			}
		}
		return 0;

	case 'r':
	case 'w':
		rc = do_rw(fd, cmd == 'r', param[0], param[1],
			   &prev_size, &new_size);
		if (rc < 0)
			return 1;
		fprintf(stderr, "transferred %d bytes (%zu -> %zu)\n",
			rc, prev_size, new_size);
		return 0;
	}

 usage:
	fprintf(stderr, "%s", usage);
	return 1;
}