Skip to content
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

Fix whitespace error: use of shlex.split instead of .split() (fix #2164) #2171

Merged
merged 1 commit into from
Dec 16, 2024

Conversation

goebbe
Copy link
Contributor

@goebbe goebbe commented Dec 16, 2024

Description

As described in but #2164, Vorta/ borg raises an error when valid borg commands with whitespace are
provided via Vortas "Extra borg commands".
The issues arise when a folder/ filename contains whitespace.
The issue came up here: #2156 (reply in thread)

Related Issue

Extra borg arguments with white space raise an error #2164

Motivation and Context

See the following borg issue: borgbackup/borg#8578

There are different ways how borg can receive options (with folder/ name containing whitespace).
The following is a list of valid borg options - all of them raise an error in Vorta:

--pattern=-subdir/test\ folder
'--pattern=-subdir/test folder'
--pattern='-subdir/test folder'

When investigating, I found that this issue is due to the use of standard python .split() function in create.py.
The .split() function separates by whitespace - as a result, item names containing whitespace are split up:

s1 = "--pattern=-subdir/test\ folder"
x = s1.split()
print(x)
$ ['--pattern=-subdir/test\\', 'folder']

If this type of list is passed to Popen, 'folder' is treated as an attribute, which led to the error.

The solution for this type of error is the use of shlex.split() instead of standard .split().
Note that shlex.split is alredy used by Vorta, in other places, e.g. in /borg/borg_job.py
shlex.split() recognises the escaped whitespace in the item names, as required - the resulting attribute can
be passed to Popen as is.

When using shlex.split(), one, minor, resulting issue is that the borg command (shell command) printed to the Vorta logs does not escape the whitespace, since shlex.split() removes the escape character.
Since I wanted to provide a working borg command equivalent in the Vorta logs, I just added a additional step, where I replace whitespace by escaped whitespace (in the cmd attributes) - before printing the borg commands to the logs.
As a result, borg prints a command with escaped whitespaces (inside attributes only) that could be coppied/ run in the Shell.

An alternative would be to print the list of attributes, as is - but this does not lead to an easily readable borg command in the logs - that could be coppied/ pasted to the shell (a property I would like to keep)

How Has This Been Tested?

  • I tested with the following Extra borg arguments - and checked the resulting logs - and if borg did in fact exclude my "test folder"
--pattern=-subdir/test\ folder
'--pattern=-subdir/test folder'
--pattern='-subdir/test folder'
  • In the Vora logs, the whitespace in item names are escaped.
  • I also tested the case where no Extra borg arguments where delivered.
  • I also tested cased with extra borg arguments and suffix commands.

Checklist:

  • [x ] I have read the CONTRIBUTING guide.
  • [x ] My code follows the code style of this project.

My change does not requires a change to the documentation.

I provide my contribution under the terms of the license of this repository and I affirm the Developer Certificate of Origin.

@m3nu
Copy link
Contributor

m3nu commented Dec 16, 2024

Solid explanation! Thanks for looking into it and making a PR!

@m3nu m3nu merged commit 4e0c555 into borgbase:master Dec 16, 2024
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants