usb-mtp: use a list for keeping track of children

To support adding/removal of objects, we will need to update
the object cache hierarchy we have built internally. Convert
to using a Qlist for easier management.

Signed-off-by: Bandan Das <bsd@redhat.com>
Message-id: 1448314625-3855-2-git-send-email-bsd@redhat.com
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
This commit is contained in:
Bandan Das 2015-11-23 16:37:02 -05:00 committed by Gerd Hoffmann
parent f05b42d3fd
commit 4c7a67f5cd
2 changed files with 41 additions and 16 deletions

View File

@ -109,8 +109,9 @@ struct MTPObject {
char *path; char *path;
struct stat stat; struct stat stat;
MTPObject *parent; MTPObject *parent;
MTPObject **children;
uint32_t nchildren; uint32_t nchildren;
QLIST_HEAD(, MTPObject) children;
QLIST_ENTRY(MTPObject) list;
bool have_children; bool have_children;
QTAILQ_ENTRY(MTPObject) next; QTAILQ_ENTRY(MTPObject) next;
}; };
@ -317,15 +318,24 @@ ignore:
static void usb_mtp_object_free(MTPState *s, MTPObject *o) static void usb_mtp_object_free(MTPState *s, MTPObject *o)
{ {
int i; MTPObject *iter;
if (!o) {
return;
}
trace_usb_mtp_object_free(s->dev.addr, o->handle, o->path); trace_usb_mtp_object_free(s->dev.addr, o->handle, o->path);
QTAILQ_REMOVE(&s->objects, o, next); QTAILQ_REMOVE(&s->objects, o, next);
for (i = 0; i < o->nchildren; i++) { if (o->parent) {
usb_mtp_object_free(s, o->children[i]); QLIST_REMOVE(o, list);
o->parent->nchildren--;
}
while (!QLIST_EMPTY(&o->children)) {
iter = QLIST_FIRST(&o->children);
usb_mtp_object_free(s, iter);
} }
g_free(o->children);
g_free(o->name); g_free(o->name);
g_free(o->path); g_free(o->path);
g_free(o); g_free(o);
@ -343,6 +353,25 @@ static MTPObject *usb_mtp_object_lookup(MTPState *s, uint32_t handle)
return NULL; return NULL;
} }
static MTPObject *usb_mtp_add_child(MTPState *s, MTPObject *o,
char *name)
{
MTPObject *child =
usb_mtp_object_alloc(s, s->next_handle++, o, name);
if (child) {
trace_usb_mtp_add_child(s->dev.addr, child->handle, child->path);
QLIST_INSERT_HEAD(&o->children, child, list);
o->nchildren++;
if (child->format == FMT_ASSOCIATION) {
QLIST_INIT(&child->children);
}
}
return child;
}
static void usb_mtp_object_readdir(MTPState *s, MTPObject *o) static void usb_mtp_object_readdir(MTPState *s, MTPObject *o)
{ {
struct dirent *entry; struct dirent *entry;
@ -358,14 +387,7 @@ static void usb_mtp_object_readdir(MTPState *s, MTPObject *o)
return; return;
} }
while ((entry = readdir(dir)) != NULL) { while ((entry = readdir(dir)) != NULL) {
if ((o->nchildren % 32) == 0) { usb_mtp_add_child(s, o, entry->d_name);
o->children = g_renew(MTPObject *, o->children, o->nchildren + 32);
}
o->children[o->nchildren] =
usb_mtp_object_alloc(s, s->next_handle++, o, entry->d_name);
if (o->children[o->nchildren] != NULL) {
o->nchildren++;
}
} }
closedir(dir); closedir(dir);
} }
@ -617,13 +639,15 @@ static MTPData *usb_mtp_get_object_handles(MTPState *s, MTPControl *c,
MTPObject *o) MTPObject *o)
{ {
MTPData *d = usb_mtp_data_alloc(c); MTPData *d = usb_mtp_data_alloc(c);
uint32_t i, handles[o->nchildren]; uint32_t i = 0, handles[o->nchildren];
MTPObject *iter;
trace_usb_mtp_op_get_object_handles(s->dev.addr, o->handle, o->path); trace_usb_mtp_op_get_object_handles(s->dev.addr, o->handle, o->path);
for (i = 0; i < o->nchildren; i++) { QLIST_FOREACH(iter, &o->children, list) {
handles[i] = o->children[i]->handle; handles[i++] = iter->handle;
} }
assert(i == o->nchildren);
usb_mtp_add_u32_array(d, o->nchildren, handles); usb_mtp_add_u32_array(d, o->nchildren, handles);
return d; return d;

View File

@ -552,6 +552,7 @@ usb_mtp_op_get_partial_object(int dev, uint32_t handle, const char *path, uint32
usb_mtp_op_unknown(int dev, uint32_t code) "dev %d, command code 0x%x" usb_mtp_op_unknown(int dev, uint32_t code) "dev %d, command code 0x%x"
usb_mtp_object_alloc(int dev, uint32_t handle, const char *path) "dev %d, handle 0x%x, path %s" usb_mtp_object_alloc(int dev, uint32_t handle, const char *path) "dev %d, handle 0x%x, path %s"
usb_mtp_object_free(int dev, uint32_t handle, const char *path) "dev %d, handle 0x%x, path %s" usb_mtp_object_free(int dev, uint32_t handle, const char *path) "dev %d, handle 0x%x, path %s"
usb_mtp_add_child(int dev, uint32_t handle, const char *path) "dev %d, handle 0x%x, path %s"
# hw/usb/host-libusb.c # hw/usb/host-libusb.c
usb_host_open_started(int bus, int addr) "dev %d:%d" usb_host_open_started(int bus, int addr) "dev %d:%d"