1//go:build openbsd || solaris
2// +build openbsd solaris
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
34	st := syscall.Stat_t{}
35	err := syscall.Stat(file, &st)
36	if err != nil {
37		return "", err
38	}
39
40	var fileAttr strings.Builder
41	fileAttr.WriteString("atime:")
42	fileAttr.WriteString(strconv.FormatInt(int64(st.Atim.Sec), 10) + "#" + strconv.FormatInt(int64(st.Atim.Nsec), 10))
43	fileAttr.WriteString("/gid:")
44	fileAttr.WriteString(strconv.Itoa(int(st.Gid)))
45
46	g, err := user.LookupGroupId(strconv.FormatUint(uint64(st.Gid), 10))
47	if err == nil {
48		fileAttr.WriteString("/gname:")
49		fileAttr.WriteString(g.Name)
50	}
51
52	fileAttr.WriteString("/mode:")
53	fileAttr.WriteString(strconv.Itoa(int(st.Mode)))
54	fileAttr.WriteString("/mtime:")
55	fileAttr.WriteString(strconv.FormatInt(int64(st.Mtim.Sec), 10) + "#" + strconv.FormatInt(int64(st.Mtim.Nsec), 10))
56	fileAttr.WriteString("/uid:")
57	fileAttr.WriteString(strconv.Itoa(int(st.Uid)))
58
59	u, err := user.LookupId(strconv.FormatUint(uint64(st.Uid), 10))
60	if err == nil {
61		fileAttr.WriteString("/uname:")
62		fileAttr.WriteString(u.Username)
63	}
64
65	return fileAttr.String(), nil
66}
67