def clean_up(file_handle, volume_handle, tmp_file_path): try: if file_handle: CloseHandle(file_handle) if volume_handle: CloseHandle(volume_handle) if tmp_file_path: DeleteFile(tmp_file_path) except: pass
def close(self): if self._closed: return False self._closed = True CloseHandle(self._conin_pipe) CloseHandle(self._conout_pipe) if self._conerr_pipe: CloseHandle(self._conerr_pipe) winpty_free(self._pty)
def modifyFileTime(filePath, createTime, modifyTime, accessTime, offset): """ 用来修改任意文件的相关时间属性,时间格式:YYYY-MM-DD HH:MM:SS 例如:2019-02-02 00:01:02 :param filePath: 文件路径名 :param createTime: 创建时间 :param modifyTime: 修改时间 :param accessTime: 访问时间 :param offset: 时间偏移的秒数,tuple格式,顺序和参数时间对应 """ try: format = "%Y-%m-%d %H:%M:%S" # 时间格式 cTime_t = timeOffsetAndStruct(createTime, format, offset[0]) mTime_t = timeOffsetAndStruct(modifyTime, format, offset[1]) aTime_t = timeOffsetAndStruct(accessTime, format, offset[2]) fh = CreateFile(filePath, GENERIC_READ | GENERIC_WRITE, 0, None, OPEN_EXISTING, 0, 0) createTimes, accessTimes, modifyTimes = GetFileTime(fh) createTimes = Time(time.mktime(cTime_t)) accessTimes = Time(time.mktime(aTime_t)) modifyTimes = Time(time.mktime(mTime_t)) SetFileTime(fh, createTimes, accessTimes, modifyTimes) CloseHandle(fh) return 0 except: return 1
def readlink(path: str, *, dir_fd=None) -> str: """ Cross-platform implementation of readlink for Python < 3.8 Supports Windows NT symbolic links and reparse points. """ if sys.version_info >= (3, 8) or sys.platform != "win32": return os.readlink(path, dir_fd=dir_fd) if not os.path.exists(path): raise OSError(22, 'Invalid argument', path) elif islink(path): # may be a symbolic link. return os.readlink(path, dir_fd=dir_fd) if sys.platform == "win32": # FILE_FLAG_OPEN_REPARSE_POINT alone is not enough if 'path' # is a symbolic link to a directory or a NTFS junction. # We need to set FILE_FLAG_BACKUP_SEMANTICS as well. # See https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-createfilea handle = CreateFileW(path, GENERIC_READ, 0, None, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, 0) MAXIMUM_REPARSE_DATA_BUFFER_SIZE = 16 * 1024 buf = DeviceIoControl(handle, FSCTL_GET_REPARSE_POINT, None, MAXIMUM_REPARSE_DATA_BUFFER_SIZE) CloseHandle(handle) result = _parse_reparse_buffer(buf) if result['tag'] in (stat.IO_REPARSE_TAG_MOUNT_POINT, stat.IO_REPARSE_TAG_SYMLINK): offset = result['substitute_name_offset'] ending = offset + result['substitute_name_length'] rpath = result['buffer'][offset:ending].decode('UTF-16-LE') else: rpath = result['buffer'] if result['tag'] == stat.IO_REPARSE_TAG_MOUNT_POINT: rpath[:0] = '\\??\\' return rpath
def modify_file_all_time(filePath, createTime): """ 用来修改任意文件的相关时间属性,时间格式:YYYY-MM-DD HH:MM:SS 例如:2019-02-02 00:01:02 :param filePath: 文件路径名 :param createTime: 创建时间 :param modifyTime: 修改时间 :param accessTime: 访问时间 :param offset: 时间偏移的秒数,tuple格式,顺序和参数时间对应 """ print(filePath, createTime) try: format = "%Y-%m-%d %H:%M:%S" # 时间格式 fh = CreateFile(filePath, GENERIC_READ | GENERIC_WRITE, 0, None, OPEN_EXISTING, 0, 0) createTimes, accessTimes, modifyTimes = GetFileTime(fh) createTimes = Time(time.mktime(createTime)) accessTimes = Time(time.mktime(createTime)) modifyTimes = Time(time.mktime(createTime)) SetFileTime(fh, createTimes, accessTimes, modifyTimes) CloseHandle(fh) return 0 except Exception as e: print(e) return 1
def _preserve_timestamps(file_entry, output_path): """Obtain and set (to preserve) original timestamps of exported files.""" stat_object = file_entry.GetStat() if os.name == WINDOWS_IDENTIFIER: accessed = created = modified = dt.now() if stat_object.atime: if stat_object.atime_nano: accessed = dt.fromtimestamp((float(str(stat_object.atime) + '.' + str(stat_object.atime_nano)))) else: accessed = dt.fromtimestamp(stat_object.atime) if stat_object.crtime: if stat_object.crtime_nano: created = dt.fromtimestamp((float(str(stat_object.crtime) + '.' + str(stat_object.crtime_nano)))) else: created = dt.fromtimestamp(stat_object.crtime) if stat_object.mtime: if stat_object.mtime_nano: modified = dt.fromtimestamp((float(str(stat_object.mtime) + '.' + str(stat_object.mtime_nano)))) else: modified = dt.fromtimestamp(stat_object.mtime) handle = CreateFileW(output_path, GENERIC_WRITE, FILE_SHARE_WRITE, None, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, None) SetFileTime(handle, created, accessed, modified) # does not seem to preserve nano precision of timestamps CloseHandle(handle) else: os.utime(output_path, (stat_object.atime, stat_object.mtime))
def run(self): root = self.rootPath print("Start monitoring of '%s'" % root) hDir = CreateFile(root, FILE_LIST_DIRECTORY, FILE_SHARE_READ | FILE_SHARE_WRITE, None, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, None ) while not self._exit_request: changes = ReadDirectoryChangesW(hDir, 1024, True, FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME | FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_LAST_WRITE, None, None ) for action, file in changes: changed = join(root, file) if changed == self.trigger_file: continue self.changes.put((action, file)) print(changed, ACTIONS.get(action, "[unknown 0x%X]" % action) ) print("Stop monitoring of '%s'" % root) CloseHandle(hDir) self.onExit()
def close(self): try: DisconnectNamedPipe(self.pipe_handle) except Exception as e: log.error("Error: DisconnectNamedPipe(%s) %s", self.pipe_handle, e) try: CloseHandle(self.pipe_handle) except Exception as e: log.error("Error: CloseHandle(%s) %s", self.pipe_handle, e)
def do_run(self): while not self.exit_loop: pipe_handle = self.CreatePipeHandle() try: hr = ConnectNamedPipe(pipe_handle) assert hr in (0, winerror.ERROR_PIPE_CONNECTED), "ConnectNamedPipe returned %i" % hr except error as e: log.error("error connecting pipe: %s", e) CloseHandle(pipe_handle) break log("new client connected to pipe: %s", hr) if self.exit_loop: break if self.new_connection_cb: self.new_connection_cb(self, pipe_handle) else: log.warn("Warning: no callback defined for new named pipe connection on %s", self.pipe_name) CloseHandle(pipe_handle)
def TerminateExtension(self, status): for worker in self.workers: worker.running = False for worker in self.workers: PostQueuedCompletionStatus(self.io_req_port, 0, ISAPI_SHUTDOWN, None) for worker in self.workers: worker.join(self.worker_shutdown_wait) self.dispatch_map = {} # break circles CloseHandle(self.io_req_port)
def deinit(self): import pythoncom from win32file import CloseHandle for h in self.handles: if h is not None: CloseHandle(h) self.handles = None pythoncom.CoUninitialize() pass
def file_name(file_dir): for root, dirs, files in os.walk(file_dir): for filename in files: fh = CreateFile(filename, GENERIC_READ | GENERIC_WRITE, 0, None, OPEN_EXISTING, 0, 0) createTime, accessTime, modifyTime = GetFileTime(fh) createTime = Time(time.mktime(time.gmtime(random.uniform(1514780000,1546300000)))) accessTime = Time(time.mktime(time.gmtime(random.uniform(1514780000,1546300000)))) modifyTime = Time(time.mktime(time.gmtime(random.uniform(1514780000,1546300000)))) SetFileTime(fh, createTime, accessTime, modifyTime) CloseHandle(fh)
def close(self, silent=True): # pylint disable=W0613 CloseHandle(self.fd) self.fd = None self.r_terminator.shutdown(socket.SHUT_RDWR) self.r_terminator.close() self.w_terminator.close() self.r_terminator = self.w_terminator = None
def set_mp4_timestamp(path): '''用mvhd中的creation_time和modification_time设置mp4文件的创建时间和修改时间。 如果目标时间与当前文件的创建时间一致(时间差小于1s)则不修改。 如果creation_time和modification_time之中有1个为0,则用另一个赋值,都为零则不修改文件时间。 ''' try: mvhd = get_mvhd(path) except ValueError as error: print(f'When processing {path} a error occurs: {error}. Skip it.') return # mvhd is FullBox,是Box的扩展,Box结构的基础上在Header中增加8bits version和24bits flags try: version = mvhd[8:9] if version == b'\x00': creation_time = struct.unpack('>I', mvhd[12:16])[0] modification_time = struct.unpack('>I', mvhd[16:20])[0] elif version == b'\x01': creation_time = struct.unpack('>Q', mvhd[12:20])[0] modification_time = struct.unpack('>Q', mvhd[20:28])[0] except (IndexError, struct.error): print( f'{path} contains no valid mvhd box. May not be a valid mp4 file. Skip it.' ) return if creation_time + modification_time == 0: print( f'{path} contains no creation_time or modification_time. Skip it.') return if creation_time == 0: creation_time = modification_time if modification_time == 0: modification_time = creation_time new_ctimestamp = time.mktime(get_datetime(creation_time).timetuple()) new_mtimestamp = time.mktime(get_datetime(modification_time).timetuple()) mod_flag = False old_mtimestamp = getmtime(path) mtime = Time(old_mtimestamp) if abs(old_mtimestamp - new_mtimestamp) >= 1: mtime = Time(new_mtimestamp) mod_flag = True old_ctimestamp = getctime(path) ctime = Time(old_ctimestamp) if abs(old_ctimestamp - new_ctimestamp) >= 1: ctime = Time(new_ctimestamp) mod_flag = True if mod_flag: handle = CreateFile(path, GENERIC_WRITE, FILE_SHARE_WRITE, None, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, None) SetFileTime(handle, ctime, None, mtime) CloseHandle(handle)
def build_config(root): curr_path = os.path.dirname(os.path.abspath(os.path.expanduser(__file__))) save_path = os.path.join(curr_path, "save") hasher0 = hashlib.sha1() hasher0.update(root.encode("utf-8")) save_path = os.path.join(save_path, str(hasher0.hexdigest())) if not os.path.exists(save_path): os.mkdir(save_path) save_build_config = os.path.join(save_path, "build_config.h") build_config = os.path.join( root, "mxnet/3rdparty/dmlc-core/include/dmlc/build_config.h") if not os.path.exists(build_config): return if not os.path.exists(save_build_config): copyfile(build_config, save_build_config) hasher1 = hashlib.sha512() with open(build_config, 'rb') as afile: buf = afile.read() hasher1.update(buf) hasher2 = hashlib.sha512() with open(save_build_config, 'rb') as afile: buf = afile.read() hasher2.update(buf) if hasher1.hexdigest() == hasher2.hexdigest(): fh = CreateFile(save_build_config, GENERIC_READ | GENERIC_WRITE, 0, None, OPEN_EXISTING, 0, 0) create_times, access_times, modify_times = GetFileTime(fh) CloseHandle(fh) fh = CreateFile(build_config, GENERIC_READ | GENERIC_WRITE, 0, None, OPEN_EXISTING, 0, 0) SetFileTime(fh, create_times, access_times, modify_times) CloseHandle(fh) else: copyfile(build_config, save_build_config)
def mod_ctime(path): """ increments file creation time by a nanosecond parameters: string path, int mod_size | return: none """ ctime = os.path.getctime(path) + 0.001 # increment ctime by 0.001 fh = CreateFile(path, GENERIC_WRITE, FILE_SHARE_WRITE, None, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0) SetFileTime(fh, datetime.datetime.fromtimestamp(ctime), None, None) # update file ctime CloseHandle(fh)
def is_running(self): pid = self.state.get('pid') if not pid: return False try: handle = OpenProcess(SYNCHRONIZE, 0, pid) CloseHandle(handle) return True except Win32Error: return False
def spike_cluster(volume_handle, cluster, tmp_file_path): spike_file_path = os.path.dirname(tmp_file_path) if spike_file_path[-1] != os.sep: spike_file_path += os.sep spike_file_path += spike_file_name + str(cluster) file_handle = CreateFile(spike_file_path, GENERIC_READ | GENERIC_WRITE, 0, None, CREATE_ALWAYS, 0, None) # 2000 bytes is enough to direct the file to its own cluster and not # land entirely in the MFT. write_zero_fill(file_handle, 2000) move_file(volume_handle, file_handle, 0, cluster, 1) CloseHandle(file_handle) logger.debug("Spiked cluster %d with %s" % (cluster, spike_file_path))
def spike_cluster(volume_handle, cluster, tmp_file_path): spike_file_path = os.path.join(os.path.dirname(tmp_file_path), spike_file_name + str(cluster)) file_handle = CreateFile( spike_file_path, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, None, CREATE_ALWAYS, 0, None) # 2000 bytes is enough to direct the file to its own cluster and not # land entirely in the MFT. write_zero_fill(file_handle, 2000) move_file(volume_handle, file_handle, 0, cluster, 1) CloseHandle(file_handle) logging.debug("Spiked cluster %d with %s" % (cluster, spike_file_path))
def create_file(file_name, c_file, time, a_path, ftp): current_file = open(file_name, "wb") ftp.retrbinary("RETR " + a_path + "/" + c_file, current_file.write) print('\tAdded:', file_name) current_file.close() win_file = CreateFile( file_name, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, None, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, None) # noinspection PyUnresolvedReferences win_time = Time(time) SetFileTime(win_file, win_time, None, None) CloseHandle(win_file)
def close(self): if self._closed: return False self._closed = True # Ensure _conin_pipe empty if self._process_handle: try: TerminateProcess(self._process_handle, 0) except: pass while True: try: error, data = ReadFile(self._conout_pipe, 8192) if not data: break except: break if self._conerr_pipe: while True: try: error, data = ReadFile(self._conerr_pipe, 8192) if not data: break except: break CloseHandle(self._conin_pipe) CloseHandle(self._conout_pipe) if self._conerr_pipe: CloseHandle(self._conerr_pipe) winpty_free(self._pty)
def change_f_time(f, f_create_time, f_access_time, f_modify_time): """ 修改文件或文件夹的时间 f: 文件/文件夹的路径 f_create_time, f_access_time, f_modify_time: 文件/文件夹的创建时间、访问时间、修改时间 """ f_type = 0 if os.path.isfile(f) else FILE_FLAG_BACKUP_SEMANTICS file_handle = CreateFile(f, GENERIC_READ | GENERIC_WRITE, 0, None, OPEN_EXISTING, f_type, 0) # todo: f_type=0表示创建文件,=FILE_FLAG_BACKUP_SEMANTICS 表示创建文件夹! f_create_time = Time(f_create_time) f_access_time = f_create_time f_modify_time = Time(f_modify_time) SetFileTime(file_handle, f_create_time, f_access_time, f_modify_time) # createTimes, accessTimes, modifyTimes CloseHandle(file_handle)
def close(self): def _close_err(fn, e): l = log.error code = e[0] if code==winerror.ERROR_PIPE_NOT_CONNECTED: l = log.debug l("Error: %s(%s) %i: %s", fn, self.pipe_handle, code, e) try: DisconnectNamedPipe(self.pipe_handle) except Exception as e: _close_err("DisconnectNamedPipe", e) try: CloseHandle(self.pipe_handle) except Exception as e: _close_err("CloseHandle", e) self.pipe_handle = None
def set_time(path, ctime=None, atime=None, mtime=None): """ changes file times to input times parameters: string path, int ctime | return: none """ # get datetime value of time if time has a value if ctime != None: ctime = datetime.datetime.fromtimestamp(ctime) if atime != None: atime = datetime.datetime.fromtimestamp(atime) if mtime != None: mtime = datetime.datetime.fromtimestamp(mtime) fh = CreateFile(path, GENERIC_WRITE, FILE_SHARE_WRITE, None, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0) SetFileTime(fh, ctime, atime, mtime) # update file times (None value keeps previous value) CloseHandle(fh)
def TerminateExtension(self, status): for worker in self.workers: worker.running = False for worker in self.workers: PostQueuedCompletionStatus(self.io_req_port, 0, ISAPI_SHUTDOWN, None) # wait for them to terminate - pity we aren't using 'native' threads # as then we could do a smart wait - but now we need to poll.... end_time = time.time() + self.worker_shutdown_wait/1000 alive = self.workers while alive: if time.time() > end_time: # xxx - might be nice to log something here. break time.sleep(0.2) alive = [w for w in alive if w.is_alive()] self.dispatch_map = {} # break circles CloseHandle(self.io_req_port)
def randomizeFileTime( file ): #uses os.utime to set a+m times and pywin_cstructs to set file creation times name = os.path.basename(file) rd = getRandomDate() at = random.randint(200000000, 1000050000) mt = random.randint(200000000, 1000050000) os.utime(file, (at, mt)) ctimeform = "%d.%m.%Y %H:%M:%S" off = 0 ct = time.localtime(time.mktime(time.strptime( rd, ctimeform))) #prepares time in format specified tmp = CreateFile(file, GENERIC_READ | GENERIC_WRITE, 0, None, OPEN_EXISTING, 0, 0) newct = Time(time.mktime(ct)) SetFileTime(tmp, newct) CloseHandle(tmp) print(name)
def modify_model_file_time(model_file_dir): for root, dirs, files in os.walk(model_file_dir): start_time=1514780000 latest_time=1546300000 for filename in files: if filename!='untitled1.py': fh = CreateFile(filename, GENERIC_READ | GENERIC_WRITE, 0, None, OPEN_EXISTING, 0, 0) createTime, accessTime, modifyTime = GetFileTime(fh) start=random.uniform(start_time,latest_time) createTime = Time(time.mktime(time.gmtime(start))) modify_time=start+random.uniform(0,latest_time-start) modifyTime = Time(time.mktime(time.gmtime(modify_time))) access_time=modify_time+random.uniform(0,latest_time-modify_time) accessTime = Time(time.mktime(time.gmtime(access_time))) SetFileTime(fh, createTime, accessTime, modifyTime) CloseHandle(fh)
def change_file_time(path, delta): if not os.path.exists(path): log_message("Pfad: " + path + " existiert nicht!", "info") return if platform.system() == "Windows": # modify filetimes on Windows fh = CreateFile(path, GENERIC_WRITE, FILE_SHARE_WRITE, None, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0) cTime, aTime, mTime = GetFileTime(fh) cTime = datetime.fromtimestamp(cTime.timestamp() - delta) aTime = datetime.fromtimestamp(aTime.timestamp() - delta) mTime = datetime.fromtimestamp(mTime.timestamp() - delta) SetFileTime(fh, cTime, aTime, mTime) CloseHandle(fh) else: # modify filetimes on Linux/Mac a_time = os.path.getatime(path) m_time = os.path.getmtime(path) a_time = a_time - delta m_time = m_time - delta os.utime(path, (a_time, m_time))
def modify_file_all_time(file_path, create_time): """ 用来修改任意文件的相关时间属性,时间格式:YYYY-MM-DD HH:MM:SS 例如:2019-02-02 00:01:02 :param file_path: 文件路径名 :param create_time: 创建时间 :param modifyTime: 修改时间 :param accessTime: 访问时间 :param offset: 时间偏移的秒数,tuple格式,顺序和参数时间对应 """ print(file_path, create_time) try: fh = CreateFile(file_path, GENERIC_READ | GENERIC_WRITE, 0, None, OPEN_EXISTING, 0, 0) final_create_time = Time(time.mktime(create_time)) final_access_time = Time(time.mktime(create_time)) final_modify_time = Time(time.mktime(create_time)) SetFileTime(fh, final_create_time, final_access_time, final_modify_time) CloseHandle(fh) return 0 except Exception as e: print(e) return 1
# change timestamp of file fh = CreateFile(fName, GENERIC_READ | GENERIC_WRITE, 0, None, OPEN_EXISTING, 0, 0) createTime, accessTime, modifyTime = GetFileTime(fh) print("Change Create from", createTime, "to %s" % (time.strftime(format, cTime_t))) print("Change Modify from", modifyTime, "to %s" % (time.strftime(format, mTime_t))) print("Change Access from", accessTime, "to %s" % (time.strftime(format, aTime_t))) print() createTime = Time(time.mktime(cTime_t)) accessTime = Time(time.mktime(aTime_t)) modifyTime = Time(time.mktime(mTime_t)) SetFileTime(fh, createTime, accessTime, modifyTime) CloseHandle(fh) # check if all was ok ctime = time.strftime(format, time.localtime(os.path.getctime(fName))) mtime = time.strftime(format, time.localtime(os.path.getmtime(fName))) atime = time.strftime(format, time.localtime(os.path.getatime(fName))) print("CHECK MODIFICATION:") print("FileName: %s" % fName) print("Create : %s" % (ctime)) print("Modify : %s" % (mtime)) print("Access : %s" % (atime)) # from zhihu