cmd/syncthing: Apply file permissions and modtimes when decrypting (#8412)

Directories are not represented in the encrypted storagage so they get
to keep default permissions...
This commit is contained in:
Jakob Borg 2022-07-04 10:57:45 +02:00 committed by GitHub
parent 13d545c9f2
commit abea3d7552
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 1 deletions

View File

@ -129,6 +129,9 @@ func (c *CLI) getFolderID() (string, error) {
// process handles the file named path in srcFs, decrypting it into dstFs
// unless dstFs is nil.
func (c *CLI) process(srcFs fs.Filesystem, dstFs fs.Filesystem, path string) error {
// Which filemode bits to preserve
const retainBits = fs.ModePerm | fs.ModeSetgid | fs.ModeSetuid | fs.ModeSticky
if c.Verbose {
log.Printf("Processing %q", path)
}
@ -168,6 +171,9 @@ func (c *CLI) process(srcFs fs.Filesystem, dstFs fs.Filesystem, path string) err
return fmt.Errorf("%s: %w", plainFi.Name, err)
}
defer plainFd.Close() // also closed explicitly in the return
if err := dstFs.Chmod(plainFi.Name, fs.FileMode(plainFi.Permissions&uint32(retainBits))); err != nil {
return fmt.Errorf("%s: %w", plainFi.Name, err)
}
}
if err := c.decryptFile(encFi, &plainFi, encFd, plainFd); err != nil {
@ -184,7 +190,12 @@ func (c *CLI) process(srcFs fs.Filesystem, dstFs fs.Filesystem, path string) err
}
if plainFd != nil {
return plainFd.Close()
if err := plainFd.Close(); err != nil {
return fmt.Errorf("%s: %w", plainFi.Name, err)
}
if err := dstFs.Chtimes(plainFi.Name, plainFi.ModTime(), plainFi.ModTime()); err != nil {
return fmt.Errorf("%s: %w", plainFi.Name, err)
}
}
return nil
}