Commit 224899c3d3 for qemu.org

commit 224899c3d3bee6d7c4b02eb1c6e7bf5f66ce6159
Author: Tommaso Califano <tcalifano@redhat.com>
Date:   Thu Nov 12 18:48:58 2026 +0100

    hw/usb/dev-mtp: Fix recursive delete of directories

    usb_mtp_deletefn() only recursed into the grandchildren of the target,
    never deleting the files directly inside it. The directory was left
    non-empty, so rmdir() on the host always failed, and objects nested more
    than two levels deep were silently kept.

    Recurse into every child of the target instead. Since the root directory
    can now actually become empty, skip the root object itself: rmdir()'ing
    it and freeing it would leave s->objects empty.

    Cc: qemu-stable@nongnu.org
    Fixes: ec6206a68f70 ("usb-mtp: Support delete of mtp objects")
    Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4529
    Suggested-by: Hui Peng <https://gitlab.com/benquike>
    Signed-off-by: Tommaso Califano <tcalifano@redhat.com>
    Reviewed-by: Junjie Cao <junjie.cao@linux.dev>
    Tested-by: Junjie Cao <junjie.cao@linux.dev>
    Message-ID: <20261112174907.158825-2-tcalifano@redhat.com>
    Signed-off-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com>

diff --git a/hw/usb/dev-mtp.c b/hw/usb/dev-mtp.c
index 8507e516ab..bc5a6f0891 100644
--- a/hw/usb/dev-mtp.c
+++ b/hw/usb/dev-mtp.c
@@ -1152,19 +1152,20 @@ enum {

 static int usb_mtp_deletefn(MTPState *s, MTPObject *o, uint32_t trans)
 {
-    MTPObject *iter, *iter2, *iter2_next;
+    MTPObject *iter, *iter_next;
     int ret = 0;

     /*
      * TODO: Add support for Protection Status
      */

-    QLIST_FOREACH(iter, &o->children, list) {
-        if (iter->format == FMT_ASSOCIATION) {
-            QLIST_FOREACH_SAFE(iter2, &iter->children, list, iter2_next) {
-                ret |= usb_mtp_deletefn(s, iter2, trans);
-            }
-        }
+    QLIST_FOREACH_SAFE(iter, &o->children, list, iter_next) {
+        ret |= usb_mtp_deletefn(s, iter, trans);
+    }
+
+    /* Avoid deleting the mtp root */
+    if (o->parent == NULL) {
+        return ret ? ret : DELETE_SUCCESS;
     }

     if (o->format == FMT_UNDEFINED_OBJECT) {