def GetFileInformationByHandle(handle): """ Note that pywin32 FILETIME structure is buggy, and here nFileSizeHigh/nFileSizeLow return 0 on FAT32 whereas on ctypes backend they have a proper value. """ info = BY_HANDLE_FILE_INFORMATION() (info.dwFileAttributes, _ftCreationTime, _ftLastAccessTime, _ftLastWriteTime, info.dwVolumeSerialNumber, info.nFileSizeHigh, info.nFileSizeLow, info.nNumberOfLinks, info.nFileIndexHigh, info.nFileIndexLow) = win32file.GetFileInformationByHandle(handle) ##print(">>>>>>", info.__dict__) # to workaround bugs, we don't deal with "PyTime" objects, we fallback to stdlib... mystat = os.fstat(_open_osfhandle(handle, 0)) info.ftCreationTime = FILETIME( *_utilities.python_timestamp_to_win32_filetime(mystat.st_ctime)) info.ftLastAccessTime = FILETIME( *_utilities.python_timestamp_to_win32_filetime(mystat.st_atime)) info.ftLastWriteTime = FILETIME( *_utilities.python_timestamp_to_win32_filetime(mystat.st_mtime)) return info
def taskManagerCpuTimes(): idle = FILETIME() sys = FILETIME() usr = FILETIME() i = 0.0 s = 0.0 u = 0.0 if windll.kernel32.GetSystemTimes(byref(idle), byref(sys), byref(usr)): i = (idle.dwHighDateTime << 32 | idle.dwLowDateTime) / nanosecs100 s = (sys.dwHighDateTime << 32 | sys.dwLowDateTime) / nanosecs100 u = (usr.dwHighDateTime << 32 | usr.dwLowDateTime) / nanosecs100 return (i, s, u)
def GetProcessTimes(handle): creation_time = FILETIME() exit_time = FILETIME() kernel_time = FILETIME() user_time = FILETIME() ok = _GetProcessTimes(handle, byref(creation_time), byref(exit_time), byref(kernel_time), byref(user_time)) if not ok: raise ctypes_winerror() return (filetime2py(creation_time), filetime2py(exit_time), filetime2py(kernel_time), filetime2py(user_time))
def create_item(self, logo, service, username, password, notes): cred_flags = DWORD(0) cred_type = self.CRED_TYPE_GENERIC # I make this the target_name because otherwise users with multiple # accounts on the same service won't be able to create those # credentials. They would end up just overwriting the same # credential for the service each time a new account is added. cred_target_name = LPWSTR(repr((service, username))) cred_comment = LPWSTR('') cred_last_written = FILETIME(0, 0) blob = repr((password, notes)) cred_blob = ctypes.create_unicode_buffer(blob) cred_blob_size = ctypes.sizeof(cred_blob) cred_blob = LPBYTE(cred_blob) cred_persist = self.CRED_PERSIST_LOCAL_MACHINE cred_attribute_count = DWORD(1) logo_value = ctypes.cast(LPWSTR(logo), LPBYTE) cred_attributes = CredAttribute(LPWSTR(service + '_logo'), DWORD(0), ctypes.sizeof(logo_value), logo_value) cred_attributes = ctypes.pointer(cred_attributes) # I keep this alias to mark which credentials are created using # PassMan, the ones that aren't, won't be displayed. cred_target_alias = LPWSTR(self.alias) cred_username = LPWSTR(username) cred = Credential(cred_flags, cred_type, cred_target_name, cred_comment, cred_last_written, cred_blob_size, cred_blob, cred_persist, cred_attribute_count, cred_attributes, cred_target_alias, cred_username) self.advapi32.CredWriteW(ctypes.byref(cred), 0) item = CredItem(service, username) self.collection.items.append(item) return item
def get_mdate_ns(self): filetime = FILETIME(0xFFFFFFFF, 0xFFFFFFFF) if GetFileTime(self._hFile, NULL, NULL, byref(filetime)) == FALSE: raise WinError(get_last_error()) return FILETIME_to_time_ns(filetime)
def findFilesWithPattern(self, fileName, searchPattern, fillFindData, dokanFileInfo): """ Find files in a certain path that match the search pattern. :param fileName: path to search :type fileName: ctypes.c_wchar_p :param searchPattern: pattern to search for :type searchPattern: ctypes.c_wchar_p :param fillFindData: function pointer for populating search results :type fillFindData: PFillFindData :param dokanFileInfo: used by Dokan :type dokanFileInfo: PDOKAN_FILE_INFO :return: error code :rtype: ctypes.c_int """ try: ret = self.operations('findFilesWithPattern', fileName, searchPattern) if ret is None: return const.DOKAN_ERROR for r in ret: create_ft = self.python_timestamp_to_win32_filetime(r['ctime']) last_access_ft = self.python_timestamp_to_win32_filetime( r['atime']) last_write_ft = self.python_timestamp_to_win32_filetime( r['wtime']) cft = FILETIME(create_ft[0], create_ft[1]) laft = FILETIME(last_access_ft[0], last_access_ft[1]) lwft = FILETIME(last_write_ft[0], last_write_ft[1]) size = self.pyint_to_double_dwords(r['size']) File = WIN32_FIND_DATAW( ctypes.c_ulong(r['attr']), # attributes cft, # creation time laft, # last access time lwft, # last write time size[1], # upper bits of size size[0], # lower bits of size ctypes.c_ulong(0), # reserved for FS ctypes.c_ulong(0), # reserved for FS r['name'], # file name '') # alternate name pFile = PWIN32_FIND_DATAW(File) fillFindData(pFile, dokanFileInfo) return const.DOKAN_SUCCESS except Exception, e: logging.error('%s', e) return const.DOKAN_ERROR
def getFileInformation(self, fileName, buffer, dokanFileInfo): """ Get information about a file. :param fileName: name of file fetch information for :type fileName: ctypes.c_wchar_p :param buffer: buffer to fill with file information :type buffer: PBY_HANDLE_FILE_INFORMATION :param dokanFileInfo: used by Dokan :type dokanFileInfo: PDOKAN_FILE_INFO :return: error code :rtype: ctypes.c_int """ try: ret = self.operations('getFileInformation', fileName) if ret is None: return -const.ERROR_FILE_NOT_FOUND create_ft = self.python_timestamp_to_win32_filetime(ret['ctime']) last_access_ft = self.python_timestamp_to_win32_filetime(ret['atime']) last_write_ft = self.python_timestamp_to_win32_filetime(ret['wtime']) cft = FILETIME(create_ft[0], create_ft[1]) laft = FILETIME(last_access_ft[0], last_access_ft[1]) lwft = FILETIME(last_write_ft[0], last_write_ft[1]) size = self.pyint_to_double_dwords(ret['size']) _Buffer = BY_HANDLE_FILE_INFORMATION( ctypes.c_ulong(ret['attr']), # attributes cft, # creation time laft, # last access time lwft, # last write time ctypes.c_ulong(self.serialNumber), # serial number size[1], # size (upper bits) size[0], # size (lower bits) ctypes.c_ulong(1), # num links to this file ctypes.c_ulong(0), # index (upper) ctypes.c_ulong(0) ) # index (lower) ctypes.memmove( buffer, ctypes.byref(_Buffer), ctypes.sizeof(BY_HANDLE_FILE_INFORMATION) ) return const.DOKAN_SUCCESS except Exception, e: logging.error('%s', e) return -const.ERROR_FILE_NOT_FOUND
def time_ns_to_FILETIME(time_ns): """ Converts time_ns to FILETIME. FILETIME counts 100-nanosecond intervals since January 1, 1601. time_ns counts 1-nanosecond intervals since January 1, 1970. Reference: https://docs.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-filetime """ filetime = (time_ns + __NANOSECONDS_BETWEEN_EPOCHS) // 100 return FILETIME(filetime & 0xFFFFFFFF, filetime >> 32)
def wait4(pid, options=0): h = OpenProcess(PROCESS_QUERY_INFORMATION | SYNCHRONIZE, True, pid) WaitForSingleObject(h, INFINITE) exitcode = DWORD(-1) GetExitCodeProcess(h, byref(exitcode)) ctime, etime, stime, utime = FILETIME(), FILETIME(), FILETIME( ), FILETIME() GetProcessTimes(h, byref(ctime), byref(etime), byref(stime), byref(utime)) mem = PROCESS_MEMORY_COUNTERS(cb=sizeof(PROCESS_MEMORY_COUNTERS)) GetProcessMemoryInfo(h, byref(mem), sizeof(PROCESS_MEMORY_COUNTERS)) io = IO_COUNTERS() GetProcessIoCounters(h, byref(io)) CloseHandle(h) rusage = struct_rusage( ru_utime=ft2Sec(utime), ru_stime=ft2Sec(stime), ru_maxrss=mem.PeakWorkingSetSize // 1024, ru_ixrss=0L, ru_idrss=0L, ru_isrss=0L, ru_minflt=mem.PageFaultCount, ru_majflt=0L, ru_nswap=0L, ru_inblock=io.ReadOperationCount, ru_oublock=io.WriteOperationCount, ru_msgsnd=0L, ru_msgrcv=0L, ru_nsignals=0L, ru_nvcsw=0L, ru_nivcsw=0L, ) return pid, exitcode.value, rusage
def QueryInfoKey(key): """tuple = QueryInfoKey(key) - Returns information about a key. key is an already open key, or any one of the predefined HKEY_* constants. The result is a tuple of 3 items:" An integer that identifies the number of sub keys this key has. An integer that identifies the number of values this key has. A long integer that identifies when the key was last modified (if available) as 100's of nanoseconds since Jan 1, 1600. """ from cygwinreg3.w32api import RegQueryInfoKeyW num_sub_keys = DWORD() num_values = DWORD() last_write_time = FILETIME() wincall( RegQueryInfoKeyW(PyHKEY.make(key), None, None, None, byref(num_sub_keys), None, None, byref(num_values), None, None, None, byref(last_write_time))) last_write_time = (last_write_time.dwHighDateTime << 32 | last_write_time.dwLowDateTime) return (num_sub_keys.value, num_values.value, last_write_time)
def GetSystemTimeAsFileTime(): system_time = FILETIME() _GetSystemTimeAsFileTime(byref(system_time)) return filetime2py(system_time)