MTP driver: Fix Internal storage and SD card being swapped on some devices that have buggy firmware that assigns the SD card a lower id than the internal storage

This commit is contained in:
Kovid Goyal 2025-01-16 12:34:13 +05:30
parent 543eb5fa10
commit 04a55c66df
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 25 additions and 3 deletions

View File

@ -30,6 +30,17 @@ def fingerprint(d):
d.serial, d.manufacturer, d.product)
def sorted_storage(storage_info):
storage = sorted(storage_info, key=operator.itemgetter('id'))
if len(storage) > 1 and storage[0].get('removable', False):
for i in range(1, len(storage)):
x = storage[i]
if not x.get('removable', False):
storage[0], storage[i] = storage[i], storage[0]
break
return storage
APPLE = 0x05ac
@ -222,7 +233,7 @@ class MTP_DEVICE(MTPDeviceBase):
connected_device, as_unicode(e)))
try:
storage = sorted(self.dev.storage_info, key=operator.itemgetter('id'))
storage = sorted_storage(self.dev.storage_info)
except self.libmtp.MTPError as e:
if "The device has no storage information." in str(e):
# This happens on newer Android devices while waiting for
@ -277,7 +288,7 @@ class MTP_DEVICE(MTPDeviceBase):
ans += '\nids: %s'%(self.dev.ids,)
ans += '\nDevice version: %s'%self.dev.device_version
ans += '\nStorage:\n'
storage = sorted(self.dev.storage_info, key=operator.itemgetter('id'))
storage = sorted_storage(self.dev.storage_info)
ans += pprint.pformat(storage)
return ans

View File

@ -38,6 +38,17 @@ def same_thread(func):
return check_thread
def sorted_storage(storage):
storage = sorted(storage, key=lambda x:x.get('id', 'zzzzz'))
if len(storage) > 1 and 'removable' in storage[0].get('type', ''):
for i in range(1, len(storage)):
x = storage[i]
if 'fixed' in x.get('type', ''):
storage[0], storage[i] = storage[i], storage[0]
break
return storage
class MTP_DEVICE(MTPDeviceBase):
supported_platforms = ['windows']
@ -332,7 +343,7 @@ class MTP_DEVICE(MTPDeviceBase):
raise BlacklistedDevice(
'The %s device has been blacklisted by the user'%(connected_device,))
storage.sort(key=lambda x:x.get('id', 'zzzzz'))
storage = sorted_storage(storage)
self._main_id = storage[0]['id']
if len(storage) > 1: