├── .gitignore ├── README.md ├── CMakeLists.txt ├── LICENSE ├── CMake └── FindFUSE.cmake └── fuse-example.c /.gitignore: -------------------------------------------------------------------------------- 1 | CMakeCache.txt 2 | CMakeFiles 3 | Makefile 4 | cmake_install.cmake 5 | install_manifest.txt 6 | compile_commands.json 7 | bin/ 8 | lib/ 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Fuse Example 2 | 3 | This is the companion code to my ["Write a filesystem with FUSE"](http://engineering.facile.it/blog/eng/write-filesystem-fuse/) blog post. 4 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.0 FATAL_ERROR) 2 | project(fuse-example VERSION 0.0.1 LANGUAGES C) 3 | 4 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_FILE_OFFSET_BITS=64") 5 | set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -Wall --pedantic -g") 6 | set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/CMake" ${CMAKE_MODULE_PATH}) 7 | set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) 8 | set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) 9 | set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) 10 | set(CMAKE_EXPORT_COMPILE_COMMANDS 1) 11 | 12 | find_package(FUSE REQUIRED) 13 | 14 | include_directories(${FUSE_INCLUDE_DIR}) 15 | add_executable(fuse-example fuse-example.c) 16 | target_link_libraries(fuse-example ${FUSE_LIBRARIES}) 17 | 18 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2017 Lorenzo Fontana 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /CMake/FindFUSE.cmake: -------------------------------------------------------------------------------- 1 | # Find the FUSE includes and library 2 | # 3 | # FUSE_INCLUDE_DIR - where to find fuse.h, etc. 4 | # FUSE_LIBRARIES - List of libraries when using FUSE. 5 | # FUSE_FOUND - True if FUSE lib is found. 6 | 7 | # check if already in cache, be silent 8 | IF (FUSE_INCLUDE_DIR) 9 | SET (FUSE_FIND_QUIETLY TRUE) 10 | ENDIF (FUSE_INCLUDE_DIR) 11 | 12 | # find includes 13 | FIND_PATH (FUSE_INCLUDE_DIR fuse.h 14 | /usr/local/include/osxfuse 15 | /usr/local/include 16 | /usr/include 17 | ) 18 | 19 | # find lib 20 | if (APPLE) 21 | SET(FUSE_NAMES libosxfuse.dylib fuse) 22 | else (APPLE) 23 | SET(FUSE_NAMES fuse) 24 | endif (APPLE) 25 | FIND_LIBRARY(FUSE_LIBRARIES 26 | NAMES ${FUSE_NAMES} 27 | PATHS /lib64 /lib /usr/lib64 /usr/lib /usr/local/lib64 /usr/local/lib /usr/lib/x86_64-linux-gnu 28 | ) 29 | 30 | include ("FindPackageHandleStandardArgs") 31 | find_package_handle_standard_args ("FUSE" DEFAULT_MSG 32 | FUSE_INCLUDE_DIR FUSE_LIBRARIES) 33 | 34 | mark_as_advanced (FUSE_INCLUDE_DIR FUSE_LIBRARIES) 35 | -------------------------------------------------------------------------------- /fuse-example.c: -------------------------------------------------------------------------------- 1 | #define FUSE_USE_VERSION 26 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | static const char *filepath = "/file"; 8 | static const char *filename = "file"; 9 | static const char *filecontent = "I'm the content of the only file available there\n"; 10 | 11 | static int getattr_callback(const char *path, struct stat *stbuf) { 12 | memset(stbuf, 0, sizeof(struct stat)); 13 | 14 | if (strcmp(path, "/") == 0) { 15 | stbuf->st_mode = S_IFDIR | 0755; 16 | stbuf->st_nlink = 2; 17 | return 0; 18 | } 19 | 20 | if (strcmp(path, filepath) == 0) { 21 | stbuf->st_mode = S_IFREG | 0777; 22 | stbuf->st_nlink = 1; 23 | stbuf->st_size = strlen(filecontent); 24 | return 0; 25 | } 26 | 27 | return -ENOENT; 28 | } 29 | 30 | static int readdir_callback(const char *path, void *buf, fuse_fill_dir_t filler, 31 | off_t offset, struct fuse_file_info *fi) { 32 | (void) offset; 33 | (void) fi; 34 | 35 | filler(buf, ".", NULL, 0); 36 | filler(buf, "..", NULL, 0); 37 | 38 | filler(buf, filename, NULL, 0); 39 | 40 | return 0; 41 | } 42 | 43 | static int open_callback(const char *path, struct fuse_file_info *fi) { 44 | return 0; 45 | } 46 | 47 | static int read_callback(const char *path, char *buf, size_t size, off_t offset, 48 | struct fuse_file_info *fi) { 49 | 50 | if (strcmp(path, filepath) == 0) { 51 | size_t len = strlen(filecontent); 52 | if (offset >= len) { 53 | return 0; 54 | } 55 | 56 | if (offset + size > len) { 57 | memcpy(buf, filecontent + offset, len - offset); 58 | return len - offset; 59 | } 60 | 61 | memcpy(buf, filecontent + offset, size); 62 | return size; 63 | } 64 | 65 | return -ENOENT; 66 | } 67 | 68 | static struct fuse_operations fuse_example_operations = { 69 | .getattr = getattr_callback, 70 | .open = open_callback, 71 | .read = read_callback, 72 | .readdir = readdir_callback, 73 | }; 74 | 75 | int main(int argc, char *argv[]) 76 | { 77 | return fuse_main(argc, argv, &fuse_example_operations, NULL); 78 | } 79 | --------------------------------------------------------------------------------