Advertisement
Guest User

Untitled

a guest
May 6th, 2016
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <unistd.h>
  4. #include <strings.h>
  5. #include <sys/stat.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8.  
  9. #include <fcntl.h>
  10. #include <errno.h>
  11.  
  12. #include <file/nbio.h>
  13.  
  14. #include <aio.h>
  15.  
  16. #define BUFSIZE 111
  17.  
  18. struct nbio_t
  19. {
  20. int f;
  21. void* data;
  22. size_t len;
  23. struct aiocb aiocb;
  24. /*
  25. * possible values:
  26. * NBIO_READ, NBIO_WRITE - obvious
  27. * -1 - currently doing nothing
  28. * -2 - the pointer was reallocated since the last operation
  29. */
  30. signed char op;
  31. signed char mode;
  32. };
  33.  
  34. static const char * modes[]={ "rb", "wb", "r+b", "rb", "wb", "r+b" };
  35.  
  36. struct nbio_t* nbio_open(const char * filename, unsigned mode)
  37. {
  38. unsigned aio_mode = 0;
  39. int fd, ret;
  40. struct nbio_t* handle = NULL;
  41.  
  42. switch (mode)
  43. {
  44. case 0:
  45. case 2:
  46. case 3:
  47. case 5:
  48. aio_mode = O_RDONLY;
  49. break;
  50. case 1:
  51. case 4:
  52. aio_mode = O_WRONLY | O_CREAT | O_TRUNC;
  53. #ifndef _WIN32
  54. aio_mode |= S_IRUSR | S_IWUSR;
  55. #endif
  56. break;
  57. default:
  58. break;
  59. }
  60. unlink(filename);
  61. int f = open(filename, aio_mode);
  62. if (f < 0)
  63. return NULL;
  64.  
  65. unlink(filename);
  66.  
  67. handle = (struct nbio_t*)malloc(sizeof(struct nbio_t));
  68.  
  69. if (!handle)
  70. goto error;
  71.  
  72. handle->f = f;
  73. handle->len = 0;
  74.  
  75. memset((char*)&handle->aiocb, 0, sizeof(struct aiocb));
  76.  
  77. handle->aiocb.aio_buf = malloc(BUFSIZE+1);
  78. if (!handle->aiocb.aio_buf)
  79. goto error;
  80.  
  81. /* Initialize the necessary fields in the aiocb */
  82. handle->aiocb.aio_fildes = f;
  83. handle->aiocb.aio_nbytes = BUFSIZE;
  84. handle->aiocb.aio_offset = 0;
  85.  
  86. switch (mode)
  87. {
  88. case NBIO_WRITE:
  89. case BIO_WRITE:
  90. break;
  91. default:
  92. #if 0
  93. fseek(handle->f, 0, SEEK_END);
  94. handle->len = ftell(handle->f);
  95. #endif
  96. break;
  97. }
  98.  
  99. handle->mode = mode;
  100.  
  101. handle->op = -2;
  102.  
  103. return handle;
  104.  
  105. error:
  106. if (handle)
  107. {
  108. free(handle);
  109. }
  110. handle = NULL;
  111. close(f);
  112. return NULL;
  113. }
  114.  
  115. void nbio_begin_read(struct nbio_t* handle)
  116. {
  117. if (!handle)
  118. return;
  119.  
  120. if (handle->op >= 0)
  121. {
  122. puts("ERROR - attempted file read operation while busy");
  123. abort();
  124. }
  125.  
  126. if (aio_read(&handle->aiocb) != 0)
  127. {
  128. puts("aio_read_error");
  129. abort();
  130. }
  131.  
  132. handle->op = NBIO_READ;
  133. }
  134.  
  135. void nbio_begin_write(struct nbio_t* handle)
  136. {
  137. if (!handle)
  138. return;
  139.  
  140. if (handle->op >= 0)
  141. {
  142. puts("ERROR - attempted file write operation while busy");
  143. abort();
  144. }
  145.  
  146. if (aio_write(&handle->aiocb) != 0)
  147. {
  148. puts("aio_write_error");
  149. abort();
  150. }
  151.  
  152. handle->op = NBIO_WRITE;
  153. }
  154.  
  155. bool nbio_iterate(struct nbio_t* handle)
  156. {
  157. int err, ret;
  158. if (!handle)
  159. return false;
  160.  
  161. if (aio_error(&handle->aiocb) == EINPROGRESS)
  162. return false;
  163.  
  164. err = aio_error(&handle->aiocb);
  165. ret = aio_return(&handle->aiocb);
  166.  
  167. if (err != 0)
  168. {
  169. puts("ERROR - at aio_error():\n");
  170. }
  171. else if (ret != BUFSIZE)
  172. {
  173. puts("ERROR - at aio_return()\n");
  174. }
  175. else
  176. handle->op = -1;
  177.  
  178. return (handle->op < 0);
  179. }
  180.  
  181. void nbio_resize(struct nbio_t* handle, size_t len)
  182. {
  183. if (!handle)
  184. return;
  185.  
  186. if (handle->op >= 0)
  187. {
  188. puts("ERROR - attempted file resize operation while busy");
  189. abort();
  190. }
  191. if (len < handle->len)
  192. {
  193. puts("ERROR - attempted file shrink operation, not implemented");
  194. abort();
  195. }
  196.  
  197. handle->len = len;
  198. handle->op = -1;
  199. }
  200.  
  201. void* nbio_get_ptr(struct nbio_t* handle, size_t* len)
  202. {
  203. if (!handle)
  204. return NULL;
  205. if (len)
  206. *len = handle->len;
  207. return NULL;
  208. }
  209.  
  210. void nbio_cancel(struct nbio_t* handle)
  211. {
  212. if (!handle)
  213. return;
  214.  
  215. handle->op = -1;
  216. }
  217.  
  218. void nbio_free(struct nbio_t* handle)
  219. {
  220. if (!handle)
  221. return;
  222. if (handle->op >= 0)
  223. {
  224. puts("ERROR - attempted free() while busy");
  225. abort();
  226. }
  227. close(handle->f);
  228.  
  229. handle->f = 0;
  230. free(handle);
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement