github.com/khulnasoft/cli@v0.0.0-20240402070845-01bcad7beefa/cli/config/configfile/file_unix.go (about)

     1  //go:build !windows
     2  
     3  package configfile
     4  
     5  import (
     6  	"os"
     7  	"syscall"
     8  )
     9  
    10  // copyFilePermissions copies file ownership and permissions from "src" to "dst",
    11  // ignoring any error during the process.
    12  func copyFilePermissions(src, dst string) {
    13  	var (
    14  		mode     os.FileMode = 0o600
    15  		uid, gid int
    16  	)
    17  
    18  	fi, err := os.Stat(src)
    19  	if err != nil {
    20  		return
    21  	}
    22  	if fi.Mode().IsRegular() {
    23  		mode = fi.Mode()
    24  	}
    25  	if err := os.Chmod(dst, mode); err != nil {
    26  		return
    27  	}
    28  
    29  	uid = int(fi.Sys().(*syscall.Stat_t).Uid)
    30  	gid = int(fi.Sys().(*syscall.Stat_t).Gid)
    31  
    32  	if uid > 0 && gid > 0 {
    33  		_ = os.Chown(dst, uid, gid)
    34  	}
    35  }