gopkg.in/essentialkaos/ek.v3@v3.5.1/system/user_freebsd.go (about)

     1  // +build freebsd
     2  
     3  package system
     4  
     5  // ////////////////////////////////////////////////////////////////////////////////// //
     6  //                                                                                    //
     7  //                     Copyright (c) 2009-2016 Essential Kaos                         //
     8  //      Essential Kaos Open Source License <http://essentialkaos.com/ekol?en>         //
     9  //                                                                                    //
    10  // ////////////////////////////////////////////////////////////////////////////////// //
    11  
    12  import (
    13  	"errors"
    14  	"fmt"
    15  	"os/exec"
    16  	"strconv"
    17  	"strings"
    18  	"syscall"
    19  	"time"
    20  )
    21  
    22  // ////////////////////////////////////////////////////////////////////////////////// //
    23  
    24  // getTimes is copy of fsutil.GetTimes
    25  func getTimes(path string) (time.Time, time.Time, time.Time, error) {
    26  	if path == "" {
    27  		return time.Time{}, time.Time{}, time.Time{}, errors.New("Path is empty")
    28  	}
    29  
    30  	var stat = &syscall.Stat_t{}
    31  
    32  	err := syscall.Stat(path, stat)
    33  
    34  	if err != nil {
    35  		return time.Time{}, time.Time{}, time.Time{}, err
    36  	}
    37  
    38  	return time.Unix(int64(stat.Atimespec.Sec), int64(stat.Atimespec.Nsec)),
    39  		time.Unix(int64(stat.Mtimespec.Sec), int64(stat.Mtimespec.Nsec)),
    40  		time.Unix(int64(stat.Ctimespec.Sec), int64(stat.Ctimespec.Nsec)),
    41  		nil
    42  }
    43  
    44  // getUserInfo find user info by name or id
    45  func getUserInfo(nameOrID string) (*User, error) {
    46  	cmd := exec.Command("getent", "passwd", nameOrID)
    47  
    48  	out, err := cmd.Output()
    49  
    50  	if err != nil {
    51  		return nil, fmt.Errorf("User with this name/id %s is not exist", nameOrID)
    52  	}
    53  
    54  	sOut := string(out[:])
    55  	sOut = strings.Trim(sOut, "\n")
    56  	aOut := strings.Split(sOut, ":")
    57  
    58  	uid, _ := strconv.Atoi(aOut[2])
    59  	gid, _ := strconv.Atoi(aOut[3])
    60  
    61  	return &User{
    62  		Name:     aOut[0],
    63  		UID:      uid,
    64  		GID:      gid,
    65  		Comment:  aOut[4],
    66  		HomeDir:  aOut[5],
    67  		Shell:    aOut[6],
    68  		RealName: aOut[0],
    69  		RealUID:  uid,
    70  		RealGID:  gid,
    71  	}, nil
    72  }