WindowsRemovableDrivePlugin.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. # Copyright (c) 2015 Ultimaker B.V.
  2. # Copyright (c) 2013 David Braam
  3. # Uranium is released under the terms of the LGPLv3 or higher.
  4. from . import RemovableDrivePlugin
  5. import string
  6. import ctypes
  7. from ctypes import wintypes # Using ctypes.wintypes in the code below does not seem to work
  8. from UM.i18n import i18nCatalog
  9. catalog = i18nCatalog("cura")
  10. # Ignore windows error popups. Fixes the whole "Can't open drive X" when user has an SD card reader.
  11. ctypes.windll.kernel32.SetErrorMode(1) #type: ignore
  12. # WinAPI Constants that we need
  13. # Hardcoded here due to stupid WinDLL stuff that does not give us access to these values.
  14. DRIVE_REMOVABLE = 2 # [CodeStyle: Windows Enum value]
  15. GENERIC_READ = 2147483648 # [CodeStyle: Windows Enum value]
  16. GENERIC_WRITE = 1073741824 # [CodeStyle: Windows Enum value]
  17. FILE_SHARE_READ = 1 # [CodeStyle: Windows Enum value]
  18. FILE_SHARE_WRITE = 2 # [CodeStyle: Windows Enum value]
  19. IOCTL_STORAGE_EJECT_MEDIA = 2967560 # [CodeStyle: Windows Enum value]
  20. OPEN_EXISTING = 3 # [CodeStyle: Windows Enum value]
  21. # Setup the DeviceIoControl function arguments and return type.
  22. # See ctypes documentation for details on how to call C functions from python, and why this is important.
  23. ctypes.windll.kernel32.DeviceIoControl.argtypes = [ #type: ignore
  24. wintypes.HANDLE, # _In_ HANDLE hDevice
  25. wintypes.DWORD, # _In_ DWORD dwIoControlCode
  26. wintypes.LPVOID, # _In_opt_ LPVOID lpInBuffer
  27. wintypes.DWORD, # _In_ DWORD nInBufferSize
  28. wintypes.LPVOID, # _Out_opt_ LPVOID lpOutBuffer
  29. wintypes.DWORD, # _In_ DWORD nOutBufferSize
  30. ctypes.POINTER(wintypes.DWORD), # _Out_opt_ LPDWORD lpBytesReturned
  31. wintypes.LPVOID # _Inout_opt_ LPOVERLAPPED lpOverlapped
  32. ]
  33. ctypes.windll.kernel32.DeviceIoControl.restype = wintypes.BOOL #type: ignore
  34. class WindowsRemovableDrivePlugin(RemovableDrivePlugin.RemovableDrivePlugin):
  35. """Removable drive support for windows"""
  36. def checkRemovableDrives(self):
  37. drives = {}
  38. # The currently available disk drives, e.g.: bitmask = ...1100 <-- ...DCBA
  39. bitmask = ctypes.windll.kernel32.GetLogicalDrives()
  40. # Since we are ignoring drives A and B, the bitmask has has to shift twice to the right
  41. bitmask >>= 2
  42. # Check possible drive letters, from C to Z
  43. # Note: using ascii_uppercase because we do not want this to change with locale!
  44. # Skip A and B, since those drives are typically reserved for floppy disks.
  45. # Those drives can theoretically be reassigned but it's safer to not check them for removable drives.
  46. # Windows will also behave weirdly even with some of its internal functions if you do this (e.g. search indexing doesn't search it).
  47. # Users that have removable drives in A or B will just have to save to file and select the drive there.
  48. for letter in string.ascii_uppercase[2:]:
  49. drive = "{0}:/".format(letter)
  50. # Do we really want to skip A and B?
  51. # GetDriveTypeA explicitly wants a byte array of type ascii. It will accept a string, but this wont work
  52. if bitmask & 1 and ctypes.windll.kernel32.GetDriveTypeA(drive.encode("ascii")) == DRIVE_REMOVABLE:
  53. volume_name = ""
  54. name_buffer = ctypes.create_unicode_buffer(1024)
  55. filesystem_buffer = ctypes.create_unicode_buffer(1024)
  56. error = ctypes.windll.kernel32.GetVolumeInformationW(ctypes.c_wchar_p(drive), name_buffer, ctypes.sizeof(name_buffer), None, None, None, filesystem_buffer, ctypes.sizeof(filesystem_buffer))
  57. if error != 0:
  58. volume_name = name_buffer.value
  59. if not volume_name:
  60. volume_name = catalog.i18nc("@item:intext", "Removable Drive")
  61. # Certain readers will report themselves as a volume even when there is no card inserted, but will show an
  62. # "No volume in drive" warning when trying to call GetDiskFreeSpace. However, they will not report a valid
  63. # filesystem, so we can filter on that. In addition, this excludes other things with filesystems Windows
  64. # does not support.
  65. if filesystem_buffer.value == "":
  66. continue
  67. # Check for the free space. Some card readers show up as a drive with 0 space free when there is no card inserted.
  68. free_bytes = ctypes.c_longlong(0)
  69. if ctypes.windll.kernel32.GetDiskFreeSpaceExA(drive.encode("ascii"), ctypes.byref(free_bytes), None, None) == 0:
  70. continue
  71. if free_bytes.value < 1:
  72. continue
  73. drives[drive] = "{0} ({1}:)".format(volume_name, letter)
  74. bitmask >>= 1
  75. return drives
  76. def performEjectDevice(self, device):
  77. # Magic WinAPI stuff
  78. # First, open a handle to the Device
  79. handle = ctypes.windll.kernel32.CreateFileA("\\\\.\\{0}".format(device.getId()[:-1]).encode("ascii"), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, None, OPEN_EXISTING, 0, None )
  80. if handle == -1:
  81. # ctypes.WinError sets up an GetLastError API call for windows as an Python OSError exception.
  82. # So we use this to raise the error to our caller.
  83. raise ctypes.WinError()
  84. # The DeviceIoControl requires a bytes_returned pointer to be a valid pointer.
  85. # So create a ctypes DWORD to reference. (Without this pointer the DeviceIoControl function will crash with an access violation after doing its job.
  86. bytes_returned = wintypes.DWORD(0)
  87. error = None
  88. # Then, try and tell it to eject
  89. return_code = ctypes.windll.kernel32.DeviceIoControl(handle, IOCTL_STORAGE_EJECT_MEDIA, None, 0, None, 0, ctypes.pointer(bytes_returned), None)
  90. # DeviceIoControl with IOCTL_STORAGE_EJECT_MEDIA return 0 on error.
  91. if return_code == 0:
  92. # ctypes.WinError sets up an GetLastError API call for windows as an Python OSError exception.
  93. # So we use this to raise the error to our caller.
  94. error = ctypes.WinError()
  95. # Do not raise an error here yet, so we can properly close the handle.
  96. # Finally, close the handle
  97. ctypes.windll.kernel32.CloseHandle(handle)
  98. # If an error happened in the DeviceIoControl, raise it now.
  99. if error:
  100. raise error
  101. # Return success
  102. return True