1. 下载安装
wget
tar zxvf fuse-2.9.1.tar.gz
cd fuse-2.9.1
./configure
make
sudo make install
2. 测试代码
-
/*--------------------------------------------------------------------------------------------
-
gcc -Wall -O2 hello.c -o hello `pkg-config fuse --cflags --libs`
-
-
mkdir /tmp/fuse //在/tmp下建立fuse目录,用于挂载hello文件系统
-
./hello /tmp/fuse //挂载hello文件系统
-
ls -l /tmp/fuse //执行ls时,会调用到readdir函数,该函数会添加一个hello文件
-
-r--r--r-- 1 root root 13 1970-01-01 07:00 hello
-
cat /tmp/fuse/hello //执行cat 字符串hello_str中的内容读出
-
fusermount -u /tmp/fuse //卸载hello文件系统
-
---------------------------------------------------------------------------------------------*/
-
#define FUSE_USE_VERSION 26 //先定义, fuse.h中有判断
-
-
#include <fuse.h>
-
#include <stdio.h>
-
#include <string.h>
-
#include <errno.h>
-
#include <fcntl.h>
-
-
static const char *hello_str = "Hello World!\n";
-
static const char *hello_path = "/hello";
-
-
//该函数与stat()类似,用于得到文件的属性,将其存入到结构体struct stat中
-
static int hello_getattr(const char *path, struct stat *stbuf)
-
{
-
int res = 0;
-
-
memset(stbuf, 0, sizeof(struct stat)); //用于初始化结构体stat
-
if (strcmp(path, "/") == 0) {
-
stbuf->st_mode = S_IFDIR | 0755; //S_IFDIR 用于说明/为目录,详见S_IFDIR定义
-
stbuf->st_nlink = 2; //文件链接数
-
} else if (strcmp(path, hello_path) == 0) {
-
stbuf->st_mode = S_IFREG | 0444; //S_IFREG用于说明/hello为常规文件
-
stbuf->st_nlink = 1;
-
stbuf->st_size = strlen(hello_str); //设置文件长度为hello_str的长度
-
} else
-
res = -ENOENT; //返回错误信息,没有该文件或目录
-
-
return res; //执行成功返回0
-
}
-
-
//该函数用于读取目录中的内容,并在/目录下增加了. .. hello三个目录项
-
static int hello_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
-
off_t offset, struct fuse_file_info *fi)
-
{
-
(void) offset;
-
(void) fi;
-
-
if (strcmp(path, "/") != 0)
-
return -ENOENT;
-
-
/* fill, 其作用是在readdir函数中增加一个目录项
-
typedef int (*fuse_fill_dir_t) (void *buf, const char *name,
-
const struct stat *stbuf, off_t off);
-
*/
-
filler(buf, ".", NULL, 0);
-
filler(buf, "..", NULL, 0);
-
filler(buf, hello_path + 1, NULL, 0); //指针+1(/hello), 即增加 hello 目录项
-
-
return 0;
-
}
-
-
//打开文件
-
static int hello_open(const char *path, struct fuse_file_info *fi)
-
{
-
if (strcmp(path, hello_path) != 0)
-
return -ENOENT;
-
-
if ((fi->flags & 3) != O_RDONLY)
-
return -EACCES;
-
-
return 0;
-
}
-
-
//读文件
-
static int hello_read(const char *path, char *buf, size_t size, off_t offset,
-
struct fuse_file_info *fi)
-
{
-
size_t len;
-
(void) fi;
-
if(strcmp(path, hello_path) != 0)
-
return -ENOENT;
-
-
len = strlen(hello_str);
-
if (offset < len) {
-
if (offset + size > len)
-
size = len - offset;
-
memcpy(buf, hello_str + offset, size);
-
} else
-
size = 0;
-
-
return size;
-
}
-
-
//注册自定义函数
-
static struct fuse_operations hello_oper = {
-
.getattr = hello_getattr,
-
.readdir = hello_readdir,
-
.open = hello_open,
-
.read = hello_read,
-
};
-
-
//只需调用fuse_main(),剩下的事就交给FUSE了*
-
int main(int argc, char *argv[])
-
{
-
return fuse_main(argc, argv, &hello_oper, NULL);
-
}
3.Makefile
-
OPENWRT = 1
-
-
ifeq ($(OPENWRT), 1)
-
CC = ~/OpenWrt-SDK-ar71xx-for-linux-i486-gcc-4.6-linaro_uClibc-0.9.33.2/staging_dir/toolchain-mips_r2_gcc-4.6-linaro_uClibc-0.9.33.2/bin/mips-openwrt-linux-gcc
-
CFLAGS += -I ~/openwrt-lib/include -L ~/openwrt-lib/lib
-
-
else
-
CC = gcc
-
endif
-
-
CFLAGS += -Wall -O2
-
#CFLAGS += -g
-
-
#可执行文件名和相关的obj文件
-
APP_BINARY = hello_fuse
-
SRCS += hello.c
-
OBJS += hello.o
-
-
all: APP_FILE
-
-
hello.o:
-
$(CC) $(CFLAGS) -c $(SRCS) `pkg-config fuse --cflags --libs`
-
-
APP_FILE: $(OBJS)
-
$(CC) $(CFLAGS) $(OBJS) -o $(APP_BINARY) $(LFLAGS) `pkg-config fuse --cflags --libs`
-
-
.PHONY: clean
-
clean:
-
@echo "cleanning project"
-
$(RM) *.a *.o *~ *.so *.lo $(APP_BINARY)
-
@echo "clean completed"
测试过程:
$mkdir /tmp/fuse
$./hello /tmp/fuse
$ls -l /tmp/fuse
-r--r--r-- 1 root root 13 Jan 1 1970 hello
$cat /tmp/fuse/hello
Hello World!
$fusermount -u /tmp/fuse
阅读(3992) | 评论(0) | 转发(2) |