backups: Remove incorrectly set buffer size during download

- Don't set bufsize to 1 while streaming backup download. This is only effective
  with text streams with universal_newline flag set. An actual buffer size of 1
  is very inefficient and plain wrong. Leave the python default of
  io.DEFAULT_BUFFER_SIZE.

- Minor simplification to argument passing.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa 2019-01-30 10:58:37 -08:00 committed by James Valleroy
parent 484992fe37
commit 623bbc87e8
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
2 changed files with 18 additions and 24 deletions

View File

@ -100,38 +100,35 @@ from plinth.errors import ActionError
LOGGER = logging.getLogger(__name__) LOGGER = logging.getLogger(__name__)
def run(action, options=None, input=None, run_in_background=False, def run(action, options=None, input=None, run_in_background=False):
bufsize=None):
"""Safely run a specific action as the current user. """Safely run a specific action as the current user.
See actions._run for more information. See actions._run for more information.
""" """
return _run(action, options, input, run_in_background, False, return _run(action, options, input, run_in_background, False)
bufsize=bufsize)
def superuser_run(action, options=None, input=None, run_in_background=False, def superuser_run(action, options=None, input=None, run_in_background=False,
bufsize=None, log_error=True): log_error=True):
"""Safely run a specific action as root. """Safely run a specific action as root.
See actions._run for more information. See actions._run for more information.
""" """
return _run(action, options, input, run_in_background, True, return _run(action, options, input, run_in_background, True,
bufsize=bufsize, log_error=log_error) log_error=log_error)
def run_as_user(action, options=None, input=None, run_in_background=False, def run_as_user(action, options=None, input=None, run_in_background=False,
bufsize=None, become_user=None): become_user=None):
"""Run a command as a different user. """Run a command as a different user.
If become_user is None, run as current user. If become_user is None, run as current user.
""" """
return _run(action, options, input, run_in_background, False, become_user, return _run(action, options, input, run_in_background, False, become_user)
bufsize=bufsize)
def _run(action, options=None, input=None, run_in_background=False, def _run(action, options=None, input=None, run_in_background=False,
run_as_root=False, become_user=None, log_error=True, bufsize=None): run_as_root=False, become_user=None, log_error=True):
"""Safely run a specific action as a normal user or root. """Safely run a specific action as a normal user or root.
Actions are pulled from the actions directory. Actions are pulled from the actions directory.
@ -196,8 +193,7 @@ def _run(action, options=None, input=None, run_in_background=False,
if cfg.develop: if cfg.develop:
# In development mode pass on local pythonpath to access Plinth # In development mode pass on local pythonpath to access Plinth
kwargs['env'] = {'PYTHONPATH': cfg.root} kwargs['env'] = {'PYTHONPATH': cfg.root}
if bufsize is not None:
kwargs['bufsize'] = bufsize
proc = subprocess.Popen(cmd, **kwargs) proc = subprocess.Popen(cmd, **kwargs)
if not run_in_background: if not run_in_background:

View File

@ -139,9 +139,7 @@ class BorgRepository(object):
archive_path = self.get_archive_path(archive_name) archive_path = self.get_archive_path(archive_name)
args = ['export-tar', '--path', archive_path] args = ['export-tar', '--path', archive_path]
args = self.append_encryption_passphrase(args, self.credentials) args = self.append_encryption_passphrase(args, self.credentials)
kwargs = {'run_in_background': True, proc = self._run('backups', args, run_in_background=True)
'bufsize': 1}
proc = self._run('backups', args, kwargs=kwargs)
return zipstream.ZipStream(proc.stdout, 'readline') return zipstream.ZipStream(proc.stdout, 'readline')
def get_archive(self, name): def get_archive(self, name):
@ -166,15 +164,13 @@ class BorgRepository(object):
def get_archive_path(self, archive_name): def get_archive_path(self, archive_name):
return "::".join([self.repo_path, archive_name]) return "::".join([self.repo_path, archive_name])
def _run(self, cmd, arguments, superuser=True, kwargs=None): def _run(self, cmd, arguments, superuser=True, **kwargs):
"""Run a backups or sshfs action script command.""" """Run a backups or sshfs action script command."""
if kwargs is None:
kwargs = {}
try: try:
if superuser: if superuser:
return actions.superuser_run(cmd, arguments, **kwargs) return actions.superuser_run(cmd, arguments, **kwargs)
else:
return actions.run(cmd, arguments, **kwargs) return actions.run(cmd, arguments, **kwargs)
except ActionError as err: except ActionError as err:
self.reraise_known_error(err) self.reraise_known_error(err)
@ -293,7 +289,7 @@ class SshBorgRepository(BorgRepository):
self._path] self._path]
arguments, kwargs = self._append_sshfs_arguments(arguments, arguments, kwargs = self._append_sshfs_arguments(arguments,
self.credentials) self.credentials)
self._run('sshfs', arguments, kwargs=kwargs) self._run('sshfs', arguments, **kwargs)
def umount(self): def umount(self):
if not self.is_mounted: if not self.is_mounted:
@ -314,14 +310,16 @@ class SshBorgRepository(BorgRepository):
except Exception as err: except Exception as err:
logger.error(err) logger.error(err)
def _append_sshfs_arguments(self, arguments, credentials, kwargs=None): def _append_sshfs_arguments(self, arguments, credentials):
"""Add credentials to a run command and kwargs""" """Add credentials to a run command and kwargs"""
if kwargs is None: kwargs = {}
kwargs = {}
if 'ssh_password' in credentials and credentials['ssh_password']: if 'ssh_password' in credentials and credentials['ssh_password']:
kwargs['input'] = credentials['ssh_password'].encode() kwargs['input'] = credentials['ssh_password'].encode()
if 'ssh_keyfile' in credentials and credentials['ssh_keyfile']: if 'ssh_keyfile' in credentials and credentials['ssh_keyfile']:
arguments += ['--ssh-keyfile', credentials['ssh_keyfile']] arguments += ['--ssh-keyfile', credentials['ssh_keyfile']]
return (arguments, kwargs) return (arguments, kwargs)
def run(self, arguments, superuser=True): def run(self, arguments, superuser=True):