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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
#!/usr/bin/env python
#
# Copyright (C) 2019 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""deapexer is a tool that prints out content of an APEX.
To print content of an APEX to stdout:
deapexer list foo.apex
To extract content of an APEX to the given directory:
deapexer extract foo.apex dest
"""
import argparse
import os
import shutil
import sys
import subprocess
import tempfile
import zipfile
class ApexImageEntry(object):
def __init__(self, name, base_dir, permissions, is_directory=False, is_symlink=False):
self._name = name
self._base_dir = base_dir
self._permissions = permissions
self._is_directory = is_directory
self._is_symlink = is_symlink
@property
def name(self):
return self._name
@property
def full_path(self):
return os.path.join(self._base_dir, self._name)
@property
def is_directory(self):
return self._is_directory
@property
def is_symlink(self):
return self._is_symlink
@property
def is_regular_file(self):
return not self.is_directory and not self.is_symlink
@property
def permissions(self):
return self._permissions
def __str__(self):
ret = ''
if self._is_directory:
ret += 'd'
elif self._is_symlink:
ret += 'l'
else:
ret += '-'
def mask_as_string(m):
ret = 'r' if m & 4 == 4 else '-'
ret += 'w' if m & 2 == 2 else '-'
ret += 'x' if m & 1 == 1 else '-'
return ret
ret += mask_as_string(self._permissions >> 6)
ret += mask_as_string((self._permissions >> 3) & 7)
ret += mask_as_string(self._permissions & 7)
return ret + ' ' + self._name
class ApexImageDirectory(object):
def __init__(self, path, entries, apex):
self._path = path
self._entries = sorted(entries, key=lambda e: e.name)
self._apex = apex
def list(self, is_recursive=False):
for e in self._entries:
yield e
if e.is_directory and e.name != '.' and e.name != '..':
for ce in self.enter_subdir(e).list(is_recursive):
yield ce
def enter_subdir(self, entry):
return self._apex._list(self._path + entry.name + '/')
def extract(self, dest):
path = self._path
self._apex._extract(self._path, dest)
class Apex(object):
def __init__(self, apex):
self._debugfs = '%s/bin/debugfs' % os.environ['ANDROID_HOST_OUT']
self._apex = apex
self._tempdir = tempfile.mkdtemp()
# TODO(b/139125405): support flattened APEXes.
with zipfile.ZipFile(self._apex, 'r') as zip_ref:
self._payload = zip_ref.extract('apex_payload.img', path=self._tempdir)
self._cache = {}
def __del__(self):
shutil.rmtree(self._tempdir)
def __enter__(self):
return self._list('./')
def __exit__(self, type, value, traceback):
pass
def _list(self, path):
if path in self._cache:
return self._cache[path]
process = subprocess.Popen([self._debugfs, '-R', 'ls -l -p %s' % path, self._payload],
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
universal_newlines=True)
stdout, _ = process.communicate()
res = str(stdout)
entries = []
for line in res.split('\n'):
if not line:
continue
parts = line.split('/')
if len(parts) != 8:
continue
name = parts[5]
if not name:
continue
bits = parts[2]
entries.append(ApexImageEntry(name, base_dir=path, permissions=int(bits[3:], 8),
is_directory=bits[1]=='4', is_symlink=bits[1]=='2'))
return ApexImageDirectory(path, entries, self)
def _extract(self, path, dest):
process = subprocess.Popen([self._debugfs, '-R', 'rdump %s %s' % (path, dest), self._payload],
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
universal_newlines=True)
_, stderr = process.communicate()
print(stderr, file=sys.stderr)
def RunList(args):
with Apex(args.apex) as apex:
for e in apex.list(is_recursive=True):
if e.is_regular_file:
print(e.full_path)
def RunExtract(args):
with Apex(args.apex) as apex:
os.makedirs(args.dest, mode=0o755, exist_ok=True)
apex.extract(args.dest)
def main(argv):
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()
parser_list = subparsers.add_parser('list', help='prints content of an APEX to stdout')
parser_list.add_argument('apex', type=str, help='APEX file')
parser_list.set_defaults(func=RunList)
parser_extract = subparsers.add_parser('extract', help='extracts content of an APEX to the given '
'directory')
parser_extract.add_argument('apex', type=str, help='APEX file')
parser_extract.add_argument('dest', type=str, help='Directory to extract content of APEX to')
parser_extract.set_defaults(func=RunExtract)
args = parser.parse_args(argv)
args.func(args)
if __name__ == '__main__':
main(sys.argv[1:])
|