Ejemplo n.º 1
0
    async def list(self, connection):
        """
		Lists all files and folders in the directory
		directory: SMBDirectory
		fills the SMBDirectory's data
		"""
        desired_access = FileAccessMask.FILE_READ_DATA
        share_mode = ShareAccess.FILE_SHARE_READ
        create_options = CreateOptions.FILE_DIRECTORY_FILE | CreateOptions.FILE_SYNCHRONOUS_IO_NONALERT
        file_attrs = 0
        create_disposition = CreateDisposition.FILE_OPEN

        try:
            file_id = await connection.create(self.tree_id, self.fullpath,
                                              desired_access, share_mode,
                                              create_options,
                                              create_disposition, file_attrs)
        except Exception as e:
            return False, e
        try:
            while True:
                fileinfos = await connection.query_directory(
                    self.tree_id, file_id)
                if not fileinfos:
                    break
                for info in fileinfos:
                    if info.FileAttributes & FileAttributes.FILE_ATTRIBUTE_DIRECTORY:
                        dirname = info.FileName
                        if info.FileName in ['.', '..']:
                            continue
                        subdir = SMBDirectory()
                        subdir.tree_id = self.tree_id
                        if self.fullpath != '':
                            subdir.fullpath = '%s\\%s' % (self.fullpath,
                                                          info.FileName)
                        else:
                            subdir.fullpath = info.FileName
                        subdir.unc_path = '%s\\%s' % (self.unc_path,
                                                      info.FileName)
                        subdir.parent_dir = self
                        subdir.name = info.FileName
                        subdir.creation_time = info.CreationTime
                        subdir.last_access_time = info.LastAccessTime
                        subdir.last_write_time = info.LastWriteTime
                        subdir.change_time = info.ChangeTime
                        subdir.allocation_size = info.AllocationSize
                        subdir.attributes = info.FileAttributes

                        self.subdirs[subdir.name] = subdir

                    else:
                        file = SMBFile()
                        #file.parent_share = directory.parent_share
                        file.tree_id = self.tree_id
                        file.parent_dir = None
                        if self.fullpath != '':
                            file.fullpath = '%s\\%s' % (self.fullpath,
                                                        info.FileName)
                            file.unc_path = '%s\\%s' % (self.unc_path,
                                                        info.FileName)
                        else:
                            file.fullpath = info.FileName
                            file.unc_path = '%s\\%s' % (self.unc_path,
                                                        info.FileName)
                        file.name = info.FileName
                        file.size = info.EndOfFile
                        file.creation_time = info.CreationTime
                        file.last_access_time = info.LastAccessTime
                        file.last_write_time = info.LastWriteTime
                        file.change_time = info.ChangeTime
                        file.allocation_size = info.AllocationSize
                        file.attributes = info.FileAttributes
                        self.files[file.name] = file
            return True, None
        except Exception as e:
            return False, e
        finally:
            if file_id is not None:
                await connection.close(self.tree_id, file_id)
Ejemplo n.º 2
0
    async def list_gen(self, connection):
        """
		Lists all files and folders in the directory, yields the results as they arrive
		directory: SMBDirectory
		DOESN'T fill the SMBDirectory's data
		"""
        self.files = {}
        self.subdirs = {}

        desired_access = FileAccessMask.FILE_READ_DATA
        share_mode = ShareAccess.FILE_SHARE_READ
        create_options = CreateOptions.FILE_DIRECTORY_FILE | CreateOptions.FILE_SYNCHRONOUS_IO_NONALERT
        file_attrs = 0
        create_disposition = CreateDisposition.FILE_OPEN

        if not self.tree_id:
            tree_entry, err = await connection.tree_connect(
                self.get_share_path())
            if err is not None:
                yield self, 'dir', err
                return
            self.tree_id = tree_entry.tree_id

        file_id, err = await connection.create(self.tree_id, self.fullpath,
                                               desired_access, share_mode,
                                               create_options,
                                               create_disposition, file_attrs)
        if err is not None:
            yield self, 'dir', err
            return
        try:
            while True:
                fileinfos, err = await connection.query_directory(
                    self.tree_id, file_id)
                if err is not None:
                    raise err
                if not fileinfos:
                    break
                for info in fileinfos:
                    if info.FileAttributes & FileAttributes.FILE_ATTRIBUTE_DIRECTORY:
                        dirname = info.FileName
                        if info.FileName in ['.', '..']:
                            continue
                        subdir = SMBDirectory()
                        subdir.tree_id = self.tree_id
                        if self.fullpath != '':
                            subdir.fullpath = '%s\\%s' % (self.fullpath,
                                                          info.FileName)
                        else:
                            subdir.fullpath = info.FileName
                        subdir.unc_path = '%s\\%s' % (self.unc_path,
                                                      info.FileName)
                        subdir.parent_dir = self
                        subdir.name = info.FileName
                        subdir.creation_time = info.CreationTime
                        subdir.last_access_time = info.LastAccessTime
                        subdir.last_write_time = info.LastWriteTime
                        subdir.change_time = info.ChangeTime
                        subdir.allocation_size = info.AllocationSize
                        subdir.attributes = info.FileAttributes

                        yield subdir, 'dir', None

                    else:
                        file = SMBFile()
                        file.tree_id = self.tree_id
                        file.parent_dir = None
                        if self.fullpath != '':
                            file.fullpath = '%s\\%s' % (self.fullpath,
                                                        info.FileName)
                            file.unc_path = '%s\\%s' % (self.unc_path,
                                                        info.FileName)
                        else:
                            file.fullpath = info.FileName
                            file.unc_path = '%s\\%s' % (self.unc_path,
                                                        info.FileName)
                        file.name = info.FileName
                        file.size = info.EndOfFile
                        file.creation_time = info.CreationTime
                        file.last_access_time = info.LastAccessTime
                        file.last_write_time = info.LastWriteTime
                        file.change_time = info.ChangeTime
                        file.allocation_size = info.AllocationSize
                        file.attributes = info.FileAttributes
                        yield file, 'file', None

        except Exception as e:
            yield self, 'dir', e
            return
        finally:
            if file_id is not None:
                await connection.close(self.tree_id, file_id)