Repository: borgbackup/borg

Issue:
smb: Backups no longer working (1.2.1 patch regression)
<!--
Thank you for reporting an issue.

*IMPORTANT* -  *before* creating a new issue please look around:
 - Borgbackup documentation: http://borgbackup.readthedocs.io/en/stable/index.html
 - FAQ: https://borgbackup.readthedocs.io/en/stable/faq.html
 and
 - open issues in Github tracker: https://github.com/borgbackup/borg/issues
  
If you cannot find a similar problem, then create a new issue.

Please fill in as much of the template as possible.
-->

## Have you checked borgbackup docs, FAQ, and open Github issues?

No

## Is this a BUG / ISSUE report or a QUESTION?

BUG

## System information. For client/server mode post info for both machines.

#### Your borg version (borg -V).

1.2.1

#### Operating system (distribution) and version.

#### Hardware / network configuration, and filesystems used.

Debian smb server / gvfs

#### How much data is handled by borg?

#### Full borg commandline that lead to the problem (leave away excludes and passwords)

`borg create ...` on `/run/user/1000/gvfs/smb-share:server`

## Describe the problem you're observing.

```
    os.chmod(self.tmp_fname, mode=0o666 & ~ umask)
OSError: [Errno 95] Operation not supported: '/run/user/1000/gvfs/smb-share:server=…/test/nonce-wgo81cle.tmp'

Platform: Linux desktop 5.10.0-9-amd64 #1 SMP Debian 5.10.70-1 (2021-09-30) x86_64
Linux: Unknown Linux  
Borg: 1.2.1  Python: CPython 3.9.9 msgpack: 1.0.4 fuse: pyfuse3 3.2.1 [pyfuse3,llfuse]
```

Looks like this is a problem because SMB just doesn't support chmod (at least I can't do it via terminal with gvfs)? 0f9d88bf2e1f2d14be8fa30810948ef0e4107f42

Original issue is https://gitlab.gnome.org/World/pika-backup/-/issues/241


Discussion:
Yeah, 0f9d88b is the changeset that introduced this to fix another issue. Guess we need a try / except there.

If the chmod does not work, it is usually no problem except that the mode of the file will look like the umask was not respected. As long as the stuff is owned by the borg user, the owner permissions are enough.


Resolving patch:
diff --git a/src/borg/platform/base.py b/src/borg/platform/base.py
--- a/src/borg/platform/base.py
+++ b/src/borg/platform/base.py
@@ -239,13 +239,18 @@ def __exit__(self, exc_type, exc_val, exc_tb):
             safe_unlink(self.tmp_fname)  # with-body has failed, clean up tmp file
             return  # continue processing the exception normally
 
-        # tempfile.mkstemp always uses owner-only file permissions for the temp file,
-        # but as we'll rename it to the non-temp permanent file now, we need to respect
-        # the umask and change the file mode to what a normally created file would have.
-        # thanks to the crappy os.umask api, we can't query the umask without setting it. :-(
-        umask = os.umask(UMASK_DEFAULT)
-        os.umask(umask)
-        os.chmod(self.tmp_fname, mode=0o666 & ~ umask)
+        try:
+            # tempfile.mkstemp always uses owner-only file permissions for the temp file,
+            # but as we'll rename it to the non-temp permanent file now, we need to respect
+            # the umask and change the file mode to what a normally created file would have.
+            # thanks to the crappy os.umask api, we can't query the umask without setting it. :-(
+            umask = os.umask(UMASK_DEFAULT)
+            os.umask(umask)
+            os.chmod(self.tmp_fname, mode=0o666 & ~ umask)
+        except OSError:
+            # chmod might fail if the fs does not support it.
+            # this is not harmful, the file will still have permissions for the owner.
+            pass
 
         try:
             os.replace(self.tmp_fname, self.path)  # POSIX: atomic rename


Test patch: