FUSE 文件系統 example部分 源碼注釋 (libfuse 2.9.9)


本篇文章主要是針對fuse-2.9.9 Example 部分 給出的源碼,結合官方文檔,以及網上的資料給出注釋,希望能給正在學習的你們一點幫助。


Hello.c

/*
  FUSE: Filesystem in Userspace
  Copyright (C) 2001-2007  Miklos Szeredi <miklos@szeredi.hu>

  
  This program can be distributed under the terms of the GNU GPL.
  See the file COPYING.

  gcc -Wall hello.c `pkg-config fuse --cflags --libs` -o 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當中 struct stat *stbuf
static int hello_getattr(const char *path, struct stat *stbuf)
{
    int res = 0;

    memset(stbuf, 0, sizeof(struct stat));  // 使用memset進行初始化結構體
    if (strcmp(path, "/") == 0) {
        stbuf->st_mode = S_IFDIR | 0755; // 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);
}

 2019.6.30

這個文檔好像很久沒有更新了,項目fuse使用完成了,例子不想更新了。大家有什么問題可以在評論區給我留言。

保持更新,轉載請注明出處。如果你有什么問題,歡迎在留言區進行留言交流。https://www.cnblogs.com/xuyaowen/p/fuse-example-source-code.html 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM