Ejemplo n.º 1
0
    def testIsFunctions(self):
        """Test the Is? functions."""
        path_spec = cpio_path_spec.CPIOPathSpec(location=u'/syslog',
                                                parent=self._os_path_spec)
        file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)
        self.assertIsNotNone(file_entry)

        self.assertFalse(file_entry.IsRoot())
        self.assertFalse(file_entry.IsVirtual())
        self.assertTrue(file_entry.IsAllocated())

        self.assertFalse(file_entry.IsDevice())
        self.assertFalse(file_entry.IsDirectory())
        self.assertTrue(file_entry.IsFile())
        self.assertFalse(file_entry.IsLink())
        self.assertFalse(file_entry.IsPipe())
        self.assertFalse(file_entry.IsSocket())

        path_spec = cpio_path_spec.CPIOPathSpec(location=u'/',
                                                parent=self._os_path_spec)
        file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)
        self.assertIsNotNone(file_entry)

        self.assertTrue(file_entry.IsRoot())
        self.assertTrue(file_entry.IsVirtual())
        self.assertTrue(file_entry.IsAllocated())

        self.assertFalse(file_entry.IsDevice())
        self.assertTrue(file_entry.IsDirectory())
        self.assertFalse(file_entry.IsFile())
        self.assertFalse(file_entry.IsLink())
        self.assertFalse(file_entry.IsPipe())
        self.assertFalse(file_entry.IsSocket())
Ejemplo n.º 2
0
    def testDataStreams(self):
        """Test the data streams functionality."""
        path_spec = cpio_path_spec.CPIOPathSpec(location=u'/syslog',
                                                parent=self._os_path_spec)
        file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)
        self.assertIsNotNone(file_entry)

        self.assertEqual(file_entry.number_of_data_streams, 1)

        data_stream_names = []
        for data_stream in file_entry.data_streams:
            data_stream_names.append(data_stream.name)

        self.assertEqual(data_stream_names, [u''])

        path_spec = cpio_path_spec.CPIOPathSpec(location=u'/',
                                                parent=self._os_path_spec)
        file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)
        self.assertIsNotNone(file_entry)

        self.assertEqual(file_entry.number_of_data_streams, 0)

        data_stream_names = []
        for data_stream in file_entry.data_streams:
            data_stream_names.append(data_stream.name)

        self.assertEqual(data_stream_names, [])
Ejemplo n.º 3
0
    def GetParentFileEntry(self):
        """Retrieves the parent file entry.

    Returns:
      CPIOFileEntry: parent file entry or None if not available.
    """
        location = getattr(self.path_spec, 'location', None)
        if location is None:
            return None

        parent_location = self._file_system.DirnamePath(location)
        if parent_location is None:
            return None

        if parent_location == '':
            parent_location = self._file_system.PATH_SEPARATOR
            is_root = True
            is_virtual = True
        else:
            is_root = False
            is_virtual = False

        parent_path_spec = getattr(self.path_spec, 'parent', None)
        path_spec = cpio_path_spec.CPIOPathSpec(location=parent_location,
                                                parent=parent_path_spec)
        return CPIOFileEntry(self._resolver_context,
                             self._file_system,
                             path_spec,
                             is_root=is_root,
                             is_virtual=is_virtual)
Ejemplo n.º 4
0
    def _EntriesGenerator(self):
        """Retrieves directory entries.

    Since a directory can contain a vast number of entries using
    a generator is more memory efficient.

    Yields:
      CPIOPathSpec: path specification.
    """
        location = getattr(self.path_spec, 'location', None)

        if location and location.startswith(self._file_system.PATH_SEPARATOR):
            cpio_archive_file = self._file_system.GetCPIOArchiveFile()
            for cpio_archive_file_entry in cpio_archive_file.GetFileEntries(
                    path_prefix=location[1:]):

                path = cpio_archive_file_entry.path
                if not path:
                    continue

                _, suffix = self._file_system.GetPathSegmentAndSuffix(
                    location[1:], path)

                # Ignore anything that is part of a sub directory or the directory
                # itself.
                if suffix or path == location:
                    continue

                path_spec_location = self._file_system.JoinPath([path])
                yield cpio_path_spec.CPIOPathSpec(location=path_spec_location,
                                                  parent=self.path_spec.parent)
Ejemplo n.º 5
0
 def setUp(self):
   """Sets up the needed objects used throughout the test."""
   self._resolver_context = context.Context()
   test_file = os.path.join(u'test_data', u'syslog.bin.cpio')
   self._os_path_spec = os_path_spec.OSPathSpec(location=test_file)
   self._cpio_path_spec = cpio_path_spec.CPIOPathSpec(
       location=u'/syslog', parent=self._os_path_spec)
Ejemplo n.º 6
0
 def setUp(self):
     """Sets up the needed objects used throughout the test."""
     self._resolver_context = context.Context()
     test_file = self._GetTestFilePath(['syslog.bin.cpio'])
     self._os_path_spec = os_path_spec.OSPathSpec(location=test_file)
     self._cpio_path_spec = cpio_path_spec.CPIOPathSpec(
         location='/syslog', parent=self._os_path_spec)
Ejemplo n.º 7
0
    def testFileEntryExistsByPathSpec(self):
        """Test the file entry exists by path specification functionality."""
        file_system = cpio_file_system.CPIOFileSystem(self._resolver_context)
        self.assertIsNotNone(file_system)

        file_system.Open(self._cpio_path_spec)

        path_spec = cpio_path_spec.CPIOPathSpec(location='/syslog',
                                                parent=self._os_path_spec)
        self.assertTrue(file_system.FileEntryExistsByPathSpec(path_spec))

        path_spec = cpio_path_spec.CPIOPathSpec(location='/bogus',
                                                parent=self._os_path_spec)
        self.assertFalse(file_system.FileEntryExistsByPathSpec(path_spec))

        file_system.Close()
Ejemplo n.º 8
0
 def setUp(self):
     """Sets up the needed objects used throughout the test."""
     super(CPIOPortableASCIIFileTest, self).setUp()
     self._resolver_context = context.Context()
     test_file = self._GetTestFilePath(['syslog.odc.cpio'])
     path_spec = os_path_spec.OSPathSpec(location=test_file)
     self._cpio_path_spec = cpio_path_spec.CPIOPathSpec(location='/syslog',
                                                        parent=path_spec)
Ejemplo n.º 9
0
 def setUp(self):
     """Sets up the needed objects used throughout the test."""
     super(CPIONewASCIIFileTest, self).setUp()
     self._resolver_context = context.Context()
     test_file = os.path.join(u'test_data', u'syslog.newc.cpio')
     path_spec = os_path_spec.OSPathSpec(location=test_file)
     self._cpio_path_spec = cpio_path_spec.CPIOPathSpec(location=u'/syslog',
                                                        parent=path_spec)
Ejemplo n.º 10
0
    def testInitialize(self):
        """Tests the path specification initialization."""
        path_spec = cpio_path_spec.CPIOPathSpec(location='/test',
                                                parent=self._path_spec)

        self.assertIsNotNone(path_spec)

        with self.assertRaises(ValueError):
            cpio_path_spec.CPIOPathSpec(location='/test', parent=None)

        with self.assertRaises(ValueError):
            cpio_path_spec.CPIOPathSpec(location=None, parent=self._path_spec)

        with self.assertRaises(ValueError):
            cpio_path_spec.CPIOPathSpec(location='/test',
                                        parent=self._path_spec,
                                        bogus='BOGUS')
Ejemplo n.º 11
0
    def GetRootFileEntry(self):
        """Retrieves the root file entry.

    Returns:
      A file entry (instance of vfs.FileEntry).
    """
        path_spec = cpio_path_spec.CPIOPathSpec(location=self.LOCATION_ROOT,
                                                parent=self._path_spec.parent)
        return self.GetFileEntryByPathSpec(path_spec)
Ejemplo n.º 12
0
  def GetRootFileEntry(self):
    """Retrieves the root file entry.

    Returns:
      CPIOFileEntry: a file entry or None if not available.
    """
    path_spec = cpio_path_spec.CPIOPathSpec(
        location=self.LOCATION_ROOT, parent=self._path_spec.parent)
    return self.GetFileEntryByPathSpec(path_spec)
Ejemplo n.º 13
0
    def setUp(self):
        """Sets up the needed objects used throughout the test."""
        super(CPIOBinaryFileTest, self).setUp()
        self._resolver_context = context.Context()
        test_file = self._GetTestFilePath(['syslog.bin.cpio'])
        self._SkipIfPathNotExists(test_file)

        path_spec = os_path_spec.OSPathSpec(location=test_file)
        self._cpio_path_spec = cpio_path_spec.CPIOPathSpec(location='/syslog',
                                                           parent=path_spec)
Ejemplo n.º 14
0
    def testComparable(self):
        """Tests the path specification comparable property."""
        path_spec = cpio_path_spec.CPIOPathSpec(location=u'/test',
                                                parent=self._path_spec)

        self.assertIsNotNone(path_spec)

        expected_comparable = u'\n'.join(
            [u'type: TEST', u'type: CPIO, location: /test', u''])

        self.assertEqual(path_spec.comparable, expected_comparable)
Ejemplo n.º 15
0
    def testGetFileEntryByPathSpec(self):
        """Tests the GetFileEntryByPathSpec function."""
        file_system = cpio_file_system.CPIOFileSystem(self._resolver_context)
        self.assertIsNotNone(file_system)

        file_system.Open(self._cpio_path_spec)

        path_spec = cpio_path_spec.CPIOPathSpec(location='/syslog',
                                                parent=self._os_path_spec)
        file_entry = file_system.GetFileEntryByPathSpec(path_spec)

        self.assertIsNotNone(file_entry)
        self.assertEqual(file_entry.name, 'syslog')

        path_spec = cpio_path_spec.CPIOPathSpec(location='/bogus',
                                                parent=self._os_path_spec)
        file_entry = file_system.GetFileEntryByPathSpec(path_spec)

        self.assertIsNone(file_entry)

        file_system.Close()
Ejemplo n.º 16
0
    def testGetParentFileEntry(self):
        """Tests the GetParentFileEntry function."""
        path_spec = cpio_path_spec.CPIOPathSpec(location=u'/syslog',
                                                parent=self._os_path_spec)
        file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)
        self.assertIsNotNone(file_entry)

        parent_file_entry = file_entry.GetParentFileEntry()

        self.assertIsNotNone(parent_file_entry)

        self.assertEqual(parent_file_entry.name, u'')
Ejemplo n.º 17
0
    def _EntriesGenerator(self):
        """Retrieves directory entries.

    Since a directory can contain a vast number of entries using
    a generator is more memory efficient.

    Yields:
      CPIOPathSpec: path specification.
    """
        location = getattr(self.path_spec, 'location', None)

        if location and location.startswith(self._file_system.PATH_SEPARATOR):
            cpio_archive_file = self._file_system.GetCPIOArchiveFile()
            sub_directories = set()
            for cpio_archive_file_entry in cpio_archive_file.GetFileEntries(
                    path_prefix=location[1:]):

                path = cpio_archive_file_entry.path
                if not path or path == location:
                    continue

                prefix, suffix = self._file_system.GetPathSegmentAndSuffix(
                    location[1:], path)

                if not suffix:
                    path_spec_location = self._file_system.JoinPath([path])
                    yield cpio_path_spec.CPIOPathSpec(
                        location=path_spec_location,
                        parent=self.path_spec.parent)

                elif prefix not in sub_directories:
                    sub_directories.add(prefix)

                    # Include prefixes as virtual sub directories.
                    path_spec_location = self._file_system.JoinPath([prefix])
                    yield cpio_path_spec.CPIOPathSpec(
                        location=path_spec_location,
                        parent=self.path_spec.parent)
Ejemplo n.º 18
0
    def testGetDataStream(self):
        """Tests the GetDataStream function."""
        path_spec = cpio_path_spec.CPIOPathSpec(location=u'/syslog',
                                                parent=self._os_path_spec)
        file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)
        self.assertIsNotNone(file_entry)

        data_stream_name = u''
        data_stream = file_entry.GetDataStream(data_stream_name)
        self.assertIsNotNone(data_stream)
        self.assertEqual(data_stream.name, data_stream_name)

        data_stream = file_entry.GetDataStream(u'bogus')
        self.assertIsNone(data_stream)
Ejemplo n.º 19
0
    def GetParentFileEntry(self):
        """Retrieves the parent file entry."""
        location = getattr(self.path_spec, u'location', None)
        if location is None:
            return

        parent_location = self._file_system.DirnamePath(location)
        if parent_location is None:
            return
        if parent_location == u'':
            parent_location = self._file_system.PATH_SEPARATOR

        parent_path_spec = getattr(self.path_spec, u'parent', None)
        path_spec = cpio_path_spec.CPIOPathSpec(location=parent_location,
                                                parent=parent_path_spec)
        return CPIOFileEntry(self._resolver_context, self._file_system,
                             path_spec)
Ejemplo n.º 20
0
    def testGetStat(self):
        """Tests the GetStat function."""
        path_spec = cpio_path_spec.CPIOPathSpec(location=u'/syslog',
                                                parent=self._os_path_spec)
        file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)
        self.assertIsNotNone(file_entry)

        stat_object = file_entry.GetStat()

        self.assertIsNotNone(stat_object)
        self.assertEqual(stat_object.type, stat_object.TYPE_FILE)
        self.assertEqual(stat_object.size, 1247)

        self.assertEqual(stat_object.mode, 436)
        self.assertEqual(stat_object.uid, 1000)
        self.assertEqual(stat_object.gid, 1000)

        self.assertEqual(stat_object.mtime, 1432702913)
        self.assertFalse(hasattr(stat_object, u'mtime_nano'))
Ejemplo n.º 21
0
    def testSubFileEntries(self):
        """Test the sub file entries iteration functionality."""
        path_spec = cpio_path_spec.CPIOPathSpec(location=u'/',
                                                parent=self._os_path_spec)
        file_entry = self._file_system.GetFileEntryByPathSpec(path_spec)
        self.assertIsNotNone(file_entry)

        self.assertIsNotNone(file_entry)

        self.assertEqual(file_entry.number_of_sub_file_entries, 1)

        expected_sub_file_entry_names = [u'syslog']

        sub_file_entry_names = []
        for sub_file_entry in file_entry.sub_file_entries:
            sub_file_entry_names.append(sub_file_entry.name)

        self.assertEqual(len(sub_file_entry_names),
                         len(expected_sub_file_entry_names))
        self.assertEqual(sorted(sub_file_entry_names),
                         sorted(expected_sub_file_entry_names))