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

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