2 FUSE fioclient: FUSE ioctl example client
3 Copyright (C) 2008 SUSE Linux Products GmbH
4 Copyright (C) 2008 Tejun Heo <teheo@suse.de>
6 This program can be distributed under the terms of the GNU GPL.
13 * fioclient.c - FUSE fioclient: FUSE ioctl example client
15 * \section section_compile compiling this example
17 * gcc -Wall fioclient.c -o fioclient
19 * \section section_source the complete source
21 * \include fioclient.c
26 #include <sys/types.h>
27 #include <sys/fcntl.h>
29 #include <sys/ioctl.h>
37 "Usage: fioclient FIOC_FILE COMMAND\n"
40 " s [SIZE] : get size if SIZE is omitted, set size otherwise\n"
41 " r SIZE [OFF] : read SIZE bytes @ OFF (dfl 0) and output to stdout\n"
42 " w SIZE [OFF] : write SIZE bytes @ OFF (dfl 0) from stdin\n"
45 static int do_rw(int fd
, int is_read
, size_t size
, off_t offset
,
46 size_t *prev_size
, size_t *new_size
)
48 struct fioc_rw_arg arg
= { .offset
= offset
};
51 arg
.buf
= calloc(1, size
);
53 fprintf(stderr
, "failed to allocated %zu bytes\n", size
);
59 ret
= ioctl(fd
, FIOC_READ
, &arg
);
61 fwrite(arg
.buf
, 1, ret
, stdout
);
63 arg
.size
= fread(arg
.buf
, 1, size
, stdin
);
64 fprintf(stderr
, "Writing %zu bytes\n", arg
.size
);
65 ret
= ioctl(fd
, FIOC_WRITE
, &arg
);
69 *prev_size
= arg
.prev_size
;
70 *new_size
= arg
.new_size
;
78 int main(int argc
, char **argv
)
80 size_t param
[2] = { };
81 size_t size
, prev_size
= 0, new_size
= 0;
88 fd
= open(argv
[1], O_RDWR
);
94 cmd
= tolower(argv
[2][0]);
98 for (i
= 0; i
< argc
; i
++) {
100 param
[i
] = strtoul(argv
[i
], &endp
, 0);
101 if (endp
== argv
[i
] || *endp
!= '\0')
108 if (ioctl(fd
, FIOC_GET_SIZE
, &size
)) {
112 printf("%zu\n", size
);
115 if (ioctl(fd
, FIOC_SET_SIZE
, &size
)) {
124 rc
= do_rw(fd
, cmd
== 'r', param
[0], param
[1],
125 &prev_size
, &new_size
);
128 fprintf(stderr
, "transferred %d bytes (%zu -> %zu)\n",
129 rc
, prev_size
, new_size
);
134 fprintf(stderr
, "%s", usage
);