io.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /** @file io.c
  2. * @brief Enchanced base IO functions.
  3. */
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <assert.h>
  7. #include <errno.h>
  8. #include <stdio.h>
  9. #include <sys/types.h>
  10. #include <sys/socket.h>
  11. #include <sys/uio.h>
  12. /** @brief Writes data to file.
  13. *
  14. * The system write() can be interrupted by signal. This function will retry to
  15. * write in a case of interrupted call.
  16. *
  17. * @param [in] fd File descriptor.
  18. * @param [in] buf Buffer to write.
  19. * @param [in] n Number of bytes to write.
  20. * @return Number of bytes written or < 0 on error.
  21. */
  22. ssize_t faux_write(int fd, const void *buf, size_t n)
  23. {
  24. ssize_t bytes_written = 0;
  25. assert(fd != -1);
  26. assert(buf);
  27. if ((-1 == fd) || !buf)
  28. return -1;
  29. if (0 == n)
  30. return 0;
  31. do {
  32. bytes_written = write(fd, buf, n);
  33. } while ((bytes_written < 0) && (EINTR == errno));
  34. return bytes_written;
  35. }
  36. /** @brief Writes data block to file.
  37. *
  38. * The system write() can be interrupted by signal or can write less bytes
  39. * than specified. This function will continue to write data until all data
  40. * will be written or error occured.
  41. *
  42. * @param [in] fd File descriptor.
  43. * @param [in] buf Buffer to write.
  44. * @param [in] n Number of bytes to write.
  45. * @return Number of bytes written.
  46. * < n then insufficient space or error (but some data was already written).
  47. * < 0 - error.
  48. */
  49. ssize_t faux_write_block(int fd, const void *buf, size_t n)
  50. {
  51. ssize_t bytes_written = 0;
  52. size_t total_written = 0;
  53. size_t left = n;
  54. const void *data = buf;
  55. do {
  56. bytes_written = faux_write(fd, data, left);
  57. if (bytes_written < 0) { // Error
  58. if (total_written != 0)
  59. return total_written;
  60. return -1;
  61. }
  62. if (0 == bytes_written) // Insufficient space
  63. return total_written;
  64. data += bytes_written;
  65. left = left - bytes_written;
  66. total_written += bytes_written;
  67. } while (left > 0);
  68. return total_written;
  69. }
  70. /** @brief Reads data from file.
  71. *
  72. * The system read() can be interrupted by signal. This function will retry to
  73. * read if it was interrupted by signal.
  74. *
  75. * @param [in] fd File descriptor.
  76. * @param [in] buf Buffer to write.
  77. * @param [in] n Number of bytes to write.
  78. * @return Number of bytes readed or < 0 on error.
  79. * 0 bytes indicates EOF
  80. */
  81. ssize_t faux_read(int fd, void *buf, size_t n)
  82. {
  83. ssize_t bytes_readed = 0;
  84. assert(fd != -1);
  85. assert(buf);
  86. if ((-1 == fd) || !buf)
  87. return -1;
  88. if (0 == n)
  89. return 0;
  90. do {
  91. bytes_readed = read(fd, buf, n);
  92. } while ((bytes_readed < 0) && (EINTR == errno));
  93. return bytes_readed;
  94. }
  95. /** @brief Reads data block from file.
  96. *
  97. * The system read() can be interrupted by signal or can read less bytes
  98. * than specified. This function will continue to read data until all data
  99. * will be readed or error occured.
  100. *
  101. * @param [in] fd File descriptor.
  102. * @param [in] buf Buffer to write.
  103. * @param [in] n Number of bytes to write.
  104. * @return Number of bytes readed.
  105. * < n EOF or error (but some data was already readed).
  106. * < 0 Error.
  107. */
  108. size_t faux_read_block(int fd, void *buf, size_t n)
  109. {
  110. ssize_t bytes_readed = 0;
  111. size_t total_readed = 0;
  112. size_t left = n;
  113. void *data = buf;
  114. do {
  115. bytes_readed = read(fd, data, left);
  116. if (bytes_readed < 0) {
  117. if (total_readed != 0)
  118. return total_readed;
  119. return -1;
  120. }
  121. if (0 == bytes_readed) // EOF
  122. return total_readed;
  123. data += bytes_readed;
  124. left = left - bytes_readed;
  125. total_readed += bytes_readed;
  126. } while (left > 0);
  127. return total_readed;
  128. }
  129. /** @brief Sends data to socket.
  130. *
  131. * The system send() can be interrupted by signal. This function will retry to
  132. * send in a case of interrupted call.
  133. *
  134. * @param [in] fd Socket.
  135. * @param [in] buf Buffer to write.
  136. * @param [in] n Number of bytes to write.
  137. * @param [in] flags Flags.
  138. * @return Number of bytes written or < 0 on error.
  139. */
  140. ssize_t faux_send(int fd, const void *buf, size_t n, int flags)
  141. {
  142. ssize_t bytes_written = 0;
  143. assert(fd != -1);
  144. assert(buf);
  145. if ((-1 == fd) || !buf)
  146. return -1;
  147. if (0 == n)
  148. return 0;
  149. do {
  150. bytes_written = send(fd, buf, n, flags);
  151. } while ((bytes_written < 0) && (EINTR == errno));
  152. return bytes_written;
  153. }
  154. /** @brief Sends data block to socket.
  155. *
  156. * The system send() can be interrupted by signal or can write less bytes
  157. * than specified. This function will continue to send data until all data
  158. * will be sent or error occured.
  159. *
  160. * @param [in] fd Socket.
  161. * @param [in] buf Buffer to write.
  162. * @param [in] n Number of bytes to write.
  163. * @param [in] flags Flags.
  164. * @return Number of bytes written.
  165. * < n then insufficient space or error (but some data was already written).
  166. * < 0 - error.
  167. */
  168. ssize_t faux_send_block(int fd, const void *buf, size_t n, int flags)
  169. {
  170. ssize_t bytes_written = 0;
  171. size_t total_written = 0;
  172. size_t left = n;
  173. const void *data = buf;
  174. do {
  175. bytes_written = faux_send(fd, data, left, flags);
  176. if (bytes_written < 0) { // Error
  177. if (total_written != 0)
  178. return total_written;
  179. return -1;
  180. }
  181. if (0 == bytes_written) // Insufficient space
  182. return total_written;
  183. data += bytes_written;
  184. left = left - bytes_written;
  185. total_written += bytes_written;
  186. } while (left > 0);
  187. return total_written;
  188. }
  189. /** @brief Sends struct iovec data blocks to socket.
  190. *
  191. * This function is like a faux_send_block() function but uses scatter/gather.
  192. *
  193. * @see faux_send_block().
  194. * @param [in] fd Socket.
  195. * @param [in] buf Buffer to write.
  196. * @param [in] n Number of bytes to write.
  197. * @param [in] flags Flags.
  198. * @return Number of bytes written.
  199. * < n then insufficient space or error (but some data was already written).
  200. * < 0 - error.
  201. */
  202. ssize_t faux_sendv_block(int fd, const struct iovec *iov, int iovcnt, int flags)
  203. {
  204. ssize_t bytes_written = 0;
  205. size_t total_written = 0;
  206. int i = 0;
  207. if (!iov)
  208. return -1;
  209. if (iovcnt == 0)
  210. return 0;
  211. for (i = 0; i < iovcnt; i++) {
  212. if (iov[i].iov_len == 0)
  213. continue;
  214. bytes_written = faux_send_block(fd, iov[i].iov_base, iov[i].iov_len, flags);
  215. if (bytes_written < 0) { // Error
  216. if (total_written != 0)
  217. return total_written;
  218. return -1;
  219. }
  220. if (0 == bytes_written) // Insufficient space
  221. return total_written;
  222. total_written += bytes_written;
  223. }
  224. return total_written;
  225. }
  226. /** @brief Receive data from socket.
  227. *
  228. * The system recv() can be interrupted by signal. This function will retry to
  229. * receive if it was interrupted by signal.
  230. *
  231. * @param [in] fd Socket.
  232. * @param [in] buf Buffer to write.
  233. * @param [in] n Number of bytes to write.
  234. * @param [in] flags Flags.
  235. * @return Number of bytes readed or < 0 on error.
  236. * 0 bytes indicates EOF
  237. */
  238. ssize_t faux_recv(int fd, void *buf, size_t n, int flags)
  239. {
  240. ssize_t bytes_readed = 0;
  241. assert(fd != -1);
  242. assert(buf);
  243. if ((-1 == fd) || !buf)
  244. return -1;
  245. if (0 == n)
  246. return 0;
  247. do {
  248. bytes_readed = recv(fd, buf, n, flags);
  249. } while ((bytes_readed < 0) && (EINTR == errno));
  250. return bytes_readed;
  251. }
  252. /** @brief Receive data block from socket.
  253. *
  254. * The system recv() can be interrupted by signal or can read less bytes
  255. * than specified. This function will continue to read data until all data
  256. * will be readed or error occured.
  257. *
  258. * @param [in] fd Socket.
  259. * @param [in] buf Buffer to write.
  260. * @param [in] n Number of bytes to write.
  261. * @param [in] flags Flags.
  262. * @return Number of bytes readed.
  263. * < n EOF or error (but some data was already readed).
  264. * < 0 Error.
  265. */
  266. size_t faux_recv_block(int fd, void *buf, size_t n, int flags)
  267. {
  268. ssize_t bytes_readed = 0;
  269. size_t total_readed = 0;
  270. size_t left = n;
  271. void *data = buf;
  272. do {
  273. bytes_readed = recv(fd, data, left, flags);
  274. if (bytes_readed < 0) {
  275. if (total_readed != 0)
  276. return total_readed;
  277. return -1;
  278. }
  279. if (0 == bytes_readed) // EOF
  280. return total_readed;
  281. data += bytes_readed;
  282. left = left - bytes_readed;
  283. total_readed += bytes_readed;
  284. } while (left > 0);
  285. return total_readed;
  286. }