Ejemplo n.º 1
0
def _stub(cmd_name, stdin_name, stdout_name, stderr_name):
    """INTERNAL: Stub process that will start up the child process."""
    # Open the 4 pipes (command, stdin, stdout, stderr)
    cmd_pipe = CreateFile(cmd_name, GENERIC_READ|GENERIC_WRITE, 0, None,
                          OPEN_EXISTING, 0, None)
    SetHandleInformation(cmd_pipe, HANDLE_FLAG_INHERIT, 1)
    stdin_pipe = CreateFile(stdin_name, GENERIC_READ, 0, None,
                            OPEN_EXISTING, 0, None)
    SetHandleInformation(stdin_pipe, HANDLE_FLAG_INHERIT, 1)
    stdout_pipe = CreateFile(stdout_name, GENERIC_WRITE, 0, None,
                             OPEN_EXISTING, 0, None)
    SetHandleInformation(stdout_pipe, HANDLE_FLAG_INHERIT, 1)
    stderr_pipe = CreateFile(stderr_name, GENERIC_WRITE, 0, None,
                             OPEN_EXISTING, 0, None)
    SetHandleInformation(stderr_pipe, HANDLE_FLAG_INHERIT, 1)

    # Learn what we need to do..
    header = _read_header(cmd_pipe)
    input = _parse_header(header)
    if 'command' not in input or 'args' not in input:
        ExitProcess(2)

    # http://msdn.microsoft.com/en-us/library/ms682499(VS.85).aspx
    startupinfo = STARTUPINFO()
    startupinfo.dwFlags |= STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW
    startupinfo.hStdInput = stdin_pipe
    startupinfo.hStdOutput = stdout_pipe
    startupinfo.hStdError = stderr_pipe
    startupinfo.wShowWindow = SW_HIDE

    # Grant access so that our parent can open its grandchild.
    if 'parent_sid' in input:
        mysid = _get_current_sid()
        parent = ConvertStringSidToSid(input['parent_sid'])
        sattrs = _create_security_attributes(mysid, parent,
                                             access=PROCESS_ALL_ACCESS)
    else:
        sattrs = None

    try:
        res = CreateProcess(input['command'], input['args'], sattrs, None,
                            True, CREATE_NEW_CONSOLE, os.environ, os.getcwd(),
                            startupinfo)
    except WindowsError as e:
        message = _quote_header(str(e))
        WriteFile(cmd_pipe, 'status=error\nmessage=%s\n\n' % message)
        ExitProcess(3)
    else:
        pid = res[2]

    # Pass back results and exit
    err, nbytes = WriteFile(cmd_pipe, 'status=ok\npid=%s\n\n' % pid)
    ExitProcess(0)
Ejemplo n.º 2
0
def obtain_readwrite(volume):
    # Optional protection that we are running on removable media only.
    assert volume
    #if drive_letter_safety:
    #    drive_containing_file = volume[0].upper()
    #    assert drive_containing_file >= drive_letter_safety.upper()

    volume = '\\\\.\\' + volume
    if volume[-1] == os.sep:
        volume = volume.rstrip(os.sep)

    # We need the FILE_SHARE flags so that this open call can succeed
    # despite something on the volume being in use by another process.
    #다른 프로세스에서 사용 중인 볼륨에서도 이 개방형 호출을 성공할 수 있도록 FILE_SHARE 플래그가 필요함.
    #볼륨의 정보를 CreateFile를 통해 초기화 
    volume_handle = CreateFile(volume, GENERIC_READ | GENERIC_WRITE,
                               FILE_SHARE_READ | FILE_SHARE_WRITE,
                               None, OPEN_EXISTING,
                               FILE_FLAG_RANDOM_ACCESS |
                               FILE_FLAG_NO_BUFFERING |
                               FILE_FLAG_WRITE_THROUGH,
                               None)
    #logger.debug("Opened volume %s", volume)

    #volume_handle반환 
    return volume_handle
Ejemplo n.º 3
0
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
Ejemplo n.º 4
0
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
Ejemplo n.º 5
0
    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()
Ejemplo n.º 6
0
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)
Ejemplo n.º 7
0
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)
Ejemplo n.º 8
0
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)
Ejemplo n.º 9
0
 def _open_device(self):
     try:
         self.fd = CreateFile(self.path,
                              FILE_GENERIC_READ | FILE_GENERIC_WRITE, 0,
                              None, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
                              None)
     except Exception as exc:
         self._logger.exception('Exception opening GPLPV device:')
         six.raise_from(
             GPLPVDeviceOpenError("Error while opening {0!r}".format(
                 self.path)), exc)
Ejemplo n.º 10
0
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)
Ejemplo n.º 11
0
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))
Ejemplo n.º 12
0
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))
Ejemplo n.º 13
0
    def __init__(self):
        QtCore.QThread.__init__(self)
        self.path_to_watch = DEFAULT_PATH

        self.hDir = CreateFile (
            self.path_to_watch,
            0x0001, # dunno, magic. FILE_LIST_DIRECTORY
            win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE | win32con.FILE_SHARE_DELETE,
            None,
            win32con.OPEN_EXISTING,
            win32con.FILE_FLAG_BACKUP_SEMANTICS,
            None
        )
Ejemplo n.º 14
0
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)
Ejemplo n.º 15
0
def change_file_creation_time(log, path, time):
    """ Change the creation time for a file.
    'path' is the full path to the file.
    'time' is the new windows time to set the file creation time to.
    """

    log.trace("Changing creation time for %s to %s ..." % (path, time))
    result = {}

    # open file
    winfile = CreateFile(
        path, GENERIC_WRITE,
        FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
        None, OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL, None)

    # set new creation time
    SetFileTime(winfile, time, None, None)

    # close file
    winfile.close()

    # return
    return result
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)
Ejemplo n.º 17
0
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)
Ejemplo n.º 18
0
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)
Ejemplo n.º 19
0
    def get_serial_ports():
        success = False
        ports = []
        global wmi

        # try WMI first
        try:
            if wmi is None:
                wmi = win32com.client.GetObject('winmgmts:')

            for port in wmi.InstancesOf('Win32_SerialPort'):
                ports.append((port.DeviceID, port.Name, ''))

            success = True
        except:
            pass

        if success:
            return ports

        ports = []

        # fallback to simple filename probing, if WMI fails
        for i in range(1, 256):
            # FIXME: get friendly names
            name = 'COM%u' % i
            try:
                hFile = CreateFile(
                    '\\\\.\\' + name,
                    win32con.GENERIC_READ | win32con.GENERIC_WRITE, 0, None,
                    win32con.OPEN_EXISTING, 0, None)
                CloseHandle(hFile)
                ports.append((name, name, name))
            except pywintypes.error as e:
                if e[0] in [
                        winerror.ERROR_ACCESS_DENIED,
                        winerror.ERROR_GEN_FAILURE,
                        winerror.ERROR_SHARING_VIOLATION,
                        winerror.ERROR_SEM_TIMEOUT
                ]:
                    ports.append((name, name, name))

        return ports
Ejemplo n.º 20
0
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))
Ejemplo n.º 21
0
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)
Ejemplo n.º 22
0
    def _transact(self, query):

        try:
            h = CreateFile(
                self.pipe_name, GENERIC_READ|GENERIC_WRITE, 0, None, OPEN_EXISTING, 0, None
            )

            packet = self._construct_query(query)
            WriteFile(h, packet)
            read = 0
            err, out = ReadFile(h, DI_PIPE_BUF_SIZE)
            length = struct.unpack('i', out[:SIZEOF_INT])[0]

            while len(out[SIZEOF_INT:]) < length:
                out += ReadFile(h, DI_PIPE_BUF_SIZE)[1]

            data = json.loads(out[SIZEOF_INT:], encoding='utf-8')

        except pywintypes.error as e:
            print e

        else:
            return data
Ejemplo n.º 23
0
def exact(filePath):
    starttime = time.time()
    unZf = zipfile.ZipFile(filePath, 'r')
    shutil.rmtree(myconfig['save_path'],ignore_errors=True)
    time.sleep(0.1)
    # os.makedirs(save_folder, exist_ok=True)
    # unZf.extractall(path=save_folder,members=unZf.namelist())


    for name in unZf.namelist():

        unZfTarge = os.path.join(save_folder, name)
        dirname = os.path.dirname(unZfTarge)
        os.makedirs(dirname, exist_ok=True)
        with open(unZfTarge, 'wb') as f:
            f.write(unZf.read(name))
        fh = CreateFile(unZfTarge, GENERIC_READ | GENERIC_WRITE, 0, None, OPEN_EXISTING, 0, 0)
        #伪造写入时间
        createtime = unZf.getinfo(name).date_time
        time_formated = time.strptime(str(createtime), "(%Y, %m, %d, %H, %M, %S)")
        time_win = Time(time.mktime(time_formated))
        SetFileTime(fh,time_win,time_win,time_win)
    unZf.close()
    print(f"解压用时{time.time() - starttime}")
Ejemplo n.º 24
0
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
def set_jpg_timestamp(path):
    '''用保存在EXIF中的拍摄时间设置图片的创建时间和修改时间。

    如果目标时间与当前文件的创建时间一致(时间差小于1s)则不修改。
    '''
    f = open(path, 'rb')
    tags = exifread.process_file(f, details=False)
    f.close()

    # new_ctimestamp1 for timestamp found in file tags, new_ctimestamp2 for timestamp found in file name.
    new_ctimestamp1 = None
    new_ctimestamp2 = None

    file_name = split(path)[1]
    print(f' [{file_name}]')

    # 尝试获取'Image DateTime'的时间
    try:
        new_ctimestamp1 = time.mktime(time.strptime(
            tags['Image DateTime'].values, '%Y:%m:%d %H:%M:%S'))
        print('  └─Found "Image DateTime"', end='')
    except KeyError:
        print('  └─"Image DateTime" not found, try next method.')
    except ValueError:
        print('  └─"Image DateTime" is not valid, try next method.')

    # 尝试获取'EXIF DateTimeOriginal'的时间
    if not new_ctimestamp1:
        try:
            new_ctimestamp1 = time.mktime(time.strptime(
                tags['EXIF DateTimeOriginal'].values, '%Y:%m:%d %H:%M:%S'))
            print('  └─Found "EXIF DateTimeOriginal"', end='')
        except KeyError:
            print('  └─"EXIF DateTimeOriginal" not found, try next method.')
        except ValueError:
            print('  └─"EXIF DateTimeOriginal" is not valid, try next method.')

    # 尝试获取'EXIF DateTimeDigitized'的时间
    if not new_ctimestamp1:
        try:
            new_ctimestamp1 = time.mktime(time.strptime(
                tags['EXIF DateTimeDigitized'].values, '%Y:%m:%d %H:%M:%S'))
            print('  └─Found "EXIF DateTimeDigitized"', end='')
        except KeyError:
            print('  └─"EXIF DateTimeDigitized" not found, try next method.')
        except ValueError:
            print('  └─"EXIF DateTimeDigitized" is not valid, try next method.')

    # 尝试从文件名获取时间,文件名需形如'XXX_YYYYmmdd_HHMMSS(_XXXXX).jp(e)g'
    file_name_no_ext = splitext(split(path)[1])[0]
    try:
        time_strs = file_name_no_ext.split('_')[1:3]
        new_ctimestamp2 = time.mktime(time.strptime(
            ''.join(time_strs), '%Y%m%d%H%M%S'))
        if new_ctimestamp1:
            print(' and file name contains timestamp', end='')
        else:
            print('  └─File name contains timestamp', end='')
    except (ValueError, OverflowError):
        if not new_ctimestamp1:
            print(
                '  └─File name dose not match XXX_YYYYmmdd_HHMMSS(_XXXXX).jp(e)g format.')

    if new_ctimestamp1 or new_ctimestamp2:
        if new_ctimestamp1 is None:
            new_ctimestamp = new_ctimestamp2
        elif new_ctimestamp2 is None:
            new_ctimestamp = new_ctimestamp1
        else:
            new_ctimestamp = new_ctimestamp1 if new_ctimestamp1 < new_ctimestamp2 else new_ctimestamp2
        old_ctimestamp = getctime(path)

        if abs(old_ctimestamp - new_ctimestamp) >= 1:
            ctime = Time(new_ctimestamp)
            try:
                handle = CreateFile(path, GENERIC_WRITE,
                                    FILE_SHARE_WRITE, None, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, None)
                SetFileTime(handle, ctime, None, ctime)
                CloseHandle(handle)
                print('. Done.')
            except:
                print(
                    '. Can not change ctime and mtime. Make sure you have right permission. Skip it.')
        else:
            print('. No need to change. Skip it.')
    else:
        print('  └─No valid timestamp found. Skip it.')
Ejemplo n.º 26
0
def edit_file_list_time(c_time, dir_path, file_name_list):
    # get arguments
    # c_time = "08.10.2018 15:34:44"
    m_time = c_time
    a_time = c_time

    time_format = "%d.%m.%Y %H:%M:%S"
    c_time_stripe = time.strptime(c_time, time_format)
    m_time_stripe = time.strptime(m_time, time_format)
    a_time_stripe = time.strptime(a_time, time_format)

    c_time_second = time.mktime(c_time_stripe)
    m_time_second = time.mktime(m_time_stripe)
    a_time_second = time.mktime(a_time_stripe)

    for fileName in file_name_list:
        # f_name = "h:\Downloads\待处理文件\20180702.xls"
        f_name = dir_path + fileName
        # specify time format
        offset = random.randint(60, 150)  # in seconds

        # create struct_time object
        c_time_second = c_time_second + offset
        m_time_second = m_time_second + offset
        a_time_second = a_time_second + offset

        c_time_t = time.localtime(c_time_second)
        m_time_t = time.localtime(m_time_second)
        a_time_t = time.localtime(a_time_second)

        # visually check if conversion was ok
        print()
        print("FileName: %s" % f_name)
        print("Create  : %s --> %s OK" %
              (c_time, time.strftime(time_format, c_time_t)))
        print("Modify  : %s --> %s OK" %
              (m_time, time.strftime(time_format, m_time_t)))
        print("Access  : %s --> %s OK" %
              (a_time, time.strftime(time_format, a_time_t)))
        print()

        # change timestamp of file
        fh = CreateFile(f_name, GENERIC_READ | GENERIC_WRITE, 0, None,
                        OPEN_EXISTING, 0, 0)
        create_time, access_time, modify_time = GetFileTime(fh)
        print("Change Create from", create_time,
              "to %s" % (time.strftime(time_format, c_time_t)))
        print("Change Modify from", modify_time,
              "to %s" % (time.strftime(time_format, m_time_t)))
        print("Change Access from", access_time,
              "to %s" % (time.strftime(time_format, a_time_t)))
        print()

        create_time = Time(time.mktime(c_time_t))
        access_time = Time(time.mktime(a_time_t))
        modify_time = Time(time.mktime(m_time_t))
        SetFileTime(fh, create_time, access_time, modify_time)
        CloseHandle(fh)

        # check if all was ok
        ctime = time.strftime(time_format,
                              time.localtime(os.path.getctime(f_name)))
        mtime = time.strftime(time_format,
                              time.localtime(os.path.getmtime(f_name)))
        atime = time.strftime(time_format,
                              time.localtime(os.path.getatime(f_name)))

        print("CHECK MODIFICATION:")
        print("FileName: %s" % f_name)
        print("Create  : %s" % ctime)
        print("Modify  : %s" % mtime)
        print("Access  : %s" % atime)
Ejemplo n.º 27
0
# create struct_time object
cTime_t = time.localtime(time.mktime(time.strptime(cTime, format)) + offset)
mTime_t = time.localtime(time.mktime(time.strptime(mTime, format)) + offset)
aTime_t = time.localtime(time.mktime(time.strptime(aTime, format)) + offset)

# visually check if conversion was ok
print()
print("FileName: %s" % fName)
print("Create  : %s --> %s OK" % (cTime, time.strftime(format, cTime_t)))
print("Modify  : %s --> %s OK" % (mTime, time.strftime(format, mTime_t)))
print("Access  : %s --> %s OK" % (aTime, time.strftime(format, aTime_t)))
print()

# 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)
Ejemplo n.º 28
0
def wipe_extent_by_defrag(volume_handle, lcn_start, lcn_end, cluster_size,
                          total_clusters, tmp_file_path):
    assert cluster_size > 0
    logger.debug("Examining extent from %d to %d for wipe...", lcn_start,
                 lcn_end)
    write_length = (lcn_end - lcn_start + 1) * cluster_size

    # Check the state of the volume bitmap for the extent we want to
    # overwrite. If any sectors are allocated, reduce the task
    # into smaller parts.
    # We also reduce to smaller pieces if the extent is larger than
    # 2 megabytes. For no particular reason except to avoid the entire
    # request failing because one cluster became allocated.
    volume_bitmap, bitmap_size = get_volume_bitmap(volume_handle,
                                                   total_clusters)
    # This option simulates another process that grabs clusters on disk
    # from time to time.
    # It should be moved away after QA is complete.
    if not simulate_concurrency:
        count_free, count_allocated = check_extents([(lcn_start, lcn_end)],
                                                    volume_bitmap)
    else:
        count_free, count_allocated = check_extents_concurrency(
            [(lcn_start, lcn_end)], volume_bitmap, tmp_file_path,
            volume_handle, total_clusters)
    if count_allocated > 0 and count_free == 0:
        return False
    if count_allocated > 0 or write_length > write_buf_size * 4:
        if lcn_start < lcn_end:
            for split_s, split_e in split_extent(lcn_start, lcn_end):
                wipe_extent_by_defrag(volume_handle, split_s, split_e,
                                      cluster_size, total_clusters,
                                      tmp_file_path)
            return True
        else:
            return False

    # Put the zero-fill file in place.
    file_handle = CreateFile(tmp_file_path, GENERIC_READ | GENERIC_WRITE, 0,
                             None, CREATE_ALWAYS, FILE_ATTRIBUTE_HIDDEN, None)
    write_zero_fill(file_handle, write_length)
    new_extents = get_extents(file_handle)

    # We know the original extent was contiguous.
    # The new zero-fill file may not be contiguous, so it requires a
    # loop to be sure of reaching the end of the new file's clusters.
    new_vcn = 0
    for new_lcn_start, new_lcn_end in new_extents:
        # logger.debug("Zero-fill wrote from %d to %d",
        #                   new_lcn_start, new_lcn_end)
        cluster_count = new_lcn_end - new_lcn_start + 1
        cluster_dest = lcn_start + new_vcn

        if new_lcn_start != cluster_dest:
            logger.debug("Move %d clusters to %d", cluster_count, cluster_dest)
            try:
                move_file(volume_handle, file_handle, new_vcn, cluster_dest,
                          cluster_count)
            except:
                # Move file failed, probably because another process
                # has allocated a cluster on disk.
                # Break into smaller pieces and do what we can.
                logger.debug("!! Move encountered an error !!")
                CloseHandle(file_handle)
                if lcn_start < lcn_end:
                    for split_s, split_e in split_extent(lcn_start, lcn_end):
                        wipe_extent_by_defrag(volume_handle, split_s, split_e,
                                              cluster_size, total_clusters,
                                              tmp_file_path)
                    return True
                else:
                    return False
        else:
            # If Windows put the zero-fill extent on the exact clusters we
            # intended to place it, no need to attempt a move.
            logging.debug("No need to move extent from %d", new_lcn_start)
        new_vcn += cluster_count

    CloseHandle(file_handle)
    DeleteFile(tmp_file_path)
    return True
Ejemplo n.º 29
0
    def __init__(self,
                 program,
                 cmdline=None,
                 cwd=None,
                 env=None,
                 spawn_flags=WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN
                 | WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
                 pty_flags=0,
                 pty_size=(80, 25),
                 pty_mouse=WINPTY_MOUSE_MODE_NONE):

        self._closed = False

        config = None
        try:
            with winpty_error() as error:
                config = winpty_config_new(pty_flags, error)

            cols, rows = pty_size
            if cols and rows:
                winpty_config_set_initial_size(config, cols, rows)
            winpty_config_set_mouse_mode(config, pty_mouse)

            with winpty_error() as error:
                self._pty = winpty_open(config, error)
        finally:
            winpty_config_free(config)

        self._conin = winpty_conin_name(self._pty)
        self._conout = winpty_conout_name(self._pty)
        self._conerr = winpty_conerr_name(self._pty)
        self._process_handle = None

        try:
            self._conin_pipe = CreateFile(self._conin, GENERIC_WRITE, 0, None,
                                          OPEN_EXISTING, 0, None)

            self._conout_pipe = CreateFile(self._conout, GENERIC_READ, 0, None,
                                           OPEN_EXISTING, 0, None)

            if self._conerr:
                self._conerr_pipe = CreateFile(self._conerr, GENERIC_READ, 0,
                                               None, OPEN_EXISTING, 0, None)
            else:
                self._conerr_pipe = None

            try:
                spawn_ctx = None
                process_handle = HANDLE()
                thread_handle = HANDLE()
                create_process_error = DWORD()

                with winpty_error() as error:
                    spawn_ctx = winpty_spawn_config_new(
                        spawn_flags, program, cmdline, cwd, env, error)

                with winpty_error() as error:
                    winpty_spawn(self._pty, spawn_ctx, pointer(process_handle),
                                 pointer(thread_handle),
                                 pointer(create_process_error), error)

                self._process_handle = process_handle

            finally:
                winpty_spawn_config_free(spawn_ctx)

        except:
            self.close()
            raise
Ejemplo n.º 30
0
tz = pytz.timezone(args.timezone)

shutil.copy2(source, dest)
if os.path.isdir(dest):
    dest_file = os.path.join(dest, src_file_name)
else:
    dest_file = dest

created = dt.fromtimestamp(os.path.getctime(source))
created = Time(tz.localize(created))
modified = dt.fromtimestamp(os.path.getmtime(source))
modified = Time(tz.localize(modified))
accessed = dt.fromtimestamp(os.path.getatime(source))
accessed = Time(tz.localize(accessed))

print("Source\n======")
print("Created:  {}\nModified: {}\nAccessed: {}".format(
    created, modified, accessed))

handle = CreateFile(dest_file, GENERIC_WRITE, FILE_SHARE_WRITE, None,
                    OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, None)
SetFileTime(handle, created, accessed, modified)
CloseHandle(handle)

created = tz.localize(dt.fromtimestamp(os.path.getctime(dest_file)))
modified = tz.localize(dt.fromtimestamp(os.path.getmtime(dest_file)))
accessed = tz.localize(dt.fromtimestamp(os.path.getatime(dest_file)))
print("\nDestination\n===========")
print("Created:  {}\nModified: {}\nAccessed: {}".format(
    created, modified, accessed))
Ejemplo n.º 31
0
 def osnmopen(path, *args):
     # CreateFile(path, FILE_GENERIC_READ|FILE_GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
     # http://docs.activestate.com/activepython/2.7/pywin32/win32file__CreateFile_meth.html
     # PyHANDLE = CreateFile(fileName, desiredAccess , shareMode , attributes , CreationDisposition , flagsAndAttributes , hTemplateFile )
     return CreateFile(path, FILE_GENERIC_READ|FILE_GENERIC_WRITE, 0, None, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, None)