1// Copyright 2009 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package os
6
7import (
8	"syscall"
9	"time"
10)
11
12func sameFile(fs1, fs2 *fileStat) bool {
13	stat1 := fs1.sys.(*syscall.Stat_t)
14	stat2 := fs2.sys.(*syscall.Stat_t)
15	return stat1.Dev == stat2.Dev && stat1.Ino == stat2.Ino
16}
17
18func fileInfoFromStat(st *syscall.Stat_t, name string) FileInfo {
19	fs := &fileStat{
20		name:    basename(name),
21		size:    int64(st.Size),
22		modTime: timespecToTime(st.Mtim),
23		sys:     st,
24	}
25	fs.mode = FileMode(st.Mode & 0777)
26	switch st.Mode & syscall.S_IFMT {
27	case syscall.S_IFBLK:
28		fs.mode |= ModeDevice
29	case syscall.S_IFCHR:
30		fs.mode |= ModeDevice | ModeCharDevice
31	case syscall.S_IFDIR:
32		fs.mode |= ModeDir
33	case syscall.S_IFIFO:
34		fs.mode |= ModeNamedPipe
35	case syscall.S_IFLNK:
36		fs.mode |= ModeSymlink
37	case syscall.S_IFREG:
38		// nothing to do
39	case syscall.S_IFSOCK:
40		fs.mode |= ModeSocket
41	}
42	if st.Mode&syscall.S_ISGID != 0 {
43		fs.mode |= ModeSetgid
44	}
45	if st.Mode&syscall.S_ISUID != 0 {
46		fs.mode |= ModeSetuid
47	}
48	if st.Mode&syscall.S_ISVTX != 0 {
49		fs.mode |= ModeSticky
50	}
51	return fs
52}
53
54func timespecToTime(ts syscall.Timespec) time.Time {
55	return time.Unix(int64(ts.Sec), int64(ts.Nsec))
56}
57
58// For testing.
59func atime(fi FileInfo) time.Time {
60	return timespecToTime(fi.Sys().(*syscall.Stat_t).Atim)
61}
62