def setUpClass(cls): super().setUpClass() cls._exit_stack = contextlib.ExitStack() ntfs_image = cls._exit_stack.enter_context( open(os.path.join(config.CONFIG["Test.data_dir"], "ntfs.img"), "rb")) cls._server = server.CreateFilesystemServer(ntfs_image.fileno()) cls._server.Start() cls._client = cls._exit_stack.enter_context( client.CreateFilesystemClient(cls._server.Connect(), cls._IMPLEMENTATION_TYPE, client.FileDevice(ntfs_image)))
def __init__(self, base_fd: Optional[vfs_base.VFSHandler], handlers: Dict[Any, Type[vfs_base.VFSHandler]], pathspec: Optional[rdf_paths.PathSpec] = None, progress_callback: Optional[Callable[[], None]] = None): super().__init__(base_fd, handlers=handlers, pathspec=pathspec, progress_callback=progress_callback) # self.pathspec is initialized to a copy of base_fd if base_fd is None: raise ValueError( "UnprivilegedFileBase driver must have a file base.") elif isinstance(base_fd, UnprivilegedFileBase) and base_fd.IsDirectory(): self.client = base_fd.client last_path = utils.JoinPath(self.pathspec.last.path, pathspec.path) # Replace the last component with this one. self.pathspec.Pop(-1) self.pathspec.Append(pathspec) self.pathspec.last.path = last_path elif not base_fd.IsDirectory(): cache_key = base_fd.pathspec.SerializeToBytes() + str( self.implementation_type).encode("utf-8") try: self.client = MOUNT_CACHE.Get(cache_key).client except KeyError: device_path = base_fd.native_path with contextlib.ExitStack() as stack: if device_path is None: server_obj = server.CreateFilesystemServer() stack.enter_context(server_obj) self.client = stack.enter_context( client.CreateFilesystemClient( server_obj.Connect(), self.implementation_type, VFSHandlerDevice(base_fd))) else: with open(device_path, "rb") as device_file: server_obj = server.CreateFilesystemServer( device_file.fileno()) stack.enter_context(server_obj) self.client = stack.enter_context( client.CreateFilesystemClient( server_obj.Connect(), self.implementation_type, VFSHandlerDevice(base_fd, device_file.fileno()))) MOUNT_CACHE.Put( cache_key, MountCacheItem(server=server_obj, client=self.client)) # Transfer ownership of resources to MOUNT_CACHE. stack.pop_all() self.pathspec.Append(pathspec) elif base_fd.IsDirectory(): raise IOError("Base must be a file.") self.fd = None if pathspec is None: raise ValueError("pathspec can't be None.") try: if pathspec.HasField("stream_name"): if pathspec.path_options == rdf_paths.PathSpec.Options.CASE_LITERAL: # If the stream name is case literal, we just open the stream. self.fd = self._OpenPathSpec(pathspec) else: # The VFS layer is not taking care of case insensitive stream names # (as it does for case insensitive paths). # We have to find the corresponding case literal stream name in this # case ourselves. self.fd, self.pathspec.last.stream_name = ( self._OpenStreamCaseInsensitive(pathspec)) else: self.fd = self._OpenPathSpec(pathspec) except client.OperationError as e: raise IOError(f"Failed to open {pathspec}.") from e # self.pathspec will be used for future access to this file. # The name is now literal, so disable case-insensitive lookup (expensive). self.pathspec.last.path_options = rdf_paths.PathSpec.Options.CASE_LITERAL # Access the file by file_reference, to skip path lookups. self.pathspec.last.inode = self.fd.inode self._stat_result = _ConvertStatEntry(self.fd.Stat(), self.pathspec)
def setUpClass(cls): super(NtfsTest, cls).setUpClass() cls._server = server.CreateFilesystemServer() cls._server.Start()
def setUpClass(cls): super().setUpClass() cls._ntfs_image = open( os.path.join(config.CONFIG["Test.data_dir"], "ntfs.img"), "rb") cls._server = server.CreateFilesystemServer(cls._ntfs_image.fileno()) cls._server.Start()