You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
1.9 KiB

  1. /*
  2. FUSE: Filesystem in Userspace
  3. Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
  4. This program can be distributed under the terms of the GNU GPL.
  5. See the file COPYING.
  6. gcc -Wall hello.c `pkg-config fuse --cflags --libs` -o hello
  7. */
  8. #define FUSE_USE_VERSION 26
  9. #include <fuse.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <errno.h>
  13. #include <fcntl.h>
  14. static const char *hello_str = "Hello World!\n";
  15. static const char *hello_path = "/hello";
  16. static int hello_getattr(const char *path, struct stat *stbuf)
  17. {
  18. int res = 0;
  19. memset(stbuf, 0, sizeof(struct stat));
  20. if (strcmp(path, "/") == 0) {
  21. stbuf->st_mode = S_IFDIR | 0755;
  22. stbuf->st_nlink = 2;
  23. } else if (strcmp(path, hello_path) == 0) {
  24. stbuf->st_mode = S_IFREG | 0444;
  25. stbuf->st_nlink = 1;
  26. stbuf->st_size = strlen(hello_str);
  27. } else
  28. res = -ENOENT;
  29. return res;
  30. }
  31. static int hello_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
  32. off_t offset, struct fuse_file_info *fi)
  33. {
  34. (void) offset;
  35. (void) fi;
  36. if (strcmp(path, "/") != 0)
  37. return -ENOENT;
  38. filler(buf, ".", NULL, 0);
  39. filler(buf, "..", NULL, 0);
  40. filler(buf, hello_path + 1, NULL, 0);
  41. return 0;
  42. }
  43. static int hello_open(const char *path, struct fuse_file_info *fi)
  44. {
  45. if (strcmp(path, hello_path) != 0)
  46. return -ENOENT;
  47. if ((fi->flags & 3) != O_RDONLY)
  48. return -EACCES;
  49. return 0;
  50. }
  51. static int hello_read(const char *path, char *buf, size_t size, off_t offset,
  52. struct fuse_file_info *fi)
  53. {
  54. size_t len;
  55. (void) fi;
  56. if(strcmp(path, hello_path) != 0)
  57. return -ENOENT;
  58. len = strlen(hello_str);
  59. if (offset < len) {
  60. if (offset + size > len)
  61. size = len - offset;
  62. memcpy(buf, hello_str + offset, size);
  63. } else
  64. size = 0;
  65. return size;
  66. }
  67. static struct fuse_operations hello_oper = {
  68. .getattr = hello_getattr,
  69. .readdir = hello_readdir,
  70. .open = hello_open,
  71. .read = hello_read,
  72. };
  73. int main(int argc, char *argv[])
  74. {
  75. return fuse_main(argc, argv, &hello_oper, NULL);
  76. }