qemu-file.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  1. /*
  2. * QEMU System Emulator
  3. *
  4. * Copyright (c) 2003-2008 Fabrice Bellard
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu/osdep.h"
  25. #include <zlib.h>
  26. #include "qemu-common.h"
  27. #include "qemu/error-report.h"
  28. #include "qemu/iov.h"
  29. #include "qemu/sockets.h"
  30. #include "qemu/coroutine.h"
  31. #include "migration/migration.h"
  32. #include "qemu-file.h"
  33. #include "trace.h"
  34. #define IO_BUF_SIZE 32768
  35. #define MAX_IOV_SIZE MIN(IOV_MAX, 64)
  36. struct QEMUFile {
  37. const QEMUFileOps *ops;
  38. const QEMUFileHooks *hooks;
  39. void *opaque;
  40. int64_t bytes_xfer;
  41. int64_t xfer_limit;
  42. int64_t pos; /* start of buffer when writing, end of buffer
  43. when reading */
  44. int buf_index;
  45. int buf_size; /* 0 when writing */
  46. uint8_t buf[IO_BUF_SIZE];
  47. DECLARE_BITMAP(may_free, MAX_IOV_SIZE);
  48. struct iovec iov[MAX_IOV_SIZE];
  49. unsigned int iovcnt;
  50. int last_error;
  51. };
  52. /*
  53. * Stop a file from being read/written - not all backing files can do this
  54. * typically only sockets can.
  55. */
  56. int qemu_file_shutdown(QEMUFile *f)
  57. {
  58. if (!f->ops->shut_down) {
  59. return -ENOSYS;
  60. }
  61. return f->ops->shut_down(f->opaque, true, true);
  62. }
  63. /*
  64. * Result: QEMUFile* for a 'return path' for comms in the opposite direction
  65. * NULL if not available
  66. */
  67. QEMUFile *qemu_file_get_return_path(QEMUFile *f)
  68. {
  69. if (!f->ops->get_return_path) {
  70. return NULL;
  71. }
  72. return f->ops->get_return_path(f->opaque);
  73. }
  74. bool qemu_file_mode_is_not_valid(const char *mode)
  75. {
  76. if (mode == NULL ||
  77. (mode[0] != 'r' && mode[0] != 'w') ||
  78. mode[1] != 'b' || mode[2] != 0) {
  79. fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
  80. return true;
  81. }
  82. return false;
  83. }
  84. QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops)
  85. {
  86. QEMUFile *f;
  87. f = g_new0(QEMUFile, 1);
  88. f->opaque = opaque;
  89. f->ops = ops;
  90. return f;
  91. }
  92. void qemu_file_set_hooks(QEMUFile *f, const QEMUFileHooks *hooks)
  93. {
  94. f->hooks = hooks;
  95. }
  96. /*
  97. * Get last error for stream f
  98. *
  99. * Return negative error value if there has been an error on previous
  100. * operations, return 0 if no error happened.
  101. *
  102. */
  103. int qemu_file_get_error(QEMUFile *f)
  104. {
  105. return f->last_error;
  106. }
  107. void qemu_file_set_error(QEMUFile *f, int ret)
  108. {
  109. if (f->last_error == 0) {
  110. f->last_error = ret;
  111. }
  112. }
  113. bool qemu_file_is_writable(QEMUFile *f)
  114. {
  115. return f->ops->writev_buffer;
  116. }
  117. static void qemu_iovec_release_ram(QEMUFile *f)
  118. {
  119. struct iovec iov;
  120. unsigned long idx;
  121. /* Find and release all the contiguous memory ranges marked as may_free. */
  122. idx = find_next_bit(f->may_free, f->iovcnt, 0);
  123. if (idx >= f->iovcnt) {
  124. return;
  125. }
  126. iov = f->iov[idx];
  127. /* The madvise() in the loop is called for iov within a continuous range and
  128. * then reinitialize the iov. And in the end, madvise() is called for the
  129. * last iov.
  130. */
  131. while ((idx = find_next_bit(f->may_free, f->iovcnt, idx + 1)) < f->iovcnt) {
  132. /* check for adjacent buffer and coalesce them */
  133. if (iov.iov_base + iov.iov_len == f->iov[idx].iov_base) {
  134. iov.iov_len += f->iov[idx].iov_len;
  135. continue;
  136. }
  137. if (qemu_madvise(iov.iov_base, iov.iov_len, QEMU_MADV_DONTNEED) < 0) {
  138. error_report("migrate: madvise DONTNEED failed %p %zd: %s",
  139. iov.iov_base, iov.iov_len, strerror(errno));
  140. }
  141. iov = f->iov[idx];
  142. }
  143. if (qemu_madvise(iov.iov_base, iov.iov_len, QEMU_MADV_DONTNEED) < 0) {
  144. error_report("migrate: madvise DONTNEED failed %p %zd: %s",
  145. iov.iov_base, iov.iov_len, strerror(errno));
  146. }
  147. memset(f->may_free, 0, sizeof(f->may_free));
  148. }
  149. /**
  150. * Flushes QEMUFile buffer
  151. *
  152. * If there is writev_buffer QEMUFileOps it uses it otherwise uses
  153. * put_buffer ops. This will flush all pending data. If data was
  154. * only partially flushed, it will set an error state.
  155. */
  156. void qemu_fflush(QEMUFile *f)
  157. {
  158. ssize_t ret = 0;
  159. ssize_t expect = 0;
  160. if (!qemu_file_is_writable(f)) {
  161. return;
  162. }
  163. if (f->iovcnt > 0) {
  164. expect = iov_size(f->iov, f->iovcnt);
  165. ret = f->ops->writev_buffer(f->opaque, f->iov, f->iovcnt, f->pos);
  166. qemu_iovec_release_ram(f);
  167. }
  168. if (ret >= 0) {
  169. f->pos += ret;
  170. }
  171. /* We expect the QEMUFile write impl to send the full
  172. * data set we requested, so sanity check that.
  173. */
  174. if (ret != expect) {
  175. qemu_file_set_error(f, ret < 0 ? ret : -EIO);
  176. }
  177. f->buf_index = 0;
  178. f->iovcnt = 0;
  179. }
  180. void ram_control_before_iterate(QEMUFile *f, uint64_t flags)
  181. {
  182. int ret = 0;
  183. if (f->hooks && f->hooks->before_ram_iterate) {
  184. ret = f->hooks->before_ram_iterate(f, f->opaque, flags, NULL);
  185. if (ret < 0) {
  186. qemu_file_set_error(f, ret);
  187. }
  188. }
  189. }
  190. void ram_control_after_iterate(QEMUFile *f, uint64_t flags)
  191. {
  192. int ret = 0;
  193. if (f->hooks && f->hooks->after_ram_iterate) {
  194. ret = f->hooks->after_ram_iterate(f, f->opaque, flags, NULL);
  195. if (ret < 0) {
  196. qemu_file_set_error(f, ret);
  197. }
  198. }
  199. }
  200. void ram_control_load_hook(QEMUFile *f, uint64_t flags, void *data)
  201. {
  202. int ret = -EINVAL;
  203. if (f->hooks && f->hooks->hook_ram_load) {
  204. ret = f->hooks->hook_ram_load(f, f->opaque, flags, data);
  205. if (ret < 0) {
  206. qemu_file_set_error(f, ret);
  207. }
  208. } else {
  209. /*
  210. * Hook is a hook specifically requested by the source sending a flag
  211. * that expects there to be a hook on the destination.
  212. */
  213. if (flags == RAM_CONTROL_HOOK) {
  214. qemu_file_set_error(f, ret);
  215. }
  216. }
  217. }
  218. size_t ram_control_save_page(QEMUFile *f, ram_addr_t block_offset,
  219. ram_addr_t offset, size_t size,
  220. uint64_t *bytes_sent)
  221. {
  222. if (f->hooks && f->hooks->save_page) {
  223. int ret = f->hooks->save_page(f, f->opaque, block_offset,
  224. offset, size, bytes_sent);
  225. if (ret != RAM_SAVE_CONTROL_DELAYED) {
  226. if (bytes_sent && *bytes_sent > 0) {
  227. qemu_update_position(f, *bytes_sent);
  228. } else if (ret < 0) {
  229. qemu_file_set_error(f, ret);
  230. }
  231. }
  232. return ret;
  233. }
  234. return RAM_SAVE_CONTROL_NOT_SUPP;
  235. }
  236. /*
  237. * Attempt to fill the buffer from the underlying file
  238. * Returns the number of bytes read, or negative value for an error.
  239. *
  240. * Note that it can return a partially full buffer even in a not error/not EOF
  241. * case if the underlying file descriptor gives a short read, and that can
  242. * happen even on a blocking fd.
  243. */
  244. static ssize_t qemu_fill_buffer(QEMUFile *f)
  245. {
  246. int len;
  247. int pending;
  248. assert(!qemu_file_is_writable(f));
  249. pending = f->buf_size - f->buf_index;
  250. if (pending > 0) {
  251. memmove(f->buf, f->buf + f->buf_index, pending);
  252. }
  253. f->buf_index = 0;
  254. f->buf_size = pending;
  255. len = f->ops->get_buffer(f->opaque, f->buf + pending, f->pos,
  256. IO_BUF_SIZE - pending);
  257. if (len > 0) {
  258. f->buf_size += len;
  259. f->pos += len;
  260. } else if (len == 0) {
  261. qemu_file_set_error(f, -EIO);
  262. } else if (len != -EAGAIN) {
  263. qemu_file_set_error(f, len);
  264. }
  265. return len;
  266. }
  267. void qemu_update_position(QEMUFile *f, size_t size)
  268. {
  269. f->pos += size;
  270. }
  271. /** Closes the file
  272. *
  273. * Returns negative error value if any error happened on previous operations or
  274. * while closing the file. Returns 0 or positive number on success.
  275. *
  276. * The meaning of return value on success depends on the specific backend
  277. * being used.
  278. */
  279. int qemu_fclose(QEMUFile *f)
  280. {
  281. int ret;
  282. qemu_fflush(f);
  283. ret = qemu_file_get_error(f);
  284. if (f->ops->close) {
  285. int ret2 = f->ops->close(f->opaque);
  286. if (ret >= 0) {
  287. ret = ret2;
  288. }
  289. }
  290. /* If any error was spotted before closing, we should report it
  291. * instead of the close() return value.
  292. */
  293. if (f->last_error) {
  294. ret = f->last_error;
  295. }
  296. g_free(f);
  297. trace_qemu_file_fclose();
  298. return ret;
  299. }
  300. static void add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size,
  301. bool may_free)
  302. {
  303. /* check for adjacent buffer and coalesce them */
  304. if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base +
  305. f->iov[f->iovcnt - 1].iov_len &&
  306. may_free == test_bit(f->iovcnt - 1, f->may_free))
  307. {
  308. f->iov[f->iovcnt - 1].iov_len += size;
  309. } else {
  310. if (may_free) {
  311. set_bit(f->iovcnt, f->may_free);
  312. }
  313. f->iov[f->iovcnt].iov_base = (uint8_t *)buf;
  314. f->iov[f->iovcnt++].iov_len = size;
  315. }
  316. if (f->iovcnt >= MAX_IOV_SIZE) {
  317. qemu_fflush(f);
  318. }
  319. }
  320. void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, size_t size,
  321. bool may_free)
  322. {
  323. if (f->last_error) {
  324. return;
  325. }
  326. f->bytes_xfer += size;
  327. add_to_iovec(f, buf, size, may_free);
  328. }
  329. void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, size_t size)
  330. {
  331. size_t l;
  332. if (f->last_error) {
  333. return;
  334. }
  335. while (size > 0) {
  336. l = IO_BUF_SIZE - f->buf_index;
  337. if (l > size) {
  338. l = size;
  339. }
  340. memcpy(f->buf + f->buf_index, buf, l);
  341. f->bytes_xfer += l;
  342. add_to_iovec(f, f->buf + f->buf_index, l, false);
  343. f->buf_index += l;
  344. if (f->buf_index == IO_BUF_SIZE) {
  345. qemu_fflush(f);
  346. }
  347. if (qemu_file_get_error(f)) {
  348. break;
  349. }
  350. buf += l;
  351. size -= l;
  352. }
  353. }
  354. void qemu_put_byte(QEMUFile *f, int v)
  355. {
  356. if (f->last_error) {
  357. return;
  358. }
  359. f->buf[f->buf_index] = v;
  360. f->bytes_xfer++;
  361. add_to_iovec(f, f->buf + f->buf_index, 1, false);
  362. f->buf_index++;
  363. if (f->buf_index == IO_BUF_SIZE) {
  364. qemu_fflush(f);
  365. }
  366. }
  367. void qemu_file_skip(QEMUFile *f, int size)
  368. {
  369. if (f->buf_index + size <= f->buf_size) {
  370. f->buf_index += size;
  371. }
  372. }
  373. /*
  374. * Read 'size' bytes from file (at 'offset') without moving the
  375. * pointer and set 'buf' to point to that data.
  376. *
  377. * It will return size bytes unless there was an error, in which case it will
  378. * return as many as it managed to read (assuming blocking fd's which
  379. * all current QEMUFile are)
  380. */
  381. size_t qemu_peek_buffer(QEMUFile *f, uint8_t **buf, size_t size, size_t offset)
  382. {
  383. ssize_t pending;
  384. size_t index;
  385. assert(!qemu_file_is_writable(f));
  386. assert(offset < IO_BUF_SIZE);
  387. assert(size <= IO_BUF_SIZE - offset);
  388. /* The 1st byte to read from */
  389. index = f->buf_index + offset;
  390. /* The number of available bytes starting at index */
  391. pending = f->buf_size - index;
  392. /*
  393. * qemu_fill_buffer might return just a few bytes, even when there isn't
  394. * an error, so loop collecting them until we get enough.
  395. */
  396. while (pending < size) {
  397. int received = qemu_fill_buffer(f);
  398. if (received <= 0) {
  399. break;
  400. }
  401. index = f->buf_index + offset;
  402. pending = f->buf_size - index;
  403. }
  404. if (pending <= 0) {
  405. return 0;
  406. }
  407. if (size > pending) {
  408. size = pending;
  409. }
  410. *buf = f->buf + index;
  411. return size;
  412. }
  413. /*
  414. * Read 'size' bytes of data from the file into buf.
  415. * 'size' can be larger than the internal buffer.
  416. *
  417. * It will return size bytes unless there was an error, in which case it will
  418. * return as many as it managed to read (assuming blocking fd's which
  419. * all current QEMUFile are)
  420. */
  421. size_t qemu_get_buffer(QEMUFile *f, uint8_t *buf, size_t size)
  422. {
  423. size_t pending = size;
  424. size_t done = 0;
  425. while (pending > 0) {
  426. size_t res;
  427. uint8_t *src;
  428. res = qemu_peek_buffer(f, &src, MIN(pending, IO_BUF_SIZE), 0);
  429. if (res == 0) {
  430. return done;
  431. }
  432. memcpy(buf, src, res);
  433. qemu_file_skip(f, res);
  434. buf += res;
  435. pending -= res;
  436. done += res;
  437. }
  438. return done;
  439. }
  440. /*
  441. * Read 'size' bytes of data from the file.
  442. * 'size' can be larger than the internal buffer.
  443. *
  444. * The data:
  445. * may be held on an internal buffer (in which case *buf is updated
  446. * to point to it) that is valid until the next qemu_file operation.
  447. * OR
  448. * will be copied to the *buf that was passed in.
  449. *
  450. * The code tries to avoid the copy if possible.
  451. *
  452. * It will return size bytes unless there was an error, in which case it will
  453. * return as many as it managed to read (assuming blocking fd's which
  454. * all current QEMUFile are)
  455. *
  456. * Note: Since **buf may get changed, the caller should take care to
  457. * keep a pointer to the original buffer if it needs to deallocate it.
  458. */
  459. size_t qemu_get_buffer_in_place(QEMUFile *f, uint8_t **buf, size_t size)
  460. {
  461. if (size < IO_BUF_SIZE) {
  462. size_t res;
  463. uint8_t *src;
  464. res = qemu_peek_buffer(f, &src, size, 0);
  465. if (res == size) {
  466. qemu_file_skip(f, res);
  467. *buf = src;
  468. return res;
  469. }
  470. }
  471. return qemu_get_buffer(f, *buf, size);
  472. }
  473. /*
  474. * Peeks a single byte from the buffer; this isn't guaranteed to work if
  475. * offset leaves a gap after the previous read/peeked data.
  476. */
  477. int qemu_peek_byte(QEMUFile *f, int offset)
  478. {
  479. int index = f->buf_index + offset;
  480. assert(!qemu_file_is_writable(f));
  481. assert(offset < IO_BUF_SIZE);
  482. if (index >= f->buf_size) {
  483. qemu_fill_buffer(f);
  484. index = f->buf_index + offset;
  485. if (index >= f->buf_size) {
  486. return 0;
  487. }
  488. }
  489. return f->buf[index];
  490. }
  491. int qemu_get_byte(QEMUFile *f)
  492. {
  493. int result;
  494. result = qemu_peek_byte(f, 0);
  495. qemu_file_skip(f, 1);
  496. return result;
  497. }
  498. int64_t qemu_ftell_fast(QEMUFile *f)
  499. {
  500. int64_t ret = f->pos;
  501. int i;
  502. for (i = 0; i < f->iovcnt; i++) {
  503. ret += f->iov[i].iov_len;
  504. }
  505. return ret;
  506. }
  507. int64_t qemu_ftell(QEMUFile *f)
  508. {
  509. qemu_fflush(f);
  510. return f->pos;
  511. }
  512. int qemu_file_rate_limit(QEMUFile *f)
  513. {
  514. if (qemu_file_get_error(f)) {
  515. return 1;
  516. }
  517. if (f->xfer_limit > 0 && f->bytes_xfer > f->xfer_limit) {
  518. return 1;
  519. }
  520. return 0;
  521. }
  522. int64_t qemu_file_get_rate_limit(QEMUFile *f)
  523. {
  524. return f->xfer_limit;
  525. }
  526. void qemu_file_set_rate_limit(QEMUFile *f, int64_t limit)
  527. {
  528. f->xfer_limit = limit;
  529. }
  530. void qemu_file_reset_rate_limit(QEMUFile *f)
  531. {
  532. f->bytes_xfer = 0;
  533. }
  534. void qemu_put_be16(QEMUFile *f, unsigned int v)
  535. {
  536. qemu_put_byte(f, v >> 8);
  537. qemu_put_byte(f, v);
  538. }
  539. void qemu_put_be32(QEMUFile *f, unsigned int v)
  540. {
  541. qemu_put_byte(f, v >> 24);
  542. qemu_put_byte(f, v >> 16);
  543. qemu_put_byte(f, v >> 8);
  544. qemu_put_byte(f, v);
  545. }
  546. void qemu_put_be64(QEMUFile *f, uint64_t v)
  547. {
  548. qemu_put_be32(f, v >> 32);
  549. qemu_put_be32(f, v);
  550. }
  551. unsigned int qemu_get_be16(QEMUFile *f)
  552. {
  553. unsigned int v;
  554. v = qemu_get_byte(f) << 8;
  555. v |= qemu_get_byte(f);
  556. return v;
  557. }
  558. unsigned int qemu_get_be32(QEMUFile *f)
  559. {
  560. unsigned int v;
  561. v = (unsigned int)qemu_get_byte(f) << 24;
  562. v |= qemu_get_byte(f) << 16;
  563. v |= qemu_get_byte(f) << 8;
  564. v |= qemu_get_byte(f);
  565. return v;
  566. }
  567. uint64_t qemu_get_be64(QEMUFile *f)
  568. {
  569. uint64_t v;
  570. v = (uint64_t)qemu_get_be32(f) << 32;
  571. v |= qemu_get_be32(f);
  572. return v;
  573. }
  574. /* Compress size bytes of data start at p with specific compression
  575. * level and store the compressed data to the buffer of f.
  576. *
  577. * When f is not writable, return -1 if f has no space to save the
  578. * compressed data.
  579. * When f is wirtable and it has no space to save the compressed data,
  580. * do fflush first, if f still has no space to save the compressed
  581. * data, return -1.
  582. */
  583. ssize_t qemu_put_compression_data(QEMUFile *f, const uint8_t *p, size_t size,
  584. int level)
  585. {
  586. ssize_t blen = IO_BUF_SIZE - f->buf_index - sizeof(int32_t);
  587. if (blen < compressBound(size)) {
  588. if (!qemu_file_is_writable(f)) {
  589. return -1;
  590. }
  591. qemu_fflush(f);
  592. blen = IO_BUF_SIZE - sizeof(int32_t);
  593. if (blen < compressBound(size)) {
  594. return -1;
  595. }
  596. }
  597. if (compress2(f->buf + f->buf_index + sizeof(int32_t), (uLongf *)&blen,
  598. (Bytef *)p, size, level) != Z_OK) {
  599. error_report("Compress Failed!");
  600. return 0;
  601. }
  602. qemu_put_be32(f, blen);
  603. if (f->ops->writev_buffer) {
  604. add_to_iovec(f, f->buf + f->buf_index, blen, false);
  605. }
  606. f->buf_index += blen;
  607. if (f->buf_index == IO_BUF_SIZE) {
  608. qemu_fflush(f);
  609. }
  610. return blen + sizeof(int32_t);
  611. }
  612. /* Put the data in the buffer of f_src to the buffer of f_des, and
  613. * then reset the buf_index of f_src to 0.
  614. */
  615. int qemu_put_qemu_file(QEMUFile *f_des, QEMUFile *f_src)
  616. {
  617. int len = 0;
  618. if (f_src->buf_index > 0) {
  619. len = f_src->buf_index;
  620. qemu_put_buffer(f_des, f_src->buf, f_src->buf_index);
  621. f_src->buf_index = 0;
  622. f_src->iovcnt = 0;
  623. }
  624. return len;
  625. }
  626. /*
  627. * Get a string whose length is determined by a single preceding byte
  628. * A preallocated 256 byte buffer must be passed in.
  629. * Returns: len on success and a 0 terminated string in the buffer
  630. * else 0
  631. * (Note a 0 length string will return 0 either way)
  632. */
  633. size_t qemu_get_counted_string(QEMUFile *f, char buf[256])
  634. {
  635. size_t len = qemu_get_byte(f);
  636. size_t res = qemu_get_buffer(f, (uint8_t *)buf, len);
  637. buf[res] = 0;
  638. return res == len ? res : 0;
  639. }
  640. /*
  641. * Set the blocking state of the QEMUFile.
  642. * Note: On some transports the OS only keeps a single blocking state for
  643. * both directions, and thus changing the blocking on the main
  644. * QEMUFile can also affect the return path.
  645. */
  646. void qemu_file_set_blocking(QEMUFile *f, bool block)
  647. {
  648. if (f->ops->set_blocking) {
  649. f->ops->set_blocking(f->opaque, block);
  650. }
  651. }