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