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.

155 lines
2.5 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_kernel.h"
  10. #include <assert.h>
  11. #include <errno.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <unistd.h>
  16. #include <sys/uio.h>
  17. struct fuse_chan
  18. {
  19. struct fuse_session *se;
  20. int fd;
  21. size_t bufsize;
  22. };
  23. struct fuse_session *
  24. fuse_session_new(void *data,
  25. void *receive_buf,
  26. void *process_buf,
  27. void *destroy)
  28. {
  29. struct fuse_session *se = (struct fuse_session *) malloc(sizeof(*se));
  30. if (se == NULL) {
  31. fprintf(stderr, "fuse: failed to allocate session\n");
  32. return NULL;
  33. }
  34. memset(se, 0, sizeof(*se));
  35. se->f = data;
  36. se->receive_buf = receive_buf;
  37. se->process_buf = process_buf;
  38. se->destroy = destroy;
  39. return se;
  40. }
  41. void fuse_session_add_chan(struct fuse_session *se, struct fuse_chan *ch)
  42. {
  43. assert(se->ch == NULL);
  44. assert(ch->se == NULL);
  45. se->ch = ch;
  46. ch->se = se;
  47. }
  48. void fuse_session_remove_chan(struct fuse_chan *ch)
  49. {
  50. struct fuse_session *se = ch->se;
  51. if (se) {
  52. assert(se->ch == ch);
  53. se->ch = NULL;
  54. ch->se = NULL;
  55. }
  56. }
  57. void
  58. fuse_session_destroy(struct fuse_session *se)
  59. {
  60. se->destroy(se->f);
  61. if(se->ch != NULL)
  62. fuse_chan_destroy(se->ch);
  63. free(se);
  64. }
  65. void fuse_session_reset(struct fuse_session *se)
  66. {
  67. se->exited = 0;
  68. }
  69. int
  70. fuse_session_exited(struct fuse_session *se)
  71. {
  72. return se->exited;
  73. }
  74. void
  75. fuse_session_exit(struct fuse_session *se_)
  76. {
  77. se_->exited = 1;
  78. }
  79. void*
  80. fuse_session_data(struct fuse_session *se)
  81. {
  82. return se->f;
  83. }
  84. struct fuse_chan *
  85. fuse_chan_new(int fd,
  86. size_t bufsize)
  87. {
  88. struct fuse_chan *ch;
  89. ch = (struct fuse_chan*)malloc(sizeof(*ch));
  90. if(ch == NULL)
  91. {
  92. fprintf(stderr, "fuse: failed to allocate channel\n");
  93. return NULL;
  94. }
  95. memset(ch, 0, sizeof(*ch));
  96. ch->fd = fd;
  97. ch->bufsize = bufsize;
  98. return ch;
  99. }
  100. int fuse_chan_fd(struct fuse_chan *ch)
  101. {
  102. return ch->fd;
  103. }
  104. int fuse_chan_clearfd(struct fuse_chan *ch)
  105. {
  106. int fd = ch->fd;
  107. ch->fd = -1;
  108. return fd;
  109. }
  110. size_t fuse_chan_bufsize(struct fuse_chan *ch)
  111. {
  112. return ch->bufsize;
  113. }
  114. struct fuse_session *fuse_chan_session(struct fuse_chan *ch)
  115. {
  116. return ch->se;
  117. }
  118. void
  119. fuse_chan_destroy(struct fuse_chan *ch)
  120. {
  121. int fd;
  122. fuse_session_remove_chan(ch);
  123. fd = fuse_chan_fd(ch);
  124. if(fd != -1)
  125. close(fd);
  126. free(ch);
  127. }