From 9ffa1a13af3f943c80162819a67e4fa051e9449e Mon Sep 17 00:00:00 2001 From: Thomas Perl Date: Thu, 9 May 2019 11:38:00 +0200 Subject: [PATCH 1/3] Default to Python 3 --- adbfs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adbfs b/adbfs index c4e2686..cd5a238 100755 --- a/adbfs +++ b/adbfs @@ -1,4 +1,4 @@ -#! /usr/bin/env python +#! /usr/bin/env python3 """ adbfs Virtual filesystem for Midnight Commander From c5559f7d4198aee89a20264f86102d80627c6310 Mon Sep 17 00:00:00 2001 From: Thomas Perl Date: Thu, 9 May 2019 11:38:21 +0200 Subject: [PATCH 2/3] Python 3's ConfigParser is already the safe one --- adbfs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adbfs b/adbfs index cd5a238..e3a7160 100755 --- a/adbfs +++ b/adbfs @@ -189,7 +189,7 @@ class Conf(object): if not os.path.exists(conf_fname): return - cfg = configparser.SafeConfigParser() + cfg = configparser.ConfigParser() cfg_map = {'debug': (cfg.getboolean, 'debug'), 'dirs_to_skip': (cfg.get, 'dirs_to_skip'), 'suppress_colors': (cfg.get, 'suppress_colors'), From b088c45d3f56755b44704d07aafb5d86010bfc32 Mon Sep 17 00:00:00 2001 From: Thomas Perl Date: Thu, 9 May 2019 11:48:19 +0200 Subject: [PATCH 3/3] Fix quoting for 'adb shell' --- adbfs | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/adbfs b/adbfs index e3a7160..d1900c6 100755 --- a/adbfs +++ b/adbfs @@ -16,6 +16,7 @@ import os import re import subprocess import sys +import shlex __version__ = 0.11 @@ -337,9 +338,16 @@ class Adb(object): self.__su_check() + def _shell_cmd(self, *args, with_root=False): + if with_root and self._got_root: + args = ('su', '-c', shlex.quote(' '.join(shlex.quote(x) for x in args))) + + cmd = [self.conf.adb_command, 'shell'] + [shlex.quote(x) for x in args] + return cmd + def __su_check(self): """Check if we are able to get elevated privileges""" - cmd = [self.conf.adb_command] + 'shell su -c whoami'.split() + cmd = self._shell_cmd('su', '-c', 'whoami') try: with open(os.devnull, 'w') as fnull: result = check_output(cmd, stderr=fnull) @@ -389,10 +397,8 @@ class Adb(object): def _retrieve_single_dir_list(self, dir_): """Retrieve file list using adb""" - lscmd = self.conf.box['rls'].format(dir_) - if self._got_root: - lscmd = 'su -c "{}"'.format(lscmd) - command = [self.conf.adb_command, 'shell', lscmd] + lscmd = self.conf.box['rls'].format(shlex.quote(dir_)) + command = self._shell_cmd(*shlex.split(lscmd), with_root=True) try: if self.conf.debug: @@ -449,12 +455,9 @@ class Adb(object): if not root: lscmd = self.conf.box['ls'] else: - lscmd = self.conf.box['rls'].format(root.filepath) + lscmd = self.conf.box['rls'].format(shlex.quite(root.filepath)) - if self._got_root: - lscmd = 'su -c "{}"'.format(lscmd) - - command = [self.conf.adb_command, 'shell', lscmd] + command = self._shell_cmd(*shlex.split(lscmd), with_root=True) try: if self.conf.debug: @@ -566,7 +569,7 @@ class Adb(object): sys.stderr.write(self.error) return 1 - cmd = [self.conf.adb_command, 'shell', 'rm', dst] + cmd = self._shell_cmd('rm', dst) try: err = check_output(cmd) except subprocess.CalledProcessError: @@ -584,7 +587,7 @@ class Adb(object): sys.stderr.write(self.error) return 1 - cmd = [self.conf.adb_command, 'shell', 'rm', '-r', dst] + cmd = self._shell_cmd('rm', '-r', dst) try: err = check_output(cmd) except subprocess.CalledProcessError: @@ -605,7 +608,7 @@ class Adb(object): if not dst.startswith('/'): dst = '/' + dst - cmd = [self.conf.adb_command, 'shell', 'mkdir', dst] + cmd = self._shell_cmd('mkdir', dst) try: err = check_output(cmd) except subprocess.CalledProcessError: