Repository: borgbackup/borg

Issue:
Borg adds files from local working directory if an empty string is added as argument
## Have you checked borgbackup docs, FAQ, and open Github issues?

Yes

## Is this a BUG / ISSUE report or a QUESTION?

Question

## System information. For client/server mode post info for both machines.

#### Your borg version (borg -V).

borg 1.1.14

#### Operating system (distribution) and version.

Arch Linux ARM 5.4.83-3

#### Hardware / network configuration, and filesystems used.

ext4

#### How much data is handled by borg?

As needed

#### Full borg commandline that lead to the problem (leave away excludes and passwords)

```bash
#!/usr/bin/env bash

# two collections
declare -A collections=(
 ["/path/to/coll1"]="/borg/repo1"
 ["/path/to/coll2"]="/borg/repo2"
)

# only one has a subdirectory to exclude
declare -A extraArguments=(
 ["/path/to/coll1"]="--exclude subdir"
)

# iterate for every collection
for collection in "${!collections[@]}"; do

 # set borg repo
 repo="${collections[collection]}"

 # set extra arguments for the collection if defined
 # else set the variable to an empty string
 if [ "${extraArguments[collection]+isSet}" ]; then
  args="${extraArguments[collection]}"
 else  
  args=""
 fi

 # run borg with custom arguments
 borg create --progress --list --stats "$repo"::archive "$collection" "$args"

done
```

## Describe the problem you're observing.

In case "$args" is set to a zero lenght string (second collection), Borg also adds file from the current working directory after the ones from "$collection", like if "" means "." or "$PWD". Is this an expected behaviour?

This of course does not happen if "$args" is undefined, but this case would fail `set -u`
My idea was avoiding two different "borg create" directives in the "if" statement (instead of the two "args=")

Empty strings are treated as an argument also by `rclone`, which instead fails saying the number of arguments ("origin", "destination", "") exceeds the expected one ("origin", "destination"). https://github.com/rclone/rclone/issues/4239 may be relevant.

#### Can you reproduce the problem? If so, describe how. If not, describe troubleshooting steps you took before opening the issue.

By running a script similar to the above one



Discussion:
Some hints:
- do not mix --list and --progress, output will be rather ugly
- do not give empty arguments (empty PATHs)

The problem with empty PATHs is that borg expects a path to be either absolute (starts with /) or relative (does not start with /). For the relative case, the cwd is prepended to transform it to an absolute path. And if you prepend the cwd to an empty string, you get the cwd.

Theoretically, we could error in case of an empty PATH arg, but I do not know if that will break somebody's scripts.

OTOH, you could also just not give empty PATH args to borg if you do not want to backup the cwd.
Thanks, it makes sense.
I guess I'll have to use two different `borg create` commands depending on the presence or not of extra arguments.
+1 for the fail message / ignore argument, I cannot think about any reason on why one should prefer "" over "." to refer to the current directory

Resolving patch:
diff --git a/src/borg/archiver/create_cmd.py b/src/borg/archiver/create_cmd.py
--- a/src/borg/archiver/create_cmd.py
+++ b/src/borg/archiver/create_cmd.py
@@ -132,6 +132,9 @@ def create_inner(archive, cache, fso):
                         return self.exit_code
             else:
                 for path in args.paths:
+                    if path == "":  # issue #5637
+                        self.print_warning("An empty string was given as PATH, ignoring.")
+                        continue
                     if path == "-":  # stdin
                         path = args.stdin_name
                         mode = args.stdin_mode


Test patch: