block: Rename BlockDriverAIOCB* to BlockAIOCB*
[qemu-kvm.git] / qemu-file.c
blobf938e36fe8586bfcdcb7a634be61f6963d3fb0c2
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
24 #include "qemu-common.h"
25 #include "qemu/iov.h"
26 #include "qemu/sockets.h"
27 #include "block/coroutine.h"
28 #include "migration/migration.h"
29 #include "migration/qemu-file.h"
30 #include "trace.h"
32 #define IO_BUF_SIZE 32768
33 #define MAX_IOV_SIZE MIN(IOV_MAX, 64)
35 struct QEMUFile {
36 const QEMUFileOps *ops;
37 void *opaque;
39 int64_t bytes_xfer;
40 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];
48 struct iovec iov[MAX_IOV_SIZE];
49 unsigned int iovcnt;
51 int last_error;
54 bool qemu_file_mode_is_not_valid(const char *mode)
56 if (mode == NULL ||
57 (mode[0] != 'r' && mode[0] != 'w') ||
58 mode[1] != 'b' || mode[2] != 0) {
59 fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
60 return true;
63 return false;
66 QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops)
68 QEMUFile *f;
70 f = g_malloc0(sizeof(QEMUFile));
72 f->opaque = opaque;
73 f->ops = ops;
74 return f;
78 * Get last error for stream f
80 * Return negative error value if there has been an error on previous
81 * operations, return 0 if no error happened.
84 int qemu_file_get_error(QEMUFile *f)
86 return f->last_error;
89 void qemu_file_set_error(QEMUFile *f, int ret)
91 if (f->last_error == 0) {
92 f->last_error = ret;
96 bool qemu_file_is_writable(QEMUFile *f)
98 return f->ops->writev_buffer || f->ops->put_buffer;
102 * Flushes QEMUFile buffer
104 * If there is writev_buffer QEMUFileOps it uses it otherwise uses
105 * put_buffer ops.
107 void qemu_fflush(QEMUFile *f)
109 ssize_t ret = 0;
111 if (!qemu_file_is_writable(f)) {
112 return;
115 if (f->ops->writev_buffer) {
116 if (f->iovcnt > 0) {
117 ret = f->ops->writev_buffer(f->opaque, f->iov, f->iovcnt, f->pos);
119 } else {
120 if (f->buf_index > 0) {
121 ret = f->ops->put_buffer(f->opaque, f->buf, f->pos, f->buf_index);
124 if (ret >= 0) {
125 f->pos += ret;
127 f->buf_index = 0;
128 f->iovcnt = 0;
129 if (ret < 0) {
130 qemu_file_set_error(f, ret);
134 void ram_control_before_iterate(QEMUFile *f, uint64_t flags)
136 int ret = 0;
138 if (f->ops->before_ram_iterate) {
139 ret = f->ops->before_ram_iterate(f, f->opaque, flags);
140 if (ret < 0) {
141 qemu_file_set_error(f, ret);
146 void ram_control_after_iterate(QEMUFile *f, uint64_t flags)
148 int ret = 0;
150 if (f->ops->after_ram_iterate) {
151 ret = f->ops->after_ram_iterate(f, f->opaque, flags);
152 if (ret < 0) {
153 qemu_file_set_error(f, ret);
158 void ram_control_load_hook(QEMUFile *f, uint64_t flags)
160 int ret = -EINVAL;
162 if (f->ops->hook_ram_load) {
163 ret = f->ops->hook_ram_load(f, f->opaque, flags);
164 if (ret < 0) {
165 qemu_file_set_error(f, ret);
167 } else {
168 qemu_file_set_error(f, ret);
172 size_t ram_control_save_page(QEMUFile *f, ram_addr_t block_offset,
173 ram_addr_t offset, size_t size, int *bytes_sent)
175 if (f->ops->save_page) {
176 int ret = f->ops->save_page(f, f->opaque, block_offset,
177 offset, size, bytes_sent);
179 if (ret != RAM_SAVE_CONTROL_DELAYED) {
180 if (bytes_sent && *bytes_sent > 0) {
181 qemu_update_position(f, *bytes_sent);
182 } else if (ret < 0) {
183 qemu_file_set_error(f, ret);
187 return ret;
190 return RAM_SAVE_CONTROL_NOT_SUPP;
194 * Attempt to fill the buffer from the underlying file
195 * Returns the number of bytes read, or negative value for an error.
197 * Note that it can return a partially full buffer even in a not error/not EOF
198 * case if the underlying file descriptor gives a short read, and that can
199 * happen even on a blocking fd.
201 static ssize_t qemu_fill_buffer(QEMUFile *f)
203 int len;
204 int pending;
206 assert(!qemu_file_is_writable(f));
208 pending = f->buf_size - f->buf_index;
209 if (pending > 0) {
210 memmove(f->buf, f->buf + f->buf_index, pending);
212 f->buf_index = 0;
213 f->buf_size = pending;
215 len = f->ops->get_buffer(f->opaque, f->buf + pending, f->pos,
216 IO_BUF_SIZE - pending);
217 if (len > 0) {
218 f->buf_size += len;
219 f->pos += len;
220 } else if (len == 0) {
221 qemu_file_set_error(f, -EIO);
222 } else if (len != -EAGAIN) {
223 qemu_file_set_error(f, len);
226 return len;
229 int qemu_get_fd(QEMUFile *f)
231 if (f->ops->get_fd) {
232 return f->ops->get_fd(f->opaque);
234 return -1;
237 void qemu_update_position(QEMUFile *f, size_t size)
239 f->pos += size;
242 /** Closes the file
244 * Returns negative error value if any error happened on previous operations or
245 * while closing the file. Returns 0 or positive number on success.
247 * The meaning of return value on success depends on the specific backend
248 * being used.
250 int qemu_fclose(QEMUFile *f)
252 int ret;
253 qemu_fflush(f);
254 ret = qemu_file_get_error(f);
256 if (f->ops->close) {
257 int ret2 = f->ops->close(f->opaque);
258 if (ret >= 0) {
259 ret = ret2;
262 /* If any error was spotted before closing, we should report it
263 * instead of the close() return value.
265 if (f->last_error) {
266 ret = f->last_error;
268 g_free(f);
269 trace_qemu_file_fclose();
270 return ret;
273 static void add_to_iovec(QEMUFile *f, const uint8_t *buf, int size)
275 /* check for adjacent buffer and coalesce them */
276 if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base +
277 f->iov[f->iovcnt - 1].iov_len) {
278 f->iov[f->iovcnt - 1].iov_len += size;
279 } else {
280 f->iov[f->iovcnt].iov_base = (uint8_t *)buf;
281 f->iov[f->iovcnt++].iov_len = size;
284 if (f->iovcnt >= MAX_IOV_SIZE) {
285 qemu_fflush(f);
289 void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, int size)
291 if (!f->ops->writev_buffer) {
292 qemu_put_buffer(f, buf, size);
293 return;
296 if (f->last_error) {
297 return;
300 f->bytes_xfer += size;
301 add_to_iovec(f, buf, size);
304 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
306 int l;
308 if (f->last_error) {
309 return;
312 while (size > 0) {
313 l = IO_BUF_SIZE - f->buf_index;
314 if (l > size) {
315 l = size;
317 memcpy(f->buf + f->buf_index, buf, l);
318 f->bytes_xfer += l;
319 if (f->ops->writev_buffer) {
320 add_to_iovec(f, f->buf + f->buf_index, l);
322 f->buf_index += l;
323 if (f->buf_index == IO_BUF_SIZE) {
324 qemu_fflush(f);
326 if (qemu_file_get_error(f)) {
327 break;
329 buf += l;
330 size -= l;
334 void qemu_put_byte(QEMUFile *f, int v)
336 if (f->last_error) {
337 return;
340 f->buf[f->buf_index] = v;
341 f->bytes_xfer++;
342 if (f->ops->writev_buffer) {
343 add_to_iovec(f, f->buf + f->buf_index, 1);
345 f->buf_index++;
346 if (f->buf_index == IO_BUF_SIZE) {
347 qemu_fflush(f);
351 void qemu_file_skip(QEMUFile *f, int size)
353 if (f->buf_index + size <= f->buf_size) {
354 f->buf_index += size;
359 * Read 'size' bytes from file (at 'offset') into buf without moving the
360 * pointer.
362 * It will return size bytes unless there was an error, in which case it will
363 * return as many as it managed to read (assuming blocking fd's which
364 * all current QEMUFile are)
366 int qemu_peek_buffer(QEMUFile *f, uint8_t *buf, int size, size_t offset)
368 int pending;
369 int index;
371 assert(!qemu_file_is_writable(f));
372 assert(offset < IO_BUF_SIZE);
373 assert(size <= IO_BUF_SIZE - offset);
375 /* The 1st byte to read from */
376 index = f->buf_index + offset;
377 /* The number of available bytes starting at index */
378 pending = f->buf_size - index;
381 * qemu_fill_buffer might return just a few bytes, even when there isn't
382 * an error, so loop collecting them until we get enough.
384 while (pending < size) {
385 int received = qemu_fill_buffer(f);
387 if (received <= 0) {
388 break;
391 index = f->buf_index + offset;
392 pending = f->buf_size - index;
395 if (pending <= 0) {
396 return 0;
398 if (size > pending) {
399 size = pending;
402 memcpy(buf, f->buf + index, size);
403 return size;
407 * Read 'size' bytes of data from the file into buf.
408 * 'size' can be larger than the internal buffer.
410 * It will return size bytes unless there was an error, in which case it will
411 * return as many as it managed to read (assuming blocking fd's which
412 * all current QEMUFile are)
414 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
416 int pending = size;
417 int done = 0;
419 while (pending > 0) {
420 int res;
422 res = qemu_peek_buffer(f, buf, MIN(pending, IO_BUF_SIZE), 0);
423 if (res == 0) {
424 return done;
426 qemu_file_skip(f, res);
427 buf += res;
428 pending -= res;
429 done += res;
431 return done;
435 * Peeks a single byte from the buffer; this isn't guaranteed to work if
436 * offset leaves a gap after the previous read/peeked data.
438 int qemu_peek_byte(QEMUFile *f, int offset)
440 int index = f->buf_index + offset;
442 assert(!qemu_file_is_writable(f));
443 assert(offset < IO_BUF_SIZE);
445 if (index >= f->buf_size) {
446 qemu_fill_buffer(f);
447 index = f->buf_index + offset;
448 if (index >= f->buf_size) {
449 return 0;
452 return f->buf[index];
455 int qemu_get_byte(QEMUFile *f)
457 int result;
459 result = qemu_peek_byte(f, 0);
460 qemu_file_skip(f, 1);
461 return result;
464 int64_t qemu_ftell(QEMUFile *f)
466 qemu_fflush(f);
467 return f->pos;
470 int qemu_file_rate_limit(QEMUFile *f)
472 if (qemu_file_get_error(f)) {
473 return 1;
475 if (f->xfer_limit > 0 && f->bytes_xfer > f->xfer_limit) {
476 return 1;
478 return 0;
481 int64_t qemu_file_get_rate_limit(QEMUFile *f)
483 return f->xfer_limit;
486 void qemu_file_set_rate_limit(QEMUFile *f, int64_t limit)
488 f->xfer_limit = limit;
491 void qemu_file_reset_rate_limit(QEMUFile *f)
493 f->bytes_xfer = 0;
496 void qemu_put_be16(QEMUFile *f, unsigned int v)
498 qemu_put_byte(f, v >> 8);
499 qemu_put_byte(f, v);
502 void qemu_put_be32(QEMUFile *f, unsigned int v)
504 qemu_put_byte(f, v >> 24);
505 qemu_put_byte(f, v >> 16);
506 qemu_put_byte(f, v >> 8);
507 qemu_put_byte(f, v);
510 void qemu_put_be64(QEMUFile *f, uint64_t v)
512 qemu_put_be32(f, v >> 32);
513 qemu_put_be32(f, v);
516 unsigned int qemu_get_be16(QEMUFile *f)
518 unsigned int v;
519 v = qemu_get_byte(f) << 8;
520 v |= qemu_get_byte(f);
521 return v;
524 unsigned int qemu_get_be32(QEMUFile *f)
526 unsigned int v;
527 v = qemu_get_byte(f) << 24;
528 v |= qemu_get_byte(f) << 16;
529 v |= qemu_get_byte(f) << 8;
530 v |= qemu_get_byte(f);
531 return v;
534 uint64_t qemu_get_be64(QEMUFile *f)
536 uint64_t v;
537 v = (uint64_t)qemu_get_be32(f) << 32;
538 v |= qemu_get_be32(f);
539 return v;
542 #define QSB_CHUNK_SIZE (1 << 10)
543 #define QSB_MAX_CHUNK_SIZE (16 * QSB_CHUNK_SIZE)
546 * Create a QEMUSizedBuffer
547 * This type of buffer uses scatter-gather lists internally and
548 * can grow to any size. Any data array in the scatter-gather list
549 * can hold different amount of bytes.
551 * @buffer: Optional buffer to copy into the QSB
552 * @len: size of initial buffer; if @buffer is given, buffer must
553 * hold at least len bytes
555 * Returns a pointer to a QEMUSizedBuffer or NULL on allocation failure
557 QEMUSizedBuffer *qsb_create(const uint8_t *buffer, size_t len)
559 QEMUSizedBuffer *qsb;
560 size_t alloc_len, num_chunks, i, to_copy;
561 size_t chunk_size = (len > QSB_MAX_CHUNK_SIZE)
562 ? QSB_MAX_CHUNK_SIZE
563 : QSB_CHUNK_SIZE;
565 num_chunks = DIV_ROUND_UP(len ? len : QSB_CHUNK_SIZE, chunk_size);
566 alloc_len = num_chunks * chunk_size;
568 qsb = g_try_new0(QEMUSizedBuffer, 1);
569 if (!qsb) {
570 return NULL;
573 qsb->iov = g_try_new0(struct iovec, num_chunks);
574 if (!qsb->iov) {
575 g_free(qsb);
576 return NULL;
579 qsb->n_iov = num_chunks;
581 for (i = 0; i < num_chunks; i++) {
582 qsb->iov[i].iov_base = g_try_malloc0(chunk_size);
583 if (!qsb->iov[i].iov_base) {
584 /* qsb_free is safe since g_free can cope with NULL */
585 qsb_free(qsb);
586 return NULL;
589 qsb->iov[i].iov_len = chunk_size;
590 if (buffer) {
591 to_copy = (len - qsb->used) > chunk_size
592 ? chunk_size : (len - qsb->used);
593 memcpy(qsb->iov[i].iov_base, &buffer[qsb->used], to_copy);
594 qsb->used += to_copy;
598 qsb->size = alloc_len;
600 return qsb;
604 * Free the QEMUSizedBuffer
606 * @qsb: The QEMUSizedBuffer to free
608 void qsb_free(QEMUSizedBuffer *qsb)
610 size_t i;
612 if (!qsb) {
613 return;
616 for (i = 0; i < qsb->n_iov; i++) {
617 g_free(qsb->iov[i].iov_base);
619 g_free(qsb->iov);
620 g_free(qsb);
624 * Get the number of used bytes in the QEMUSizedBuffer
626 * @qsb: A QEMUSizedBuffer
628 * Returns the number of bytes currently used in this buffer
630 size_t qsb_get_length(const QEMUSizedBuffer *qsb)
632 return qsb->used;
636 * Set the length of the buffer; the primary usage of this
637 * function is to truncate the number of used bytes in the buffer.
638 * The size will not be extended beyond the current number of
639 * allocated bytes in the QEMUSizedBuffer.
641 * @qsb: A QEMUSizedBuffer
642 * @new_len: The new length of bytes in the buffer
644 * Returns the number of bytes the buffer was truncated or extended
645 * to.
647 size_t qsb_set_length(QEMUSizedBuffer *qsb, size_t new_len)
649 if (new_len <= qsb->size) {
650 qsb->used = new_len;
651 } else {
652 qsb->used = qsb->size;
654 return qsb->used;
658 * Get the iovec that holds the data for a given position @pos.
660 * @qsb: A QEMUSizedBuffer
661 * @pos: The index of a byte in the buffer
662 * @d_off: Pointer to an offset that this function will indicate
663 * at what position within the returned iovec the byte
664 * is to be found
666 * Returns the index of the iovec that holds the byte at the given
667 * index @pos in the byte stream; a negative number if the iovec
668 * for the given position @pos does not exist.
670 static ssize_t qsb_get_iovec(const QEMUSizedBuffer *qsb,
671 off_t pos, off_t *d_off)
673 ssize_t i;
674 off_t curr = 0;
676 if (pos > qsb->used) {
677 return -1;
680 for (i = 0; i < qsb->n_iov; i++) {
681 if (curr + qsb->iov[i].iov_len > pos) {
682 *d_off = pos - curr;
683 return i;
685 curr += qsb->iov[i].iov_len;
687 return -1;
691 * Convert the QEMUSizedBuffer into a flat buffer.
693 * Note: If at all possible, try to avoid this function since it
694 * may unnecessarily copy memory around.
696 * @qsb: pointer to QEMUSizedBuffer
697 * @start: offset to start at
698 * @count: number of bytes to copy
699 * @buf: a pointer to a buffer to write into (at least @count bytes)
701 * Returns the number of bytes copied into the output buffer
703 ssize_t qsb_get_buffer(const QEMUSizedBuffer *qsb, off_t start,
704 size_t count, uint8_t *buffer)
706 const struct iovec *iov;
707 size_t to_copy, all_copy;
708 ssize_t index;
709 off_t s_off;
710 off_t d_off = 0;
711 char *s;
713 if (start > qsb->used) {
714 return 0;
717 all_copy = qsb->used - start;
718 if (all_copy > count) {
719 all_copy = count;
720 } else {
721 count = all_copy;
724 index = qsb_get_iovec(qsb, start, &s_off);
725 if (index < 0) {
726 return 0;
729 while (all_copy > 0) {
730 iov = &qsb->iov[index];
732 s = iov->iov_base;
734 to_copy = iov->iov_len - s_off;
735 if (to_copy > all_copy) {
736 to_copy = all_copy;
738 memcpy(&buffer[d_off], &s[s_off], to_copy);
740 d_off += to_copy;
741 all_copy -= to_copy;
743 s_off = 0;
744 index++;
747 return count;
751 * Grow the QEMUSizedBuffer to the given size and allocate
752 * memory for it.
754 * @qsb: A QEMUSizedBuffer
755 * @new_size: The new size of the buffer
757 * Return:
758 * a negative error code in case of memory allocation failure
759 * or
760 * the new size of the buffer. The returned size may be greater or equal
761 * to @new_size.
763 static ssize_t qsb_grow(QEMUSizedBuffer *qsb, size_t new_size)
765 size_t needed_chunks, i;
767 if (qsb->size < new_size) {
768 struct iovec *new_iov;
769 size_t size_diff = new_size - qsb->size;
770 size_t chunk_size = (size_diff > QSB_MAX_CHUNK_SIZE)
771 ? QSB_MAX_CHUNK_SIZE : QSB_CHUNK_SIZE;
773 needed_chunks = DIV_ROUND_UP(size_diff, chunk_size);
775 new_iov = g_try_new(struct iovec, qsb->n_iov + needed_chunks);
776 if (new_iov == NULL) {
777 return -ENOMEM;
780 /* Allocate new chunks as needed into new_iov */
781 for (i = qsb->n_iov; i < qsb->n_iov + needed_chunks; i++) {
782 new_iov[i].iov_base = g_try_malloc0(chunk_size);
783 new_iov[i].iov_len = chunk_size;
784 if (!new_iov[i].iov_base) {
785 size_t j;
787 /* Free previously allocated new chunks */
788 for (j = qsb->n_iov; j < i; j++) {
789 g_free(new_iov[j].iov_base);
791 g_free(new_iov);
793 return -ENOMEM;
798 * Now we can't get any allocation errors, copy over to new iov
799 * and switch.
801 for (i = 0; i < qsb->n_iov; i++) {
802 new_iov[i] = qsb->iov[i];
805 qsb->n_iov += needed_chunks;
806 g_free(qsb->iov);
807 qsb->iov = new_iov;
808 qsb->size += (needed_chunks * chunk_size);
811 return qsb->size;
815 * Write into the QEMUSizedBuffer at a given position and a given
816 * number of bytes. This function will automatically grow the
817 * QEMUSizedBuffer.
819 * @qsb: A QEMUSizedBuffer
820 * @source: A byte array to copy data from
821 * @pos: The position within the @qsb to write data to
822 * @size: The number of bytes to copy into the @qsb
824 * Returns @size or a negative error code in case of memory allocation failure,
825 * or with an invalid 'pos'
827 ssize_t qsb_write_at(QEMUSizedBuffer *qsb, const uint8_t *source,
828 off_t pos, size_t count)
830 ssize_t rc = qsb_grow(qsb, pos + count);
831 size_t to_copy;
832 size_t all_copy = count;
833 const struct iovec *iov;
834 ssize_t index;
835 char *dest;
836 off_t d_off, s_off = 0;
838 if (rc < 0) {
839 return rc;
842 if (pos + count > qsb->used) {
843 qsb->used = pos + count;
846 index = qsb_get_iovec(qsb, pos, &d_off);
847 if (index < 0) {
848 return -EINVAL;
851 while (all_copy > 0) {
852 iov = &qsb->iov[index];
854 dest = iov->iov_base;
856 to_copy = iov->iov_len - d_off;
857 if (to_copy > all_copy) {
858 to_copy = all_copy;
861 memcpy(&dest[d_off], &source[s_off], to_copy);
863 s_off += to_copy;
864 all_copy -= to_copy;
866 d_off = 0;
867 index++;
870 return count;
874 * Create a deep copy of the given QEMUSizedBuffer.
876 * @qsb: A QEMUSizedBuffer
878 * Returns a clone of @qsb or NULL on allocation failure
880 QEMUSizedBuffer *qsb_clone(const QEMUSizedBuffer *qsb)
882 QEMUSizedBuffer *out = qsb_create(NULL, qsb_get_length(qsb));
883 size_t i;
884 ssize_t res;
885 off_t pos = 0;
887 if (!out) {
888 return NULL;
891 for (i = 0; i < qsb->n_iov; i++) {
892 res = qsb_write_at(out, qsb->iov[i].iov_base,
893 pos, qsb->iov[i].iov_len);
894 if (res < 0) {
895 qsb_free(out);
896 return NULL;
898 pos += res;
901 return out;
904 typedef struct QEMUBuffer {
905 QEMUSizedBuffer *qsb;
906 QEMUFile *file;
907 } QEMUBuffer;
909 static int buf_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
911 QEMUBuffer *s = opaque;
912 ssize_t len = qsb_get_length(s->qsb) - pos;
914 if (len <= 0) {
915 return 0;
918 if (len > size) {
919 len = size;
921 return qsb_get_buffer(s->qsb, pos, len, buf);
924 static int buf_put_buffer(void *opaque, const uint8_t *buf,
925 int64_t pos, int size)
927 QEMUBuffer *s = opaque;
929 return qsb_write_at(s->qsb, buf, pos, size);
932 static int buf_close(void *opaque)
934 QEMUBuffer *s = opaque;
936 qsb_free(s->qsb);
938 g_free(s);
940 return 0;
943 const QEMUSizedBuffer *qemu_buf_get(QEMUFile *f)
945 QEMUBuffer *p;
947 qemu_fflush(f);
949 p = f->opaque;
951 return p->qsb;
954 static const QEMUFileOps buf_read_ops = {
955 .get_buffer = buf_get_buffer,
956 .close = buf_close,
959 static const QEMUFileOps buf_write_ops = {
960 .put_buffer = buf_put_buffer,
961 .close = buf_close,
964 QEMUFile *qemu_bufopen(const char *mode, QEMUSizedBuffer *input)
966 QEMUBuffer *s;
968 if (mode == NULL || (mode[0] != 'r' && mode[0] != 'w') ||
969 mode[1] != '\0') {
970 error_report("qemu_bufopen: Argument validity check failed");
971 return NULL;
974 s = g_malloc0(sizeof(QEMUBuffer));
975 if (mode[0] == 'r') {
976 s->qsb = input;
979 if (s->qsb == NULL) {
980 s->qsb = qsb_create(NULL, 0);
982 if (!s->qsb) {
983 g_free(s);
984 error_report("qemu_bufopen: qsb_create failed");
985 return NULL;
989 if (mode[0] == 'r') {
990 s->file = qemu_fopen_ops(s, &buf_read_ops);
991 } else {
992 s->file = qemu_fopen_ops(s, &buf_write_ops);
994 return s->file;