1 // goredo -- djb's redo implementation on pure Go
2 // Copyright (C) 2020-2024 Sergey Matveev <stargrave@stargrave.org>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, version 3 of the License.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // Inode metainformation
26 "go.cypherpunks.ru/recfile"
27 "golang.org/x/sys/unix"
30 const InodeLen = 6 * 8
32 type InodeTrustType int
34 //go:generate stringer -type=InodeTrustType
36 EnvInodeTrust = "REDO_INODE_TRUST"
38 InodeTrustNone InodeTrustType = iota
43 var InodeTrust InodeTrustType
45 // It is big-endian 64-bit unsigned integers: size, inodeNum,
46 // ctime sec, ctime nsec, mtime sec, mtime nsec.
47 type Inode [InodeLen]byte
49 func (our *Inode) Equals(their *Inode) bool {
50 if !bytes.Equal(our[:2*8], their[:2*8]) {
55 if !bytes.Equal(our[2*8:4*8], their[2*8:4*8]) {
59 if !bytes.Equal(our[4*8:6*8], their[4*8:6*8]) {
66 func (inode *Inode) RecfileFields() []recfile.Field {
67 return []recfile.Field{
68 {Name: "Size", Value: strconv.FormatUint(binary.BigEndian.Uint64(
69 []byte(inode[0*8:1*8])), 10)},
70 {Name: "InodeNum", Value: strconv.FormatUint(binary.BigEndian.Uint64(
71 []byte(inode[1*8:2*8])), 10)},
72 {Name: "CtimeSec", Value: strconv.FormatUint(binary.BigEndian.Uint64(
73 []byte(inode[2*8:3*8])), 10)},
74 {Name: "CtimeNsec", Value: strconv.FormatUint(binary.BigEndian.Uint64(
75 []byte(inode[3*8:4*8])), 10)},
76 {Name: "MtimeSec", Value: strconv.FormatUint(binary.BigEndian.Uint64(
77 []byte(inode[4*8:5*8])), 10)},
78 {Name: "MtimeNsec", Value: strconv.FormatUint(binary.BigEndian.Uint64(
79 []byte(inode[5*8:6*8])), 10)},
83 func inodeFromFileStat(fi os.FileInfo, stat unix.Stat_t) *Inode {
84 ctimeSec, ctimeNsec := stat.Ctim.Unix()
85 mtimeSec := fi.ModTime().Unix()
86 mtimeNsec := fi.ModTime().UnixNano()
88 binary.BigEndian.PutUint64(inode[0*8:1*8], uint64(fi.Size()))
89 binary.BigEndian.PutUint64(inode[1*8:2*8], uint64(stat.Ino))
90 binary.BigEndian.PutUint64(inode[2*8:3*8], uint64(ctimeSec))
91 binary.BigEndian.PutUint64(inode[3*8:4*8], uint64(ctimeNsec))
92 binary.BigEndian.PutUint64(inode[4*8:5*8], uint64(mtimeSec))
93 binary.BigEndian.PutUint64(inode[5*8:6*8], uint64(mtimeNsec))
97 func inodeFromFileByFd(fd *os.File) (inode *Inode, isDir bool, err error) {
107 err = unix.Fstat(int(fd.Fd()), &stat)
111 inode = inodeFromFileStat(fi, stat)
115 func inodeFromFileByPath(p string) (*Inode, error) {
116 fi, err := os.Stat(p)
121 err = unix.Stat(p, &stat)
125 return inodeFromFileStat(fi, stat), nil