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
27 "go.cypherpunks.ru/recfile"
28 "golang.org/x/sys/unix"
31 const InodeLen = 6 * 8
33 type InodeTrustType int
35 //go:generate stringer -type=InodeTrustType
37 EnvInodeTrust = "REDO_INODE_TRUST"
39 InodeTrustNone InodeTrustType = iota
44 var InodeTrust InodeTrustType
46 // It is big-endian 64-bit unsigned integers: size, inodeNum,
47 // ctime sec, ctime nsec, mtime sec, mtime nsec.
50 func (our Inode) Equals(their Inode) bool {
51 if our[:2*8] != their[:2*8] {
56 if our[2*8:4*8] != their[2*8:4*8] {
60 if our[4*8:6*8] != their[4*8:6*8] {
67 func (inode Inode) RecfileFields() []recfile.Field {
68 return []recfile.Field{
69 {Name: "Size", Value: strconv.FormatUint(binary.BigEndian.Uint64(
70 []byte(inode[0*8:1*8])), 10)},
71 {Name: "InodeNum", Value: strconv.FormatUint(binary.BigEndian.Uint64(
72 []byte(inode[1*8:2*8])), 10)},
73 {Name: "CtimeSec", Value: strconv.FormatUint(binary.BigEndian.Uint64(
74 []byte(inode[2*8:3*8])), 10)},
75 {Name: "CtimeNsec", Value: strconv.FormatUint(binary.BigEndian.Uint64(
76 []byte(inode[3*8:4*8])), 10)},
77 {Name: "MtimeSec", Value: strconv.FormatUint(binary.BigEndian.Uint64(
78 []byte(inode[4*8:5*8])), 10)},
79 {Name: "MtimeNsec", Value: strconv.FormatUint(binary.BigEndian.Uint64(
80 []byte(inode[5*8:6*8])), 10)},
84 func inodeFromFileStat(fi os.FileInfo, stat unix.Stat_t) Inode {
85 ctimeSec, ctimeNsec := stat.Ctim.Unix()
86 mtimeSec := fi.ModTime().Unix()
87 mtimeNsec := fi.ModTime().UnixNano()
88 buf := make([]byte, InodeLen)
89 binary.BigEndian.PutUint64(buf[0*8:1*8], uint64(fi.Size()))
90 binary.BigEndian.PutUint64(buf[1*8:2*8], uint64(stat.Ino))
91 binary.BigEndian.PutUint64(buf[2*8:3*8], uint64(ctimeSec))
92 binary.BigEndian.PutUint64(buf[3*8:4*8], uint64(ctimeNsec))
93 binary.BigEndian.PutUint64(buf[4*8:5*8], uint64(mtimeSec))
94 binary.BigEndian.PutUint64(buf[5*8:6*8], uint64(mtimeNsec))
98 func inodeFromFileByFd(fd *os.File) (inode Inode, isDir bool, err error) {
108 err = unix.Fstat(int(fd.Fd()), &stat)
112 inode = inodeFromFileStat(fi, stat)
116 func inodeFromFileByPath(p string) (Inode, error) {
117 fi, err := os.Stat(p)
122 err = unix.Stat(p, &stat)
126 return inodeFromFileStat(fi, stat), nil