github.com/minio/mc@v0.0.0-20240507152021-646712d5e5fb/pkg/disk/stat_freebsd.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  	// NOTE: Keep int64 casts. See https://github.com/minio/mc/issues/4005
    39  	fileAttr.WriteString(strconv.FormatInt(int64(st.Atimespec.Sec), 10) + "#" + strconv.FormatInt(int64(st.Atimespec.Nsec), 10))
    40  	fileAttr.WriteString("/gid:")
    41  	fileAttr.WriteString(strconv.Itoa(int(st.Gid)))
    42  
    43  	g, err := user.LookupGroupId(strconv.FormatUint(uint64(st.Gid), 10))
    44  	if err == nil {
    45  		fileAttr.WriteString("/gname:")
    46  		fileAttr.WriteString(g.Name)
    47  	}
    48  
    49  	fileAttr.WriteString("/mode:")
    50  	fileAttr.WriteString(strconv.Itoa(int(st.Mode)))
    51  	fileAttr.WriteString("/mtime:")
    52  	// NOTE: Keep int64 casts. See https://github.com/minio/mc/issues/4005
    53  	fileAttr.WriteString(strconv.FormatInt(int64(st.Mtimespec.Sec), 10) + "#" + strconv.FormatInt(int64(st.Mtimespec.Nsec), 10))
    54  	fileAttr.WriteString("/uid:")
    55  	fileAttr.WriteString(strconv.Itoa(int(st.Uid)))
    56  
    57  	u, err := user.LookupId(strconv.FormatUint(uint64(st.Uid), 10))
    58  	if err == nil {
    59  		fileAttr.WriteString("/uname:")
    60  		fileAttr.WriteString(u.Username)
    61  	}
    62  
    63  	return fileAttr.String(), nil
    64  }