github.com/xhghs/rclone@v1.51.1-0.20200430155106-e186a28cced8/fs/config/config_unix.go (about)

     1  // Read, write and edit the config file
     2  // Unix specific functions.
     3  
     4  // +build darwin dragonfly freebsd linux netbsd openbsd solaris
     5  
     6  package config
     7  
     8  import (
     9  	"os"
    10  	"os/user"
    11  	"strconv"
    12  	"syscall"
    13  
    14  	"github.com/rclone/rclone/fs"
    15  )
    16  
    17  // attemptCopyGroups tries to keep the group the same. User will be the one
    18  // who is currently running this process.
    19  func attemptCopyGroup(fromPath, toPath string) {
    20  	info, err := os.Stat(fromPath)
    21  	if err != nil || info.Sys() == nil {
    22  		return
    23  	}
    24  	if stat, ok := info.Sys().(*syscall.Stat_t); ok {
    25  		uid := int(stat.Uid)
    26  		// prefer self over previous owner of file, because it has a higher chance
    27  		// of success
    28  		if user, err := user.Current(); err == nil {
    29  			if tmpUID, err := strconv.Atoi(user.Uid); err == nil {
    30  				uid = tmpUID
    31  			}
    32  		}
    33  		if err = os.Chown(toPath, uid, int(stat.Gid)); err != nil {
    34  			fs.Debugf(nil, "Failed to keep previous owner of config file: %v", err)
    35  		}
    36  	}
    37  }