def setatime(filepath, timestamp): """Set the "atime" (accessed time) attribute of a file given an unix timestamp (Windows only).""" if not SUPPORTED: raise OSError( "This function is only available for the Windows platform.") filepath = os.path.normpath(os.path.abspath(str(filepath))) timestamp = int((timestamp * 10000000) + 116444736000000000) if not 0 < timestamp < (1 << 64): raise ValueError( "The system value of the timestamp exceeds u64 size: %d" % timestamp) atime = wintypes.FILETIME(timestamp & 0xFFFFFFFF, timestamp >> 32) mtime = wintypes.FILETIME(0xFFFFFFFF, 0xFFFFFFFF) ctime = wintypes.FILETIME(0xFFFFFFFF, 0xFFFFFFFF) handle = wintypes.HANDLE(CreateFileW(filepath, 256, 0, None, 3, 128, None)) if handle.value == wintypes.HANDLE(-1).value: raise WinError() if not wintypes.BOOL( SetFileTime(handle, byref(ctime), byref(atime), byref(mtime))): raise WinError() if not wintypes.BOOL(CloseHandle(handle)): raise WinError()
def callback(smb_fs: smbfs.SMBFS): for walk_path in smb_fs.walk.dirs(path, max_depth=1): if (smb_fs.exists(walk_path)): info = smb_fs.getinfo(walk_path) find_data = wintypes.WIN32_FIND_DATAW() find_data.dwFileAttributes = 16 find_data.cFileName = info.name argus[1](pointer(find_data), argus[2]) for walk_path in smb_fs.walk.files(path, max_depth=1): if (smb_fs.exists(walk_path)): info = smb_fs.getinfo(walk_path) filesize = smb_fs.getsize(walk_path) find_data = wintypes.WIN32_FIND_DATAW() find_data.dwFileAttributes = 32 find_data.cFileName = info.name if (len(info.name) >= 11): info_cut = info.name.split(".") cAlternateFileName = info_cut[0][ 0:6] + "~1." + info.suffix[0:3] else: find_data.cAlternateFileName = info.name find_data.ftCreationTime = wintypes.FILETIME() find_data.ftLastAccessTime = wintypes.FILETIME() find_data.ftLastWriteTime = wintypes.FILETIME() find_data.nFileSizeHigh = wintypes.DWORD(0) find_data.nFileSizeLow = wintypes.DWORD(filesize) find_data.dwReserved0 = wintypes.DWORD(0) find_data.dwReserved1 = wintypes.DWORD(0) argus[1](pointer(find_data), argus[2])
def setCreationTime(filepath, epochtime): # Convert Unix timestamp to Windows FileTime using some magic numbers # See documentation: https://support.microsoft.com/en-us/help/167296 timestamp = int((epochtime * 10000000) + 116444736000000000) ctime = wintypes.FILETIME(timestamp & 0xFFFFFFFF, timestamp >> 32) # Call Win32 API to modify the file creation date handle = windll.kernel32.CreateFileW(filepath, 256, 0, None, 3, 128, None) windll.kernel32.SetFileTime(handle, byref(ctime), None, None) #(handle, ctime, atime, mtime) windll.kernel32.CloseHandle(handle)
def GetFileInformation_handle(self, *argus): path = self.get_path_from_dokan_path(argus[0]) # print("GetFileInformation_handle") # print(path) if (not self.path_is_exists(path)): return ntstatus.STATUS_SUCCESS filesize = None def callback(smb_fs: smbfs.SMBFS): nonlocal filesize filesize = smb_fs.getsize(path) self.get_fs(callback) if (self.path_is_file(path)): argus[1].contents.dwFileAttributes = wintypes.DWORD(32) else: argus[2].contents.IsDirectory = c_ubyte(True) argus[1].contents.dwFileAttributes = wintypes.DWORD(16) argus[1].contents.ftCreationTime = wintypes.FILETIME() argus[1].contents.ftLastAccessTime = wintypes.FILETIME() argus[1].contents.ftLastWriteTime = wintypes.FILETIME() argus[1].contents.nFileSizeHigh = wintypes.DWORD(0) argus[1].contents.nFileSizeLow = wintypes.DWORD(filesize) return ntstatus.STATUS_SUCCESS
def changeCreationDate(self): now = datetime.datetime.utcnow() nowUnix = (now - datetime.datetime(1970,1,1)).total_seconds() timestamp = int((nowUnix * 10000000) + 116444736000000000) if not 0 < timestamp < (1 << 64): raise ValueError('Timestamp ist groeßer als 64-bit: ' + str(self.path)) ctime = wintypes.FILETIME(timestamp & 0xFFFFFFFF, timestamp >> 32) handle = windll.kernel32.CreateFileW(str(self.path), 256, 0, None, 3, 128, None) if not wintypes.BOOL(windll.kernel32.SetFileTime(handle, byref(ctime), None, None)): raise WinError() if not wintypes.BOOL(windll.kernel32.CloseHandle(handle)): raise WinError() print('Erstellungsdatum geaendert: ' + str(self.path))