From 36084d7e31997d0f4aec67e05c2ef44de89bc4de Mon Sep 17 00:00:00 2001 From: Gerd Hoffmann Date: Tue, 20 May 2014 12:54:08 +0200 Subject: usb-mtp: use bool to track MTPObject init status Stop setting nchildren to -1. Use separate bool variable to track whenever we've already fetched the child objects instead. Also make nchildren unsigned. Cc: Dr. David Alan Gilbert Signed-off-by: Gerd Hoffmann --- hw/usb/dev-mtp.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'hw/usb/dev-mtp.c') diff --git a/hw/usb/dev-mtp.c b/hw/usb/dev-mtp.c index 943f930404..c2750e4bd2 100644 --- a/hw/usb/dev-mtp.c +++ b/hw/usb/dev-mtp.c @@ -109,7 +109,8 @@ struct MTPObject { struct stat stat; MTPObject *parent; MTPObject **children; - int32_t nchildren; + uint32_t nchildren; + bool have_children; QTAILQ_ENTRY(MTPObject) next; }; @@ -273,7 +274,6 @@ static MTPObject *usb_mtp_object_alloc(MTPState *s, uint32_t handle, o->handle = handle; o->parent = parent; o->name = g_strdup(name); - o->nchildren = -1; if (parent == NULL) { o->path = g_strdup(name); } else { @@ -340,7 +340,11 @@ static void usb_mtp_object_readdir(MTPState *s, MTPObject *o) struct dirent *entry; DIR *dir; - o->nchildren = 0; + if (o->have_children) { + return; + } + o->have_children = true; + dir = opendir(o->path); if (!dir) { return; @@ -789,9 +793,7 @@ static void usb_mtp_command(MTPState *s, MTPControl *c) c->trans, 0, 0, 0); return; } - if (o->nchildren == -1) { - usb_mtp_object_readdir(s, o); - } + usb_mtp_object_readdir(s, o); if (c->code == CMD_GET_NUM_OBJECTS) { trace_usb_mtp_op_get_num_objects(s->dev.addr, o->handle, o->path); nres = 1; -- cgit v1.2.3 From 68206d734233f6d2500762b93d81674a6f0aa033 Mon Sep 17 00:00:00 2001 From: Gerd Hoffmann Date: Tue, 20 May 2014 13:02:26 +0200 Subject: usb-mtp: handle lseek failure Cc: Dr. David Alan Gilbert Signed-off-by: Gerd Hoffmann --- hw/usb/dev-mtp.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'hw/usb/dev-mtp.c') diff --git a/hw/usb/dev-mtp.c b/hw/usb/dev-mtp.c index c2750e4bd2..a95298bd34 100644 --- a/hw/usb/dev-mtp.c +++ b/hw/usb/dev-mtp.c @@ -702,7 +702,10 @@ static MTPData *usb_mtp_get_partial_object(MTPState *s, MTPControl *c, if (offset > o->stat.st_size) { offset = o->stat.st_size; } - lseek(d->fd, offset, SEEK_SET); + if (lseek(d->fd, offset, SEEK_SET) < 0) { + usb_mtp_data_free(d); + return NULL; + } d->length = c->argv[2]; if (d->length > o->stat.st_size - offset) { -- cgit v1.2.3 From 22513a9b4448a7c70ef49b88b58487d031c2e23e Mon Sep 17 00:00:00 2001 From: Gerd Hoffmann Date: Tue, 20 May 2014 13:06:55 +0200 Subject: usb-mtp: handle usb_mtp_get_object failure Cc: Dr. David Alan Gilbert Signed-off-by: Gerd Hoffmann --- hw/usb/dev-mtp.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'hw/usb/dev-mtp.c') diff --git a/hw/usb/dev-mtp.c b/hw/usb/dev-mtp.c index a95298bd34..380b465621 100644 --- a/hw/usb/dev-mtp.c +++ b/hw/usb/dev-mtp.c @@ -46,6 +46,7 @@ enum mtp_code { /* response codes */ RES_OK = 0x2001, + RES_GENERAL_ERROR = 0x2002, RES_SESSION_NOT_OPEN = 0x2003, RES_INVALID_TRANSACTION_ID = 0x2004, RES_OPERATION_NOT_SUPPORTED = 0x2005, @@ -828,7 +829,9 @@ static void usb_mtp_command(MTPState *s, MTPControl *c) } data_in = usb_mtp_get_object(s, c, o); if (NULL == data_in) { - fprintf(stderr, "%s: TODO: handle error\n", __func__); + usb_mtp_queue_result(s, RES_GENERAL_ERROR, + c->trans, 0, 0, 0); + return; } break; case CMD_GET_PARTIAL_OBJECT: @@ -845,7 +848,9 @@ static void usb_mtp_command(MTPState *s, MTPControl *c) } data_in = usb_mtp_get_partial_object(s, c, o); if (NULL == data_in) { - fprintf(stderr, "%s: TODO: handle error\n", __func__); + usb_mtp_queue_result(s, RES_GENERAL_ERROR, + c->trans, 0, 0, 0); + return; } nres = 1; res0 = data_in->length; -- cgit v1.2.3