From 0c4dae5e89edfd6851c00ca05803891c53507d34 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 25 Jan 2025 10:19:06 +0530 Subject: [PATCH] When bulk get of properties fails retry with a single get --- .../mtp/windows/content_enumeration.cpp | 30 ++++++++++++------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/src/calibre/devices/mtp/windows/content_enumeration.cpp b/src/calibre/devices/mtp/windows/content_enumeration.cpp index 208e9f62ab..7ad33a710b 100644 --- a/src/calibre/devices/mtp/windows/content_enumeration.cpp +++ b/src/calibre/devices/mtp/windows/content_enumeration.cpp @@ -210,6 +210,7 @@ single_get_filesystem(unsigned int level, CComPtr &conte if (!item) { if (!get_properties_failed) return false; fprintf(stderr, "Ignoring object with id: %ls because getting its properties failed.\n", pv.pwszVal); + if (PyErr_Occurred()) PyErr_Clear(); } if (PyDict_SetItem(ans, PyDict_GetItemString(item.ptr(), "id"), item.ptr()) != 0) return false; if (callback) { @@ -341,8 +342,9 @@ static bool bulk_get_filesystem( unsigned int level, IPortableDevice *device, IPortableDevicePropertiesBulk *bulk_properties, CComPtr &object_ids, - PyObject *pycallback, PyObject *ans, PyObject *subfolders + PyObject *pycallback, PyObject *ans, PyObject *subfolders, bool *retry_with_single_get ) { + *retry_with_single_get = false; CComPtr properties(create_filesystem_properties_collection()); if (!properties) return false; @@ -401,7 +403,7 @@ bulk_get_filesystem( bulk_properties_callback->Release(); if (PyErr_Occurred()) return false; if (FAILED(hr)) { - hresult_set_exc("Bulk get properties failed in OnEnd", hr); + *retry_with_single_get = true; return false; } return true; @@ -512,11 +514,15 @@ list_folder(IPortableDevice *device, CComPtr &content, I bool enum_failed = false; if (!find_objects_in(content, object_ids, folder_id, &enum_failed)) return NULL; +#define single_get if (!single_get_filesystem(0, content, object_ids, NULL, ans.ptr(), NULL)) return NULL; if (bulk_properties) { - if (!bulk_get_filesystem(0, device, bulk_properties, object_ids, NULL, ans.ptr(), NULL)) return NULL; - } else { - if (!single_get_filesystem(0, content, object_ids, NULL, ans.ptr(), NULL)) return NULL; - } + bool retry_with_single_get; + if (!bulk_get_filesystem(0, device, bulk_properties, object_ids, NULL, ans.ptr(), NULL, &retry_with_single_get)) { + if (retry_with_single_get) { single_get; } + else return NULL; + } + } else { single_get; } +#undef single_get return ans.detach(); } @@ -591,11 +597,15 @@ get_files_and_folders(unsigned int level, IPortableDevice *device, CComPtr