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.

181 lines
3.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_ll.c `pkg-config fuse --cflags --libs` -o hello_ll
  7. */
  8. #define FUSE_USE_VERSION 26
  9. #include <fuse_lowlevel.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <errno.h>
  14. #include <fcntl.h>
  15. #include <unistd.h>
  16. #include <assert.h>
  17. static const char *hello_str = "Hello World!\n";
  18. static const char *hello_name = "hello";
  19. static int hello_stat(fuse_ino_t ino, struct stat *stbuf)
  20. {
  21. stbuf->st_ino = ino;
  22. switch (ino) {
  23. case 1:
  24. stbuf->st_mode = S_IFDIR | 0755;
  25. stbuf->st_nlink = 2;
  26. break;
  27. case 2:
  28. stbuf->st_mode = S_IFREG | 0444;
  29. stbuf->st_nlink = 1;
  30. stbuf->st_size = strlen(hello_str);
  31. break;
  32. default:
  33. return -1;
  34. }
  35. return 0;
  36. }
  37. static void hello_ll_getattr(fuse_req_t req, fuse_ino_t ino,
  38. struct fuse_file_info *fi)
  39. {
  40. struct stat stbuf;
  41. (void) fi;
  42. memset(&stbuf, 0, sizeof(stbuf));
  43. if (hello_stat(ino, &stbuf) == -1)
  44. fuse_reply_err(req, ENOENT);
  45. else
  46. fuse_reply_attr(req, &stbuf, 1.0);
  47. }
  48. static void hello_ll_lookup(fuse_req_t req, fuse_ino_t parent, const char *name)
  49. {
  50. struct fuse_entry_param e;
  51. if (parent != 1 || strcmp(name, hello_name) != 0)
  52. fuse_reply_err(req, ENOENT);
  53. else {
  54. memset(&e, 0, sizeof(e));
  55. e.ino = 2;
  56. e.attr_timeout = 1.0;
  57. e.entry_timeout = 1.0;
  58. hello_stat(e.ino, &e.attr);
  59. fuse_reply_entry(req, &e);
  60. }
  61. }
  62. struct dirbuf {
  63. char *p;
  64. size_t size;
  65. };
  66. static void dirbuf_add(fuse_req_t req, struct dirbuf *b, const char *name,
  67. fuse_ino_t ino)
  68. {
  69. struct stat stbuf;
  70. size_t oldsize = b->size;
  71. b->size += fuse_add_direntry(req, NULL, 0, name, NULL, 0);
  72. b->p = (char *) realloc(b->p, b->size);
  73. memset(&stbuf, 0, sizeof(stbuf));
  74. stbuf.st_ino = ino;
  75. fuse_add_direntry(req, b->p + oldsize, b->size - oldsize, name, &stbuf,
  76. b->size);
  77. }
  78. #define min(x, y) ((x) < (y) ? (x) : (y))
  79. static int reply_buf_limited(fuse_req_t req, const char *buf, size_t bufsize,
  80. off_t off, size_t maxsize)
  81. {
  82. if (off < bufsize)
  83. return fuse_reply_buf(req, buf + off,
  84. min(bufsize - off, maxsize));
  85. else
  86. return fuse_reply_buf(req, NULL, 0);
  87. }
  88. static void hello_ll_readdir(fuse_req_t req, fuse_ino_t ino, size_t size,
  89. off_t off, struct fuse_file_info *fi)
  90. {
  91. (void) fi;
  92. if (ino != 1)
  93. fuse_reply_err(req, ENOTDIR);
  94. else {
  95. struct dirbuf b;
  96. memset(&b, 0, sizeof(b));
  97. dirbuf_add(req, &b, ".", 1);
  98. dirbuf_add(req, &b, "..", 1);
  99. dirbuf_add(req, &b, hello_name, 2);
  100. reply_buf_limited(req, b.p, b.size, off, size);
  101. free(b.p);
  102. }
  103. }
  104. static void hello_ll_open(fuse_req_t req, fuse_ino_t ino,
  105. struct fuse_file_info *fi)
  106. {
  107. if (ino != 2)
  108. fuse_reply_err(req, EISDIR);
  109. else if ((fi->flags & 3) != O_RDONLY)
  110. fuse_reply_err(req, EACCES);
  111. else
  112. fuse_reply_open(req, fi);
  113. }
  114. static void hello_ll_read(fuse_req_t req, fuse_ino_t ino, size_t size,
  115. off_t off, struct fuse_file_info *fi)
  116. {
  117. (void) fi;
  118. assert(ino == 2);
  119. reply_buf_limited(req, hello_str, strlen(hello_str), off, size);
  120. }
  121. static struct fuse_lowlevel_ops hello_ll_oper = {
  122. .lookup = hello_ll_lookup,
  123. .getattr = hello_ll_getattr,
  124. .readdir = hello_ll_readdir,
  125. .open = hello_ll_open,
  126. .read = hello_ll_read,
  127. };
  128. int main(int argc, char *argv[])
  129. {
  130. struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
  131. struct fuse_chan *ch;
  132. char *mountpoint;
  133. int err = -1;
  134. if (fuse_parse_cmdline(&args, &mountpoint, NULL, NULL) != -1 &&
  135. (ch = fuse_mount(mountpoint, &args)) != NULL) {
  136. struct fuse_session *se;
  137. se = fuse_lowlevel_new(&args, &hello_ll_oper,
  138. sizeof(hello_ll_oper), NULL);
  139. if (se != NULL) {
  140. if (fuse_set_signal_handlers(se) != -1) {
  141. fuse_session_add_chan(se, ch);
  142. err = fuse_session_loop(se);
  143. fuse_remove_signal_handlers(se);
  144. fuse_session_remove_chan(ch);
  145. }
  146. fuse_session_destroy(se);
  147. }
  148. fuse_unmount(mountpoint, ch);
  149. }
  150. fuse_opt_free_args(&args);
  151. return err ? 1 : 0;
  152. }