Esempio n. 1
0
def test_iterdir_without_hidden_files(tmp_path: pathlib.Path) -> None:
    """Test that hidden files are filtered from the path."""
    pathlib.Path(tmp_path / 'visible.txt').touch()
    pathlib.Path(tmp_path / 'visibleDir/').mkdir()

    if file_utils.is_windows():
        """Windows"""
        hidden_file = tmp_path / 'hidden.txt'
        hidden_dir = tmp_path / 'hiddenDir/'
        hidden_file.touch()
        hidden_dir.mkdir()
        atts = win32api.GetFileAttributes(str(hidden_file))
        win32api.SetFileAttributes(str(hidden_file),
                                   win32con.FILE_ATTRIBUTE_HIDDEN | atts)
        atts = win32api.GetFileAttributes(str(hidden_dir))
        win32api.SetFileAttributes(str(hidden_dir),
                                   win32con.FILE_ATTRIBUTE_HIDDEN | atts)

        assert len(list(
            file_utils.iterdir_without_hidden_files(tmp_path))) == 3
    else:

        pathlib.Path(tmp_path / '.DS_Store').touch()
        pathlib.Path(tmp_path / '.hidden.txt').touch()
        pathlib.Path(tmp_path / '.hiddenDir/').mkdir()

        assert len(list(
            file_utils.iterdir_without_hidden_files(tmp_path))) == 3
Esempio n. 2
0
    def testLongLongPathNames(self):
        # We need filename where the FQN is > 256 - simplest way is to create a
        # 250 character directory in the cwd (except - cwd may be on a drive
        # not supporting \\\\?\\ (eg, network share) - so use temp.
        import win32file
        basename = "a" * 250
        # but we need to ensure we use the 'long' version of the
        # temp dir for later comparison.
        long_temp_dir = win32api.GetLongPathNameW(tempfile.gettempdir())
        fname = "\\\\?\\" + os.path.join(long_temp_dir, basename)
        try:
            win32file.CreateDirectoryW(fname, None)
        except win32api.error as details:
            if details.winerror != winerror.ERROR_ALREADY_EXISTS:
                raise
        try:
            # GetFileAttributes automatically calls GetFileAttributesW when
            # passed unicode
            try:
                attr = win32api.GetFileAttributes(fname)
            except win32api.error as details:
                if details.winerror != winerror.ERROR_FILENAME_EXCED_RANGE:
                    raise

            attr = win32api.GetFileAttributes(str(fname))
            assert attr & win32con.FILE_ATTRIBUTE_DIRECTORY, attr

            long_name = win32api.GetLongPathNameW(fname)
            assert long_name.lower() == fname.lower()
        finally:
            win32file.RemoveDirectory(fname)
Esempio n. 3
0
def clone_attributes(input_file, output_file):
    """
    Try to clone system attributes from input_file to output_file
    """

    if platform.system() == 'Windows':
        import win32api
        att_i = win32api.GetFileAttributes(str(input_file))
        # Setting att_o to have the same exact attributes as att_i
        win32api.SetFileAttributes(str(output_file), att_i)
    else:
        st = os.stat(input_file)

        try:
            # File owner
            os.chown(output_file, st.st_uid, st.st_gid)
        except Exception as e:
            print(f"Can't copy file owner to {output_file}: {e}")

        try:
            # File permissions
            os.chmod(output_file, st.st_mode)
        except Exception as e:
            print(f"Can't copy file permissions to {output_file}: {e}")

        try:
            # File dates
            os.utime(output_file, times=(st.st_atime, st.st_mtime))
        except Exception as e:
            print(f"Can't copy file date to {output_file}: {e}")
Esempio n. 4
0
def getFileAttributes(filePath):
    iFileAttr = win32api.GetFileAttributes(filePath)
    print iFileAttr
    _str = ''
    attr_map = {
        'FILE_ATTRIBUTE_READONLY': 0x01,
        'FILE_ATTRIBUTE_HIDDEN': 0x02,
        'FILE_ATTRIBUTE_COMPRESSED': 0x800,
        'FILE_ATTRIBUTE_DIRECTORY': 0x10,
        'FILE_ATTRIBUTE_ENCRYPTED': 0x4000,
        'FILE_ATTRIBUTE_NORMAL': 0x80,
        'FILE_ATTRIBUTE_ARCHIVE': 0x20,
        'FILE_ATTRIBUTE_SYSTEM': 0x4,
        'FILE_ATTRIBUTE_TEMPORARY': 0x100,
        'FILE_ATTRIBUTE_DEVICE': 0x40,
        'FILE_ATTRIBUTE_NOT_CONTENT_INDEXED': 0x2000,
        'FILE_ATTRIBUTE_OFFLINE': 0x1000,
        'FILE_ATTRIBUTE_REPARSE_POINT': 0x400,
        'FILE_ATTRIBUTE_SPARSE_FILE': 0x200,
        'FILE_ATTRIBUTE_VIRTUAL': 0x10000
    }
    for key in attr_map.keys():
        # print iFileAttr & attr_map[key] # & 按位与运算符:按位运算符是把数字看作二进制来进行计算的。参与运算的两个值,如果两个相应位都为1,则该位的结果为1,否则为0
        if iFileAttr & attr_map[key]:
            if _str:
                _str += '|%s' % (key)
            else:
                _str += '%s' % (key)
    return _str
Esempio n. 5
0
def remove(path, force=False):
    '''
    Remove the named file or directory

    :param str path: The path to the file or directory to remove.

    :param bool force: Remove even if marked Read-Only

    :return: True if successful, False if unsuccessful
    :rtype: bool

    CLI Example:

    .. code-block:: bash

        salt '*' file.remove C:\\Temp
    '''
    # This must be a recursive function in windows to properly deal with
    # Symlinks. The shutil.rmtree function will remove the contents of
    # the Symlink source in windows.

    path = os.path.expanduser(path)

    # Does the file/folder exists
    if not os.path.exists(path):
        return 'File/Folder not found: {0}'.format(path)

    if not os.path.isabs(path):
        raise SaltInvocationError('File path must be absolute.')

    # Remove ReadOnly Attribute
    if force:
        # Get current file attributes
        file_attributes = win32api.GetFileAttributes(path)
        win32api.SetFileAttributes(path, win32con.FILE_ATTRIBUTE_NORMAL)

    try:
        if os.path.isfile(path):
            # A file and a symlinked file are removed the same way
            os.remove(path)
        elif is_link(path):
            # If it's a symlink directory, use the rmdir command
            os.rmdir(path)
        else:
            for name in os.listdir(path):
                item = '{0}\\{1}'.format(path, name)
                # If it's a normal directory, recurse to remove it's contents
                remove(item, force)

            # rmdir will work now because the directory is empty
            os.rmdir(path)
    except (OSError, IOError) as exc:
        if force:
            # Reset attributes to the original if delete fails.
            win32api.SetFileAttributes(path, file_attributes)
        raise CommandExecutionError(
            'Could not remove {0!r}: {1}'.format(path, exc)
        )

    return True
Esempio n. 6
0
    def is_ignored(self, parent_ref, file_name):
        # type: (Text, Text) -> bool
        """ Note: added parent_ref to be able to filter on size if needed. """

        file_name = file_name.lower()

        if (file_name.endswith(Options.ignored_suffixes)
                or file_name.startswith(Options.ignored_prefixes)):
            return True

        if sys.platform == 'win32':
            # NXDRIVE-465: ignore hidden files on Windows
            ref = self.get_children_ref(parent_ref, file_name)
            path = self.abspath(ref)
            is_system = win32con.FILE_ATTRIBUTE_SYSTEM
            is_hidden = win32con.FILE_ATTRIBUTE_HIDDEN
            try:
                attrs = win32api.GetFileAttributes(path)
            except win32file.error:
                return False
            if attrs & is_system == is_system:
                return True
            if attrs & is_hidden == is_hidden:
                return True

        # NXDRIVE-655: need to check every parent if they are ignored
        result = False
        if parent_ref != '/':
            file_name = os.path.basename(parent_ref)
            parent_ref = os.path.dirname(parent_ref)
            result = self.is_ignored(parent_ref, file_name)

        return result
Esempio n. 7
0
    def get_exe_files(self, directory):
        file = []
        for dirpath, dirs, files in os.walk(directory):
            if os.name == 'nt':
                exclude = []
                for dir in dirs:
                    attribute = win32api.GetFileAttributes(
                        os.path.join(dirpath, dir))
                    hidden = (attribute & (win32con.FILE_ATTRIBUTE_HIDDEN
                                           | win32con.FILE_ATTRIBUTE_SYSTEM))
                    if hidden == 2:  # 2 = hidden
                        exclude.append(dir)
                dirs[:] = [d for d in dirs if d not in exclude]
            else:  # linux-osx
                dirs[:] = [d for d in dirs if not d[0] == '.']

            for filename in files:
                file.append(os.path.join(dirpath, filename))
        # file = ['C:\\Users\\Public\\Pictures\\desktop.ini', 'C:\\Users\\Public\\Videos\\desktop.ini']

        exes = []
        for i in file:
            filename, file_extension = os.path.splitext(i)
            if file_extension == '.exe':
                exes.append(i)
        # 		print('[+] exe found :: file ::', i)
        # print("[+] Total number of exe files: ", len(exes))
        return exes
Esempio n. 8
0
def update_init(dest_dir):
    vec_f_file = []

    if os.name == 'nt':
        import win32api
        import win32con

    for d_file in dest_dir.iterdir():
        f_file = d_file.stem

        if os.name == 'nt':
            attribute = win32api.GetFileAttributes(str(d_file))
            is_hidden = attribute & (win32con.FILE_ATTRIBUTE_HIDDEN
                                     | win32con.FILE_ATTRIBUTE_SYSTEM)
        else:
            is_hidden = f_file.startswith('.')

        if not is_hidden:
            vec_f_file.append(f_file)

    if "__init__" in vec_f_file:
        vec_f_file.remove("__init__")

    if "__pycache__" in vec_f_file:
        vec_f_file.remove("__pycache__")

    with open(dest_dir / '__init__.py', 'w') as f:
        for f_file in vec_f_file:
            f.write("from models.{} import *\n".format(f_file))

    f.close()
Esempio n. 9
0
    def isGivenFileHidden(self, path_of_file):
        """
        Check weather this file is Hidden or Not

        @param path_of_file: Path Of File or Directory to be checked

        @return: 0 if hidden and 2 if not in windows and in MAC true if hidden and false if not

        """
        try:
            self.Fixity = SharedApp.SharedApp.App
        except:
            pass
        if os.name == 'nt':
            if '\\' in path_of_file:
                '''Windows Condition'''
                try:
                    path_of_file = path_of_file.strip("'")
                except:
                    try:
                        path_of_file = path_of_file.encode('utf-8').strip("'")
                    except:
                        path_of_file = path_of_file.decode('utf-8').strip("'")
                        pass
                    pass

                attribute = win32api.GetFileAttributes(path_of_file)
                return attribute & (win32con.FILE_ATTRIBUTE_HIDDEN
                                    | win32con.FILE_ATTRIBUTE_SYSTEM)
            return 0
        else:
            '''linux'''
            return path_of_file.startswith('.')
Esempio n. 10
0
def is_hidden(path):
    name = os.path.basename(path)
    if os.name== 'nt': # windows
        attribute = win32api.GetFileAttributes(path)
        return attribute & (win32con.FILE_ATTRIBUTE_HIDDEN | win32con.FILE_ATTRIBUTE_SYSTEM)
    else:
        return name.startswith('.') # unix (linux-osx)
Esempio n. 11
0
def isLink(path):
    """
            pathがシンボリックリンクまたはジャンクションならTrue
            呼び出す際は権限がない場合等のエラーをキャッチすべき
    """
    attrs = win32api.GetFileAttributes(path)
    return attrs & win32con.FILE_ATTRIBUTE_REPARSE_POINT != 0
Esempio n. 12
0
    def is_ignored(self, parent_ref: Path, file_name: str) -> bool:
        """ Note: added parent_ref to be able to filter on size if needed. """

        file_name = safe_filename(force_decode(file_name.lower()))

        if file_name.endswith(
                Options.ignored_suffixes) or file_name.startswith(
                    Options.ignored_prefixes):
            return True

        # NXDRIVE-465: ignore hidden files on Windows
        ref = parent_ref / file_name
        path = self.abspath(ref)
        is_system = win32con.FILE_ATTRIBUTE_SYSTEM
        is_hidden = win32con.FILE_ATTRIBUTE_HIDDEN
        try:
            attrs = win32api.GetFileAttributes(str(path))
        except win32file.error:
            return False
        if attrs & is_system == is_system:
            return True
        if attrs & is_hidden == is_hidden:
            return True

        # NXDRIVE-655: need to check every parent if they are ignored
        result = False
        if parent_ref != ROOT:
            file_name = parent_ref.name
            parent_ref = parent_ref.parent
            result = self.is_ignored(parent_ref, file_name)

        return result
Esempio n. 13
0
def folder_is_hidden(fname, dir):
    if os.name == 'nt':
        attribute = win32api.GetFileAttributes(dir + fname)
        return attribute & (win32con.FILE_ATTRIBUTE_HIDDEN
                            | win32con.FILE_ATTRIBUTE_SYSTEM)
    else:
        return fname.startswith('.')  # linux-osx
Esempio n. 14
0
def file_is_hidden(p):
    if os.name == 'nt':
        attribute = win32api.GetFileAttributes(p)
        return attribute & (win32con.FILE_ATTRIBUTE_HIDDEN
                            | win32con.FILE_ATTRIBUTE_SYSTEM)
    else:
        return p.startswith('.')
Esempio n. 15
0
	def GetBitmapColumn(self):
		col = 4 # Default
		try:
			if win32api.GetFileAttributes(self.path) & win32con.FILE_ATTRIBUTE_READONLY:
				col = 5
		except win32api.error:
			pass
		return col
Esempio n. 16
0
 def __init__(self, path):
     self.path = path
     self.name = path.split('/')[-1]
     self.hidden = win32api.GetFileAttributes(self.get_path())
     if self.hidden in [2, 18]:
         self.hidden = True
     else:
         self.hidden = False
 def is_symlink(self):
     """Is this file a symbolic link to an other file?"""
     if os.name == "posix": return os.path.islink(self.path)
     if os.name == "nt":
         import win32api
         import win32con
         num = win32con.FILE_ATTRIBUTE_REPARSE_POINT
         return bool(win32api.GetFileAttributes(self.path) & num)
Esempio n. 18
0
 def folder_is_hidden(self, p):
     #See SO question: https://stackoverflow.com/questions/7099290/how-to-ignore-hidden-files-using-os-listdir
     if platform.system() is 'Windows':
         attribute = win32api.GetFileAttributes(p)
         return attribute & (win32con.FILE_ATTRIBUTE_HIDDEN
                             | win32con.FILE_ATTRIBUTE_SYSTEM)
     else:
         return p.startswith('.')
Esempio n. 19
0
 def has_hidden_attribute(filepath):
     import win32api
     import win32con
     try:
         attribute = win32api.GetFileAttributes(filepath)
         return attribute & (win32con.FILE_ATTRIBUTE_HIDDEN | win32con.FILE_ATTRIBUTE_SYSTEM)
     except (AttributeError, AssertionError):
         return False
Esempio n. 20
0
def make_hidden_file(file_path: pathlib.Path) -> None:
    """Make hidden file."""
    if not file_path.name.startswith('.') and not is_windows():
        file_path = file_path.parent / ('.' + file_path.name)

    file_path.touch()
    if is_windows():
        atts = win32api.GetFileAttributes(str(file_path))
        win32api.SetFileAttributes(str(file_path), win32con.FILE_ATTRIBUTE_HIDDEN | atts)
Esempio n. 21
0
 def isHiddenOrSystem(path: str) -> bool:
     if os.name == "nt":
         attribute = win32api.GetFileAttributes(path)
         result = True if attribute & (
             win32con.FILE_ATTRIBUTE_HIDDEN
             | win32con.FILE_ATTRIBUTE_SYSTEM) != 0 else False
         return result
     else:
         return path.rsplit("/", 1)[-1].startswith(".")
Esempio n. 22
0
 def __init__(self, name, type, path):
     self.name = name
     self.type = type
     self.path = path + '/' + name
     self.hidden = win32api.GetFileAttributes(self.get_path())
     if self.hidden == 128:
         self.hidden = False
     else:
         self.hidden = True
Esempio n. 23
0
def _真视(文件名):
    if not os.path.isfile(文件名):
        yield
    elif win32api.GetFileAttributes(文件名) & win32con.FILE_ATTRIBUTE_HIDDEN:
        win32api.SetFileAttributes(文件名, win32con.FILE_ATTRIBUTE_NORMAL)
        yield
        win32api.SetFileAttributes(文件名, win32con.FILE_ATTRIBUTE_HIDDEN)
    else:
        yield
Esempio n. 24
0
 def path_is_hidden(self, path):
     if its.on_windows:
         attribute = win32api.GetFileAttributes(path)
         if attribute & (win32con.FILE_ATTRIBUTE_HIDDEN
                         | win32con.FILE_ATTRIBUTE_SYSTEM):
             return True
     elif self.path_mod.basename(path).startswith('.'):
         return True
     return False
Esempio n. 25
0
def has_system_attribute(filename):
    try:
        st = lstat(filename)
        flag = bool(st.st_file_attributes & stat.FILE_ATTRIBUTE_SYSTEM)

    except AttributeError:
        attributes = win32api.GetFileAttributes(filename)
        flag = attributes & win32con.FILE_ATTRIBUTE_SYSTEM

    return flag
Esempio n. 26
0
def FileIsHidden(f):
    """Returns a boolean indicating whether the file ``f`` is hidden or not.
    This function is Windows/Linux safe.
    """
    if platform.system() == "Windows":
        # Note this is bitwise and as the RHS is a mask
        return bool(
            win32api.GetFileAttributes(f) & win32con.FILE_ATTRIBUTE_HIDDEN)
    # Linux/Mac hidden files simply have file names that start with a dot
    return FileName(f).startswith('.')
Esempio n. 27
0
def is_reparse_point(d):
    try:
        attr = win32api.GetFileAttributes(d)
        # reparse point http://msdn.microsoft.com/en-us/library/windows/desktop/gg258117(v=vs.85).aspx
        if attr & 0x400:
            print "[D] Is reparse point: %s" % d
            return 1
    except:
        pass
    return 0
Esempio n. 28
0
def has_hidden_attribute(filename):
    try:
        st = lstat(filename)
        flag = bool(st.st_file_attributes & stat.FILE_ATTRIBUTE_HIDDEN)

    except AttributeError:
        attributes = win32api.GetFileAttributes(filename)
        flag = attributes & win32con.FILE_ATTRIBUTE_HIDDEN

    return flag
Esempio n. 29
0
def make_file_hidden(file_path: pathlib.Path, if_dot=False) -> None:
    """
    Make a file hidden on windows.

    if_dot will make the change only if the filename is of the form .*
    """
    if file_utils.is_windows():
        if not if_dot or file_path.stem.startswith('.'):
            atts = win32api.GetFileAttributes(str(file_path))
            win32api.SetFileAttributes(str(file_path), win32con.FILE_ATTRIBUTE_HIDDEN | atts)
Esempio n. 30
0
def is_hidden(_filename):

    if os.name == 'nt':
        attribute = win32api.GetFileAttributes(_filename)
        return attribute & (win32con.FILE_ATTRIBUTE_HIDDEN
                            | win32con.FILE_ATTRIBUTE_SYSTEM)

    else:
        ## macos && linux dotfiles
        return os.path.basename(_filename).startswith('.')