资源简介
共享文件系统需要的软件包
代码片段和文件信息
/*
CUSE example: Character device in Userspace
Copyright (C) 2008-2009 SUSE Linux Products GmbH
Copyright (C) 2008-2009 Tejun Heo
This program can be distributed under the terms of the GNU GPL.
See the file COPYING.
gcc -Wall cusexmp.c ‘pkg-config fuse --cflags --libs‘ -o cusexmp
*/
#define FUSE_USE_VERSION 29
#include
#include
#include
#include
#include
#include
#include
#include
#include “fioc.h“
static void *cusexmp_buf;
static size_t cusexmp_size;
static const char *usage =
“usage: cusexmp [options]\n“
“\n“
“options:\n“
“ --help|-h print this help message\n“
“ --maj=MAJ|-M MAJ device major number\n“
“ --min=MIN|-m MIN device minor number\n“
“ --name=NAME|-n NAME device name (mandatory)\n“
“\n“;
static int cusexmp_resize(size_t new_size)
{
void *new_buf;
if (new_size == cusexmp_size)
return 0;
new_buf = realloc(cusexmp_buf new_size);
if (!new_buf && new_size)
return -ENOMEM;
if (new_size > cusexmp_size)
memset(new_buf + cusexmp_size 0 new_size - cusexmp_size);
cusexmp_buf = new_buf;
cusexmp_size = new_size;
return 0;
}
static int cusexmp_expand(size_t new_size)
{
if (new_size > cusexmp_size)
return cusexmp_resize(new_size);
return 0;
}
static void cusexmp_open(fuse_req_t req struct fuse_file_info *fi)
{
fuse_reply_open(req fi);
}
static void cusexmp_read(fuse_req_t req size_t size off_t off
struct fuse_file_info *fi)
{
(void)fi;
if (off >= cusexmp_size)
off = cusexmp_size;
if (size > cusexmp_size - off)
size = cusexmp_size - off;
fuse_reply_buf(req cusexmp_buf + off size);
}
static void cusexmp_write(fuse_req_t req const char *buf size_t size
off_t off struct fuse_file_info *fi)
{
(void)fi;
if (cusexmp_expand(off + size)) {
fuse_reply_err(req ENOMEM);
return;
}
memcpy(cusexmp_buf + off buf size);
fuse_reply_write(req size);
}
static void fioc_do_rw(fuse_req_t req void *addr const void *in_buf
size_t in_bufsz size_t out_bufsz int is_read)
{
const struct fioc_rw_arg *arg;
struct iovec in_iov[2] out_iov[3] iov[3];
size_t cur_size;
/* read in arg */
in_iov[0].iov_base = addr;
in_iov[0].iov_len = sizeof(*arg);
if (!in_bufsz) {
fuse_reply_ioctl_retry(req in_iov 1 NULL 0);
return;
}
arg = in_buf;
in_buf += sizeof(*arg);
in_bufsz -= sizeof(*arg);
/* prepare size outputs */
out_iov[0].iov_base =
addr + (unsigned long)&(((struct fioc_rw_arg *)0)->prev_size);
out_iov[0].iov_len = sizeof(arg->prev_size);
out_iov[1].iov_base =
addr + (unsigned long)&(((struct fioc_rw_arg *)0)->new_size);
out_iov[1].iov_len = sizeof(arg->new_size);
/* prepare client buf */
if (is_read) {
out_iov[2].iov_base = arg->buf;
out_iov[2].iov_len = arg->size;
if (!out_bufsz) {
fuse_reply_ioctl_retry(req in_iov 1 out_iov 3);
return;
}
} else {
in_iov[1].iov_base = arg->buf
- 上一篇:社交接口设计文档 适合小白参考
- 下一篇:Kinect获取深度图像
评论
共有 条评论