blob: 0bbd2574a802dbe673cafdbf2931b98f1cfc5f44 [file] [log] [blame]
Eduardo Habkost9a4ac512014-10-01 14:47:33 -03001/*
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 */
Liang Li44f0ead2015-03-23 16:32:19 +080024#include <zlib.h>
Eduardo Habkost093c4552013-11-28 12:01:16 -020025#include "qemu-common.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010026#include "qemu/error-report.h"
Eduardo Habkost093c4552013-11-28 12:01:16 -020027#include "qemu/iov.h"
28#include "qemu/sockets.h"
Daniel P. Berrange10817bf2015-09-01 14:48:02 +010029#include "qemu/coroutine.h"
Eduardo Habkost093c4552013-11-28 12:01:16 -020030#include "migration/migration.h"
31#include "migration/qemu-file.h"
Dr. David Alan Gilbert4f9d0902014-12-12 11:13:40 +000032#include "migration/qemu-file-internal.h"
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +110033#include "trace.h"
Eduardo Habkost093c4552013-11-28 12:01:16 -020034
Dr. David Alan Gilberte1a8c9b2015-01-08 11:11:30 +000035/*
36 * Stop a file from being read/written - not all backing files can do this
37 * typically only sockets can.
38 */
39int qemu_file_shutdown(QEMUFile *f)
40{
41 if (!f->ops->shut_down) {
42 return -ENOSYS;
43 }
44 return f->ops->shut_down(f->opaque, true, true);
45}
46
Dr. David Alan Gilbertadc468e2015-11-05 18:10:43 +000047/*
48 * Result: QEMUFile* for a 'return path' for comms in the opposite direction
49 * NULL if not available
50 */
51QEMUFile *qemu_file_get_return_path(QEMUFile *f)
52{
53 if (!f->ops->get_return_path) {
54 return NULL;
55 }
56 return f->ops->get_return_path(f->opaque);
57}
58
Eduardo Habkost093c4552013-11-28 12:01:16 -020059bool qemu_file_mode_is_not_valid(const char *mode)
60{
61 if (mode == NULL ||
62 (mode[0] != 'r' && mode[0] != 'w') ||
63 mode[1] != 'b' || mode[2] != 0) {
64 fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
65 return true;
66 }
67
68 return false;
69}
70
Eduardo Habkost093c4552013-11-28 12:01:16 -020071QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops)
72{
73 QEMUFile *f;
74
Markus Armbruster97f3ad32015-09-14 13:51:31 +020075 f = g_new0(QEMUFile, 1);
Eduardo Habkost093c4552013-11-28 12:01:16 -020076
77 f->opaque = opaque;
78 f->ops = ops;
79 return f;
80}
81
82/*
83 * Get last error for stream f
84 *
85 * Return negative error value if there has been an error on previous
86 * operations, return 0 if no error happened.
87 *
88 */
89int qemu_file_get_error(QEMUFile *f)
90{
91 return f->last_error;
92}
93
94void qemu_file_set_error(QEMUFile *f, int ret)
95{
96 if (f->last_error == 0) {
97 f->last_error = ret;
98 }
99}
100
Eduardo Habkoste68dd362014-10-01 17:34:34 -0300101bool qemu_file_is_writable(QEMUFile *f)
Eduardo Habkost093c4552013-11-28 12:01:16 -0200102{
103 return f->ops->writev_buffer || f->ops->put_buffer;
104}
105
106/**
107 * Flushes QEMUFile buffer
108 *
109 * If there is writev_buffer QEMUFileOps it uses it otherwise uses
110 * put_buffer ops.
111 */
112void qemu_fflush(QEMUFile *f)
113{
114 ssize_t ret = 0;
115
116 if (!qemu_file_is_writable(f)) {
117 return;
118 }
119
120 if (f->ops->writev_buffer) {
121 if (f->iovcnt > 0) {
122 ret = f->ops->writev_buffer(f->opaque, f->iov, f->iovcnt, f->pos);
123 }
124 } else {
125 if (f->buf_index > 0) {
126 ret = f->ops->put_buffer(f->opaque, f->buf, f->pos, f->buf_index);
127 }
128 }
129 if (ret >= 0) {
130 f->pos += ret;
131 }
132 f->buf_index = 0;
133 f->iovcnt = 0;
134 if (ret < 0) {
135 qemu_file_set_error(f, ret);
136 }
137}
138
139void ram_control_before_iterate(QEMUFile *f, uint64_t flags)
140{
141 int ret = 0;
142
143 if (f->ops->before_ram_iterate) {
Dr. David Alan Gilbert632e3a52015-06-11 18:17:23 +0100144 ret = f->ops->before_ram_iterate(f, f->opaque, flags, NULL);
Eduardo Habkost093c4552013-11-28 12:01:16 -0200145 if (ret < 0) {
146 qemu_file_set_error(f, ret);
147 }
148 }
149}
150
151void ram_control_after_iterate(QEMUFile *f, uint64_t flags)
152{
153 int ret = 0;
154
155 if (f->ops->after_ram_iterate) {
Dr. David Alan Gilbert632e3a52015-06-11 18:17:23 +0100156 ret = f->ops->after_ram_iterate(f, f->opaque, flags, NULL);
Eduardo Habkost093c4552013-11-28 12:01:16 -0200157 if (ret < 0) {
158 qemu_file_set_error(f, ret);
159 }
160 }
161}
162
Dr. David Alan Gilbert632e3a52015-06-11 18:17:23 +0100163void ram_control_load_hook(QEMUFile *f, uint64_t flags, void *data)
Eduardo Habkost093c4552013-11-28 12:01:16 -0200164{
165 int ret = -EINVAL;
166
167 if (f->ops->hook_ram_load) {
Dr. David Alan Gilbert632e3a52015-06-11 18:17:23 +0100168 ret = f->ops->hook_ram_load(f, f->opaque, flags, data);
Eduardo Habkost093c4552013-11-28 12:01:16 -0200169 if (ret < 0) {
170 qemu_file_set_error(f, ret);
171 }
172 } else {
Dr. David Alan Gilbert632e3a52015-06-11 18:17:23 +0100173 /*
174 * Hook is a hook specifically requested by the source sending a flag
175 * that expects there to be a hook on the destination.
176 */
177 if (flags == RAM_CONTROL_HOOK) {
178 qemu_file_set_error(f, ret);
179 }
Eduardo Habkost093c4552013-11-28 12:01:16 -0200180 }
181}
182
183size_t ram_control_save_page(QEMUFile *f, ram_addr_t block_offset,
Juan Quintela6e1dea42015-02-12 19:02:42 +0100184 ram_addr_t offset, size_t size,
185 uint64_t *bytes_sent)
Eduardo Habkost093c4552013-11-28 12:01:16 -0200186{
187 if (f->ops->save_page) {
188 int ret = f->ops->save_page(f, f->opaque, block_offset,
189 offset, size, bytes_sent);
190
191 if (ret != RAM_SAVE_CONTROL_DELAYED) {
192 if (bytes_sent && *bytes_sent > 0) {
193 qemu_update_position(f, *bytes_sent);
194 } else if (ret < 0) {
195 qemu_file_set_error(f, ret);
196 }
197 }
198
199 return ret;
200 }
201
202 return RAM_SAVE_CONTROL_NOT_SUPP;
203}
204
Dr. David Alan Gilbert548f52e2014-04-08 15:29:37 +0100205/*
206 * Attempt to fill the buffer from the underlying file
207 * Returns the number of bytes read, or negative value for an error.
208 *
209 * Note that it can return a partially full buffer even in a not error/not EOF
210 * case if the underlying file descriptor gives a short read, and that can
211 * happen even on a blocking fd.
212 */
213static ssize_t qemu_fill_buffer(QEMUFile *f)
Eduardo Habkost093c4552013-11-28 12:01:16 -0200214{
215 int len;
216 int pending;
217
218 assert(!qemu_file_is_writable(f));
219
220 pending = f->buf_size - f->buf_index;
221 if (pending > 0) {
222 memmove(f->buf, f->buf + f->buf_index, pending);
223 }
224 f->buf_index = 0;
225 f->buf_size = pending;
226
227 len = f->ops->get_buffer(f->opaque, f->buf + pending, f->pos,
228 IO_BUF_SIZE - pending);
229 if (len > 0) {
230 f->buf_size += len;
231 f->pos += len;
232 } else if (len == 0) {
233 qemu_file_set_error(f, -EIO);
234 } else if (len != -EAGAIN) {
235 qemu_file_set_error(f, len);
236 }
Dr. David Alan Gilbert548f52e2014-04-08 15:29:37 +0100237
238 return len;
Eduardo Habkost093c4552013-11-28 12:01:16 -0200239}
240
241int qemu_get_fd(QEMUFile *f)
242{
243 if (f->ops->get_fd) {
244 return f->ops->get_fd(f->opaque);
245 }
246 return -1;
247}
248
249void qemu_update_position(QEMUFile *f, size_t size)
250{
251 f->pos += size;
252}
253
254/** Closes the file
255 *
256 * Returns negative error value if any error happened on previous operations or
257 * while closing the file. Returns 0 or positive number on success.
258 *
259 * The meaning of return value on success depends on the specific backend
260 * being used.
261 */
262int qemu_fclose(QEMUFile *f)
263{
264 int ret;
265 qemu_fflush(f);
266 ret = qemu_file_get_error(f);
267
268 if (f->ops->close) {
269 int ret2 = f->ops->close(f->opaque);
270 if (ret >= 0) {
271 ret = ret2;
272 }
273 }
274 /* If any error was spotted before closing, we should report it
275 * instead of the close() return value.
276 */
277 if (f->last_error) {
278 ret = f->last_error;
279 }
280 g_free(f);
Alexey Kardashevskiy9013dca2014-03-11 10:42:29 +1100281 trace_qemu_file_fclose();
Eduardo Habkost093c4552013-11-28 12:01:16 -0200282 return ret;
283}
284
Dr. David Alan Gilbert56f38352015-08-13 11:51:34 +0100285static void add_to_iovec(QEMUFile *f, const uint8_t *buf, size_t size)
Eduardo Habkost093c4552013-11-28 12:01:16 -0200286{
287 /* check for adjacent buffer and coalesce them */
288 if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base +
289 f->iov[f->iovcnt - 1].iov_len) {
290 f->iov[f->iovcnt - 1].iov_len += size;
291 } else {
292 f->iov[f->iovcnt].iov_base = (uint8_t *)buf;
293 f->iov[f->iovcnt++].iov_len = size;
294 }
295
296 if (f->iovcnt >= MAX_IOV_SIZE) {
297 qemu_fflush(f);
298 }
299}
300
Dr. David Alan Gilbert56f38352015-08-13 11:51:34 +0100301void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, size_t size)
Eduardo Habkost093c4552013-11-28 12:01:16 -0200302{
303 if (!f->ops->writev_buffer) {
304 qemu_put_buffer(f, buf, size);
305 return;
306 }
307
308 if (f->last_error) {
309 return;
310 }
311
312 f->bytes_xfer += size;
313 add_to_iovec(f, buf, size);
314}
315
Dr. David Alan Gilbert56f38352015-08-13 11:51:34 +0100316void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, size_t size)
Eduardo Habkost093c4552013-11-28 12:01:16 -0200317{
Dr. David Alan Gilbert56f38352015-08-13 11:51:34 +0100318 size_t l;
Eduardo Habkost093c4552013-11-28 12:01:16 -0200319
320 if (f->last_error) {
321 return;
322 }
323
324 while (size > 0) {
325 l = IO_BUF_SIZE - f->buf_index;
326 if (l > size) {
327 l = size;
328 }
329 memcpy(f->buf + f->buf_index, buf, l);
330 f->bytes_xfer += l;
331 if (f->ops->writev_buffer) {
332 add_to_iovec(f, f->buf + f->buf_index, l);
333 }
334 f->buf_index += l;
335 if (f->buf_index == IO_BUF_SIZE) {
336 qemu_fflush(f);
337 }
338 if (qemu_file_get_error(f)) {
339 break;
340 }
341 buf += l;
342 size -= l;
343 }
344}
345
346void qemu_put_byte(QEMUFile *f, int v)
347{
348 if (f->last_error) {
349 return;
350 }
351
352 f->buf[f->buf_index] = v;
353 f->bytes_xfer++;
354 if (f->ops->writev_buffer) {
355 add_to_iovec(f, f->buf + f->buf_index, 1);
356 }
357 f->buf_index++;
358 if (f->buf_index == IO_BUF_SIZE) {
359 qemu_fflush(f);
360 }
361}
362
363void qemu_file_skip(QEMUFile *f, int size)
364{
365 if (f->buf_index + size <= f->buf_size) {
366 f->buf_index += size;
367 }
368}
369
Dr. David Alan Gilbert548f52e2014-04-08 15:29:37 +0100370/*
Dr. David Alan Gilbert7c1e52b2015-05-21 13:24:15 +0100371 * Read 'size' bytes from file (at 'offset') without moving the
372 * pointer and set 'buf' to point to that data.
Dr. David Alan Gilbert548f52e2014-04-08 15:29:37 +0100373 *
374 * It will return size bytes unless there was an error, in which case it will
375 * return as many as it managed to read (assuming blocking fd's which
376 * all current QEMUFile are)
377 */
Dr. David Alan Gilbert56f38352015-08-13 11:51:34 +0100378size_t qemu_peek_buffer(QEMUFile *f, uint8_t **buf, size_t size, size_t offset)
Eduardo Habkost093c4552013-11-28 12:01:16 -0200379{
Dr. David Alan Gilbert56f38352015-08-13 11:51:34 +0100380 ssize_t pending;
381 size_t index;
Eduardo Habkost093c4552013-11-28 12:01:16 -0200382
383 assert(!qemu_file_is_writable(f));
Dr. David Alan Gilbert548f52e2014-04-08 15:29:37 +0100384 assert(offset < IO_BUF_SIZE);
385 assert(size <= IO_BUF_SIZE - offset);
Eduardo Habkost093c4552013-11-28 12:01:16 -0200386
Dr. David Alan Gilbert548f52e2014-04-08 15:29:37 +0100387 /* The 1st byte to read from */
Eduardo Habkost093c4552013-11-28 12:01:16 -0200388 index = f->buf_index + offset;
Dr. David Alan Gilbert548f52e2014-04-08 15:29:37 +0100389 /* The number of available bytes starting at index */
Eduardo Habkost093c4552013-11-28 12:01:16 -0200390 pending = f->buf_size - index;
Dr. David Alan Gilbert548f52e2014-04-08 15:29:37 +0100391
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
399 if (received <= 0) {
400 break;
401 }
402
Eduardo Habkost093c4552013-11-28 12:01:16 -0200403 index = f->buf_index + offset;
404 pending = f->buf_size - index;
405 }
406
407 if (pending <= 0) {
408 return 0;
409 }
410 if (size > pending) {
411 size = pending;
412 }
413
Dr. David Alan Gilbert7c1e52b2015-05-21 13:24:15 +0100414 *buf = f->buf + index;
Eduardo Habkost093c4552013-11-28 12:01:16 -0200415 return size;
416}
417
Dr. David Alan Gilbert548f52e2014-04-08 15:29:37 +0100418/*
419 * Read 'size' bytes of data from the file into buf.
420 * 'size' can be larger than the internal buffer.
421 *
422 * It will return size bytes unless there was an error, in which case it will
423 * return as many as it managed to read (assuming blocking fd's which
424 * all current QEMUFile are)
425 */
Dr. David Alan Gilbert56f38352015-08-13 11:51:34 +0100426size_t qemu_get_buffer(QEMUFile *f, uint8_t *buf, size_t size)
Eduardo Habkost093c4552013-11-28 12:01:16 -0200427{
Dr. David Alan Gilbert56f38352015-08-13 11:51:34 +0100428 size_t pending = size;
429 size_t done = 0;
Eduardo Habkost093c4552013-11-28 12:01:16 -0200430
431 while (pending > 0) {
Dr. David Alan Gilbert56f38352015-08-13 11:51:34 +0100432 size_t res;
Dr. David Alan Gilbert7c1e52b2015-05-21 13:24:15 +0100433 uint8_t *src;
Eduardo Habkost093c4552013-11-28 12:01:16 -0200434
Dr. David Alan Gilbert7c1e52b2015-05-21 13:24:15 +0100435 res = qemu_peek_buffer(f, &src, MIN(pending, IO_BUF_SIZE), 0);
Eduardo Habkost093c4552013-11-28 12:01:16 -0200436 if (res == 0) {
437 return done;
438 }
Dr. David Alan Gilbert7c1e52b2015-05-21 13:24:15 +0100439 memcpy(buf, src, res);
Eduardo Habkost093c4552013-11-28 12:01:16 -0200440 qemu_file_skip(f, res);
441 buf += res;
442 pending -= res;
443 done += res;
444 }
445 return done;
446}
447
Dr. David Alan Gilbert548f52e2014-04-08 15:29:37 +0100448/*
Dr. David Alan Gilbert9504fb52015-11-05 18:10:35 +0000449 * Read 'size' bytes of data from the file.
450 * 'size' can be larger than the internal buffer.
451 *
452 * The data:
453 * may be held on an internal buffer (in which case *buf is updated
454 * to point to it) that is valid until the next qemu_file operation.
455 * OR
456 * will be copied to the *buf that was passed in.
457 *
458 * The code tries to avoid the copy if possible.
459 *
460 * It will return size bytes unless there was an error, in which case it will
461 * return as many as it managed to read (assuming blocking fd's which
462 * all current QEMUFile are)
463 *
464 * Note: Since **buf may get changed, the caller should take care to
465 * keep a pointer to the original buffer if it needs to deallocate it.
466 */
467size_t qemu_get_buffer_in_place(QEMUFile *f, uint8_t **buf, size_t size)
468{
469 if (size < IO_BUF_SIZE) {
470 size_t res;
471 uint8_t *src;
472
473 res = qemu_peek_buffer(f, &src, size, 0);
474
475 if (res == size) {
476 qemu_file_skip(f, res);
477 *buf = src;
478 return res;
479 }
480 }
481
482 return qemu_get_buffer(f, *buf, size);
483}
484
485/*
Dr. David Alan Gilbert548f52e2014-04-08 15:29:37 +0100486 * Peeks a single byte from the buffer; this isn't guaranteed to work if
487 * offset leaves a gap after the previous read/peeked data.
488 */
Eduardo Habkost093c4552013-11-28 12:01:16 -0200489int qemu_peek_byte(QEMUFile *f, int offset)
490{
491 int index = f->buf_index + offset;
492
493 assert(!qemu_file_is_writable(f));
Dr. David Alan Gilbert548f52e2014-04-08 15:29:37 +0100494 assert(offset < IO_BUF_SIZE);
Eduardo Habkost093c4552013-11-28 12:01:16 -0200495
496 if (index >= f->buf_size) {
497 qemu_fill_buffer(f);
498 index = f->buf_index + offset;
499 if (index >= f->buf_size) {
500 return 0;
501 }
502 }
503 return f->buf[index];
504}
505
506int qemu_get_byte(QEMUFile *f)
507{
508 int result;
509
510 result = qemu_peek_byte(f, 0);
511 qemu_file_skip(f, 1);
512 return result;
513}
514
Alexander Graf97221402015-01-22 15:01:38 +0100515int64_t qemu_ftell_fast(QEMUFile *f)
516{
517 int64_t ret = f->pos;
518 int i;
519
520 if (f->ops->writev_buffer) {
521 for (i = 0; i < f->iovcnt; i++) {
522 ret += f->iov[i].iov_len;
523 }
524 } else {
525 ret += f->buf_index;
526 }
527
528 return ret;
529}
530
Eduardo Habkost093c4552013-11-28 12:01:16 -0200531int64_t qemu_ftell(QEMUFile *f)
532{
533 qemu_fflush(f);
534 return f->pos;
535}
536
537int qemu_file_rate_limit(QEMUFile *f)
538{
539 if (qemu_file_get_error(f)) {
540 return 1;
541 }
542 if (f->xfer_limit > 0 && f->bytes_xfer > f->xfer_limit) {
543 return 1;
544 }
545 return 0;
546}
547
548int64_t qemu_file_get_rate_limit(QEMUFile *f)
549{
550 return f->xfer_limit;
551}
552
553void qemu_file_set_rate_limit(QEMUFile *f, int64_t limit)
554{
555 f->xfer_limit = limit;
556}
557
558void qemu_file_reset_rate_limit(QEMUFile *f)
559{
560 f->bytes_xfer = 0;
561}
562
563void qemu_put_be16(QEMUFile *f, unsigned int v)
564{
565 qemu_put_byte(f, v >> 8);
566 qemu_put_byte(f, v);
567}
568
569void qemu_put_be32(QEMUFile *f, unsigned int v)
570{
571 qemu_put_byte(f, v >> 24);
572 qemu_put_byte(f, v >> 16);
573 qemu_put_byte(f, v >> 8);
574 qemu_put_byte(f, v);
575}
576
577void qemu_put_be64(QEMUFile *f, uint64_t v)
578{
579 qemu_put_be32(f, v >> 32);
580 qemu_put_be32(f, v);
581}
582
583unsigned int qemu_get_be16(QEMUFile *f)
584{
585 unsigned int v;
586 v = qemu_get_byte(f) << 8;
587 v |= qemu_get_byte(f);
588 return v;
589}
590
591unsigned int qemu_get_be32(QEMUFile *f)
592{
593 unsigned int v;
Peter Maydell90d6a672014-12-23 22:26:55 +0000594 v = (unsigned int)qemu_get_byte(f) << 24;
Eduardo Habkost093c4552013-11-28 12:01:16 -0200595 v |= qemu_get_byte(f) << 16;
596 v |= qemu_get_byte(f) << 8;
597 v |= qemu_get_byte(f);
598 return v;
599}
600
601uint64_t qemu_get_be64(QEMUFile *f)
602{
603 uint64_t v;
604 v = (uint64_t)qemu_get_be32(f) << 32;
605 v |= qemu_get_be32(f);
606 return v;
607}
Liang Li44f0ead2015-03-23 16:32:19 +0800608
609/* compress size bytes of data start at p with specific compression
610 * level and store the compressed data to the buffer of f.
611 */
612
613ssize_t qemu_put_compression_data(QEMUFile *f, const uint8_t *p, size_t size,
614 int level)
615{
616 ssize_t blen = IO_BUF_SIZE - f->buf_index - sizeof(int32_t);
617
618 if (blen < compressBound(size)) {
619 return 0;
620 }
621 if (compress2(f->buf + f->buf_index + sizeof(int32_t), (uLongf *)&blen,
622 (Bytef *)p, size, level) != Z_OK) {
623 error_report("Compress Failed!");
624 return 0;
625 }
626 qemu_put_be32(f, blen);
627 f->buf_index += blen;
628 return blen + sizeof(int32_t);
629}
630
631/* Put the data in the buffer of f_src to the buffer of f_des, and
632 * then reset the buf_index of f_src to 0.
633 */
634
635int qemu_put_qemu_file(QEMUFile *f_des, QEMUFile *f_src)
636{
637 int len = 0;
638
639 if (f_src->buf_index > 0) {
640 len = f_src->buf_index;
641 qemu_put_buffer(f_des, f_src->buf, f_src->buf_index);
642 f_src->buf_index = 0;
643 }
644 return len;
645}
Dr. David Alan Gilbertb3af1bc2015-05-21 13:24:11 +0100646
647/*
648 * Get a string whose length is determined by a single preceding byte
649 * A preallocated 256 byte buffer must be passed in.
650 * Returns: len on success and a 0 terminated string in the buffer
651 * else 0
652 * (Note a 0 length string will return 0 either way)
653 */
654size_t qemu_get_counted_string(QEMUFile *f, char buf[256])
655{
656 size_t len = qemu_get_byte(f);
657 size_t res = qemu_get_buffer(f, (uint8_t *)buf, len);
658
659 buf[res] = 0;
660
661 return res == len ? res : 0;
662}
Dr. David Alan Gilberta800cd52015-11-05 18:10:36 +0000663
664/*
665 * Set the blocking state of the QEMUFile.
666 * Note: On some transports the OS only keeps a single blocking state for
667 * both directions, and thus changing the blocking on the main
668 * QEMUFile can also affect the return path.
669 */
670void qemu_file_set_blocking(QEMUFile *f, bool block)
671{
672 if (block) {
673 qemu_set_block(qemu_get_fd(f));
674 } else {
675 qemu_set_nonblock(qemu_get_fd(f));
676 }
677}