Example #1
0
    def __init__(self, filename, parent=None):
        """Initialize a NTFile object.

        :param filename: path to the file or basename if parent is not None
        :type filename: str | unicode
        :param parent: the parent NTFile object
        :type parent: NTFile | None
        """
        if parent is None:
            self.path = os.path.abspath(filename)
            self.nt_filename = UnicodeString("\\??\\%s" % self.path)
            self.parent_handle = None
        else:
            assert "\\" not in filename and "/" not in filename
            self.path = os.path.join(parent.path, filename)
            self.nt_filename = UnicodeString(str(filename))
            self.parent_handle = parent.handle

        self.handle = None
        self.io_status = IOStatusBlock()
        self.basic_info = FileInfo.Basic()

        self.attr = ObjectAttributes(self.nt_filename,
                                     parent=self.parent_handle)

        self.desired_access = Access.READ_DATA | Access.READ_ATTRS

        # By default don't block access to other processes
        self.shared_access = Share.ALL

        # By default we set BACKUP_INTENT to be able to bypass ACLs in case
        # the user has sufficient rights. It has no effect otherwise
        self.open_options = OpenOptions.BACKUP_INTENT
Example #2
0
    def read_attributes(self):
        """Retrieve file basic information.

        It updates the basic_info attribute including timestamp information

        :raise: NTException
        """
        result = FileInfo.Basic()
        status = NT.QueryInformationFile(
            self.handle,
            pointer(self.io_status),
            pointer(result),
            sizeof(result),
            FileInfo.Basic.class_id,
        )
        if status < 0:  # defensive code
            # we should already have raised an error here when trying
            # to open the file
            raise NTException(
                status=status,
                message="cannot read attributes",
                origin="NTFile.read_attributes",
            )
        self.basic_info = result
        return result
Example #3
0
    def uid(self):
        """Retrieve the ID of the file.

        On NTFS system we are sure that this ID is unique on the given volume

        :return: the uid
        :rtype: int
        ;raise: NTException
        """
        result = FileInfo.Internal()
        status = NT.QueryInformationFile(
            self.handle,
            pointer(self.io_status),
            pointer(result),
            sizeof(result),
            FileInfo.Internal.class_id,
        )
        if status < 0:  # defensive code
            # we should already have raised an error here when trying
            # to open the file
            raise NTException(status=status,
                              message="cannot find file uid",
                              origin="NTFile.uid")

        return result.index_number
Example #4
0
    def __init__(self, filename: str, parent: Optional[NTFile] = None):
        """Initialize a NTFile object.

        :param filename: path to the file or basename if parent is not None
        :param parent: the parent NTFile object
        """
        self.handle: Optional[HANDLE] = None
        if parent is None:
            self.path = os.path.abspath(filename)
            self.nt_filename = UnicodeString(f"\\??\\{self.path}")
            self.parent_handle = None
        else:
            assert "\\" not in filename and "/" not in filename
            self.path = os.path.join(parent.path, filename)
            self.nt_filename = UnicodeString(str(filename))
            self.parent_handle = parent.handle

        self.io_status = IOStatusBlock()
        self.basic_info = FileInfo.Basic()

        self.attr = ObjectAttributes(self.nt_filename,
                                     parent=self.parent_handle)

        self.desired_access = Access.READ_DATA | Access.READ_ATTRS

        # By default don't block access to other processes
        self.shared_access = Share.ALL

        # By default we set BACKUP_INTENT to be able to bypass ACLs in case
        # the user has sufficient rights. It has no effect otherwise
        self.open_options = OpenOptions.BACKUP_INTENT

        # Used by is_dir_empty to store the latest seen file in a given directory
        self.is_dir_empty_last_seen_file: Optional[str] = None
Example #5
0
    def dispose(self):
        """Remove the file (low level).

        The remove is effective on call to close method
        """
        fd = FileInfo.Disposition(1)
        status = NT.SetInformationFile(self.handle, pointer(self.io_status),
                                       pointer(fd),
                                       sizeof(FileInfo.Disposition),
                                       FileInfo.Disposition.class_id)
        if status < 0:
            raise NTException(status=status,
                              message="cannot dispose",
                              origin="NTFile.dispose")
Example #6
0
    def uid(self):
        """Retrieve the ID of the file.

        On NTFS system we are sure that this ID is unique on the given volume

        :return: the uid
        :rtype: int
        ;raise: NTException
        """
        result = FileInfo.Internal()
        status = NT.QueryInformationFile(self.handle, pointer(self.io_status),
                                         pointer(result), sizeof(result),
                                         FileInfo.Internal.class_id)
        if status < 0:
            raise NTException(status=status,
                              message='cannot find file uid',
                              origin="NTFile.uid")

        return result.index_number