Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1485226
  • 博文数量: 129
  • 博客积分: 1449
  • 博客等级: 上尉
  • 技术积分: 3048
  • 用 户 组: 普通用户
  • 注册时间: 2012-07-24 18:36
文章分类

全部博文(129)

文章存档

2015年(3)

2014年(20)

2013年(65)

2012年(41)

分类: LINUX

2013-10-30 16:29:26


1. 下载安装
wget
tar zxvf fuse-2.9.1.tar.gz
cd fuse-2.9.1
./configure
make
sudo make install

2. 测试代码

点击(此处)折叠或打开

  1. /*--------------------------------------------------------------------------------------------
  2. gcc -Wall -O2 hello.c -o hello `pkg-config fuse --cflags --libs`

  3. mkdir /tmp/fuse             ///tmp下建立fuse目录,用于挂载hello文件系统
  4. ./hello /tmp/fuse         //挂载hello文件系统
  5. ls -l /tmp/fuse            //执行ls时,会调用到readdir函数,该函数会添加一个hello文件
  6.     -r--r--r-- 1 root root 13 1970-01-01 07:00 hello
  7. cat /tmp/fuse/hello    //执行cat      字符串hello_str中的内容读出
  8. fusermount -u /tmp/fuse //卸载hello文件系统
  9. ---------------------------------------------------------------------------------------------*/
  10. #define FUSE_USE_VERSION 26    //先定义, fuse.h中有判断

  11. #include <fuse.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <errno.h>
  15. #include <fcntl.h>

  16. static const char *hello_str = "Hello World!\n";
  17. static const char *hello_path = "/hello";

  18. //该函数与stat()类似,用于得到文件的属性,将其存入到结构体struct stat中
  19. static int hello_getattr(const char *path, struct stat *stbuf)
  20. {
  21.     int res = 0;

  22.     memset(stbuf, 0, sizeof(struct stat));    //用于初始化结构体stat
  23.     if (strcmp(path, "/") == 0) {
  24.         stbuf->st_mode = S_IFDIR | 0755;    //S_IFDIR 用于说明/为目录,详见S_IFDIR定义
  25.         stbuf->st_nlink = 2;    //文件链接数
  26.     } else if (strcmp(path, hello_path) == 0) {
  27.         stbuf->st_mode = S_IFREG | 0444;    //S_IFREG用于说明/hello为常规文件
  28.         stbuf->st_nlink = 1;
  29.         stbuf->st_size = strlen(hello_str);    //设置文件长度为hello_str的长度
  30.     } else
  31.         res = -ENOENT;    //返回错误信息,没有该文件或目录

  32.     return res;    //执行成功返回0
  33. }

  34. //该函数用于读取目录中的内容,并在/目录下增加了. .. hello三个目录项
  35. static int hello_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
  36.              off_t offset, struct fuse_file_info *fi)
  37. {
  38.     (void) offset;
  39.     (void) fi;

  40.     if (strcmp(path, "/") != 0)
  41.         return -ENOENT;

  42.     /* fill, 其作用是在readdir函数中增加一个目录项
  43.     typedef int (*fuse_fill_dir_t) (void *buf, const char *name,
  44.         const struct stat *stbuf, off_t off);
  45.     */
  46.     filler(buf, ".", NULL, 0);
  47.     filler(buf, "..", NULL, 0);
  48.     filler(buf, hello_path + 1, NULL, 0);    //指针+1(/hello), 即增加 hello 目录项

  49.     return 0;
  50. }

  51. //打开文件
  52. static int hello_open(const char *path, struct fuse_file_info *fi)
  53. {
  54.     if (strcmp(path, hello_path) != 0)
  55.         return -ENOENT;

  56.     if ((fi->flags & 3) != O_RDONLY)
  57.         return -EACCES;

  58.     return 0;
  59. }

  60. //读文件
  61. static int hello_read(const char *path, char *buf, size_t size, off_t offset,
  62.          struct fuse_file_info *fi)
  63. {
  64.     size_t len;
  65.     (void) fi;
  66.     if(strcmp(path, hello_path) != 0)
  67.         return -ENOENT;

  68.     len = strlen(hello_str);
  69.     if (offset < len) {
  70.         if (offset + size > len)
  71.             size = len - offset;
  72.         memcpy(buf, hello_str + offset, size);
  73.     } else
  74.         size = 0;

  75.     return size;
  76. }

  77. //注册自定义函数
  78. static struct fuse_operations hello_oper = {
  79.     .getattr    = hello_getattr,
  80.     .readdir    = hello_readdir,
  81.     .open        = hello_open,
  82.     .read        = hello_read,
  83. };

  84. //只需调用fuse_main(),剩下的事就交给FUSE了*
  85. int main(int argc, char *argv[])
  86. {
  87.     return fuse_main(argc, argv, &hello_oper, NULL);
  88. }

3.Makefile

点击(此处)折叠或打开

  1. OPENWRT = 1

  2. ifeq ($(OPENWRT), 1)
  3.     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
  4.     CFLAGS += -I ~/openwrt-lib/include -L ~/openwrt-lib/lib

  5. else
  6.     CC = gcc
  7. endif

  8. CFLAGS += -Wall -O2
  9. #CFLAGS += -g

  10. #可执行文件名和相关的obj文件
  11. APP_BINARY = hello_fuse
  12. SRCS += hello.c
  13. OBJS += hello.o

  14. all: APP_FILE

  15. hello.o:
  16.     $(CC) $(CFLAGS) -c $(SRCS) `pkg-config fuse --cflags --libs`

  17. APP_FILE: $(OBJS)
  18.     $(CC) $(CFLAGS) $(OBJS) -o $(APP_BINARY) $(LFLAGS) `pkg-config fuse --cflags --libs`

  19. .PHONY: clean
  20. clean:
  21.     @echo "cleanning project"
  22.     $(RM) *.a *.o *~ *.so *.lo $(APP_BINARY)
  23.     @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


阅读(3834) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~