Esempio n. 1
0
    def _OpenFileObject(self, path_spec):
        """Opens the file-like object defined by path specification.

    Args:
      path_spec (PathSpec): path specification.

    Returns:
      pyewf.handle: a file-like object or None.

    Raises:
      PathSpecError: if the path specification is invalid.
    """
        if not path_spec.HasParent():
            raise errors.PathSpecError(
                'Unsupported path specification without parent.')

        parent_path_spec = path_spec.parent

        file_system = resolver.Resolver.OpenFileSystem(
            parent_path_spec, resolver_context=self._resolver_context)

        # Note that we cannot use pyewf's glob function since it does not
        # handle the file system abstraction dfvfs provides.
        segment_file_path_specs = ewf.EWFGlobPathSpec(file_system, path_spec)
        if not segment_file_path_specs:
            return None

        if parent_path_spec.IsSystemLevel():
            # Typically the file-like object cache should have room for 127 items.
            self._resolver_context.SetMaximumNumberOfFileObjects(
                len(segment_file_path_specs) + 127)

        for segment_file_path_spec in segment_file_path_specs:
            file_object = resolver.Resolver.OpenFileObject(
                segment_file_path_spec,
                resolver_context=self._resolver_context)
            self._file_objects.append(file_object)

        ewf_handle = pyewf.handle()
        ewf_handle.open_file_objects(self._file_objects)
        return ewf_handle
Esempio n. 2
0
    def testGlobE01(self):
        """Test the glob function."""
        expected_segment_file_path_specs = []
        file_system = self._BuildFileFakeFileSystem(
            u'image.E01', 1, expected_segment_file_path_specs)

        # Test single segment file: E01.
        path_spec = fake_path_spec.FakePathSpec(location=u'/image.E01')
        path_spec = ewf_path_spec.EWFPathSpec(parent=path_spec)

        segment_file_path_specs = ewf.EWFGlobPathSpec(file_system, path_spec)
        self.assertEqual(len(segment_file_path_specs),
                         len(expected_segment_file_path_specs))
        self.assertEqual(segment_file_path_specs,
                         expected_segment_file_path_specs)

        # Test non exiting segment file: E01.
        expected_segment_file_path_specs = []

        path_spec = fake_path_spec.FakePathSpec(location=u'/bogus.E01')
        path_spec = ewf_path_spec.EWFPathSpec(parent=path_spec)

        segment_file_path_specs = ewf.EWFGlobPathSpec(file_system, path_spec)
        self.assertEqual(len(segment_file_path_specs),
                         len(expected_segment_file_path_specs))
        self.assertEqual(segment_file_path_specs,
                         expected_segment_file_path_specs)

        # Test multiple segment files: E01-E10.
        expected_segment_file_path_specs = []
        file_system = self._BuildFileFakeFileSystem(
            u'image.E01', 10, expected_segment_file_path_specs)

        path_spec = fake_path_spec.FakePathSpec(location=u'/image.E01')
        path_spec = ewf_path_spec.EWFPathSpec(parent=path_spec)

        segment_file_path_specs = ewf.EWFGlobPathSpec(file_system, path_spec)
        self.assertEqual(len(segment_file_path_specs),
                         len(expected_segment_file_path_specs))
        self.assertEqual(segment_file_path_specs,
                         expected_segment_file_path_specs)

        # Test multiple segment files: E01-E99,EAA.
        expected_segment_file_path_specs = []
        file_system = self._BuildFileFakeFileSystem(
            u'image.E01', 100, expected_segment_file_path_specs)

        path_spec = fake_path_spec.FakePathSpec(location=u'/image.E01')
        path_spec = ewf_path_spec.EWFPathSpec(parent=path_spec)

        segment_file_path_specs = ewf.EWFGlobPathSpec(file_system, path_spec)
        self.assertEqual(len(segment_file_path_specs),
                         len(expected_segment_file_path_specs))
        self.assertEqual(segment_file_path_specs,
                         expected_segment_file_path_specs)

        # Test multiple segment files: E01-E99,EAA-EBA.
        expected_segment_file_path_specs = []
        file_system = self._BuildFileFakeFileSystem(
            u'image.E01', 126, expected_segment_file_path_specs)

        path_spec = fake_path_spec.FakePathSpec(location=u'/image.E01')
        path_spec = ewf_path_spec.EWFPathSpec(parent=path_spec)

        segment_file_path_specs = ewf.EWFGlobPathSpec(file_system, path_spec)
        self.assertEqual(len(segment_file_path_specs),
                         len(expected_segment_file_path_specs))
        self.assertEqual(segment_file_path_specs,
                         expected_segment_file_path_specs)

        # Test multiple segment files: E01-E99,EAA-EZZ.
        expected_segment_file_path_specs = []
        file_system = self._BuildFileFakeFileSystem(
            u'image.E01', 775, expected_segment_file_path_specs)

        path_spec = fake_path_spec.FakePathSpec(location=u'/image.E01')
        path_spec = ewf_path_spec.EWFPathSpec(parent=path_spec)

        segment_file_path_specs = ewf.EWFGlobPathSpec(file_system, path_spec)
        self.assertEqual(len(segment_file_path_specs),
                         len(expected_segment_file_path_specs))
        self.assertEqual(segment_file_path_specs,
                         expected_segment_file_path_specs)

        # Test multiple segment files: E01-E99,EAA-ZZZ.
        expected_segment_file_path_specs = []
        file_system = self._BuildFileFakeFileSystem(
            u'image.E01', 14970, expected_segment_file_path_specs)

        path_spec = fake_path_spec.FakePathSpec(location=u'/image.E01')
        path_spec = ewf_path_spec.EWFPathSpec(parent=path_spec)

        segment_file_path_specs = ewf.EWFGlobPathSpec(file_system, path_spec)
        self.assertEqual(len(segment_file_path_specs),
                         len(expected_segment_file_path_specs))
        self.assertEqual(segment_file_path_specs,
                         expected_segment_file_path_specs)

        # Test multiple segment files: E01-E99,EAA-[ZZ.
        expected_segment_file_path_specs = []
        file_system = self._BuildFileFakeFileSystem(
            u'image.E01', 14971, expected_segment_file_path_specs)

        path_spec = fake_path_spec.FakePathSpec(location=u'/image.E01')
        path_spec = ewf_path_spec.EWFPathSpec(parent=path_spec)

        with self.assertRaises(RuntimeError):
            segment_file_path_specs = ewf.EWFGlobPathSpec(
                file_system, path_spec)