bio.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2006, 2009, 2013 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file util/bio.c
  18. * @brief functions for buffering IO
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #define LOG(kind, ...) GNUNET_log_from (kind, "util-bio", __VA_ARGS__)
  24. #ifndef PATH_MAX
  25. /**
  26. * Assumed maximum path length (for source file names).
  27. */
  28. #define PATH_MAX 4096
  29. #endif
  30. /**
  31. * Size for I/O buffers.
  32. */
  33. #define BIO_BUFFER_SIZE 65536
  34. /**
  35. * Maximum size allowed for meta data written/read from disk.
  36. * File-sharing limits to 64k, so this should be rather generous.
  37. */
  38. #define MAX_META_DATA (1024 * 1024)
  39. /**
  40. * Handle for buffered reading.
  41. */
  42. struct GNUNET_BIO_ReadHandle
  43. {
  44. /**
  45. * Underlying file abstraction.
  46. */
  47. struct GNUNET_DISK_FileHandle *fd;
  48. /**
  49. * Error message, NULL if there were no errors.
  50. */
  51. char *emsg;
  52. /**
  53. * I/O buffer. Allocated at the end of the struct, do not free!
  54. */
  55. char *buffer;
  56. /**
  57. * Number of bytes available in read @e buffer.
  58. */
  59. size_t have;
  60. /**
  61. * Total size of @e buffer.
  62. */
  63. size_t size;
  64. /**
  65. * Current read offset in @e buffer.
  66. */
  67. off_t pos;
  68. };
  69. /**
  70. * Open a file for reading.
  71. *
  72. * @param fn file name to be opened
  73. * @return IO handle on success, NULL on error
  74. */
  75. struct GNUNET_BIO_ReadHandle *
  76. GNUNET_BIO_read_open (const char *fn)
  77. {
  78. struct GNUNET_DISK_FileHandle *fd;
  79. struct GNUNET_BIO_ReadHandle *h;
  80. fd = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ, GNUNET_DISK_PERM_NONE);
  81. if (NULL == fd)
  82. return NULL;
  83. h = GNUNET_malloc (sizeof(struct GNUNET_BIO_ReadHandle) + BIO_BUFFER_SIZE);
  84. h->buffer = (char *) &h[1];
  85. h->size = BIO_BUFFER_SIZE;
  86. h->fd = fd;
  87. return h;
  88. }
  89. /**
  90. * Close an open file. Reports if any errors reading
  91. * from the file were encountered.
  92. *
  93. * @param h file handle
  94. * @param emsg set to the error message
  95. * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
  96. */
  97. int
  98. GNUNET_BIO_read_close (struct GNUNET_BIO_ReadHandle *h, char **emsg)
  99. {
  100. int err;
  101. err = (NULL == h->emsg) ? GNUNET_OK : GNUNET_SYSERR;
  102. if (emsg != NULL)
  103. *emsg = h->emsg;
  104. else
  105. GNUNET_free_non_null (h->emsg);
  106. GNUNET_DISK_file_close (h->fd);
  107. GNUNET_free (h);
  108. return err;
  109. }
  110. /**
  111. * Read the contents of a binary file into a buffer.
  112. *
  113. * @param h handle to an open file
  114. * @param what describes what is being read (for error message creation)
  115. * @param result the buffer to write the result to
  116. * @param len the number of bytes to read
  117. * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
  118. */
  119. int
  120. GNUNET_BIO_read (struct GNUNET_BIO_ReadHandle *h,
  121. const char *what,
  122. void *result,
  123. size_t len)
  124. {
  125. char *dst = result;
  126. size_t min;
  127. size_t pos;
  128. ssize_t ret;
  129. if (NULL != h->emsg)
  130. return GNUNET_SYSERR;
  131. pos = 0;
  132. do
  133. {
  134. /* first, use buffer */
  135. min = h->have - h->pos;
  136. if (min > 0)
  137. {
  138. if (min > len - pos)
  139. min = len - pos;
  140. GNUNET_memcpy (&dst[pos], &h->buffer[h->pos], min);
  141. h->pos += min;
  142. pos += min;
  143. }
  144. if (pos == len)
  145. return GNUNET_OK; /* done! */
  146. GNUNET_assert (((off_t) h->have) == h->pos);
  147. /* fill buffer */
  148. ret = GNUNET_DISK_file_read (h->fd, h->buffer, h->size);
  149. if (-1 == ret)
  150. {
  151. GNUNET_asprintf (&h->emsg,
  152. _ ("Error reading `%s': %s"),
  153. what,
  154. strerror (errno));
  155. return GNUNET_SYSERR;
  156. }
  157. if (0 == ret)
  158. {
  159. GNUNET_asprintf (&h->emsg,
  160. _ ("Error reading `%s': %s"),
  161. what,
  162. _ ("End of file"));
  163. return GNUNET_SYSERR;
  164. }
  165. h->pos = 0;
  166. h->have = ret;
  167. }
  168. while (pos < len); /* should always be true */
  169. return GNUNET_OK;
  170. }
  171. /**
  172. * Read the contents of a binary file into a buffer.
  173. *
  174. * @param h handle to an open file
  175. * @param file name of the source file
  176. * @param line line number in the source file
  177. * @param result the buffer to write the result to
  178. * @param len the number of bytes to read
  179. * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
  180. */
  181. int
  182. GNUNET_BIO_read_fn (struct GNUNET_BIO_ReadHandle *h,
  183. const char *file,
  184. int line,
  185. void *result,
  186. size_t len)
  187. {
  188. char what[PATH_MAX + 1024];
  189. GNUNET_snprintf (what, sizeof(what), "%s:%d", file, line);
  190. return GNUNET_BIO_read (h, what, result, len);
  191. }
  192. /**
  193. * Read 0-terminated string from a file.
  194. *
  195. * @param h handle to an open file
  196. * @param what describes what is being read (for error message creation)
  197. * @param result the buffer to store a pointer to the (allocated) string to
  198. * (note that *result could be set to NULL as well)
  199. * @param max_length maximum allowed length for the string
  200. * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
  201. */
  202. int
  203. GNUNET_BIO_read_string (struct GNUNET_BIO_ReadHandle *h,
  204. const char *what,
  205. char **result,
  206. size_t max_length)
  207. {
  208. char *buf;
  209. uint32_t big;
  210. if (GNUNET_OK != GNUNET_BIO_read_int32 (h, &big))
  211. {
  212. GNUNET_free_non_null (h->emsg);
  213. GNUNET_asprintf (&h->emsg, _ ("Error reading length of string `%s'"), what);
  214. return GNUNET_SYSERR;
  215. }
  216. if (0 == big)
  217. {
  218. *result = NULL;
  219. return GNUNET_OK;
  220. }
  221. if (big > max_length)
  222. {
  223. GNUNET_asprintf (&h->emsg,
  224. _ ("String `%s' longer than allowed (%u > %u)"),
  225. what,
  226. big,
  227. max_length);
  228. return GNUNET_SYSERR;
  229. }
  230. buf = GNUNET_malloc (big);
  231. *result = buf;
  232. buf[--big] = '\0';
  233. if (0 == big)
  234. return GNUNET_OK;
  235. if (GNUNET_OK != GNUNET_BIO_read (h, what, buf, big))
  236. {
  237. GNUNET_free (buf);
  238. *result = NULL;
  239. return GNUNET_SYSERR;
  240. }
  241. return GNUNET_OK;
  242. }
  243. /**
  244. * Read metadata container from a file.
  245. *
  246. * @param h handle to an open file
  247. * @param what describes what is being read (for error message creation)
  248. * @param result the buffer to store a pointer to the (allocated) metadata
  249. * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
  250. */
  251. int
  252. GNUNET_BIO_read_meta_data (struct GNUNET_BIO_ReadHandle *h,
  253. const char *what,
  254. struct GNUNET_CONTAINER_MetaData **result)
  255. {
  256. uint32_t size;
  257. char *buf;
  258. struct GNUNET_CONTAINER_MetaData *meta;
  259. if (GNUNET_OK != GNUNET_BIO_read_int32 (h, (int32_t *) &size))
  260. return GNUNET_SYSERR;
  261. if (size == 0)
  262. {
  263. *result = NULL;
  264. return GNUNET_OK;
  265. }
  266. if (size > MAX_META_DATA)
  267. {
  268. GNUNET_asprintf (&h->emsg,
  269. _ ("Serialized metadata `%s' larger than allowed (%u>%u)"),
  270. what,
  271. size,
  272. MAX_META_DATA);
  273. return GNUNET_SYSERR;
  274. }
  275. buf = GNUNET_malloc (size);
  276. if (GNUNET_OK != GNUNET_BIO_read (h, what, buf, size))
  277. {
  278. GNUNET_free (buf);
  279. return GNUNET_SYSERR;
  280. }
  281. meta = GNUNET_CONTAINER_meta_data_deserialize (buf, size);
  282. if (NULL == meta)
  283. {
  284. GNUNET_free (buf);
  285. GNUNET_asprintf (&h->emsg, _ ("Metadata `%s' failed to deserialize"), what);
  286. return GNUNET_SYSERR;
  287. }
  288. GNUNET_free (buf);
  289. *result = meta;
  290. return GNUNET_OK;
  291. }
  292. /**
  293. * Read an (u)int32_t.
  294. *
  295. * @param h hande to open file
  296. * @param file name of the source file
  297. * @param line line number in the source file
  298. * @param i address of 32-bit integer to read
  299. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  300. */
  301. int
  302. GNUNET_BIO_read_int32__ (struct GNUNET_BIO_ReadHandle *h,
  303. const char *file,
  304. int line,
  305. int32_t *i)
  306. {
  307. int32_t big;
  308. if (GNUNET_OK != GNUNET_BIO_read_fn (h, file, line, &big, sizeof(int32_t)))
  309. return GNUNET_SYSERR;
  310. *i = ntohl (big);
  311. return GNUNET_OK;
  312. }
  313. /**
  314. * Read an (u)int64_t.
  315. *
  316. * @param h hande to open file
  317. * @param file name of the source file
  318. * @param line line number in the source file
  319. * @param i address of 64-bit integer to read
  320. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  321. */
  322. int
  323. GNUNET_BIO_read_int64__ (struct GNUNET_BIO_ReadHandle *h,
  324. const char *file,
  325. int line,
  326. int64_t *i)
  327. {
  328. int64_t big;
  329. if (GNUNET_OK != GNUNET_BIO_read_fn (h, file, line, &big, sizeof(int64_t)))
  330. return GNUNET_SYSERR;
  331. *i = GNUNET_ntohll (big);
  332. return GNUNET_OK;
  333. }
  334. /**
  335. * Handle for buffered writing.
  336. */
  337. struct GNUNET_BIO_WriteHandle
  338. {
  339. /**
  340. * Underlying file handle.
  341. */
  342. struct GNUNET_DISK_FileHandle *fd;
  343. /**
  344. * I/O buffer. Do not free, allocated at the end of the struct.
  345. */
  346. char *buffer;
  347. /**
  348. * Number of bytes already in @e buffer.
  349. */
  350. size_t have;
  351. /**
  352. * Total size of @e buffer.
  353. */
  354. size_t size;
  355. };
  356. /**
  357. * Open a file for writing.
  358. *
  359. * @param fn file name to be opened
  360. * @return IO handle on success, NULL on error
  361. */
  362. struct GNUNET_BIO_WriteHandle *
  363. GNUNET_BIO_write_open (const char *fn)
  364. {
  365. struct GNUNET_DISK_FileHandle *fd;
  366. struct GNUNET_BIO_WriteHandle *h;
  367. fd =
  368. GNUNET_DISK_file_open (fn,
  369. GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_TRUNCATE
  370. | GNUNET_DISK_OPEN_CREATE,
  371. GNUNET_DISK_PERM_USER_READ
  372. | GNUNET_DISK_PERM_USER_WRITE);
  373. if (NULL == fd)
  374. return NULL;
  375. h = GNUNET_malloc (sizeof(struct GNUNET_BIO_WriteHandle) + BIO_BUFFER_SIZE);
  376. h->buffer = (char *) &h[1];
  377. h->size = BIO_BUFFER_SIZE;
  378. h->fd = fd;
  379. return h;
  380. }
  381. /**
  382. * Close an open file for writing.
  383. *
  384. * @param h file handle
  385. * @return #GNUNET_OK on success, #GNUNET_SYSERR otherwise
  386. */
  387. int
  388. GNUNET_BIO_write_close (struct GNUNET_BIO_WriteHandle *h)
  389. {
  390. int ret;
  391. ret = GNUNET_SYSERR;
  392. if ((NULL != h->fd) && (GNUNET_OK == (ret = GNUNET_BIO_flush (h))))
  393. GNUNET_DISK_file_close (h->fd);
  394. GNUNET_free (h);
  395. return ret;
  396. }
  397. /**
  398. * Force a buffered writer to flush its buffer
  399. *
  400. * @param h the writer handle
  401. * @return #GNUNET_OK upon success. Upon failure #GNUNET_SYSERR is returned and
  402. * the file is closed
  403. */
  404. int
  405. GNUNET_BIO_flush (struct GNUNET_BIO_WriteHandle *h)
  406. {
  407. ssize_t ret;
  408. ret = GNUNET_DISK_file_write (h->fd, h->buffer, h->have);
  409. if (ret != (ssize_t) h->have)
  410. {
  411. GNUNET_DISK_file_close (h->fd);
  412. h->fd = NULL;
  413. return GNUNET_SYSERR; /* error */
  414. }
  415. h->have = 0;
  416. return GNUNET_OK;
  417. }
  418. /**
  419. * Write a buffer to a file.
  420. *
  421. * @param h handle to open file
  422. * @param buffer the data to write
  423. * @param n number of bytes to write
  424. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  425. */
  426. int
  427. GNUNET_BIO_write (struct GNUNET_BIO_WriteHandle *h,
  428. const void *buffer,
  429. size_t n)
  430. {
  431. const char *src = buffer;
  432. size_t min;
  433. size_t pos;
  434. if (NULL == h->fd)
  435. return GNUNET_SYSERR;
  436. pos = 0;
  437. do
  438. {
  439. /* first, just use buffer */
  440. min = h->size - h->have;
  441. if (min > n - pos)
  442. min = n - pos;
  443. GNUNET_memcpy (&h->buffer[h->have], &src[pos], min);
  444. pos += min;
  445. h->have += min;
  446. if (pos == n)
  447. return GNUNET_OK; /* done */
  448. GNUNET_assert (h->have == h->size);
  449. if (GNUNET_OK != GNUNET_BIO_flush (h))
  450. return GNUNET_SYSERR; /* error */
  451. }
  452. while (pos < n); /* should always be true */
  453. GNUNET_break (0);
  454. return GNUNET_OK;
  455. }
  456. /**
  457. * Write a string to a file.
  458. *
  459. * @param h handle to open file
  460. * @param s string to write (can be NULL)
  461. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  462. */
  463. int
  464. GNUNET_BIO_write_string (struct GNUNET_BIO_WriteHandle *h, const char *s)
  465. {
  466. uint32_t slen;
  467. slen = (uint32_t) ((s == NULL) ? 0 : strlen (s) + 1);
  468. if (GNUNET_OK != GNUNET_BIO_write_int32 (h, slen))
  469. return GNUNET_SYSERR;
  470. if (0 != slen)
  471. return GNUNET_BIO_write (h, s, slen - 1);
  472. return GNUNET_OK;
  473. }
  474. /**
  475. * Write metadata container to a file.
  476. *
  477. * @param h handle to open file
  478. * @param m metadata to write
  479. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  480. */
  481. int
  482. GNUNET_BIO_write_meta_data (struct GNUNET_BIO_WriteHandle *h,
  483. const struct GNUNET_CONTAINER_MetaData *m)
  484. {
  485. ssize_t size;
  486. char *buf;
  487. if (m == NULL)
  488. return GNUNET_BIO_write_int32 (h, 0);
  489. buf = NULL;
  490. size = GNUNET_CONTAINER_meta_data_serialize (
  491. m,
  492. &buf,
  493. MAX_META_DATA,
  494. GNUNET_CONTAINER_META_DATA_SERIALIZE_PART);
  495. if (size == -1)
  496. {
  497. GNUNET_free (buf);
  498. return GNUNET_SYSERR;
  499. }
  500. if ((GNUNET_OK != GNUNET_BIO_write_int32 (h, (uint32_t) size)) ||
  501. (GNUNET_OK != GNUNET_BIO_write (h, buf, size)))
  502. {
  503. GNUNET_free (buf);
  504. return GNUNET_SYSERR;
  505. }
  506. GNUNET_free (buf);
  507. return GNUNET_OK;
  508. }
  509. /**
  510. * Write an (u)int32_t.
  511. *
  512. * @param h hande to open file
  513. * @param i 32-bit integer to write
  514. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  515. */
  516. int
  517. GNUNET_BIO_write_int32 (struct GNUNET_BIO_WriteHandle *h, int32_t i)
  518. {
  519. int32_t big;
  520. big = htonl (i);
  521. return GNUNET_BIO_write (h, &big, sizeof(int32_t));
  522. }
  523. /**
  524. * Write an (u)int64_t.
  525. *
  526. * @param h hande to open file
  527. * @param i 64-bit integer to write
  528. * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  529. */
  530. int
  531. GNUNET_BIO_write_int64 (struct GNUNET_BIO_WriteHandle *h, int64_t i)
  532. {
  533. int64_t big;
  534. big = GNUNET_htonll (i);
  535. return GNUNET_BIO_write (h, &big, sizeof(int64_t));
  536. }
  537. /* end of bio.c */