Advertisement
Guest User

null_fs 0.0.1.1

a guest
Aug 25th, 2011
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.89 KB | None | 0 0
  1. /* > /dev/null 2>&1
  2. gcc $0 -D_FILE_OFFSET_BITS=64 -DFUSE_USE_VERSION=25 -lfuse -o null_fs "$@"
  3. exit
  4. */
  5. #include <fuse.h>
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <errno.h>
  10. #include <fcntl.h>
  11.  
  12. static int dev_null_fd;
  13. static char *troll;
  14.  
  15. static int null_getattr(const char *path, struct stat *stbuf)
  16. {
  17.     memset(stbuf, 0, sizeof(struct stat));
  18.     if(strcmp(path, "/") == 0)
  19.     {
  20.         stbuf->st_mode = S_IFDIR | 0777;
  21.         stbuf->st_nlink = 2;
  22.         return 0;
  23.     }
  24.     if(strcmp(path, troll) == 0)
  25.     {
  26.         stbuf->st_mode = S_IFREG | 0777;
  27.         stbuf->st_nlink = 1;
  28.         return 0;
  29.     }
  30.     return -ENOENT;
  31. }
  32.  
  33. static int null_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
  34.                         off_t offset, struct fuse_file_info *fi)
  35. {
  36.     if(strcmp(path, "/") != 0)
  37.     {
  38.         return -ENOENT;
  39.     }
  40.     filler(buf, ".", NULL, 0);
  41.     filler(buf, "..", NULL, 0);
  42.     return 0;
  43. }
  44.  
  45. static int null_open(const char *path, struct fuse_file_info *fi)
  46. {
  47.     return 0;
  48. }
  49.  
  50. static int null_read(const char *path, char *buf, size_t size, off_t offset,
  51.                      struct fuse_file_info *fi)
  52. {
  53.     return read(dev_null_fd, buf, size);
  54. }
  55.  
  56. static int null_write(const char *path, const char *buf, size_t size, off_t offset,
  57.                       struct fuse_file_info *fi)
  58. {
  59.     return size;
  60. }
  61.  
  62. static int null_create(const char *path, mode_t mode, struct fuse_file_info *fi)
  63. {
  64.     free(troll);
  65.     troll = strdup(path);
  66.     return 0;
  67. }
  68.  
  69. static struct fuse_operations null_oper = {
  70.     .getattr = null_getattr,
  71.     .readdir = null_readdir,
  72.     .open = null_open,
  73.     .read = null_read,
  74.     .write = null_write,
  75.     .create = null_create
  76. };
  77.  
  78. int main(int argc, char *argv[])
  79. {
  80.     troll = strdup("");
  81.     dev_null_fd = open("/dev/null",O_RDWR);
  82.     return fuse_main(argc, argv, &null_oper);
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement