forked from log2timeline/dfvfs
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added attributes support for fsapfs back-end log2timeline#504
- Loading branch information
1 parent
25545e9
commit 1fdbecb
Showing
4 changed files
with
297 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
# -*- coding: utf-8 -*- | ||
"""The APFS attribute implementation.""" | ||
|
||
import os | ||
|
||
from dfvfs.lib import errors | ||
from dfvfs.vfs import attribute | ||
|
||
|
||
class APFSExtendedAttribute(attribute.Attribute): | ||
"""APFS extended attribute that uses pyfsapfs.""" | ||
|
||
def __init__(self, fsapfs_extended_attribute): | ||
"""Initializes an attribute. | ||
Args: | ||
fsapfs_extended_attribute (pyfsapfs.extended_attribute): APFS extended | ||
attribute. | ||
Raises: | ||
BackEndError: if the pyfsapfs extended attribute is missing. | ||
""" | ||
if not fsapfs_extended_attribute: | ||
raise errors.BackEndError('Missing pyfsapfs extended attribute.') | ||
|
||
super(APFSExtendedAttribute, self).__init__() | ||
self._fsapfs_extended_attribute = fsapfs_extended_attribute | ||
|
||
@property | ||
def name(self): | ||
"""str: name.""" | ||
return self._fsapfs_extended_attribute.name | ||
|
||
# Note: that the following functions do not follow the style guide | ||
# because they are part of the file-like object interface. | ||
# pylint: disable=invalid-name | ||
|
||
def read(self, size=None): | ||
"""Reads a byte string from the file input/output (IO) object. | ||
The function will read a byte string of the specified size or | ||
all of the remaining data if no size was specified. | ||
Args: | ||
size (Optional[int]): number of bytes to read, where None is all | ||
remaining data. | ||
Returns: | ||
bytes: data read. | ||
Raises: | ||
IOError: if the read failed. | ||
OSError: if the read failed. | ||
""" | ||
return self._fsapfs_extended_attribute.read_buffer(size) | ||
|
||
def seek(self, offset, whence=os.SEEK_SET): | ||
"""Seeks to an offset within the file input/output (IO) object. | ||
Args: | ||
offset (int): offset to seek. | ||
whence (Optional[int]): value that indicates whether offset is an | ||
absolute or relative position within the file. | ||
Raises: | ||
IOError: if the seek failed. | ||
OSError: if the seek failed. | ||
""" | ||
self._fsapfs_extended_attribute.seek_offset(offset, whence) | ||
|
||
# get_offset() is preferred above tell() by the libbfio layer used in libyal. | ||
def get_offset(self): | ||
"""Retrieves the current offset into the file input/output (IO) object. | ||
Returns: | ||
int: current offset into the file input/output (IO) object. | ||
Raises: | ||
IOError: if the file input/output (IO)-like object has not been opened. | ||
OSError: if the file input/output (IO)-like object has not been opened. | ||
""" | ||
return self._fsapfs_extended_attribute.get_offset() | ||
|
||
# Pythonesque alias for get_offset(). | ||
def tell(self): | ||
"""Retrieves the current offset into the file input/output (IO) object.""" | ||
return self.get_offset() | ||
|
||
def get_size(self): | ||
"""Retrieves the size of the file input/output (IO) object. | ||
Returns: | ||
int: size of the file input/output (IO) object. | ||
Raises: | ||
IOError: if the file input/output (IO) object has not been opened. | ||
OSError: if the file input/output (IO) object has not been opened. | ||
""" | ||
return self._fsapfs_extended_attribute.get_size() | ||
|
||
def seekable(self): | ||
"""Determines if a file input/output (IO) object is seekable. | ||
Returns: | ||
bool: True since a file IO object provides a seek method. | ||
""" | ||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
"""Tests for the APFS attribute.""" | ||
|
||
import unittest | ||
|
||
from dfvfs.lib import definitions | ||
from dfvfs.lib import errors | ||
from dfvfs.path import factory as path_spec_factory | ||
from dfvfs.resolver import context | ||
from dfvfs.vfs import apfs_attribute | ||
from dfvfs.vfs import apfs_file_system | ||
|
||
from tests import test_lib as shared_test_lib | ||
|
||
|
||
class APFSAttributeTest(shared_test_lib.BaseTestCase): | ||
"""Tests the APFS attribute.""" | ||
|
||
# pylint: disable=protected-access | ||
|
||
_IDENTIFIER_A_FILE = 17 | ||
|
||
def setUp(self): | ||
"""Sets up the needed objects used throughout the test.""" | ||
self._resolver_context = context.Context() | ||
test_path = self._GetTestFilePath(['apfs.raw']) | ||
self._SkipIfPathNotExists(test_path) | ||
|
||
test_os_path_spec = path_spec_factory.Factory.NewPathSpec( | ||
definitions.TYPE_INDICATOR_OS, location=test_path) | ||
test_raw_path_spec = path_spec_factory.Factory.NewPathSpec( | ||
definitions.TYPE_INDICATOR_RAW, parent=test_os_path_spec) | ||
self._apfs_container_path_spec = path_spec_factory.Factory.NewPathSpec( | ||
definitions.TYPE_INDICATOR_APFS_CONTAINER, location='/apfs1', | ||
parent=test_raw_path_spec) | ||
self._apfs_path_spec = path_spec_factory.Factory.NewPathSpec( | ||
definitions.TYPE_INDICATOR_APFS, location='/', | ||
parent=self._apfs_container_path_spec) | ||
|
||
self._file_system = apfs_file_system.APFSFileSystem( | ||
self._resolver_context, self._apfs_path_spec) | ||
self._file_system.Open() | ||
|
||
def tearDown(self): | ||
"""Cleans up the needed objects used throughout the test.""" | ||
self._resolver_context.Empty() | ||
|
||
def testIntialize(self): | ||
"""Tests the __init__ function.""" | ||
test_location = '/a_directory/a_file' | ||
path_spec = path_spec_factory.Factory.NewPathSpec( | ||
definitions.TYPE_INDICATOR_APFS, identifier=self._IDENTIFIER_A_FILE, | ||
location=test_location, parent=self._apfs_container_path_spec) | ||
file_entry = self._file_system.GetFileEntryByPathSpec(path_spec) | ||
|
||
fsapfs_attribute = file_entry._fsapfs_file_entry.get_extended_attribute(0) | ||
self.assertIsNotNone(fsapfs_attribute) | ||
self.assertEqual(fsapfs_attribute.name, 'myxattr') | ||
|
||
test_attribute = apfs_attribute.APFSExtendedAttribute(fsapfs_attribute) | ||
self.assertIsNotNone(test_attribute) | ||
|
||
with self.assertRaises(errors.BackEndError): | ||
apfs_attribute.APFSExtendedAttribute(None) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters