Advertisement
_d3f4ult

hello.c

May 9th, 2016
624
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.22 KB | None | 0 0
  1. /*
  2.   FUSE: Filesystem in Userspace
  3.   Copyright (C) 2001-2007  Miklos Szeredi <miklos@szeredi.hu>
  4.   heavily modified by Jann Horn <jannh@google.com>
  5.  
  6.   This program can be distributed under the terms of the GNU GPL.
  7.   See the file COPYING.
  8.  
  9.   gcc -Wall hello.c `pkg-config fuse --cflags --libs` -o hello
  10. */
  11.  
  12. #define FUSE_USE_VERSION 26
  13.  
  14. #include <fuse.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <errno.h>
  18. #include <fcntl.h>
  19. #include <unistd.h>
  20. #include <err.h>
  21. #include <sys/uio.h>
  22.  
  23. static const char *hello_path = "/hello";
  24.  
  25. static char data_state[sizeof(struct iovec)];
  26.  
  27. static int hello_getattr(const char *path, struct stat *stbuf)
  28. {
  29.     int res = 0;
  30.     memset(stbuf, 0, sizeof(struct stat));
  31.     if (strcmp(path, "/") == 0) {
  32.         stbuf->st_mode = S_IFDIR | 0755;
  33.         stbuf->st_nlink = 2;
  34.     } else if (strcmp(path, hello_path) == 0) {
  35.         stbuf->st_mode = S_IFREG | 0666;
  36.         stbuf->st_nlink = 1;
  37.         stbuf->st_size = sizeof(data_state);
  38.         stbuf->st_blocks = 0;
  39.     } else
  40.         res = -ENOENT;
  41.     return res;
  42. }
  43.  
  44. static int hello_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi) {
  45.     filler(buf, ".", NULL, 0);
  46.     filler(buf, "..", NULL, 0);
  47.     filler(buf, hello_path + 1, NULL, 0);
  48.     return 0;
  49. }
  50.  
  51. static int hello_open(const char *path, struct fuse_file_info *fi) {
  52.     return 0;
  53. }
  54.  
  55. static int hello_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi) {
  56.     sleep(10);
  57.     size_t len = sizeof(data_state);
  58.     if (offset < len) {
  59.         if (offset + size > len)
  60.             size = len - offset;
  61.         memcpy(buf, data_state + offset, size);
  62.     } else
  63.         size = 0;
  64.     return size;
  65. }
  66.  
  67. static int hello_write(const char *path, const char *buf, size_t size, off_t offset, struct fuse_file_info *fi) {
  68.     if (offset != 0)
  69.         errx(1, "got write with nonzero offset");
  70.     if (size != sizeof(data_state))
  71.         errx(1, "got write with size %d", (int)size);
  72.     memcpy(data_state + offset, buf, size);
  73.     return size;
  74. }
  75.  
  76. static struct fuse_operations hello_oper = {
  77.     .getattr    = hello_getattr,
  78.     .readdir    = hello_readdir,
  79.     .open       = hello_open,
  80.     .read       = hello_read,
  81.     .write      = hello_write,
  82. };
  83.  
  84. int main(int argc, char *argv[]) {
  85.     return fuse_main(argc, argv, &hello_oper, NULL);
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement