def GetFileEntryByPathSpec(self, path_spec): """Retrieves a file entry for a path specification. Args: path_spec (PathSpec): a path specification. Returns: APFSContainerFileEntry: a file entry or None if not exists. """ volume_index = apfs_helper.APFSContainerPathSpecGetVolumeIndex( path_spec) # The virtual root file has no corresponding volume index but # should have a location. if volume_index is None: location = getattr(path_spec, 'location', None) if location is None or location != self.LOCATION_ROOT: return None return apfs_container_file_entry.APFSContainerFileEntry( self._resolver_context, self, path_spec, is_root=True, is_virtual=True) if (volume_index < 0 or volume_index >= self._fsapfs_container.number_of_volumes): return None return apfs_container_file_entry.APFSContainerFileEntry( self._resolver_context, self, path_spec)
def _EntriesGenerator(self): """Retrieves directory entries. Since a directory can contain a vast number of entries using a generator is more memory efficient. Yields: APFSContainerPathSpec: a path specification. """ # Only the virtual root file has directory entries. volume_index = apfs_helper.APFSContainerPathSpecGetVolumeIndex( self.path_spec) if volume_index is not None: return location = getattr(self.path_spec, 'location', None) if location is None or location != self._file_system.LOCATION_ROOT: return fsapfs_container = self._file_system.GetAPFSContainer() for volume_index in range(0, fsapfs_container.number_of_volumes): yield apfs_container_path_spec.APFSContainerPathSpec( location='/apfs{0:d}'.format(volume_index + 1), volume_index=volume_index, parent=self.path_spec.parent)
def GetParentFileEntry(self): """Retrieves the parent file entry. Returns: APFSContainerFileEntry: parent file entry or None if not available. """ volume_index = apfs_helper.APFSContainerPathSpecGetVolumeIndex( self.path_spec) if volume_index is None: return None return self._file_system.GetRootFileEntry()
def name(self): """str: name of the file entry, which does not include the full path.""" if self._name is None: location = getattr(self.path_spec, 'location', None) if location is not None: self._name = self._file_system.BasenamePath(location) else: volume_index = apfs_helper.APFSContainerPathSpecGetVolumeIndex( self.path_spec) if volume_index is not None: self._name = 'apfs{0:d}'.format(volume_index + 1) else: self._name = '' return self._name
def GetAPFSVolumeByPathSpec(self, path_spec): """Retrieves an APFS volume for a path specification. Args: path_spec (PathSpec): path specification. Returns: pyfsapfs.volume: an APFS volume or None if not available. """ volume_index = apfs_helper.APFSContainerPathSpecGetVolumeIndex( path_spec) if volume_index is None: return None return self._fsapfs_container.get_volume(volume_index)
def testAPFSContainerPathSpecGetVolumeIndex(self): """Tests the APFSContainerPathSpecGetVolumeIndex function.""" test_fake_path_spec = fake_path_spec.FakePathSpec(location='/') path_spec = apfs_container_path_spec.APFSContainerPathSpec( parent=test_fake_path_spec) self.assertIsNotNone(path_spec) volume_index = apfs_helper.APFSContainerPathSpecGetVolumeIndex( path_spec) self.assertIsNone(volume_index) path_spec = apfs_container_path_spec.APFSContainerPathSpec( location='/apfs2', parent=test_fake_path_spec) self.assertIsNotNone(path_spec) volume_index = apfs_helper.APFSContainerPathSpecGetVolumeIndex( path_spec) self.assertEqual(volume_index, 1) path_spec = apfs_container_path_spec.APFSContainerPathSpec( volume_index=1, parent=test_fake_path_spec) self.assertIsNotNone(path_spec) volume_index = apfs_helper.APFSContainerPathSpecGetVolumeIndex( path_spec) self.assertEqual(volume_index, 1) path_spec = apfs_container_path_spec.APFSContainerPathSpec( location='/apfs2', volume_index=1, parent=test_fake_path_spec) self.assertIsNotNone(path_spec) volume_index = apfs_helper.APFSContainerPathSpecGetVolumeIndex( path_spec) self.assertEqual(volume_index, 1) path_spec = apfs_container_path_spec.APFSContainerPathSpec( location='/apfs', parent=test_fake_path_spec) volume_index = apfs_helper.APFSContainerPathSpecGetVolumeIndex( path_spec) self.assertIsNone(volume_index) path_spec = apfs_container_path_spec.APFSContainerPathSpec( location='/apfs101', parent=test_fake_path_spec) volume_index = apfs_helper.APFSContainerPathSpecGetVolumeIndex( path_spec) self.assertIsNone(volume_index)
def FileEntryExistsByPathSpec(self, path_spec): """Determines if a file entry for a path specification exists. Args: path_spec (PathSpec): a path specification. Returns: bool: True if the file entry exists. """ volume_index = apfs_helper.APFSContainerPathSpecGetVolumeIndex( path_spec) # The virtual root file has no corresponding volume index but # should have a location. if volume_index is None: location = getattr(path_spec, 'location', None) return location is not None and location == self.LOCATION_ROOT return 0 <= volume_index < self._fsapfs_container.number_of_volumes