1//go:build darwin
2// +build darwin
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
21package disk
22
23import (
24	"os/user"
25	"strconv"
26	"strings"
27	"syscall"
28)
29
30// GetFileSystemAttrs return the file system attribute as string; containing mode,
31// uid, gid, uname, Gname, atime, mtime, ctime and md5
32func GetFileSystemAttrs(file string) (string, error) {
33	st := syscall.Stat_t{}
34	err := syscall.Stat(file, &st)
35	if err != nil {
36		return "", err
37	}
38
39	var fileAttr strings.Builder
40	fileAttr.WriteString("atime:")
41	fileAttr.WriteString(strconv.FormatInt(st.Atimespec.Sec, 10) + "#" + strconv.FormatInt(st.Atimespec.Nsec, 10))
42	fileAttr.WriteString("/gid:")
43	fileAttr.WriteString(strconv.Itoa(int(st.Gid)))
44
45	g, err := user.LookupGroupId(strconv.FormatUint(uint64(st.Gid), 10))
46	if err == nil {
47		fileAttr.WriteString("/gname:")
48		fileAttr.WriteString(g.Name)
49	}
50
51	fileAttr.WriteString("/mode:")
52	fileAttr.WriteString(strconv.Itoa(int(st.Mode)))
53	fileAttr.WriteString("/mtime:")
54	fileAttr.WriteString(strconv.FormatInt(st.Mtimespec.Sec, 10) + "#" + strconv.FormatInt(st.Mtimespec.Nsec, 10))
55	fileAttr.WriteString("/uid:")
56	fileAttr.WriteString(strconv.Itoa(int(st.Uid)))
57
58	u, err := user.LookupId(strconv.FormatUint(uint64(st.Uid), 10))
59	if err == nil {
60		fileAttr.WriteString("/uname:")
61		fileAttr.WriteString(u.Username)
62	}
63
64	return fileAttr.String(), nil
65}
66