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.

236 lines
4.7 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 LGPLv2.
  5. See the file COPYING.LIB
  6. */
  7. #include "fuse_i.h"
  8. #include "fuse_misc.h"
  9. #include "fuse_common_compat.h"
  10. #include "fuse_lowlevel_compat.h"
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <assert.h>
  15. #include <errno.h>
  16. struct fuse_chan
  17. {
  18. struct fuse_chan_ops op;
  19. struct fuse_session *se;
  20. int fd;
  21. size_t bufsize;
  22. void *data;
  23. int compat;
  24. };
  25. struct fuse_session *fuse_session_new(struct fuse_session_ops *op, void *data)
  26. {
  27. struct fuse_session *se = (struct fuse_session *) malloc(sizeof(*se));
  28. if (se == NULL) {
  29. fprintf(stderr, "fuse: failed to allocate session\n");
  30. return NULL;
  31. }
  32. memset(se, 0, sizeof(*se));
  33. se->op = *op;
  34. se->data = data;
  35. return se;
  36. }
  37. void fuse_session_add_chan(struct fuse_session *se, struct fuse_chan *ch)
  38. {
  39. assert(se->ch == NULL);
  40. assert(ch->se == NULL);
  41. se->ch = ch;
  42. ch->se = se;
  43. }
  44. void fuse_session_remove_chan(struct fuse_chan *ch)
  45. {
  46. struct fuse_session *se = ch->se;
  47. if (se) {
  48. assert(se->ch == ch);
  49. se->ch = NULL;
  50. ch->se = NULL;
  51. }
  52. }
  53. struct fuse_chan *fuse_session_next_chan(struct fuse_session *se,
  54. struct fuse_chan *ch)
  55. {
  56. assert(ch == NULL || ch == se->ch);
  57. if (ch == NULL)
  58. return se->ch;
  59. else
  60. return NULL;
  61. }
  62. void fuse_session_process(struct fuse_session *se, const char *buf, size_t len,
  63. struct fuse_chan *ch)
  64. {
  65. se->op.process(se->data, buf, len, ch);
  66. }
  67. void fuse_session_process_buf(struct fuse_session *se,
  68. const struct fuse_buf *buf, struct fuse_chan *ch)
  69. {
  70. if (se->process_buf) {
  71. se->process_buf(se->data, buf, ch);
  72. } else {
  73. assert(!(buf->flags & FUSE_BUF_IS_FD));
  74. fuse_session_process(se->data, buf->mem, buf->size, ch);
  75. }
  76. }
  77. int fuse_session_receive_buf(struct fuse_session *se, struct fuse_buf *buf,
  78. struct fuse_chan **chp)
  79. {
  80. int res;
  81. if (se->receive_buf) {
  82. res = se->receive_buf(se, buf, chp);
  83. } else {
  84. res = fuse_chan_recv(chp, buf->mem, buf->size);
  85. if (res > 0)
  86. buf->size = res;
  87. }
  88. return res;
  89. }
  90. void fuse_session_destroy(struct fuse_session *se)
  91. {
  92. if (se->op.destroy)
  93. se->op.destroy(se->data);
  94. if (se->ch != NULL)
  95. fuse_chan_destroy(se->ch);
  96. free(se);
  97. }
  98. void fuse_session_exit(struct fuse_session *se)
  99. {
  100. if (se->op.exit)
  101. se->op.exit(se->data, 1);
  102. se->exited = 1;
  103. }
  104. void fuse_session_reset(struct fuse_session *se)
  105. {
  106. if (se->op.exit)
  107. se->op.exit(se->data, 0);
  108. se->exited = 0;
  109. }
  110. int fuse_session_exited(struct fuse_session *se)
  111. {
  112. if (se->op.exited)
  113. return se->op.exited(se->data);
  114. else
  115. return se->exited;
  116. }
  117. void *fuse_session_data(struct fuse_session *se)
  118. {
  119. return se->data;
  120. }
  121. static struct fuse_chan *fuse_chan_new_common(struct fuse_chan_ops *op, int fd,
  122. size_t bufsize, void *data,
  123. int compat)
  124. {
  125. struct fuse_chan *ch = (struct fuse_chan *) malloc(sizeof(*ch));
  126. if (ch == NULL) {
  127. fprintf(stderr, "fuse: failed to allocate channel\n");
  128. return NULL;
  129. }
  130. memset(ch, 0, sizeof(*ch));
  131. ch->op = *op;
  132. ch->fd = fd;
  133. ch->bufsize = bufsize;
  134. ch->data = data;
  135. ch->compat = compat;
  136. return ch;
  137. }
  138. struct fuse_chan *fuse_chan_new(struct fuse_chan_ops *op, int fd,
  139. size_t bufsize, void *data)
  140. {
  141. return fuse_chan_new_common(op, fd, bufsize, data, 0);
  142. }
  143. struct fuse_chan *fuse_chan_new_compat24(struct fuse_chan_ops_compat24 *op,
  144. int fd, size_t bufsize, void *data)
  145. {
  146. return fuse_chan_new_common((struct fuse_chan_ops *) op, fd, bufsize,
  147. data, 24);
  148. }
  149. int fuse_chan_fd(struct fuse_chan *ch)
  150. {
  151. return ch->fd;
  152. }
  153. int fuse_chan_clearfd(struct fuse_chan *ch)
  154. {
  155. int fd = ch->fd;
  156. ch->fd = -1;
  157. return fd;
  158. }
  159. size_t fuse_chan_bufsize(struct fuse_chan *ch)
  160. {
  161. return ch->bufsize;
  162. }
  163. void *fuse_chan_data(struct fuse_chan *ch)
  164. {
  165. return ch->data;
  166. }
  167. struct fuse_session *fuse_chan_session(struct fuse_chan *ch)
  168. {
  169. return ch->se;
  170. }
  171. int fuse_chan_recv(struct fuse_chan **chp, char *buf, size_t size)
  172. {
  173. struct fuse_chan *ch = *chp;
  174. if (ch->compat)
  175. return ((struct fuse_chan_ops_compat24 *) &ch->op)
  176. ->receive(ch, buf, size);
  177. else
  178. return ch->op.receive(chp, buf, size);
  179. }
  180. int fuse_chan_receive(struct fuse_chan *ch, char *buf, size_t size)
  181. {
  182. int res;
  183. res = fuse_chan_recv(&ch, buf, size);
  184. return res >= 0 ? res : (res != -EINTR && res != -EAGAIN) ? -1 : 0;
  185. }
  186. int fuse_chan_send(struct fuse_chan *ch, const struct iovec iov[], size_t count)
  187. {
  188. return ch->op.send(ch, iov, count);
  189. }
  190. void fuse_chan_destroy(struct fuse_chan *ch)
  191. {
  192. fuse_session_remove_chan(ch);
  193. if (ch->op.destroy)
  194. ch->op.destroy(ch);
  195. free(ch);
  196. }
  197. #ifndef __FreeBSD__
  198. FUSE_SYMVER(".symver fuse_chan_new_compat24,fuse_chan_new@FUSE_2.4");
  199. #endif