github.com/minio/mc@v0.0.0-20240507152021-646712d5e5fb/pkg/disk/stat_netbsd.go (about)

     1  // Copyright (c) 2015-2021 MinIO, Inc.
     2  //
     3  // This file is part of MinIO Object Storage stack
     4  //
     5  // This program is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Affero General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // This program is distributed in the hope that it will be useful
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU Affero General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Affero General Public License
    16  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package disk
    19  
    20  import (
    21  	"os/user"
    22  	"strconv"
    23  	"strings"
    24  	"syscall"
    25  )
    26  
    27  // GetFileSystemAttrs return the file system attribute as string; containing mode,
    28  // uid, gid, uname, Gname, atime, mtime, ctime and md5
    29  func GetFileSystemAttrs(file string) (string, error) {
    30  	st := syscall.Stat_t{}
    31  	err := syscall.Stat(file, &st)
    32  	if err != nil {
    33  		return "", err
    34  	}
    35  
    36  	var fileAttr strings.Builder
    37  	fileAttr.WriteString("atime:")
    38  	fileAttr.WriteString(strconv.FormatInt(st.Atimespec.Sec, 10) + "#" + strconv.FormatInt(st.Atimespec.Nsec, 10))
    39  	fileAttr.WriteString("/gid:")
    40  	fileAttr.WriteString(strconv.Itoa(int(st.Gid)))
    41  
    42  	g, err := user.LookupGroupId(strconv.FormatUint(uint64(st.Gid), 10))
    43  	if err == nil {
    44  		fileAttr.WriteString("/gname:")
    45  		fileAttr.WriteString(g.Name)
    46  	}
    47  
    48  	fileAttr.WriteString("/mode:")
    49  	fileAttr.WriteString(strconv.Itoa(int(st.Mode)))
    50  	fileAttr.WriteString("/mtime:")
    51  	fileAttr.WriteString(strconv.FormatInt(st.Mtimespec.Sec, 10) + "#" + strconv.FormatInt(st.Mtimespec.Nsec, 10))
    52  	fileAttr.WriteString("/uid:")
    53  	fileAttr.WriteString(strconv.Itoa(int(st.Uid)))
    54  
    55  	u, err := user.LookupId(strconv.FormatUint(uint64(st.Uid), 10))
    56  	if err == nil {
    57  		fileAttr.WriteString("/uname:")
    58  		fileAttr.WriteString(u.Username)
    59  	}
    60  
    61  	return fileAttr.String(), nil
    62  }