52#define MAX_XFER_SIZE (16 * 1024 * 1024)
55"Usage: cuse_client FIOC_FILE COMMAND\n"
58" s [SIZE] : get size if SIZE is omitted, set size otherwise\n"
59" r SIZE [OFF] : read SIZE bytes @ OFF (dfl 0) and output to stdout\n"
60" w SIZE [OFF] : write SIZE bytes @ OFF (dfl 0) from stdin\n"
63static int do_rw(
int fd,
int is_read,
size_t size, off_t offset,
64 size_t *prev_size,
size_t *new_size)
66 struct fioc_rw_arg arg = { .offset = offset };
69 if (size > MAX_XFER_SIZE) {
70 fprintf(stderr,
"size %zu exceeds limit of %d bytes\n",
75 arg.buf = calloc(1, size);
77 fprintf(stderr,
"failed to allocated %zu bytes\n", size);
83 ret = ioctl(fd, FIOC_READ, &arg);
85 fwrite(arg.buf, 1, ret, stdout);
87 arg.size = fread(arg.buf, 1, size, stdin);
88 fprintf(stderr,
"Writing %zu bytes\n", arg.size);
89 ret = ioctl(fd, FIOC_WRITE, &arg);
93 *prev_size = arg.prev_size;
94 *new_size = arg.new_size;
102int main(
int argc,
char **argv)
104 size_t param[2] = { };
105 size_t size, prev_size = 0, new_size = 0;
112 fd = open(argv[1], O_RDWR);
118 cmd = tolower(argv[2][0]);
122 for (i = 0; i < argc; i++) {
124 param[i] = strtoul(argv[i], &endp, 0);
125 if (endp == argv[i] || *endp !=
'\0')
132 if (ioctl(fd, FIOC_GET_SIZE, &size)) {
136 printf(
"%zu\n", size);
139 if (ioctl(fd, FIOC_SET_SIZE, &size)) {
149 rc = do_rw(fd, cmd ==
'r', param[0], param[1],
150 &prev_size, &new_size);
153 fprintf(stderr,
"transferred %d bytes (%zu -> %zu)\n",
154 rc, prev_size, new_size);
160 fprintf(stderr,
"%s", usage);