def int_read(self): fd = self.__open_dev(read_only=True) try: fd.seek(DATA_START_SEEK) data = fd.read(KSSignature.SIGN_LEN) sign = KSSignature(data) if not sign.is_valid(): raise Exception("Key chain does not found at %s" % self.__dev_path) return fd.read(sign.ks_len) except IOError, err: logger.warning("KS device open error: %s" % err) raise IOError("Device %s can not be read!" % self.__dev_path)
def unmount_media_device(cls, device, force=False): logic_drives = [] #found logical drives on device import win32com.client import pythoncom import win32file import win32con import win32api pythoncom.CoInitialize() strComputer = "." objWMIService = win32com.client.Dispatch("WbemScripting.SWbemLocator") objSWbemServices = objWMIService.ConnectServer(strComputer,"root\cimv2") logger.debug('unmounting %s ... %s'%(device, type(device))) device_id = device.replace('\\', '\\\\') partitions= objSWbemServices.ExecQuery('ASSOCIATORS OF {Win32_DiskDrive.DeviceID="%s"} \ WHERE AssocClass = Win32_DiskDriveToDiskPartition'%device_id) for part in partitions: logical_disks = objSWbemServices.ExecQuery('ASSOCIATORS OF \ {Win32_DiskPartition.DeviceID="%s"} \ WHERE AssocClass = Win32_LogicalDiskToPartition'%part.DeviceID) for logic in logical_disks: logic_drives.append(logic.DeviceID) #unmounting all logical drives FSCTL_DISMOUNT_VOLUME = 0x00090020 for vol in logic_drives: if force: p = Subprocess('mountvol %s /d'%vol) out, err = p.communicate() if p.returncode: logger.warning('"mountvol %s /d" failed with message: %s %s'%(vol, out, err)) continue try: hh = win32file.CreateFile('\\\\.\\%s'%str(vol), win32con.GENERIC_READ, win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE, None, win32file.OPEN_EXISTING, win32file.FILE_ATTRIBUTE_NORMAL, None) except Exception, err: raise Exception('Volume %s does not opened: %s'%(vol, err)) try: ret = win32file.DeviceIoControl(hh, FSCTL_DISMOUNT_VOLUME, None, None) if ret: raise Exception('Volume %s does not unmounted! Error code: %s'%(str(vol), win32api.GetLastError())) finally: win32file.CloseHandle(hh)