/
fvde_file_system.py
105 lines (80 loc) · 2.82 KB
/
fvde_file_system.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# -*- coding: utf-8 -*-
"""The FVDE file system implementation."""
from __future__ import unicode_literals
import pyfvde
from dfvfs.lib import fvde
from dfvfs.lib import definitions
from dfvfs.lib import errors
from dfvfs.path import fvde_path_spec
from dfvfs.resolver import resolver
from dfvfs.vfs import fvde_file_entry
from dfvfs.vfs import root_only_file_system
class FVDEFileSystem(root_only_file_system.RootOnlyFileSystem):
"""Class that implements a file system using FVDE."""
TYPE_INDICATOR = definitions.TYPE_INDICATOR_FVDE
def __init__(self, resolver_context):
"""Initializes a file system.
Args:
resolver_context (Context): resolver context.
"""
super(FVDEFileSystem, self).__init__(resolver_context)
self._fvde_volume = None
self._file_object = None
def _Close(self):
"""Closes the file system.
Raises:
IOError: if the close failed.
"""
self._fvde_volume.close()
self._fvde_volume = None
self._file_object.close()
self._file_object = None
def _Open(self, path_spec, mode='rb'):
"""Opens the file system defined by path specification.
Args:
path_spec (PathSpec): path specification.
mode (Optional[str]): file access mode. The default is 'rb'
read-only binary.
Raises:
AccessError: if the access to open the file was denied.
IOError: if the file system could not be opened.
PathSpecError: if the path specification is incorrect.
ValueError: if the path specification is invalid.
"""
if not path_spec.HasParent():
raise errors.PathSpecError(
'Unsupported path specification without parent.')
resolver.Resolver.key_chain.ExtractCredentialsFromPathSpec(path_spec)
fvde_volume = pyfvde.volume()
file_object = resolver.Resolver.OpenFileObject(
path_spec.parent, resolver_context=self._resolver_context)
try:
fvde.FVDEVolumeOpen(
fvde_volume, path_spec, file_object, resolver.Resolver.key_chain)
except:
file_object.close()
raise
self._fvde_volume = fvde_volume
self._file_object = file_object
def GetFVDEVolume(self):
"""Retrieves the FVDE volume.
Returns:
pyfvde.volume: FVDE volume.
"""
return self._fvde_volume
def GetFileEntryByPathSpec(self, path_spec):
"""Retrieves a file entry for a path specification.
Args:
path_spec (PathSpec): path specification.
Returns:
FVDEFileEntry: file entry or None.
"""
return fvde_file_entry.FVDEFileEntry(
self._resolver_context, self, path_spec, is_root=True, is_virtual=True)
def GetRootFileEntry(self):
"""Retrieves the root file entry.
Returns:
FVDEFileEntry: file entry or None.
"""
path_spec = fvde_path_spec.FVDEPathSpec(parent=self._path_spec.parent)
return self.GetFileEntryByPathSpec(path_spec)