diff mbox

[3/9] two new file wrappers

Message ID 1363200988-17865-4-git-send-email-jschopp@linux.vnet.ibm.com
State New
Headers show

Commit Message

Joel Schopp March 13, 2013, 6:56 p.m. UTC
Add a 3 very short file wrapper functions to make code that follows more
readable.  Also export an existing function that is currently static.

Cc: Michael Tsirkin <mst@redhat.com>
Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
Signed-off-by: Joel Schopp <jschopp@linux.vnet.ibm.com>
---
 include/migration/qemu-file.h |    3 +++
 util/qemu-file.c              |   30 ++++++++++++++++++++++++++++++
 2 files changed, 33 insertions(+)

Comments

Eric Blake March 13, 2013, 9:04 p.m. UTC | #1
On 03/13/2013 12:56 PM, Joel Schopp wrote:
> Add a 3 very short file wrapper functions to make code that follows more

s/a 3/3/

> readable.  Also export an existing function that is currently static.
> 
> Cc: Michael Tsirkin <mst@redhat.com>
> Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
> Signed-off-by: Joel Schopp <jschopp@linux.vnet.ibm.com>
> ---
>  include/migration/qemu-file.h |    3 +++
>  util/qemu-file.c              |   30 ++++++++++++++++++++++++++++++
>  2 files changed, 33 insertions(+)
> 

> +
> +int qemu_peek_bytes(QEMUFile *f, uint8_t *buf, int size, size_t offset)

Should we be using off_t instead of size_t for offset; and size_t
instead of int for size (which implies ssize_t for return value)?
Stefan Berger March 14, 2013, 10:49 a.m. UTC | #2
On 03/13/2013 05:04 PM, Eric Blake wrote:
> On 03/13/2013 12:56 PM, Joel Schopp wrote:
>> Add a 3 very short file wrapper functions to make code that follows more
> s/a 3/3/
>
>> readable.  Also export an existing function that is currently static.
>>
>> Cc: Michael Tsirkin <mst@redhat.com>
>> Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
>> Signed-off-by: Joel Schopp <jschopp@linux.vnet.ibm.com>
>> ---
>>   include/migration/qemu-file.h |    3 +++
>>   util/qemu-file.c              |   30 ++++++++++++++++++++++++++++++
>>   2 files changed, 33 insertions(+)
>>
>> +
>> +int qemu_peek_bytes(QEMUFile *f, uint8_t *buf, int size, size_t offset)
> Should we be using off_t instead of size_t for offset; and size_t
> instead of int for size (which implies ssize_t for return value)?
>

Good question. Existing functions have signatures like this:

void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
static void qemu_file_skip(QEMUFile *f, int size)
static int qemu_peek_buffer(QEMUFile *f, uint8_t *buf, int size, size_t 
offset)
static int qemu_peek_byte(QEMUFile *f, int offset)

So it's better if we line up behind them...

    Stefan
diff mbox

Patch

diff --git a/include/migration/qemu-file.h b/include/migration/qemu-file.h
index b76a901..07d8362 100644
--- a/include/migration/qemu-file.h
+++ b/include/migration/qemu-file.h
@@ -68,6 +68,9 @@  int qemu_fclose(QEMUFile *f);
 int64_t qemu_ftell(QEMUFile *f);
 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size);
 void qemu_put_byte(QEMUFile *f, int v);
+int qemu_read_bytes(QEMUFile *f, uint8_t *buf, int size);
+int qemu_peek_bytes(QEMUFile *f, uint8_t *buf, int size, size_t offset);
+int qemu_write_bytes(QEMUFile *f, const uint8_t *buf, int size);
 
 static inline void qemu_put_ubyte(QEMUFile *f, unsigned int v)
 {
diff --git a/util/qemu-file.c b/util/qemu-file.c
index 44b52b4..e698713 100644
--- a/util/qemu-file.c
+++ b/util/qemu-file.c
@@ -680,3 +680,33 @@  uint64_t qemu_get_be64(QEMUFile *f)
     return v;
 }
 
+int qemu_read_bytes(QEMUFile *f, uint8_t *buf, int size)
+{
+    if (qemu_file_get_error(f)) {
+        return -1;
+    }
+    return qemu_get_buffer(f, buf, size);
+}
+
+int qemu_peek_bytes(QEMUFile *f, uint8_t *buf, int size, size_t offset)
+{
+    if (qemu_file_get_error(f)) {
+        return -1;
+    }
+    return qemu_peek_buffer(f, buf, size, offset);
+}
+
+int qemu_write_bytes(QEMUFile *f, const uint8_t *buf, int size)
+{
+    if (qemu_file_get_error(f)) {
+        return -1;
+    }
+
+    qemu_put_buffer(f, buf, size);
+
+    if (qemu_file_get_error(f)) {
+        return -1;
+    }
+
+    return size;
+}