From nobody Mon May 6 21:07:37 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1505367437570926.1964958608788; Wed, 13 Sep 2017 22:37:17 -0700 (PDT) Received: from localhost ([::1]:45855 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dsMpw-0006TT-Tt for importer@patchew.org; Thu, 14 Sep 2017 01:37:16 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48003) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dsMl2-0002tF-LO for qemu-devel@nongnu.org; Thu, 14 Sep 2017 01:32:16 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dsMkz-0006Ow-Jq for qemu-devel@nongnu.org; Thu, 14 Sep 2017 01:32:12 -0400 Received: from mx1.redhat.com ([209.132.183.28]:34736) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dsMkz-0006JE-BH for qemu-devel@nongnu.org; Thu, 14 Sep 2017 01:32:09 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id CFCC581DE3 for ; Thu, 14 Sep 2017 05:32:07 +0000 (UTC) Received: from gigantic.usersys.redhat.com (dhcp-17-169.bos.redhat.com [10.18.17.169]) by smtp.corp.redhat.com (Postfix) with ESMTP id 632EF78219; Thu, 14 Sep 2017 05:32:07 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com CFCC581DE3 Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=bsd@redhat.com From: Bandan Das To: qemu-devel@nongnu.org Date: Thu, 14 Sep 2017 01:31:44 -0400 Message-Id: <20170914053148.2512-2-bsd@redhat.com> In-Reply-To: <20170914053148.2512-1-bsd@redhat.com> References: <20170914053148.2512-1-bsd@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Thu, 14 Sep 2017 05:32:07 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 1/5] usb-mtp: Add one more argument when building results X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kraxel@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Bandan Das The response to a SendObjectInfo consists of the storageid, parent obejct handle anad the handle reserved for the new incoming object Signed-off-by: Bandan Das --- hw/usb/dev-mtp.c | 50 +++++++++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/hw/usb/dev-mtp.c b/hw/usb/dev-mtp.c index 94c2e94..b55aa82 100644 --- a/hw/usb/dev-mtp.c +++ b/hw/usb/dev-mtp.c @@ -765,7 +765,8 @@ static void usb_mtp_add_time(MTPData *data, time_t time) /* -----------------------------------------------------------------------= */ =20 static void usb_mtp_queue_result(MTPState *s, uint16_t code, uint32_t tran= s, - int argc, uint32_t arg0, uint32_t arg1) + int argc, uint32_t arg0, uint32_t arg1, + uint32_t arg2) { MTPControl *c =3D g_new0(MTPControl, 1); =20 @@ -778,6 +779,9 @@ static void usb_mtp_queue_result(MTPState *s, uint16_t = code, uint32_t trans, if (argc > 1) { c->argv[1] =3D arg1; } + if (argc > 2) { + c->argv[2] =3D arg2; + } =20 assert(s->result =3D=3D NULL); s->result =3D c; @@ -1119,7 +1123,7 @@ static void usb_mtp_command(MTPState *s, MTPControl *= c) /* sanity checks */ if (c->code >=3D CMD_CLOSE_SESSION && s->session =3D=3D 0) { usb_mtp_queue_result(s, RES_SESSION_NOT_OPEN, - c->trans, 0, 0, 0); + c->trans, 0, 0, 0, 0); return; } =20 @@ -1131,12 +1135,12 @@ static void usb_mtp_command(MTPState *s, MTPControl= *c) case CMD_OPEN_SESSION: if (s->session) { usb_mtp_queue_result(s, RES_SESSION_ALREADY_OPEN, - c->trans, 1, s->session, 0); + c->trans, 1, s->session, 0, 0); return; } if (c->argv[0] =3D=3D 0) { usb_mtp_queue_result(s, RES_INVALID_PARAMETER, - c->trans, 0, 0, 0); + c->trans, 0, 0, 0, 0); return; } trace_usb_mtp_op_open_session(s->dev.addr); @@ -1165,7 +1169,7 @@ static void usb_mtp_command(MTPState *s, MTPControl *= c) if (c->argv[0] !=3D QEMU_STORAGE_ID && c->argv[0] !=3D 0xffffffff) { usb_mtp_queue_result(s, RES_INVALID_STORAGE_ID, - c->trans, 0, 0, 0); + c->trans, 0, 0, 0, 0); return; } data_in =3D usb_mtp_get_storage_info(s, c); @@ -1175,12 +1179,12 @@ static void usb_mtp_command(MTPState *s, MTPControl= *c) if (c->argv[0] !=3D QEMU_STORAGE_ID && c->argv[0] !=3D 0xffffffff) { usb_mtp_queue_result(s, RES_INVALID_STORAGE_ID, - c->trans, 0, 0, 0); + c->trans, 0, 0, 0, 0); return; } if (c->argv[1] !=3D 0x00000000) { usb_mtp_queue_result(s, RES_SPEC_BY_FORMAT_UNSUPPORTED, - c->trans, 0, 0, 0); + c->trans, 0, 0, 0, 0); return; } if (c->argv[2] =3D=3D 0x00000000 || @@ -1191,12 +1195,12 @@ static void usb_mtp_command(MTPState *s, MTPControl= *c) } if (o =3D=3D NULL) { usb_mtp_queue_result(s, RES_INVALID_OBJECT_HANDLE, - c->trans, 0, 0, 0); + c->trans, 0, 0, 0, 0); return; } if (o->format !=3D FMT_ASSOCIATION) { usb_mtp_queue_result(s, RES_INVALID_PARENT_OBJECT, - c->trans, 0, 0, 0); + c->trans, 0, 0, 0, 0); return; } usb_mtp_object_readdir(s, o); @@ -1212,7 +1216,7 @@ static void usb_mtp_command(MTPState *s, MTPControl *= c) o =3D usb_mtp_object_lookup(s, c->argv[0]); if (o =3D=3D NULL) { usb_mtp_queue_result(s, RES_INVALID_OBJECT_HANDLE, - c->trans, 0, 0, 0); + c->trans, 0, 0, 0, 0); return; } data_in =3D usb_mtp_get_object_info(s, c, o); @@ -1221,18 +1225,18 @@ static void usb_mtp_command(MTPState *s, MTPControl= *c) o =3D usb_mtp_object_lookup(s, c->argv[0]); if (o =3D=3D NULL) { usb_mtp_queue_result(s, RES_INVALID_OBJECT_HANDLE, - c->trans, 0, 0, 0); + c->trans, 0, 0, 0, 0); return; } if (o->format =3D=3D FMT_ASSOCIATION) { usb_mtp_queue_result(s, RES_INVALID_OBJECT_HANDLE, - c->trans, 0, 0, 0); + c->trans, 0, 0, 0, 0); return; } data_in =3D usb_mtp_get_object(s, c, o); if (data_in =3D=3D NULL) { usb_mtp_queue_result(s, RES_GENERAL_ERROR, - c->trans, 0, 0, 0); + c->trans, 0, 0, 0, 0); return; } break; @@ -1240,18 +1244,18 @@ static void usb_mtp_command(MTPState *s, MTPControl= *c) o =3D usb_mtp_object_lookup(s, c->argv[0]); if (o =3D=3D NULL) { usb_mtp_queue_result(s, RES_INVALID_OBJECT_HANDLE, - c->trans, 0, 0, 0); + c->trans, 0, 0, 0, 0); return; } if (o->format =3D=3D FMT_ASSOCIATION) { usb_mtp_queue_result(s, RES_INVALID_OBJECT_HANDLE, - c->trans, 0, 0, 0); + c->trans, 0, 0, 0, 0); return; } data_in =3D usb_mtp_get_partial_object(s, c, o); if (data_in =3D=3D NULL) { usb_mtp_queue_result(s, RES_GENERAL_ERROR, - c->trans, 0, 0, 0); + c->trans, 0, 0, 0, 0); return; } nres =3D 1; @@ -1261,7 +1265,7 @@ static void usb_mtp_command(MTPState *s, MTPControl *= c) if (c->argv[0] !=3D FMT_UNDEFINED_OBJECT && c->argv[0] !=3D FMT_ASSOCIATION) { usb_mtp_queue_result(s, RES_INVALID_OBJECT_FORMAT_CODE, - c->trans, 0, 0, 0); + c->trans, 0, 0, 0, 0); return; } data_in =3D usb_mtp_get_object_props_supported(s, c); @@ -1270,13 +1274,13 @@ static void usb_mtp_command(MTPState *s, MTPControl= *c) if (c->argv[1] !=3D FMT_UNDEFINED_OBJECT && c->argv[1] !=3D FMT_ASSOCIATION) { usb_mtp_queue_result(s, RES_INVALID_OBJECT_FORMAT_CODE, - c->trans, 0, 0, 0); + c->trans, 0, 0, 0, 0); return; } data_in =3D usb_mtp_get_object_prop_desc(s, c); if (data_in =3D=3D NULL) { usb_mtp_queue_result(s, RES_INVALID_OBJECT_PROP_CODE, - c->trans, 0, 0, 0); + c->trans, 0, 0, 0, 0); return; } break; @@ -1284,20 +1288,20 @@ static void usb_mtp_command(MTPState *s, MTPControl= *c) o =3D usb_mtp_object_lookup(s, c->argv[0]); if (o =3D=3D NULL) { usb_mtp_queue_result(s, RES_INVALID_OBJECT_HANDLE, - c->trans, 0, 0, 0); + c->trans, 0, 0, 0, 0); return; } data_in =3D usb_mtp_get_object_prop_value(s, c, o); if (data_in =3D=3D NULL) { usb_mtp_queue_result(s, RES_INVALID_OBJECT_PROP_CODE, - c->trans, 0, 0, 0); + c->trans, 0, 0, 0, 0); return; } break; default: trace_usb_mtp_op_unknown(s->dev.addr, c->code); usb_mtp_queue_result(s, RES_OPERATION_NOT_SUPPORTED, - c->trans, 0, 0, 0); + c->trans, 0, 0, 0, 0); return; } =20 @@ -1306,7 +1310,7 @@ static void usb_mtp_command(MTPState *s, MTPControl *= c) assert(s->data_in =3D=3D NULL); s->data_in =3D data_in; } - usb_mtp_queue_result(s, RES_OK, c->trans, nres, res0, 0); + usb_mtp_queue_result(s, RES_OK, c->trans, nres, res0, 0, 0); } =20 /* -----------------------------------------------------------------------= */ --=20 2.9.4 From nobody Mon May 6 21:07:37 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1505367259543391.4154858556234; Wed, 13 Sep 2017 22:34:19 -0700 (PDT) Received: from localhost ([::1]:45835 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dsMn3-00044q-MB for importer@patchew.org; Thu, 14 Sep 2017 01:34:17 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48004) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dsMl2-0002tG-LM for qemu-devel@nongnu.org; Thu, 14 Sep 2017 01:32:13 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dsMkz-0006Po-RD for qemu-devel@nongnu.org; Thu, 14 Sep 2017 01:32:12 -0400 Received: from mx1.redhat.com ([209.132.183.28]:53938) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dsMkz-0006LM-M2 for qemu-devel@nongnu.org; Thu, 14 Sep 2017 01:32:09 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 5565485542 for ; Thu, 14 Sep 2017 05:32:08 +0000 (UTC) Received: from gigantic.usersys.redhat.com (dhcp-17-169.bos.redhat.com [10.18.17.169]) by smtp.corp.redhat.com (Postfix) with ESMTP id EFBA960841; Thu, 14 Sep 2017 05:32:07 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 5565485542 Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=bsd@redhat.com From: Bandan Das To: qemu-devel@nongnu.org Date: Thu, 14 Sep 2017 01:31:45 -0400 Message-Id: <20170914053148.2512-3-bsd@redhat.com> In-Reply-To: <20170914053148.2512-1-bsd@redhat.com> References: <20170914053148.2512-1-bsd@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Thu, 14 Sep 2017 05:32:08 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 2/5] usb-mtp: print parent path in IN_IGNORED trace fn X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kraxel@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Bandan Das Fix a possible null dereference when deleting a folder and its contents. An ignored event might be received for its contents after the parent folder is deleted which will return a null object. Signed-off-by: Bandan Das --- hw/usb/dev-mtp.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/hw/usb/dev-mtp.c b/hw/usb/dev-mtp.c index b55aa82..63f8f3b 100644 --- a/hw/usb/dev-mtp.c +++ b/hw/usb/dev-mtp.c @@ -540,9 +540,8 @@ static void inotify_watchfn(void *arg) break; =20 case IN_IGNORED: - o =3D usb_mtp_object_lookup_name(parent, event->name, even= t->len); - trace_usb_mtp_inotify_event(s->dev.addr, o->path, - event->mask, "Obj ignored"); + trace_usb_mtp_inotify_event(s->dev.addr, parent->path, + event->mask, "Obj parent dir ignored= "); break; =20 default: --=20 2.9.4 From nobody Mon May 6 21:07:37 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1505367259593748.5353095545929; Wed, 13 Sep 2017 22:34:19 -0700 (PDT) Received: from localhost ([::1]:45837 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dsMn4-00047C-HH for importer@patchew.org; Thu, 14 Sep 2017 01:34:18 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48002) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dsMl2-0002tE-LP for qemu-devel@nongnu.org; Thu, 14 Sep 2017 01:32:13 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dsMl0-0006QN-0E for qemu-devel@nongnu.org; Thu, 14 Sep 2017 01:32:12 -0400 Received: from mx1.redhat.com ([209.132.183.28]:45102) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dsMkz-0006Mt-O4 for qemu-devel@nongnu.org; Thu, 14 Sep 2017 01:32:09 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id CF97DC04B303 for ; Thu, 14 Sep 2017 05:32:08 +0000 (UTC) Received: from gigantic.usersys.redhat.com (dhcp-17-169.bos.redhat.com [10.18.17.169]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7564978218; Thu, 14 Sep 2017 05:32:08 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com CF97DC04B303 Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=bsd@redhat.com From: Bandan Das To: qemu-devel@nongnu.org Date: Thu, 14 Sep 2017 01:31:46 -0400 Message-Id: <20170914053148.2512-4-bsd@redhat.com> In-Reply-To: <20170914053148.2512-1-bsd@redhat.com> References: <20170914053148.2512-1-bsd@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Thu, 14 Sep 2017 05:32:08 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 3/5] usb-mtp: Support delete of mtp objects X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kraxel@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Bandan Das This is required because write of existing objects by the initiator is acheived by making a temporary buffer with the new changes, deleting the old file and then writing a new file with the same name. Note that this operation will fail as of this patch since the store is advertised RO Signed-off-by: Bandan Das --- hw/usb/dev-mtp.c | 121 +++++++++++++++++++++++++++++++++++++++++++++++++++= ++++ 1 file changed, 121 insertions(+) diff --git a/hw/usb/dev-mtp.c b/hw/usb/dev-mtp.c index 63f8f3b..76494f3 100644 --- a/hw/usb/dev-mtp.c +++ b/hw/usb/dev-mtp.c @@ -46,6 +46,7 @@ enum mtp_code { CMD_GET_OBJECT_HANDLES =3D 0x1007, CMD_GET_OBJECT_INFO =3D 0x1008, CMD_GET_OBJECT =3D 0x1009, + CMD_DELETE_OBJECT =3D 0x100b, CMD_GET_PARTIAL_OBJECT =3D 0x101b, CMD_GET_OBJECT_PROPS_SUPPORTED =3D 0x9801, CMD_GET_OBJECT_PROP_DESC =3D 0x9802, @@ -62,6 +63,8 @@ enum mtp_code { RES_INVALID_STORAGE_ID =3D 0x2008, RES_INVALID_OBJECT_HANDLE =3D 0x2009, RES_INVALID_OBJECT_FORMAT_CODE =3D 0x200b, + RES_STORE_READ_ONLY =3D 0x200e, + RES_PARTIAL_DELETE =3D 0x2012, RES_SPEC_BY_FORMAT_UNSUPPORTED =3D 0x2014, RES_INVALID_PARENT_OBJECT =3D 0x201a, RES_INVALID_PARAMETER =3D 0x201d, @@ -799,6 +802,7 @@ static MTPData *usb_mtp_get_device_info(MTPState *s, MT= PControl *c) CMD_GET_NUM_OBJECTS, CMD_GET_OBJECT_HANDLES, CMD_GET_OBJECT_INFO, + CMD_DELETE_OBJECT, CMD_GET_OBJECT, CMD_GET_PARTIAL_OBJECT, CMD_GET_OBJECT_PROPS_SUPPORTED, @@ -1113,6 +1117,120 @@ static MTPData *usb_mtp_get_object_prop_value(MTPSt= ate *s, MTPControl *c, return d; } =20 +/* Return correct return code for a delete event */ +enum { + ALL_DELETE, + PARTIAL_DELETE, + READ_ONLY, +}; + +#ifndef CONFIG_INOTIFY1 +/* Assumes that children, if any, have been already freed */ +static void usb_mtp_object_free_one(MTPState *s, MTPObject *o) +{ + assert(o->nchildren =3D=3D 0); + QTAILQ_REMOVE(&s->objects, o, next); + g_free(o->name); + g_free(o->path); + g_free(o); +} +#endif + +static int usb_mtp_deletefn(MTPState *s, MTPObject *o, uint32_t trans) +{ + MTPObject *iter, *iter2; + bool partial_delete =3D false; + bool success =3D false; + + /* + * TODO: Add support for Protection Status + */ + + QLIST_FOREACH(iter, &o->children, list) { + if (iter->format =3D=3D FMT_ASSOCIATION) { + QLIST_FOREACH(iter2, &iter->children, list) { + usb_mtp_deletefn(s, iter2, trans); + } + } + } + + if (o->format =3D=3D FMT_UNDEFINED_OBJECT) { + if (remove(o->path)) { + partial_delete =3D true; + } else { +#ifndef CONFIG_INOTIFY1 + usb_mtp_object_free_one(s, o); +#endif + success =3D true; + } + } + + if (o->format =3D=3D FMT_ASSOCIATION) { + if (rmdir(o->path)) { + partial_delete =3D true; + } else { +#ifndef CONFIG_INOTIFY1 + usb_mtp_object_free_one(s, o); +#endif + success =3D true; + } + } + + if (success && partial_delete) { + return PARTIAL_DELETE; + } + if (!success && partial_delete) { + return READ_ONLY; + } + return ALL_DELETE; +} + +static void usb_mtp_object_delete(MTPState *s, uint32_t handle, + uint32_t format_code, uint32_t trans) +{ + MTPObject *o; + int ret; + + /* Return error if store is read-only */ + if (!FLAG_SET(s, MTP_FLAG_WRITABLE)) { + usb_mtp_queue_result(s, RES_STORE_READ_ONLY, + trans, 0, 0, 0, 0); + return; + } + + if (format_code !=3D 0) { + usb_mtp_queue_result(s, RES_SPEC_BY_FORMAT_UNSUPPORTED, + trans, 0, 0, 0, 0); + return; + } + + if (handle =3D=3D 0xFFFFFFF) { + o =3D QTAILQ_FIRST(&s->objects); + } else { + o =3D usb_mtp_object_lookup(s, handle); + } + if (o =3D=3D NULL) { + usb_mtp_queue_result(s, RES_INVALID_OBJECT_HANDLE, + trans, 0, 0, 0, 0); + return; + } + + ret =3D usb_mtp_deletefn(s, o, trans); + if (ret =3D=3D PARTIAL_DELETE) { + usb_mtp_queue_result(s, RES_PARTIAL_DELETE, + trans, 0, 0, 0, 0); + return; + } else if (ret =3D=3D READ_ONLY) { + usb_mtp_queue_result(s, RES_STORE_READ_ONLY, trans, + 0, 0, 0, 0); + return; + } else { + usb_mtp_queue_result(s, RES_OK, trans, + 0, 0, 0, 0); + return; + } +} + static void usb_mtp_command(MTPState *s, MTPControl *c) { MTPData *data_in =3D NULL; @@ -1239,6 +1357,9 @@ static void usb_mtp_command(MTPState *s, MTPControl *= c) return; } break; + case CMD_DELETE_OBJECT: + usb_mtp_object_delete(s, c->argv[0], c->argv[1], c->trans); + return; case CMD_GET_PARTIAL_OBJECT: o =3D usb_mtp_object_lookup(s, c->argv[0]); if (o =3D=3D NULL) { --=20 2.9.4 From nobody Mon May 6 21:07:37 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1505367367932454.08010072815387; Wed, 13 Sep 2017 22:36:07 -0700 (PDT) Received: from localhost ([::1]:45850 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dsMop-0005kr-6d for importer@patchew.org; Thu, 14 Sep 2017 01:36:07 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48006) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dsMl2-0002tS-Lm for qemu-devel@nongnu.org; Thu, 14 Sep 2017 01:32:16 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dsMl0-0006RW-F0 for qemu-devel@nongnu.org; Thu, 14 Sep 2017 01:32:12 -0400 Received: from mx1.redhat.com ([209.132.183.28]:49980) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dsMl0-0006PB-6G for qemu-devel@nongnu.org; Thu, 14 Sep 2017 01:32:10 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 52167C0587C5 for ; Thu, 14 Sep 2017 05:32:09 +0000 (UTC) Received: from gigantic.usersys.redhat.com (dhcp-17-169.bos.redhat.com [10.18.17.169]) by smtp.corp.redhat.com (Postfix) with ESMTP id EF93E60841; Thu, 14 Sep 2017 05:32:08 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 52167C0587C5 Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=bsd@redhat.com From: Bandan Das To: qemu-devel@nongnu.org Date: Thu, 14 Sep 2017 01:31:47 -0400 Message-Id: <20170914053148.2512-5-bsd@redhat.com> In-Reply-To: <20170914053148.2512-1-bsd@redhat.com> References: <20170914053148.2512-1-bsd@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.32]); Thu, 14 Sep 2017 05:32:09 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 4/5] usb-mtp: Introduce write support for MTP objects X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kraxel@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Bandan Das Allow write operations on behalf of the initiator. The precursor to write is the sending of the write metadata that consists of the ObjectInfo dataset. This patch introduces a flag that is set when the responder is ready to receive write data based on a previous SendObjectInfo operation by the initiator (The SendObjectInfo implementation is in a later patch) Signed-off-by: Bandan Das --- hw/usb/dev-mtp.c | 159 +++++++++++++++++++++++++++++++++++++++++++++++++++= +++- 1 file changed, 157 insertions(+), 2 deletions(-) diff --git a/hw/usb/dev-mtp.c b/hw/usb/dev-mtp.c index 76494f3..2ff9f0f 100644 --- a/hw/usb/dev-mtp.c +++ b/hw/usb/dev-mtp.c @@ -47,6 +47,7 @@ enum mtp_code { CMD_GET_OBJECT_INFO =3D 0x1008, CMD_GET_OBJECT =3D 0x1009, CMD_DELETE_OBJECT =3D 0x100b, + CMD_SEND_OBJECT =3D 0x100d, CMD_GET_PARTIAL_OBJECT =3D 0x101b, CMD_GET_OBJECT_PROPS_SUPPORTED =3D 0x9801, CMD_GET_OBJECT_PROP_DESC =3D 0x9802, @@ -66,7 +67,9 @@ enum mtp_code { RES_STORE_READ_ONLY =3D 0x200e, RES_PARTIAL_DELETE =3D 0x2012, RES_SPEC_BY_FORMAT_UNSUPPORTED =3D 0x2014, + RES_INVALID_OBJECTINFO =3D 0x2015, RES_INVALID_PARENT_OBJECT =3D 0x201a, + RES_STORE_FULL =3D 0x200c, RES_INVALID_PARAMETER =3D 0x201d, RES_SESSION_ALREADY_OPEN =3D 0x201e, RES_INVALID_OBJECT_PROP_CODE =3D 0xA801, @@ -182,6 +185,14 @@ struct MTPState { int inotifyfd; QTAILQ_HEAD(events, MTPMonEntry) events; #endif + /* Responder is expecting a write operation */ + bool write_pending; + struct { + uint32_t parent_handle; + uint16_t format; + uint32_t size; + char *filename; + } dataset; }; =20 #define TYPE_USB_MTP "usb-mtp" @@ -803,6 +814,7 @@ static MTPData *usb_mtp_get_device_info(MTPState *s, MT= PControl *c) CMD_GET_OBJECT_HANDLES, CMD_GET_OBJECT_INFO, CMD_DELETE_OBJECT, + CMD_SEND_OBJECT, CMD_GET_OBJECT, CMD_GET_PARTIAL_OBJECT, CMD_GET_OBJECT_PROPS_SUPPORTED, @@ -1381,6 +1393,14 @@ static void usb_mtp_command(MTPState *s, MTPControl = *c) nres =3D 1; res0 =3D data_in->length; break; + case CMD_SEND_OBJECT: + if (!s->write_pending) { + usb_mtp_queue_result(s, RES_INVALID_OBJECTINFO, + c->trans, 0, 0, 0, 0); + return; + } + s->data_out =3D usb_mtp_data_alloc(c); + return; case CMD_GET_OBJECT_PROPS_SUPPORTED: if (c->argv[0] !=3D FMT_UNDEFINED_OBJECT && c->argv[0] !=3D FMT_ASSOCIATION) { @@ -1475,12 +1495,133 @@ static void usb_mtp_cancel_packet(USBDevice *dev, = USBPacket *p) fprintf(stderr, "%s\n", __func__); } =20 +mode_t getumask(void) +{ + mode_t mask =3D umask(0); + umask(mask); + return mask; +} + +static void usb_mtp_write_data(MTPState *s) +{ + MTPData *d =3D s->data_out; + MTPObject *parent =3D + usb_mtp_object_lookup(s, s->dataset.parent_handle); + char *path; + int rc =3D -1; + mode_t mask =3D ~getumask() & 0777; + + assert(d !=3D NULL); + + if (parent =3D=3D NULL || !s->write_pending) { + usb_mtp_queue_result(s, RES_INVALID_OBJECTINFO, d->trans, + 0, 0, 0, 0); + return; + } + + if (s->dataset.filename) { + path =3D g_strdup_printf("%s/%s", parent->path, s->dataset.filenam= e); + if (s->dataset.format =3D=3D FMT_ASSOCIATION) { + d->fd =3D mkdir(path, mask); + goto free; + } + if (s->dataset.size < d->length) { + usb_mtp_queue_result(s, RES_STORE_FULL, d->trans, + 0, 0, 0, 0); + goto done; + } + d->fd =3D open(path, O_CREAT | O_WRONLY, mask); + if (d->fd =3D=3D -1) { + usb_mtp_queue_result(s, RES_STORE_FULL, d->trans, + 0, 0, 0, 0); + goto done; + } + + /* + * Return success if initiator sent 0 sized data + */ + if (!s->dataset.size) { + goto success; + } + + rc =3D write(d->fd, d->data, s->dataset.size); + if (rc =3D=3D -1) { + usb_mtp_queue_result(s, RES_STORE_FULL, d->trans, + 0, 0, 0, 0); + goto done; + } + if (rc !=3D s->dataset.size) { + usb_mtp_queue_result(s, RES_INCOMPLETE_TRANSFER, d->trans, + 0, 0, 0, 0); + goto done; + } + } + +success: + usb_mtp_queue_result(s, RES_OK, d->trans, + 0, 0, 0, 0); + +done: + /* + * The write dataset is kept around and freed only + * on success or if another write request comes in + */ + if (d->fd !=3D -1) { + close(d->fd); + } +free: + g_free(s->dataset.filename); + g_free(path); + s->write_pending =3D false; +} + +static void usb_mtp_get_data(MTPState *s, mtp_container *container, + USBPacket *p) +{ + MTPData *d =3D s->data_out; + uint64_t dlen; + uint32_t data_len =3D p->iov.size; + + if (d->first) { + /* Total length of incoming data */ + d->length =3D cpu_to_le32(container->length) - sizeof(mtp_containe= r); + /* Length of data in this packet */ + data_len -=3D sizeof(mtp_container); + usb_mtp_realloc(d, d->length); + d->offset =3D 0; + d->first =3D false; + } + + if (d->length - d->offset > data_len) { + dlen =3D data_len; + } else { + dlen =3D d->length - d->offset; + } + + switch (d->code) { + case CMD_SEND_OBJECT: + usb_packet_copy(p, d->data + d->offset, dlen); + d->offset +=3D dlen; + if (d->offset =3D=3D d->length) { + usb_mtp_write_data(s); + usb_mtp_data_free(s->data_out); + s->data_out =3D NULL; + return; + } + break; + default: + p->status =3D USB_RET_STALL; + return; + } +} + static void usb_mtp_handle_data(USBDevice *dev, USBPacket *p) { MTPState *s =3D USB_MTP(dev); MTPControl cmd; mtp_container container; uint32_t params[5]; + uint16_t container_type; int i, rc; =20 switch (p->ep->nr) { @@ -1570,8 +1711,13 @@ static void usb_mtp_handle_data(USBDevice *dev, USBP= acket *p) p->status =3D USB_RET_STALL; return; } - usb_packet_copy(p, &container, sizeof(container)); - switch (le16_to_cpu(container.type)) { + if (s->data_out && !s->data_out->first) { + container_type =3D TYPE_DATA; + } else { + usb_packet_copy(p, &container, sizeof(container)); + container_type =3D le16_to_cpu(container.type); + } + switch (container_type) { case TYPE_COMMAND: if (s->data_in || s->data_out || s->result) { trace_usb_mtp_stall(s->dev.addr, "transaction inflight"); @@ -1602,6 +1748,15 @@ static void usb_mtp_handle_data(USBDevice *dev, USBP= acket *p) (cmd.argc > 4) ? cmd.argv[4] : 0); usb_mtp_command(s, &cmd); break; + case TYPE_DATA: + /* One of the previous transfers has already errored but the + * responder is still sending data associated with it + */ + if (s->result !=3D NULL) { + return; + } + usb_mtp_get_data(s, &container, p); + break; default: /* not needed as long as the mtp device is read-only */ p->status =3D USB_RET_STALL; --=20 2.9.4 From nobody Mon May 6 21:07:37 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1505367259914717.7924232720948; Wed, 13 Sep 2017 22:34:19 -0700 (PDT) Received: from localhost ([::1]:45838 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dsMn5-00047Y-4a for importer@patchew.org; Thu, 14 Sep 2017 01:34:19 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48005) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dsMl2-0002tI-LW for qemu-devel@nongnu.org; Thu, 14 Sep 2017 01:32:16 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dsMl1-0006SR-0Z for qemu-devel@nongnu.org; Thu, 14 Sep 2017 01:32:12 -0400 Received: from mx1.redhat.com ([209.132.183.28]:54302) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dsMl0-0006Qo-Ny for qemu-devel@nongnu.org; Thu, 14 Sep 2017 01:32:10 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id C9F147EBD3 for ; Thu, 14 Sep 2017 05:32:09 +0000 (UTC) Received: from gigantic.usersys.redhat.com (dhcp-17-169.bos.redhat.com [10.18.17.169]) by smtp.corp.redhat.com (Postfix) with ESMTP id 73C5B60841; Thu, 14 Sep 2017 05:32:09 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com C9F147EBD3 Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx03.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=bsd@redhat.com From: Bandan Das To: qemu-devel@nongnu.org Date: Thu, 14 Sep 2017 01:31:48 -0400 Message-Id: <20170914053148.2512-6-bsd@redhat.com> In-Reply-To: <20170914053148.2512-1-bsd@redhat.com> References: <20170914053148.2512-1-bsd@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Thu, 14 Sep 2017 05:32:09 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 5/5] usb-mtp: Advertise SendObjectInfo for write support X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kraxel@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Bandan Das This patch implements a dummy ObjectInfo structure so that it's easy to typecast the incoming data. If the metadata is valid, write_pending is set. Also, the incoming filename is utf-16, so, instead of depending on external libraries, just implement a simple function to get the filename Signed-off-by: Bandan Das --- hw/usb/dev-mtp.c | 114 +++++++++++++++++++++++++++++++++++++++++++++++++++= ++++ 1 file changed, 114 insertions(+) diff --git a/hw/usb/dev-mtp.c b/hw/usb/dev-mtp.c index 2ff9f0f..e5152db 100644 --- a/hw/usb/dev-mtp.c +++ b/hw/usb/dev-mtp.c @@ -47,6 +47,7 @@ enum mtp_code { CMD_GET_OBJECT_INFO =3D 0x1008, CMD_GET_OBJECT =3D 0x1009, CMD_DELETE_OBJECT =3D 0x100b, + CMD_SEND_OBJECT_INFO =3D 0x100c, CMD_SEND_OBJECT =3D 0x100d, CMD_GET_PARTIAL_OBJECT =3D 0x101b, CMD_GET_OBJECT_PROPS_SUPPORTED =3D 0x9801, @@ -66,8 +67,10 @@ enum mtp_code { RES_INVALID_OBJECT_FORMAT_CODE =3D 0x200b, RES_STORE_READ_ONLY =3D 0x200e, RES_PARTIAL_DELETE =3D 0x2012, + RES_STORE_NOT_AVAILABLE =3D 0x2013, RES_SPEC_BY_FORMAT_UNSUPPORTED =3D 0x2014, RES_INVALID_OBJECTINFO =3D 0x2015, + RES_DESTINATION_UNSUPPORTED =3D 0x2020, RES_INVALID_PARENT_OBJECT =3D 0x201a, RES_STORE_FULL =3D 0x200c, RES_INVALID_PARAMETER =3D 0x201d, @@ -195,6 +198,24 @@ struct MTPState { } dataset; }; =20 +/* + * ObjectInfo dataset received from initiator + * Fields we don't care about are ignored + */ +typedef struct { + char __pad1[4]; + uint16_t format; + char __pad2[2]; + uint32_t size; + char __pad3[30]; + uint16_t assoc_type; + uint32_t assoc_desc; + char __pad4[4]; + uint8_t length; + uint16_t filename[0]; + /* string and other data follows */ +} QEMU_PACKED ObjectInfo; + #define TYPE_USB_MTP "usb-mtp" #define USB_MTP(obj) OBJECT_CHECK(MTPState, (obj), TYPE_USB_MTP) =20 @@ -814,6 +835,7 @@ static MTPData *usb_mtp_get_device_info(MTPState *s, MT= PControl *c) CMD_GET_OBJECT_HANDLES, CMD_GET_OBJECT_INFO, CMD_DELETE_OBJECT, + CMD_SEND_OBJECT_INFO, CMD_SEND_OBJECT, CMD_GET_OBJECT, CMD_GET_PARTIAL_OBJECT, @@ -1393,6 +1415,35 @@ static void usb_mtp_command(MTPState *s, MTPControl = *c) nres =3D 1; res0 =3D data_in->length; break; + case CMD_SEND_OBJECT_INFO: + /* First parameter points to storage id or is 0 */ + if (c->argv[0] && (c->argv[0] !=3D QEMU_STORAGE_ID)) { + usb_mtp_queue_result(s, RES_STORE_NOT_AVAILABLE, c->trans, + 0, 0, 0, 0); + } else if (c->argv[1] && !c->argv[0]) { + /* If second parameter is specified, first must also be specif= ied */ + usb_mtp_queue_result(s, RES_DESTINATION_UNSUPPORTED, c->trans, + 0, 0, 0, 0); + } else { + uint32_t handle =3D c->argv[1]; + if (handle =3D=3D 0xFFFFFFFF || handle =3D=3D 0) { + /* root object */ + o =3D QTAILQ_FIRST(&s->objects); + } else { + o =3D usb_mtp_object_lookup(s, handle); + } + if (o =3D=3D NULL) { + usb_mtp_queue_result(s, RES_INVALID_OBJECT_HANDLE, c->tran= s, + 0, 0, 0, 0); + } + if (o->format !=3D FMT_ASSOCIATION) { + usb_mtp_queue_result(s, RES_INVALID_PARENT_OBJECT, c->tran= s, + 0, 0, 0, 0); + } + } + s->dataset.parent_handle =3D o->handle; + s->data_out =3D usb_mtp_data_alloc(c); + return; case CMD_SEND_OBJECT: if (!s->write_pending) { usb_mtp_queue_result(s, RES_INVALID_OBJECTINFO, @@ -1502,6 +1553,17 @@ mode_t getumask(void) return mask; } =20 +static void utf16_to_str(uint8_t len, uint16_t *arr, char *name) +{ + int count; + + for (count =3D 0; count < len; count++) { + /* Check for valid ascii */ + assert(!(arr[count] & 0xFF80)); + name[count] =3D arr[count]; + } +} + static void usb_mtp_write_data(MTPState *s) { MTPData *d =3D s->data_out; @@ -1575,6 +1637,45 @@ free: s->write_pending =3D false; } =20 +static void usb_mtp_write_metadata(MTPState *s) +{ + MTPData *d =3D s->data_out; + ObjectInfo *dataset =3D (ObjectInfo *)d->data; + char *filename =3D g_new0(char, dataset->length); + MTPObject *o; + MTPObject *p =3D usb_mtp_object_lookup(s, s->dataset.parent_handle); + uint32_t next_handle =3D s->next_handle; + + assert(!s->write_pending); + + utf16_to_str(dataset->length, dataset->filename, filename); + + o =3D usb_mtp_object_lookup_name(p, filename, dataset->length); + if (o !=3D NULL) { + next_handle =3D o->handle; + } + + s->dataset.filename =3D filename; + s->dataset.format =3D dataset->format; + s->dataset.size =3D dataset->size; + s->dataset.filename =3D filename; + s->write_pending =3D true; + + if (s->dataset.format =3D=3D FMT_ASSOCIATION) { + usb_mtp_write_data(s); + /* next_handle will be allocated to the newly created dir */ + if (d->fd =3D=3D -1) { + usb_mtp_queue_result(s, RES_STORE_FULL, d->trans, + 0, 0, 0, 0); + return; + } + d->fd =3D -1; + } + + usb_mtp_queue_result(s, RES_OK, d->trans, 3, QEMU_STORAGE_ID, + s->dataset.parent_handle, next_handle); +} + static void usb_mtp_get_data(MTPState *s, mtp_container *container, USBPacket *p) { @@ -1599,6 +1700,19 @@ static void usb_mtp_get_data(MTPState *s, mtp_contai= ner *container, } =20 switch (d->code) { + case CMD_SEND_OBJECT_INFO: + usb_packet_copy(p, d->data + d->offset, dlen); + d->offset +=3D dlen; + if (d->offset =3D=3D d->length) { + /* The operation might have already failed */ + if (!s->result) { + usb_mtp_write_metadata(s); + } + usb_mtp_data_free(s->data_out); + s->data_out =3D NULL; + return; + } + break; case CMD_SEND_OBJECT: usb_packet_copy(p, d->data + d->offset, dlen); d->offset +=3D dlen; --=20 2.9.4