40 #include <sys/types.h> 43 #include <sys/ioctl.h> 51 "Usage: cuse_client FIOC_FILE COMMAND\n" 54 " s [SIZE] : get size if SIZE is omitted, set size otherwise\n" 55 " r SIZE [OFF] : read SIZE bytes @ OFF (dfl 0) and output to stdout\n" 56 " w SIZE [OFF] : write SIZE bytes @ OFF (dfl 0) from stdin\n" 59 static int do_rw(
int fd,
int is_read,
size_t size, off_t offset,
60 size_t *prev_size,
size_t *new_size)
62 struct fioc_rw_arg arg = { .offset = offset };
65 arg.buf = calloc(1, size);
67 fprintf(stderr,
"failed to allocated %zu bytes\n", size);
73 ret = ioctl(fd, FIOC_READ, &arg);
75 fwrite(arg.buf, 1, ret, stdout);
77 arg.size = fread(arg.buf, 1, size, stdin);
78 fprintf(stderr,
"Writing %zu bytes\n", arg.size);
79 ret = ioctl(fd, FIOC_WRITE, &arg);
83 *prev_size = arg.prev_size;
84 *new_size = arg.new_size;
92 int main(
int argc,
char **argv)
94 size_t param[2] = { };
95 size_t size, prev_size = 0, new_size = 0;
102 fd = open(argv[1], O_RDWR);
108 cmd = tolower(argv[2][0]);
112 for (i = 0; i < argc; i++) {
114 param[i] = strtoul(argv[i], &endp, 0);
115 if (endp == argv[i] || *endp !=
'\0')
122 if (ioctl(fd, FIOC_GET_SIZE, &size)) {
126 printf(
"%zu\n", size);
129 if (ioctl(fd, FIOC_SET_SIZE, &size)) {
138 rc = do_rw(fd, cmd ==
'r', param[0], param[1],
139 &prev_size, &new_size);
142 fprintf(stderr,
"transferred %d bytes (%zu -> %zu)\n",
143 rc, prev_size, new_size);
148 fprintf(stderr,
"%s", usage);