Merge "Adf: Remove unused variable"
diff --git a/adb/__init__.py b/adb/__init__.py
new file mode 100644
index 0000000..6b509c6
--- /dev/null
+++ b/adb/__init__.py
@@ -0,0 +1,17 @@
+#
+# Copyright (C) 2015 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.
+#
+from __future__ import absolute_import
+from .device import * # pylint: disable=wildcard-import
diff --git a/adb/device.py b/adb/device.py
new file mode 100644
index 0000000..601989b
--- /dev/null
+++ b/adb/device.py
@@ -0,0 +1,233 @@
+#
+# Copyright (C) 2015 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.
+#
+import os
+import re
+import subprocess
+
+
+class FindDeviceError(RuntimeError):
+ pass
+
+
+class DeviceNotFoundError(FindDeviceError):
+ def __init__(self, serial):
+ self.serial = serial
+ super(DeviceNotFoundError, self).__init__(
+ 'No device with serial {}'.format(serial))
+
+
+class NoUniqueDeviceError(FindDeviceError):
+ def __init__(self):
+ super(NoUniqueDeviceError, self).__init__('No unique device')
+
+
+def get_devices():
+ with open(os.devnull, 'wb') as devnull:
+ subprocess.check_call(['adb', 'start-server'], stdout=devnull,
+ stderr=devnull)
+ out = subprocess.check_output(['adb', 'devices']).splitlines()
+
+ # The first line of `adb devices` just says "List of attached devices", so
+ # skip that.
+ devices = []
+ for line in out[1:]:
+ if not line.strip():
+ continue
+ if 'offline' in line:
+ continue
+
+ serial, _ = re.split(r'\s+', line, maxsplit=1)
+ devices.append(serial)
+ return devices
+
+
+def _get_unique_device(product=None):
+ devices = get_devices()
+ if len(devices) != 1:
+ raise NoUniqueDeviceError()
+ return AndroidDevice(devices[0], product)
+
+def _get_device_by_serial(serial, product=None):
+ for device in get_devices():
+ if device == serial:
+ return AndroidDevice(serial, product)
+ raise DeviceNotFoundError(serial)
+
+
+def get_device(serial=None, product=None):
+ """Get a uniquely identified AndroidDevice if one is available.
+
+ Raises:
+ DeviceNotFoundError:
+ The serial specified by `serial` or $ANDROID_SERIAL is not
+ connected.
+
+ NoUniqueDeviceError:
+ Neither `serial` nor $ANDROID_SERIAL was set, and the number of
+ devices connected to the system is not 1. Having 0 connected
+ devices will also result in this error.
+
+ Returns:
+ An AndroidDevice associated with the first non-None identifier in the
+ following order of preference:
+
+ 1) The `serial` argument.
+ 2) The environment variable $ANDROID_SERIAL.
+ 3) The single device connnected to the system.
+ """
+ if serial is not None:
+ return _get_device_by_serial(serial, product)
+
+ android_serial = os.getenv('ANDROID_SERIAL')
+ if android_serial is not None:
+ return _get_device_by_serial(android_serial, product)
+
+ return _get_unique_device(product)
+
+
+class AndroidDevice(object):
+ def __init__(self, serial, product=None):
+ self.serial = serial
+ self.product = product
+ self.adb_cmd = ['adb']
+ if self.serial is not None:
+ self.adb_cmd.extend(['-s', serial])
+ if self.product is not None:
+ self.adb_cmd.extend(['-p', product])
+ self._linesep = None
+ self._shell_result_pattern = None
+
+ @property
+ def linesep(self):
+ if self._linesep is None:
+ self._linesep = subprocess.check_output(['adb', 'shell', 'echo'])
+ return self._linesep
+
+ def _make_shell_cmd(self, user_cmd):
+ # Follow any shell command with `; echo; echo $?` to get the exit
+ # status of a program since this isn't propagated by adb.
+ #
+ # The leading newline is needed because `printf 1; echo $?` would print
+ # "10", and we wouldn't be able to distinguish the exit code.
+ rc_probe = '; echo "\n$?"'
+ return self.adb_cmd + ['shell'] + user_cmd + [rc_probe]
+
+ def _parse_shell_output(self, out): # pylint: disable=no-self-use
+ search_text = out
+ max_result_len = len('{0}255{0}'.format(self.linesep))
+ if len(search_text) > max_result_len:
+ # We don't want to regex match over massive amounts of data when we
+ # know the part we want is right at the end.
+ search_text = search_text[-max_result_len:]
+ if self._shell_result_pattern is None:
+ self._shell_result_pattern = re.compile(
+ r'({0}\d+{0})$'.format(self.linesep), re.MULTILINE)
+ m = self._shell_result_pattern.search(search_text)
+ if m is None:
+ raise RuntimeError('Could not find exit status in shell output.')
+
+ result_text = m.group(1)
+ result = int(result_text.strip())
+ out = out[:-len(result_text)] # Trim the result text from the output.
+ return result, out
+
+ def _simple_call(self, cmd):
+ return subprocess.check_output(
+ self.adb_cmd + cmd, stderr=subprocess.STDOUT)
+
+ def shell(self, cmd):
+ cmd = self._make_shell_cmd(cmd)
+ out = subprocess.check_output(cmd)
+ rc, out = self._parse_shell_output(out)
+ if rc != 0:
+ error = subprocess.CalledProcessError(rc, cmd)
+ error.out = out
+ raise error
+ return out
+
+ def shell_nocheck(self, cmd):
+ cmd = self._make_shell_cmd(cmd)
+ p = subprocess.Popen(
+ cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
+ out, _ = p.communicate()
+ return self._parse_shell_output(out)
+
+ def install(self, filename):
+ return self._simple_call(['install', filename])
+
+ def push(self, local, remote):
+ return self._simple_call(['push', local, remote])
+
+ def pull(self, remote, local):
+ return self._simple_call(['pull', remote, local])
+
+ def sync(self, directory=None):
+ cmd = ['sync']
+ if directory is not None:
+ cmd.append(directory)
+ return self._simple_call(cmd)
+
+ def forward(self, local, remote):
+ return self._simple_call(['forward', local, remote])
+
+ def tcpip(self, port):
+ return self._simple_call(['tcpip', port])
+
+ def usb(self):
+ return self._simple_call(['usb'])
+
+ def root(self):
+ return self._simple_call(['root'])
+
+ def unroot(self):
+ return self._simple_call(['unroot'])
+
+ def forward_remove(self, local):
+ return self._simple_call(['forward', '--remove', local])
+
+ def forward_remove_all(self):
+ return self._simple_call(['forward', '--remove-all'])
+
+ def connect(self, host):
+ return self._simple_call(['connect', host])
+
+ def disconnect(self, host):
+ return self._simple_call(['disconnect', host])
+
+ def reverse(self, remote, local):
+ return self._simple_call(['reverse', remote, local])
+
+ def reverse_remove_all(self):
+ return self._simple_call(['reverse', '--remove-all'])
+
+ def reverse_remove(self, remote):
+ return self._simple_call(['reverse', '--remove', remote])
+
+ def wait(self):
+ return self._simple_call(['wait-for-device'])
+
+ def get_prop(self, prop_name):
+ output = self.shell(['getprop', prop_name])
+ if len(output) != 1:
+ raise RuntimeError('Too many lines in getprop output:\n' +
+ '\n'.join(output))
+ value = output[0]
+ if not value.strip():
+ return None
+ return value
+
+ def set_prop(self, prop_name, value):
+ self.shell(['setprop', prop_name, value])
diff --git a/adb/test_adb.py b/adb/test_adb.py
new file mode 100644
index 0000000..59aa14d
--- /dev/null
+++ b/adb/test_adb.py
@@ -0,0 +1,77 @@
+#!/usr/bin/env python
+#
+# Copyright (C) 2015 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.
+#
+"""Tests for the adb program itself.
+
+This differs from things in test_device.py in that there is no API for these
+things. Most of these tests involve specific error messages or the help text.
+"""
+from __future__ import print_function
+
+import random
+import subprocess
+import unittest
+
+import adb
+
+
+class NonApiTest(unittest.TestCase):
+ """Tests for ADB that aren't a part of the AndroidDevice API."""
+
+ def test_help(self):
+ """Make sure we get _something_ out of help."""
+ out = subprocess.check_output(
+ ['adb', 'help'], stderr=subprocess.STDOUT)
+ self.assertGreater(len(out), 0)
+
+ def test_version(self):
+ """Get a version number out of the output of adb."""
+ lines = subprocess.check_output(['adb', 'version']).splitlines()
+ version_line = lines[0]
+ self.assertRegexpMatches(
+ version_line, r'^Android Debug Bridge version \d+\.\d+\.\d+$')
+ if len(lines) == 2:
+ # Newer versions of ADB have a second line of output for the
+ # version that includes a specific revision (git SHA).
+ revision_line = lines[1]
+ self.assertRegexpMatches(
+ revision_line, r'^Revision [0-9a-f]{12}-android$')
+
+ def test_tcpip_error_messages(self):
+ p = subprocess.Popen(['adb', 'tcpip'], stdout=subprocess.PIPE,
+ stderr=subprocess.STDOUT)
+ out, _ = p.communicate()
+ self.assertEqual(1, p.returncode)
+ self.assertIn('help message', out)
+
+ p = subprocess.Popen(['adb', 'tcpip', 'foo'], stdout=subprocess.PIPE,
+ stderr=subprocess.STDOUT)
+ out, _ = p.communicate()
+ self.assertEqual(1, p.returncode)
+ self.assertIn('error', out)
+
+
+def main():
+ random.seed(0)
+ if len(adb.get_devices()) > 0:
+ suite = unittest.TestLoader().loadTestsFromName(__name__)
+ unittest.TextTestRunner(verbosity=3).run(suite)
+ else:
+ print('Test suite must be run with attached devices')
+
+
+if __name__ == '__main__':
+ main()
diff --git a/adb/test_device.py b/adb/test_device.py
new file mode 100644
index 0000000..6c20b6e
--- /dev/null
+++ b/adb/test_device.py
@@ -0,0 +1,424 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+#
+# Copyright (C) 2015 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.
+#
+from __future__ import print_function
+
+import hashlib
+import os
+import posixpath
+import random
+import shlex
+import shutil
+import subprocess
+import tempfile
+import unittest
+
+import mock
+
+import adb
+
+
+class GetDeviceTest(unittest.TestCase):
+ def setUp(self):
+ self.android_serial = os.getenv('ANDROID_SERIAL')
+ del os.environ['ANDROID_SERIAL']
+
+ def tearDown(self):
+ os.environ['ANDROID_SERIAL'] = self.android_serial
+
+ @mock.patch('adb.device.get_devices')
+ def test_explicit(self, mock_get_devices):
+ mock_get_devices.return_value = ['foo', 'bar']
+ device = adb.get_device('foo')
+ self.assertEqual(device.serial, 'foo')
+
+ @mock.patch('adb.device.get_devices')
+ def test_from_env(self, mock_get_devices):
+ mock_get_devices.return_value = ['foo', 'bar']
+ os.environ['ANDROID_SERIAL'] = 'foo'
+ device = adb.get_device()
+ self.assertEqual(device.serial, 'foo')
+
+ @mock.patch('adb.device.get_devices')
+ def test_arg_beats_env(self, mock_get_devices):
+ mock_get_devices.return_value = ['foo', 'bar']
+ os.environ['ANDROID_SERIAL'] = 'bar'
+ device = adb.get_device('foo')
+ self.assertEqual(device.serial, 'foo')
+
+ @mock.patch('adb.device.get_devices')
+ def test_no_such_device(self, mock_get_devices):
+ mock_get_devices.return_value = ['foo', 'bar']
+ self.assertRaises(adb.DeviceNotFoundError, adb.get_device, ['baz'])
+
+ os.environ['ANDROID_SERIAL'] = 'baz'
+ self.assertRaises(adb.DeviceNotFoundError, adb.get_device)
+
+ @mock.patch('adb.device.get_devices')
+ def test_unique_device(self, mock_get_devices):
+ mock_get_devices.return_value = ['foo']
+ device = adb.get_device()
+ self.assertEqual(device.serial, 'foo')
+
+ @mock.patch('adb.device.get_devices')
+ def test_no_unique_device(self, mock_get_devices):
+ mock_get_devices.return_value = ['foo', 'bar']
+ self.assertRaises(adb.NoUniqueDeviceError, adb.get_device)
+
+
+class DeviceTest(unittest.TestCase):
+ def setUp(self):
+ self.device = adb.get_device()
+
+
+class ShellTest(DeviceTest):
+ def test_cat(self):
+ """Check that we can at least cat a file."""
+ out = self.device.shell(['cat', '/proc/uptime']).strip()
+ elements = out.split()
+ self.assertEqual(len(elements), 2)
+
+ uptime, idle = elements
+ self.assertGreater(float(uptime), 0.0)
+ self.assertGreater(float(idle), 0.0)
+
+ def test_throws_on_failure(self):
+ self.assertRaises(subprocess.CalledProcessError,
+ self.device.shell, ['false'])
+
+ def test_output_not_stripped(self):
+ out = self.device.shell(['echo', 'foo'])
+ self.assertEqual(out, 'foo' + self.device.linesep)
+
+ def test_shell_nocheck_failure(self):
+ rc, out = self.device.shell_nocheck(['false'])
+ self.assertNotEqual(rc, 0)
+ self.assertEqual(out, '')
+
+ def test_shell_nocheck_output_not_stripped(self):
+ rc, out = self.device.shell_nocheck(['echo', 'foo'])
+ self.assertEqual(rc, 0)
+ self.assertEqual(out, 'foo' + self.device.linesep)
+
+ def test_can_distinguish_tricky_results(self):
+ # If result checking on ADB shell is naively implemented as
+ # `adb shell <cmd>; echo $?`, we would be unable to distinguish the
+ # output from the result for a cmd of `echo -n 1`.
+ rc, out = self.device.shell_nocheck(['echo', '-n', '1'])
+ self.assertEqual(rc, 0)
+ self.assertEqual(out, '1')
+
+ def test_line_endings(self):
+ """Ensure that line ending translation is not happening in the pty.
+
+ Bug: http://b/19735063
+ """
+ output = self.device.shell(['uname'])
+ self.assertEqual(output, 'Linux' + self.device.linesep)
+
+
+class ArgumentEscapingTest(DeviceTest):
+ def test_shell_escaping(self):
+ """Make sure that argument escaping is somewhat sane."""
+
+ # http://b/19734868
+ # Note that this actually matches ssh(1)'s behavior --- it's
+ # converted to `sh -c echo hello; echo world` which sh interprets
+ # as `sh -c echo` (with an argument to that shell of "hello"),
+ # and then `echo world` back in the first shell.
+ result = self.device.shell(
+ shlex.split("sh -c 'echo hello; echo world'"))
+ result = result.splitlines()
+ self.assertEqual(['', 'world'], result)
+ # If you really wanted "hello" and "world", here's what you'd do:
+ result = self.device.shell(
+ shlex.split(r'echo hello\;echo world')).splitlines()
+ self.assertEqual(['hello', 'world'], result)
+
+ # http://b/15479704
+ result = self.device.shell(shlex.split("'true && echo t'")).strip()
+ self.assertEqual('t', result)
+ result = self.device.shell(
+ shlex.split("sh -c 'true && echo t'")).strip()
+ self.assertEqual('t', result)
+
+ # http://b/20564385
+ result = self.device.shell(shlex.split('FOO=a BAR=b echo t')).strip()
+ self.assertEqual('t', result)
+ result = self.device.shell(shlex.split(r'echo -n 123\;uname')).strip()
+ self.assertEqual('123Linux', result)
+
+ def test_install_argument_escaping(self):
+ """Make sure that install argument escaping works."""
+ # http://b/20323053
+ tf = tempfile.NamedTemporaryFile('wb', suffix='-text;ls;1.apk')
+ self.assertIn("-text;ls;1.apk", self.device.install(tf.name))
+
+ # http://b/3090932
+ tf = tempfile.NamedTemporaryFile('wb', suffix="-Live Hold'em.apk")
+ self.assertIn("-Live Hold'em.apk", self.device.install(tf.name))
+
+
+class RootUnrootTest(DeviceTest):
+ def _test_root(self):
+ message = self.device.root()
+ if 'adbd cannot run as root in production builds' in message:
+ return
+ self.device.wait()
+ self.assertEqual('root', self.device.shell(['id', '-un']).strip())
+
+ def _test_unroot(self):
+ self.device.unroot()
+ self.device.wait()
+ self.assertEqual('shell', self.device.shell(['id', '-un']).strip())
+
+ def test_root_unroot(self):
+ """Make sure that adb root and adb unroot work, using id(1)."""
+ original_user = self.device.shell(['id', '-un']).strip()
+ try:
+ if original_user == 'root':
+ self._test_unroot()
+ self._test_root()
+ elif original_user == 'shell':
+ self._test_root()
+ self._test_unroot()
+ finally:
+ if original_user == 'root':
+ self.device.root()
+ else:
+ self.device.unroot()
+ self.device.wait()
+
+
+class TcpIpTest(DeviceTest):
+ def test_tcpip_failure_raises(self):
+ """adb tcpip requires a port.
+
+ Bug: http://b/22636927
+ """
+ self.assertRaises(
+ subprocess.CalledProcessError, self.device.tcpip, '')
+ self.assertRaises(
+ subprocess.CalledProcessError, self.device.tcpip, 'foo')
+
+
+def compute_md5(string):
+ hsh = hashlib.md5()
+ hsh.update(string)
+ return hsh.hexdigest()
+
+
+def get_md5_prog(device):
+ """Older platforms (pre-L) had the name md5 rather than md5sum."""
+ try:
+ device.shell(['md5sum', '/proc/uptime'])
+ return 'md5sum'
+ except subprocess.CalledProcessError:
+ return 'md5'
+
+
+class HostFile(object):
+ def __init__(self, handle, checksum):
+ self.handle = handle
+ self.checksum = checksum
+ self.full_path = handle.name
+ self.base_name = os.path.basename(self.full_path)
+
+
+class DeviceFile(object):
+ def __init__(self, checksum, full_path):
+ self.checksum = checksum
+ self.full_path = full_path
+ self.base_name = posixpath.basename(self.full_path)
+
+
+def make_random_host_files(in_dir, num_files):
+ min_size = 1 * (1 << 10)
+ max_size = 16 * (1 << 10)
+
+ files = []
+ for _ in xrange(num_files):
+ file_handle = tempfile.NamedTemporaryFile(dir=in_dir, delete=False)
+
+ size = random.randrange(min_size, max_size, 1024)
+ rand_str = os.urandom(size)
+ file_handle.write(rand_str)
+ file_handle.flush()
+ file_handle.close()
+
+ md5 = compute_md5(rand_str)
+ files.append(HostFile(file_handle, md5))
+ return files
+
+
+def make_random_device_files(device, in_dir, num_files):
+ min_size = 1 * (1 << 10)
+ max_size = 16 * (1 << 10)
+
+ files = []
+ for file_num in xrange(num_files):
+ size = random.randrange(min_size, max_size, 1024)
+
+ base_name = 'device_tmpfile' + str(file_num)
+ full_path = os.path.join(in_dir, base_name)
+
+ device.shell(['dd', 'if=/dev/urandom', 'of={}'.format(full_path),
+ 'bs={}'.format(size), 'count=1'])
+ dev_md5, _ = device.shell([get_md5_prog(device), full_path]).split()
+
+ files.append(DeviceFile(dev_md5, full_path))
+ return files
+
+
+class FileOperationsTest(DeviceTest):
+ SCRATCH_DIR = '/data/local/tmp'
+ DEVICE_TEMP_FILE = SCRATCH_DIR + '/adb_test_file'
+ DEVICE_TEMP_DIR = SCRATCH_DIR + '/adb_test_dir'
+
+ def _test_push(self, local_file, checksum):
+ self.device.shell(['rm', '-rf', self.DEVICE_TEMP_FILE])
+ try:
+ self.device.push(
+ local=local_file, remote=self.DEVICE_TEMP_FILE)
+ dev_md5, _ = self.device.shell(
+ [get_md5_prog(self.device), self.DEVICE_TEMP_FILE]).split()
+ self.assertEqual(checksum, dev_md5)
+ finally:
+ self.device.shell(['rm', '-f', self.DEVICE_TEMP_FILE])
+
+ def test_push(self):
+ """Push a randomly generated file to specified device."""
+ kbytes = 512
+ tmp = tempfile.NamedTemporaryFile(mode='wb', delete=False)
+ try:
+ rand_str = os.urandom(1024 * kbytes)
+ tmp.write(rand_str)
+ tmp.close()
+ self._test_push(tmp.name, compute_md5(rand_str))
+ finally:
+ os.remove(tmp.name)
+
+ # TODO: write push directory test.
+
+ def _test_pull(self, remote_file, checksum):
+ tmp_write = tempfile.NamedTemporaryFile(mode='wb', delete=False)
+ try:
+ tmp_write.close()
+ self.device.pull(remote=remote_file, local=tmp_write.name)
+ with open(tmp_write.name, 'rb') as tmp_read:
+ host_contents = tmp_read.read()
+ host_md5 = compute_md5(host_contents)
+ self.assertEqual(checksum, host_md5)
+ finally:
+ os.remove(tmp_write.name)
+
+ def test_pull(self):
+ """Pull a randomly generated file from specified device."""
+ kbytes = 512
+ self.device.shell(['rm', '-rf', self.DEVICE_TEMP_FILE])
+ try:
+ cmd = ['dd', 'if=/dev/urandom',
+ 'of={}'.format(self.DEVICE_TEMP_FILE), 'bs=1024',
+ 'count={}'.format(kbytes)]
+ self.device.shell(cmd)
+ dev_md5, _ = self.device.shell(
+ [get_md5_prog(self.device), self.DEVICE_TEMP_FILE]).split()
+ self._test_pull(self.DEVICE_TEMP_FILE, dev_md5)
+ finally:
+ self.device.shell_nocheck(['rm', self.DEVICE_TEMP_FILE])
+
+ def test_pull_dir(self):
+ """Pull a randomly generated directory of files from the device."""
+ host_dir = tempfile.mkdtemp()
+ try:
+ self.device.shell(['rm', '-rf', self.DEVICE_TEMP_DIR])
+ self.device.shell(['mkdir', '-p', self.DEVICE_TEMP_DIR])
+
+ # Populate device directory with random files.
+ temp_files = make_random_device_files(
+ self.device, in_dir=self.DEVICE_TEMP_DIR, num_files=32)
+
+ self.device.pull(remote=self.DEVICE_TEMP_DIR, local=host_dir)
+
+ for temp_file in temp_files:
+ host_path = os.path.join(host_dir, temp_file.base_name)
+ with open(host_path, 'rb') as host_file:
+ host_md5 = compute_md5(host_file.read())
+ self.assertEqual(host_md5, temp_file.checksum)
+ finally:
+ self.device.shell(['rm', '-rf', self.DEVICE_TEMP_DIR])
+ if host_dir is not None:
+ shutil.rmtree(host_dir)
+
+ def test_sync(self):
+ """Sync a randomly generated directory of files to specified device."""
+ base_dir = tempfile.mkdtemp()
+ try:
+ # Create mirror device directory hierarchy within base_dir.
+ full_dir_path = base_dir + self.DEVICE_TEMP_DIR
+ os.makedirs(full_dir_path)
+
+ # Create 32 random files within the host mirror.
+ temp_files = make_random_host_files(in_dir=full_dir_path,
+ num_files=32)
+
+ # Clean up any trash on the device.
+ device = adb.get_device(product=base_dir)
+ device.shell(['rm', '-rf', self.DEVICE_TEMP_DIR])
+
+ device.sync('data')
+
+ # Confirm that every file on the device mirrors that on the host.
+ for temp_file in temp_files:
+ device_full_path = posixpath.join(
+ self.DEVICE_TEMP_DIR, temp_file.base_name)
+ dev_md5, _ = device.shell(
+ [get_md5_prog(self.device), device_full_path]).split()
+ self.assertEqual(temp_file.checksum, dev_md5)
+ finally:
+ self.device.shell(['rm', '-rf', self.DEVICE_TEMP_DIR])
+ shutil.rmtree(base_dir + self.DEVICE_TEMP_DIR)
+
+
+ def test_unicode_paths(self):
+ """Ensure that we can support non-ASCII paths, even on Windows."""
+ name = u'로보카 폴리'.encode('utf-8')
+
+ ## push.
+ tf = tempfile.NamedTemporaryFile('wb', suffix=name)
+ self.device.push(tf.name, '/data/local/tmp/adb-test-{}'.format(name))
+ self.device.shell(['rm', '-f', '/data/local/tmp/adb-test-*'])
+
+ # pull.
+ cmd = ['touch', '"/data/local/tmp/adb-test-{}"'.format(name)]
+ self.device.shell(cmd)
+
+ tf = tempfile.NamedTemporaryFile('wb', suffix=name)
+ self.device.pull('/data/local/tmp/adb-test-{}'.format(name), tf.name)
+
+
+def main():
+ random.seed(0)
+ if len(adb.get_devices()) > 0:
+ suite = unittest.TestLoader().loadTestsFromName(__name__)
+ unittest.TextTestRunner(verbosity=3).run(suite)
+ else:
+ print('Test suite must be run with attached devices')
+
+
+if __name__ == '__main__':
+ main()
diff --git a/adb/tests/test_adb.py b/adb/tests/test_adb.py
deleted file mode 100755
index 7739633..0000000
--- a/adb/tests/test_adb.py
+++ /dev/null
@@ -1,496 +0,0 @@
-#!/usr/bin/env python2
-# -*- coding: utf-8 -*-
-
-"""Simple conformance test for adb.
-
-This script will use the available adb in path and run simple
-tests that attempt to touch all accessible attached devices.
-"""
-import hashlib
-import os
-import pipes
-import posixpath
-import random
-import re
-import shlex
-import subprocess
-import sys
-import tempfile
-import unittest
-
-
-def trace(cmd):
- """Print debug message if tracing enabled."""
- if False:
- print >> sys.stderr, cmd
-
-
-def call(cmd_str):
- """Run process and return output tuple (stdout, stderr, ret code)."""
- trace(cmd_str)
- process = subprocess.Popen(shlex.split(cmd_str),
- stdout=subprocess.PIPE,
- stderr=subprocess.PIPE)
- stdout, stderr = process.communicate()
- return stdout, stderr, process.returncode
-
-
-def call_combined(cmd_str):
- """Run process and return output tuple (stdout+stderr, ret code)."""
- trace(cmd_str)
- process = subprocess.Popen(shlex.split(cmd_str),
- stdout=subprocess.PIPE,
- stderr=subprocess.STDOUT)
- stdout, _ = process.communicate()
- return stdout, process.returncode
-
-
-def call_checked(cmd_str):
- """Run process and get stdout+stderr, raise an exception on trouble."""
- trace(cmd_str)
- return subprocess.check_output(shlex.split(cmd_str),
- stderr=subprocess.STDOUT)
-
-
-def call_checked_list(cmd_str):
- return call_checked(cmd_str).split('\n')
-
-
-def call_checked_list_skip(cmd_str):
- out_list = call_checked_list(cmd_str)
-
- def is_init_line(line):
- if (len(line) >= 3) and (line[0] == "*") and (line[-2] == "*"):
- return True
- else:
- return False
-
- return [line for line in out_list if not is_init_line(line)]
-
-
-def get_device_list():
- output = call_checked_list_skip("adb devices")
- dev_list = []
- for line in output[1:]:
- if line.strip() == "":
- continue
- device, _ = line.split()
- dev_list.append(device)
- return dev_list
-
-
-def get_attached_device_count():
- return len(get_device_list())
-
-
-def compute_md5(string):
- hsh = hashlib.md5()
- hsh.update(string)
- return hsh.hexdigest()
-
-
-class HostFile(object):
- def __init__(self, handle, md5):
- self.handle = handle
- self.md5 = md5
- self.full_path = handle.name
- self.base_name = os.path.basename(self.full_path)
-
-
-class DeviceFile(object):
- def __init__(self, md5, full_path):
- self.md5 = md5
- self.full_path = full_path
- self.base_name = posixpath.basename(self.full_path)
-
-
-def make_random_host_files(in_dir, num_files, rand_size=True):
- files = {}
- min_size = 1 * (1 << 10)
- max_size = 16 * (1 << 10)
- fixed_size = min_size
-
- for _ in range(num_files):
- file_handle = tempfile.NamedTemporaryFile(dir=in_dir, delete=False)
-
- if rand_size:
- size = random.randrange(min_size, max_size, 1024)
- else:
- size = fixed_size
- rand_str = os.urandom(size)
- file_handle.write(rand_str)
- file_handle.flush()
- file_handle.close()
-
- md5 = compute_md5(rand_str)
- files[file_handle.name] = HostFile(file_handle, md5)
- return files
-
-
-def make_random_device_files(adb, in_dir, num_files, rand_size=True):
- files = {}
- min_size = 1 * (1 << 10)
- max_size = 16 * (1 << 10)
- fixed_size = min_size
-
- for i in range(num_files):
- if rand_size:
- size = random.randrange(min_size, max_size, 1024)
- else:
- size = fixed_size
-
- base_name = "device_tmpfile" + str(i)
- full_path = in_dir + "/" + base_name
-
- adb.shell("dd if=/dev/urandom of={} bs={} count=1".format(full_path,
- size))
- dev_md5, _ = adb.shell("md5sum {}".format(full_path)).split()
-
- files[full_path] = DeviceFile(dev_md5, full_path)
- return files
-
-
-class AdbWrapper(object):
- """Convenience wrapper object for the adb command."""
- def __init__(self, device=None, out_dir=None):
- self.device = device
- self.out_dir = out_dir
- self.adb_cmd = "adb "
- if self.device:
- self.adb_cmd += "-s {} ".format(pipes.quote(device))
- if self.out_dir:
- self.adb_cmd += "-p {} ".format(pipes.quote(out_dir))
-
- def shell(self, cmd):
- return call_checked(self.adb_cmd + "shell " + cmd)
-
- def shell_nocheck(self, cmd):
- return call_combined(self.adb_cmd + "shell " + cmd)
-
- def install(self, filename):
- return call_checked(
- self.adb_cmd + "install {}".format(pipes.quote(filename)))
-
- def push(self, local, remote):
- return call_checked(self.adb_cmd + "push {} {}".format(
- pipes.quote(local), pipes.quote(remote)))
-
- def pull(self, remote, local):
- return call_checked(self.adb_cmd + "pull {} {}".format(
- pipes.quote(remote), pipes.quote(local)))
-
- def sync(self, directory=""):
- return call_checked(self.adb_cmd + "sync {}".format(
- pipes.quote(directory) if directory else directory))
-
- def forward(self, local, remote):
- return call_checked(self.adb_cmd + "forward {} {}".format(local,
- remote))
-
- def tcpip(self, port):
- return call_combined(self.adb_cmd + "tcpip {}".format(port))
-
- def usb(self):
- return call_checked(self.adb_cmd + "usb")
-
- def root(self):
- return call_checked(self.adb_cmd + "root")
-
- def unroot(self):
- return call_checked(self.adb_cmd + "unroot")
-
- def forward_remove(self, local):
- return call_checked(self.adb_cmd + "forward --remove {}".format(local))
-
- def forward_remove_all(self):
- return call_checked(self.adb_cmd + "forward --remove-all")
-
- def connect(self, host):
- return call_checked(self.adb_cmd + "connect {}".format(host))
-
- def disconnect(self, host):
- return call_checked(self.adb_cmd + "disconnect {}".format(host))
-
- def reverse(self, remote, local):
- return call_checked(self.adb_cmd + "reverse {} {}".format(remote,
- local))
-
- def reverse_remove_all(self):
- return call_checked(self.adb_cmd + "reverse --remove-all")
-
- def reverse_remove(self, remote):
- return call_checked(
- self.adb_cmd + "reverse --remove {}".format(remote))
-
- def wait(self):
- return call_checked(self.adb_cmd + "wait-for-device")
-
-
-class AdbBasic(unittest.TestCase):
- def test_shell(self):
- """Check that we can at least cat a file."""
- adb = AdbWrapper()
- out = adb.shell("cat /proc/uptime")
- self.assertEqual(len(out.split()), 2)
- self.assertGreater(float(out.split()[0]), 0.0)
- self.assertGreater(float(out.split()[1]), 0.0)
-
- def test_help(self):
- """Make sure we get _something_ out of help."""
- out = call_checked("adb help")
- self.assertTrue(len(out) > 0)
-
- def test_version(self):
- """Get a version number out of the output of adb."""
- out = call_checked("adb version").split()
- version_num = False
- for item in out:
- if re.match(r"[\d+\.]*\d", item):
- version_num = True
- self.assertTrue(version_num)
-
- def _test_root(self):
- adb = AdbWrapper()
- if "adbd cannot run as root in production builds" in adb.root():
- return
- adb.wait()
- self.assertEqual("root", adb.shell("id -un").strip())
-
- def _test_unroot(self):
- adb = AdbWrapper()
- adb.unroot()
- adb.wait()
- self.assertEqual("shell", adb.shell("id -un").strip())
-
- def test_root_unroot(self):
- """Make sure that adb root and adb unroot work, using id(1)."""
- adb = AdbWrapper()
- original_user = adb.shell("id -un").strip()
- try:
- if original_user == "root":
- self._test_unroot()
- self._test_root()
- elif original_user == "shell":
- self._test_root()
- self._test_unroot()
- finally:
- if original_user == "root":
- adb.root()
- else:
- adb.unroot()
- adb.wait()
-
- def test_argument_escaping(self):
- """Make sure that argument escaping is somewhat sane."""
- adb = AdbWrapper()
-
- # http://b/19734868
- # Note that this actually matches ssh(1)'s behavior --- it's
- # converted to "sh -c echo hello; echo world" which sh interprets
- # as "sh -c echo" (with an argument to that shell of "hello"),
- # and then "echo world" back in the first shell.
- result = adb.shell("sh -c 'echo hello; echo world'").splitlines()
- self.assertEqual(["", "world"], result)
- # If you really wanted "hello" and "world", here's what you'd do:
- result = adb.shell(r"echo hello\;echo world").splitlines()
- self.assertEqual(["hello", "world"], result)
-
- # http://b/15479704
- self.assertEqual('t', adb.shell("'true && echo t'").strip())
- self.assertEqual('t', adb.shell("sh -c 'true && echo t'").strip())
-
- # http://b/20564385
- self.assertEqual('t', adb.shell("FOO=a BAR=b echo t").strip())
- self.assertEqual('123Linux', adb.shell(r"echo -n 123\;uname").strip())
-
- def test_install_argument_escaping(self):
- """Make sure that install argument escaping works."""
- adb = AdbWrapper()
-
- # http://b/20323053
- tf = tempfile.NamedTemporaryFile("wb", suffix="-text;ls;1.apk")
- self.assertIn("-text;ls;1.apk", adb.install(tf.name))
-
- # http://b/3090932
- tf = tempfile.NamedTemporaryFile("wb", suffix="-Live Hold'em.apk")
- self.assertIn("-Live Hold'em.apk", adb.install(tf.name))
-
- def test_line_endings(self):
- """Ensure that line ending translation is not happening in the pty.
-
- Bug: http://b/19735063
- """
- output = AdbWrapper().shell("uname")
- if sys.platform == 'win32':
- # adb.exe running on Windows does translation to the Windows \r\n
- # convention, so we should expect those chars.
- self.assertEqual(output, "Linux\r\n")
- else:
- self.assertEqual(output, "Linux\n")
-
- def test_tcpip(self):
- """adb tcpip requires a port. http://b/22636927"""
- output, status_code = AdbWrapper().tcpip("")
- self.assertEqual(1, status_code)
- self.assertIn("help message", output)
-
- output, status_code = AdbWrapper().tcpip("blah")
- self.assertEqual(1, status_code)
- self.assertIn("error", output)
-
- def test_unicode_paths(self):
- """Ensure that we can support non-ASCII paths, even on Windows."""
- adb = AdbWrapper()
- name = u'로보카 폴리'.encode('utf-8')
-
- # push.
- tf = tempfile.NamedTemporaryFile("wb", suffix=name)
- adb.push(tf.name, "/data/local/tmp/adb-test-{}".format(name))
-
- # pull.
- adb.shell("rm \"'/data/local/tmp/adb-test-*'\"".format(name))
- adb.shell("touch \"'/data/local/tmp/adb-test-{}'\"".format(name))
- tf = tempfile.NamedTemporaryFile("wb", suffix=name)
- adb.pull("/data/local/tmp/adb-test-{}".format(name), tf.name)
-
-
-class AdbFile(unittest.TestCase):
- SCRATCH_DIR = "/data/local/tmp"
- DEVICE_TEMP_FILE = SCRATCH_DIR + "/adb_test_file"
- DEVICE_TEMP_DIR = SCRATCH_DIR + "/adb_test_dir"
-
- def test_push(self):
- """Push a randomly generated file to specified device."""
- kbytes = 512
- adb = AdbWrapper()
- with tempfile.NamedTemporaryFile(mode="wb", delete=False) as tmp:
- try:
- rand_str = os.urandom(1024 * kbytes)
- tmp.write(rand_str)
- tmp.flush()
- tmp.close()
-
- host_md5 = compute_md5(rand_str)
- adb.shell_nocheck("rm -r {}".format(AdbFile.DEVICE_TEMP_FILE))
- try:
- adb.push(local=tmp.name, remote=AdbFile.DEVICE_TEMP_FILE)
- dev_md5, _ = adb.shell(
- "md5sum {}".format(AdbFile.DEVICE_TEMP_FILE)).split()
- self.assertEqual(host_md5, dev_md5)
- finally:
- adb.shell_nocheck("rm {}".format(AdbFile.DEVICE_TEMP_FILE))
- finally:
- os.remove(tmp.name)
-
- # TODO: write push directory test.
-
- def test_pull(self):
- """Pull a randomly generated file from specified device."""
- kbytes = 512
- adb = AdbWrapper()
- adb.shell_nocheck("rm -r {}".format(AdbFile.DEVICE_TEMP_FILE))
- try:
- adb.shell("dd if=/dev/urandom of={} bs=1024 count={}".format(
- AdbFile.DEVICE_TEMP_FILE, kbytes))
- dev_md5, _ = adb.shell(
- "md5sum {}".format(AdbFile.DEVICE_TEMP_FILE)).split()
-
- with tempfile.NamedTemporaryFile(mode="wb", delete=False) \
- as tmp_write:
- try:
- tmp_write.close()
- adb.pull(remote=AdbFile.DEVICE_TEMP_FILE,
- local=tmp_write.name)
- with open(tmp_write.name, "rb") as tmp_read:
- host_contents = tmp_read.read()
- host_md5 = compute_md5(host_contents)
- self.assertEqual(dev_md5, host_md5)
- finally:
- os.remove(tmp_write.name)
- finally:
- adb.shell_nocheck("rm {}".format(AdbFile.DEVICE_TEMP_FILE))
-
- def test_pull_dir(self):
- """Pull a randomly generated directory of files from the device."""
- adb = AdbWrapper()
- temp_files = {}
- host_dir = None
- try:
- # create temporary host directory
- host_dir = tempfile.mkdtemp()
-
- # create temporary dir on device
- adb.shell_nocheck("rm -r {}".format(AdbFile.DEVICE_TEMP_DIR))
- adb.shell("mkdir -p {}".format(AdbFile.DEVICE_TEMP_DIR))
-
- # populate device dir with random files
- temp_files = make_random_device_files(
- adb, in_dir=AdbFile.DEVICE_TEMP_DIR, num_files=32)
-
- adb.pull(remote=AdbFile.DEVICE_TEMP_DIR, local=host_dir)
-
- for device_full_path in temp_files:
- host_path = os.path.join(
- host_dir, temp_files[device_full_path].base_name)
- with open(host_path, "rb") as host_file:
- host_md5 = compute_md5(host_file.read())
- self.assertEqual(host_md5,
- temp_files[device_full_path].md5)
- finally:
- for dev_file in temp_files.values():
- host_path = os.path.join(host_dir, dev_file.base_name)
- os.remove(host_path)
- adb.shell_nocheck("rm -r {}".format(AdbFile.DEVICE_TEMP_DIR))
- if host_dir:
- os.removedirs(host_dir)
-
- def test_sync(self):
- """Sync a randomly generated directory of files to specified device."""
- try:
- adb = AdbWrapper()
- temp_files = {}
-
- # create temporary host directory
- base_dir = tempfile.mkdtemp()
-
- # create mirror device directory hierarchy within base_dir
- full_dir_path = base_dir + AdbFile.DEVICE_TEMP_DIR
- os.makedirs(full_dir_path)
-
- # create 32 random files within the host mirror
- temp_files = make_random_host_files(in_dir=full_dir_path,
- num_files=32)
-
- # clean up any trash on the device
- adb = AdbWrapper(out_dir=base_dir)
- adb.shell_nocheck("rm -r {}".format(AdbFile.DEVICE_TEMP_DIR))
-
- # issue the sync
- adb.sync("data")
-
- # confirm that every file on the device mirrors that on the host
- for host_full_path in temp_files.keys():
- device_full_path = posixpath.join(
- AdbFile.DEVICE_TEMP_DIR,
- temp_files[host_full_path].base_name)
- dev_md5, _ = adb.shell(
- "md5sum {}".format(device_full_path)).split()
- self.assertEqual(temp_files[host_full_path].md5, dev_md5)
-
- finally:
- adb.shell_nocheck("rm -r {}".format(AdbFile.DEVICE_TEMP_DIR))
- if temp_files:
- for tf in temp_files.values():
- os.remove(tf.full_path)
- if base_dir:
- os.removedirs(base_dir + AdbFile.DEVICE_TEMP_DIR)
-
-
-if __name__ == '__main__':
- random.seed(0)
- dev_count = get_attached_device_count()
- if dev_count:
- suite = unittest.TestLoader().loadTestsFromName(__name__)
- unittest.TextTestRunner(verbosity=3).run(suite)
- else:
- print "Test suite must be run with attached devices"
diff --git a/gpttool/Android.mk b/gpttool/Android.mk
deleted file mode 100644
index 64ad945..0000000
--- a/gpttool/Android.mk
+++ /dev/null
@@ -1,14 +0,0 @@
-ifeq ($(HOST_OS),linux)
-
-LOCAL_PATH:= $(call my-dir)
-include $(CLEAR_VARS)
-
-LOCAL_SRC_FILES := gpttool.c
-LOCAL_STATIC_LIBRARIES := libz
-LOCAL_CFLAGS := -Werror
-
-LOCAL_MODULE := gpttool
-
-include $(BUILD_HOST_EXECUTABLE)
-
-endif
diff --git a/gpttool/gpttool.c b/gpttool/gpttool.c
deleted file mode 100644
index 398362f..0000000
--- a/gpttool/gpttool.c
+++ /dev/null
@@ -1,373 +0,0 @@
-/*
-** Copyright 2011, 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.
-*/
-
-#include <fcntl.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/ioctl.h>
-#include <sys/stat.h>
-#include <unistd.h>
-
-#include <zlib.h>
-
-#include <linux/fs.h>
-
-typedef unsigned char u8;
-typedef unsigned short u16;
-typedef unsigned int u32;
-typedef unsigned long long u64;
-
-const u8 partition_type_uuid[16] = {
- 0xa2, 0xa0, 0xd0, 0xeb, 0xe5, 0xb9, 0x33, 0x44,
- 0x87, 0xc0, 0x68, 0xb6, 0xb7, 0x26, 0x99, 0xc7,
-};
-
-
-#define EFI_VERSION 0x00010000
-#define EFI_MAGIC "EFI PART"
-#define EFI_ENTRIES 128
-#define EFI_NAMELEN 36
-
-struct efi_header {
- u8 magic[8];
-
- u32 version;
- u32 header_sz;
-
- u32 crc32;
- u32 reserved;
-
- u64 header_lba;
- u64 backup_lba;
- u64 first_lba;
- u64 last_lba;
-
- u8 volume_uuid[16];
-
- u64 entries_lba;
-
- u32 entries_count;
- u32 entries_size;
- u32 entries_crc32;
-} __attribute__((packed));
-
-struct efi_entry {
- u8 type_uuid[16];
- u8 uniq_uuid[16];
- u64 first_lba;
- u64 last_lba;
- u64 attr;
- u16 name[EFI_NAMELEN];
-};
-
-struct ptable {
- u8 mbr[512];
- union {
- struct efi_header header;
- u8 block[512];
- };
- struct efi_entry entry[EFI_ENTRIES];
-};
-
-void get_uuid(u8 *uuid)
-{
- int fd;
- fd = open("/dev/urandom", O_RDONLY);
- read(fd, uuid, 16);
- close(fd);
-}
-
-void init_mbr(u8 *mbr, u32 blocks)
-{
- mbr[0x1be] = 0x00; // nonbootable
- mbr[0x1bf] = 0xFF; // bogus CHS
- mbr[0x1c0] = 0xFF;
- mbr[0x1c1] = 0xFF;
-
- mbr[0x1c2] = 0xEE; // GPT partition
- mbr[0x1c3] = 0xFF; // bogus CHS
- mbr[0x1c4] = 0xFF;
- mbr[0x1c5] = 0xFF;
-
- mbr[0x1c6] = 0x01; // start
- mbr[0x1c7] = 0x00;
- mbr[0x1c8] = 0x00;
- mbr[0x1c9] = 0x00;
-
- memcpy(mbr + 0x1ca, &blocks, sizeof(u32));
-
- mbr[0x1fe] = 0x55;
- mbr[0x1ff] = 0xaa;
-}
-
-int add_ptn(struct ptable *ptbl, u64 first, u64 last, const char *name)
-{
- struct efi_header *hdr = &ptbl->header;
- struct efi_entry *entry = ptbl->entry;
- unsigned n;
-
- if (first < 34) {
- fprintf(stderr,"partition '%s' overlaps partition table\n", name);
- return -1;
- }
-
- if (last > hdr->last_lba) {
- fprintf(stderr,"partition '%s' does not fit on disk\n", name);
- return -1;
- }
- for (n = 0; n < EFI_ENTRIES; n++, entry++) {
- if (entry->type_uuid[0])
- continue;
- memcpy(entry->type_uuid, partition_type_uuid, 16);
- get_uuid(entry->uniq_uuid);
- entry->first_lba = first;
- entry->last_lba = last;
- for (n = 0; (n < EFI_NAMELEN) && *name; n++)
- entry->name[n] = *name++;
- return 0;
- }
- fprintf(stderr,"out of partition table entries\n");
- return -1;
-}
-
-int usage(void)
-{
- fprintf(stderr,
- "usage: gpttool write <disk> [ <partition> ]*\n"
- " gpttool read <disk>\n"
- " gpttool test [ <partition> ]*\n"
- "\n"
- "partition: [<name>]:<size>[kmg] | @<file-of-partitions>\n"
- );
- return 0;
-}
-
-void show(struct ptable *ptbl)
-{
- struct efi_entry *entry = ptbl->entry;
- unsigned n, m;
- char name[EFI_NAMELEN + 1];
-
- fprintf(stderr,"ptn start block end block name\n");
- fprintf(stderr,"---- ------------- ------------- --------------------\n");
-
- for (n = 0; n < EFI_ENTRIES; n++, entry++) {
- if (entry->type_uuid[0] == 0)
- break;
- for (m = 0; m < EFI_NAMELEN; m++) {
- name[m] = entry->name[m] & 127;
- }
- name[m] = 0;
- fprintf(stderr,"#%03d %13lld %13lld %s\n",
- n + 1, entry->first_lba, entry->last_lba, name);
- }
-}
-
-u64 find_next_lba(struct ptable *ptbl)
-{
- struct efi_entry *entry = ptbl->entry;
- unsigned n;
- u64 a = 0;
- for (n = 0; n < EFI_ENTRIES; n++, entry++) {
- if ((entry->last_lba + 1) > a)
- a = entry->last_lba + 1;
- }
- return a;
-}
-
-u64 next_lba = 0;
-
-u64 parse_size(char *sz)
-{
- int l = strlen(sz);
- u64 n = strtoull(sz, 0, 10);
- if (l) {
- switch(sz[l-1]){
- case 'k':
- case 'K':
- n *= 1024;
- break;
- case 'm':
- case 'M':
- n *= (1024 * 1024);
- break;
- case 'g':
- case 'G':
- n *= (1024 * 1024 * 1024);
- break;
- }
- }
- return n;
-}
-
-int parse_ptn(struct ptable *ptbl, char *x)
-{
- char *y = strchr(x, ':');
- u64 sz;
-
- if (!y) {
- fprintf(stderr,"invalid partition entry: %s\n", x);
- return -1;
- }
- *y++ = 0;
-
- if (*y == 0) {
- sz = ptbl->header.last_lba - next_lba;
- } else {
- sz = parse_size(y);
- if (sz & 511) {
- fprintf(stderr,"partition size must be multiple of 512\n");
- return -1;
- }
- sz /= 512;
- }
-
- if (sz == 0) {
- fprintf(stderr,"zero size partitions not allowed\n");
- return -1;
- }
-
- if (x[0] && add_ptn(ptbl, next_lba, next_lba + sz - 1, x))
- return -1;
-
- next_lba = next_lba + sz;
- return 0;
-}
-
-int main(int argc, char **argv)
-{
- struct ptable ptbl;
- struct efi_header *hdr = &ptbl.header;
- u32 n;
- u64 sz;
- int fd;
- const char *device;
- int real_disk = 0;
-
- if (argc < 2)
- return usage();
-
- if (!strcmp(argv[1], "write")) {
- if (argc < 3)
- return usage();
- device = argv[2];
- argc -= 2;
- argv += 2;
- real_disk = 1;
- } else if (!strcmp(argv[1], "test")) {
- argc -= 1;
- argv += 1;
- real_disk = 0;
- sz = 2097152 * 16;
- fprintf(stderr,"< simulating 16GB disk >\n\n");
- } else {
- return usage();
- }
-
- if (real_disk) {
- if (!strcmp(device, "/dev/sda") ||
- !strcmp(device, "/dev/sdb")) {
- fprintf(stderr,"error: refusing to partition sda or sdb\n");
- return -1;
- }
-
- fd = open(device, O_RDWR);
- if (fd < 0) {
- fprintf(stderr,"error: cannot open '%s'\n", device);
- return -1;
- }
- if (ioctl(fd, BLKGETSIZE64, &sz)) {
- fprintf(stderr,"error: cannot query block device size\n");
- return -1;
- }
- sz /= 512;
- fprintf(stderr,"blocks %lld\n", sz);
- }
-
- memset(&ptbl, 0, sizeof(ptbl));
-
- init_mbr(ptbl.mbr, sz - 1);
-
- memcpy(hdr->magic, EFI_MAGIC, sizeof(hdr->magic));
- hdr->version = EFI_VERSION;
- hdr->header_sz = sizeof(struct efi_header);
- hdr->header_lba = 1;
- hdr->backup_lba = sz - 1;
- hdr->first_lba = 34;
- hdr->last_lba = sz - 1;
- get_uuid(hdr->volume_uuid);
- hdr->entries_lba = 2;
- hdr->entries_count = 128;
- hdr->entries_size = sizeof(struct efi_entry);
-
- while (argc > 1) {
- if (argv[1][0] == '@') {
- char line[256], *p;
- FILE *f;
- f = fopen(argv[1] + 1, "r");
- if (!f) {
- fprintf(stderr,"cannot read partitions from '%s\n", argv[1]);
- return -1;
- }
- while (fgets(line, sizeof(line), f)) {
- p = line + strlen(line);
- while (p > line) {
- p--;
- if (*p > ' ')
- break;
- *p = 0;
- }
- p = line;
- while (*p && (*p <= ' '))
- p++;
- if (*p == '#')
- continue;
- if (*p == 0)
- continue;
- if (parse_ptn(&ptbl, p))
- return -1;
- }
- fclose(f);
- } else {
- if (parse_ptn(&ptbl, argv[1]))
- return -1;
- }
- argc--;
- argv++;
- }
-
- n = crc32(0, Z_NULL, 0);
- n = crc32(n, (void*) ptbl.entry, sizeof(ptbl.entry));
- hdr->entries_crc32 = n;
-
- n = crc32(0, Z_NULL, 0);
- n = crc32(n, (void*) &ptbl.header, sizeof(ptbl.header));
- hdr->crc32 = n;
-
- show(&ptbl);
-
- if (real_disk) {
- write(fd, &ptbl, sizeof(ptbl));
- fsync(fd);
-
- if (ioctl(fd, BLKRRPART, 0)) {
- fprintf(stderr,"could not re-read partition table\n");
- }
- close(fd);
- }
- return 0;
-}
diff --git a/init/init.cpp b/init/init.cpp
index c66c487..4be16ea 100644
--- a/init/init.cpp
+++ b/init/init.cpp
@@ -1057,7 +1057,7 @@
property_load_boot_defaults();
start_property_service();
- init_parse_config_file("/init.rc");
+ init_parse_config("/init.rc");
action_for_each_trigger("early-init", action_add_queue_tail);
diff --git a/init/init_parser.cpp b/init/init_parser.cpp
index 86e9ce6..460f5ac 100644
--- a/init/init_parser.cpp
+++ b/init/init_parser.cpp
@@ -15,6 +15,7 @@
*/
#include <ctype.h>
+#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
@@ -32,6 +33,7 @@
#include "property_service.h"
#include "util.h"
+#include <base/stringprintf.h>
#include <cutils/iosched_policy.h>
#include <cutils/list.h>
@@ -385,15 +387,15 @@
parser_done:
list_for_each(node, &import_list) {
struct import* import = node_to_item(node, struct import, list);
- if (!init_parse_config_file(import->filename)) {
+ if (!init_parse_config(import->filename)) {
ERROR("could not import file '%s' from '%s': %s\n",
import->filename, fn, strerror(errno));
}
}
}
-bool init_parse_config_file(const char* path) {
- INFO("Parsing %s...\n", path);
+static bool init_parse_config_file(const char* path) {
+ INFO("Parsing file %s...\n", path);
Timer t;
std::string data;
if (!read_file(path, &data)) {
@@ -408,6 +410,34 @@
return true;
}
+static bool init_parse_config_dir(const char* path) {
+ INFO("Parsing directory %s...\n", path);
+ std::unique_ptr<DIR, int(*)(DIR*)> config_dir(opendir(path), closedir);
+ if (!config_dir) {
+ ERROR("Could not import directory '%s'\n", path);
+ return false;
+ }
+ dirent* current_file;
+ while ((current_file = readdir(config_dir.get()))) {
+ std::string current_path =
+ android::base::StringPrintf("%s/%s", path, current_file->d_name);
+ // Ignore directories and only process regular files.
+ if (current_file->d_type == DT_REG) {
+ if (!init_parse_config_file(current_path.c_str())) {
+ ERROR("could not import file '%s'\n", current_path.c_str());
+ }
+ }
+ }
+ return true;
+}
+
+bool init_parse_config(const char* path) {
+ if (is_dir(path)) {
+ return init_parse_config_dir(path);
+ }
+ return init_parse_config_file(path);
+}
+
static int valid_name(const char *name)
{
if (strlen(name) > 16) {
diff --git a/init/init_parser.h b/init/init_parser.h
index fa7e67f..1ebb1ef 100644
--- a/init/init_parser.h
+++ b/init/init_parser.h
@@ -33,7 +33,7 @@
void queue_all_property_triggers();
void queue_builtin_action(int (*func)(int nargs, char **args), const char *name);
-bool init_parse_config_file(const char* path);
+bool init_parse_config(const char* path);
int expand_props(const char *src, std::string *dst);
service* make_exec_oneshot_service(int argc, char** argv);
diff --git a/init/readme.txt b/init/readme.txt
index 4dda340..5a758d7 100644
--- a/init/readme.txt
+++ b/init/readme.txt
@@ -197,8 +197,11 @@
ifup <interface>
Bring the network interface <interface> online.
-import <filename>
+import <path>
Parse an init config file, extending the current configuration.
+ If <path> is a directory, each file in the directory is parsed as
+ a config file. It is not recursive, nested directories will
+ not be parsed.
insmod <path>
Install the module at <path>
diff --git a/init/util.cpp b/init/util.cpp
index 7f29e94..f6131e3 100644
--- a/init/util.cpp
+++ b/init/util.cpp
@@ -465,3 +465,14 @@
android::base::StringAppendF(&hex, "%02x", bytes[i]);
return hex;
}
+
+/*
+ * Returns true is pathname is a directory
+ */
+bool is_dir(const char* pathname) {
+ struct stat info;
+ if (stat(pathname, &info) == -1) {
+ return false;
+ }
+ return S_ISDIR(info.st_mode);
+}
diff --git a/init/util.h b/init/util.h
index 3aba599..f08cb8d 100644
--- a/init/util.h
+++ b/init/util.h
@@ -64,4 +64,5 @@
int restorecon(const char *pathname);
int restorecon_recursive(const char *pathname);
std::string bytes_to_hex(const uint8_t *bytes, size_t bytes_len);
+bool is_dir(const char* pathname);
#endif
diff --git a/logd/CommandListener.cpp b/logd/CommandListener.cpp
index 5489cc9..489bea6 100644
--- a/logd/CommandListener.cpp
+++ b/logd/CommandListener.cpp
@@ -34,8 +34,7 @@
CommandListener::CommandListener(LogBuffer *buf, LogReader * /*reader*/,
LogListener * /*swl*/) :
- FrameworkListener(getLogSocket()),
- mBuf(*buf) {
+ FrameworkListener(getLogSocket()) {
// registerCmd(new ShutdownCmd(buf, writer, swl));
registerCmd(new ClearCmd(buf));
registerCmd(new GetBufSizeCmd(buf));
@@ -47,10 +46,9 @@
registerCmd(new ReinitCmd());
}
-CommandListener::ShutdownCmd::ShutdownCmd(LogBuffer *buf, LogReader *reader,
+CommandListener::ShutdownCmd::ShutdownCmd(LogReader *reader,
LogListener *swl) :
LogCommand("shutdown"),
- mBuf(*buf),
mReader(*reader),
mSwl(*swl) {
}
diff --git a/logd/CommandListener.h b/logd/CommandListener.h
index 83e06b4..3877675 100644
--- a/logd/CommandListener.h
+++ b/logd/CommandListener.h
@@ -27,7 +27,6 @@
void reinit_signal_handler(int /*signal*/);
class CommandListener : public FrameworkListener {
- LogBuffer &mBuf;
public:
CommandListener(LogBuffer *buf, LogReader *reader, LogListener *swl);
@@ -37,12 +36,11 @@
static int getLogSocket();
class ShutdownCmd : public LogCommand {
- LogBuffer &mBuf;
LogReader &mReader;
LogListener &mSwl;
public:
- ShutdownCmd(LogBuffer *buf, LogReader *reader, LogListener *swl);
+ ShutdownCmd(LogReader *reader, LogListener *swl);
virtual ~ShutdownCmd() {}
int runCommand(SocketClient *c, int argc, char ** argv);
};
diff --git a/logd/LogKlog.cpp b/logd/LogKlog.cpp
index d3a73a1..eff26f5 100644
--- a/logd/LogKlog.cpp
+++ b/logd/LogKlog.cpp
@@ -177,8 +177,6 @@
logbuf(buf),
reader(reader),
signature(CLOCK_MONOTONIC),
- fdWrite(fdWrite),
- fdRead(fdRead),
initialized(false),
enableLogging(true),
auditd(auditd) {
@@ -492,7 +490,6 @@
}
}
} else if (isspace(cp[size])) {
- const char *b = cp;
cp += size;
while (isspace(*++cp));
// <PRI>[<TIME>] <tag> <tag> : message
diff --git a/logd/LogKlog.h b/logd/LogKlog.h
index a898c63..24b2685 100644
--- a/logd/LogKlog.h
+++ b/logd/LogKlog.h
@@ -27,8 +27,6 @@
LogBuffer *logbuf;
LogReader *reader;
const log_time signature;
- const int fdWrite; // /dev/kmsg
- const int fdRead; // /proc/kmsg
// Set once thread is started, separates KLOG_ACTION_READ_ALL
// and KLOG_ACTION_READ phases.
bool initialized;