Repository: syncthing/syncthing (language: go)

Issue:
Copy ownership feature not working with directories and files in it
I am trying to setup an linux instance with [copyOwnershipFromParent](https://docs.syncthing.net/v1.20.2/users/config#config-option-folder.copyownershipfromparent) enabled but it seems to be a little broken. As for files at the base level of the synced directory the ownership is indeed being copied, but when a new directory and files inside it are to be synced it is not.  
  
Take this directory structure as an example, `pictures` is the synced folder:
```
pictures/
├─ img1.jpg
├─ img2.jpg
├─ 2020/
│  ├─ img3.jpg
│  ├─ img4.jpg
├─ 2021/
│  ├─ img5.jpg
├─ 2022/
│  ├─ img6.jpg
```

In this example only `img1.jpg` and `img2.jpg` has the expected ownership, directories and other files inherit owner and group from the user which started the instance and not from the parent directory.

I am using version `1.20.2`



Discussion / hints:
Whats the owner of 2020 etc? Files would have same ownership as owner of that directory (which should have ownership of the root).

Note that this is only done once, so if you change the ownership after the directory is created, it will stay changed.
> which should have ownership of the root

That's exactly the case, they don't. Directories are created with the ownership of a syncthing user.  
Lets say I run the syncthing instance as `john` and the owner of the `pictures` directory is `bob`. When a directory is to be synchronized from a remote device then the owner is `john`. Automatically all the files and nested directories and files inside are owned by `john`. It only happens with directories, when a file directly in `pictures` directory is to be synced, it has the expected owner `bob`.


I guess you can enable fs debug logs and see whats happening. 

Are you running as a user with special capabilities or as root?

Feels like the chown calls are failing and the problem is in permissions rather syncthing.

Feels like we should close this and move it to the forums.

I am not convinced its a bug yet.
Might be a timing thing... Certainly sounds like a bug to me. I'll investigate at some point. :)
Just looked into inotify logs and I don't seen any `attrib` events for the directories that are being synced, just the files. I could not find anything in the syncthing logs even after enabling all facilities. So either the chown is failing somewhere but I can't find it or it is simply not being called by some mistake.

Edit: I've run strace and it looks like the issue is `chown` not being called for directories but only for `.tmp` files which are later renamed.

Fix patch:
diff --git a/lib/model/folder_sendrecv.go b/lib/model/folder_sendrecv.go
index f71407e31e0..c12377acb47 100644
--- a/lib/model/folder_sendrecv.go
+++ b/lib/model/folder_sendrecv.go
@@ -622,7 +622,7 @@ func (f *sendReceiveFolder) handleDir(file protocol.FileInfo, snap *db.Snapshot,
 		// not MkdirAll because the parent should already exist.
 		mkdir := func(path string) error {
 			err = f.mtimefs.Mkdir(path, mode)
-			if err != nil || f.IgnorePerms || file.NoPermissions {
+			if err != nil || f.IgnorePerms {
 				return err
 			}
 
@@ -631,6 +631,10 @@ func (f *sendReceiveFolder) handleDir(file protocol.FileInfo, snap *db.Snapshot,
 				return err
 			}
 
+			if file.NoPermissions {
+				return nil
+			}
+
 			// Stat the directory so we can check its permissions.
 			info, err := f.mtimefs.Lstat(path)
 			if err != nil {


Test patch: