Skip to content

Fix adb.partitions listing #802

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 14, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 43 additions & 3 deletions pwnlib/adb/adb.py
Original file line number Diff line number Diff line change
Expand Up @@ -951,12 +951,51 @@ def data(self):
with log.waitfor('Fetching %r partition (%s)' % (self.name, self.path)):
return read(self.path)

@with_device
def walk(top, topdown=True):
join = os.path.join
isdir = lambda x: stat.S_ISDIR(x['mode'])
client = Client()
names = client.list(top)

dirs, nondirs = [], []
for name, metadata in names.items():
if isdir(metadata):
dirs.append(name)
else:
nondirs.append(name)

if topdown:
yield top, dirs, nondirs
for name in dirs:
new_path = join(top, name)
for x in walk(new_path, topdown):
yield x
if not topdown:
yield top, dirs, nondirs

@with_device
def find(top, name):
for root, dirs, files in walk(top):
if name in files or name in dirs:
yield os.path.join(root, name)

@with_device
def readlink(path):
path = process(['readlink', path]).recvall()

# Readlink will emit a single newline
# We can't use the '-n' flag since old versions don't support it
if path.endswith('\n'):
path = path[:-1]

return path

class Partitions(object):
@property
@context.quiet
def by_name_dir(self):
cmd = ['shell','find /dev/block/platform -type d -name by-name']
return adb(cmd).strip()
return next(find('/dev/block/platform','by-name'))

@context.quiet
def __dir__(self):
Expand All @@ -972,6 +1011,7 @@ def __iter__(self):
yield name

@context.quiet
@with_device
def __getattr__(self, attr):
for name in self:
if name == attr:
Expand All @@ -982,7 +1022,7 @@ def __getattr__(self, attr):
path = os.path.join(self.by_name_dir, name)

# Find the actual path of the device
devpath = process(['readlink', '-n', path]).recvall()
devpath = readlink(path)
devname = os.path.basename(devpath)

# Get the size of the partition
Expand Down