2 goredo -- djb's redo implementation on pure Go
3 Copyright (C) 2020-2022 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 type InodeTrustType int
33 //go:generate stringer -type=InodeTrustType
35 EnvInodeTrust = "REDO_INODE_TRUST"
37 InodeTrustNone InodeTrustType = iota
42 var InodeTrust InodeTrustType
53 func (our *Inode) Equals(their *Inode) bool {
54 if our.Size != their.Size {
57 if our.InodeNum != their.InodeNum {
62 if our.CtimeSec != their.CtimeSec || our.CtimeNsec != their.CtimeNsec {
66 if our.MtimeSec == 0 || our.MtimeNsec == 0 {
69 if our.MtimeSec != their.MtimeSec || our.MtimeNsec != their.MtimeNsec {
76 func (inode *Inode) RecfileFields() []recfile.Field {
77 return []recfile.Field{
78 {Name: "Size", Value: strconv.FormatInt(inode.Size, 10)},
79 {Name: "InodeNum", Value: strconv.FormatUint(inode.InodeNum, 10)},
80 {Name: "CtimeSec", Value: strconv.FormatInt(inode.CtimeSec, 10)},
81 {Name: "CtimeNsec", Value: strconv.FormatInt(inode.CtimeNsec, 10)},
82 {Name: "MtimeSec", Value: strconv.FormatInt(inode.MtimeSec, 10)},
83 {Name: "MtimeNsec", Value: strconv.FormatInt(inode.MtimeNsec, 10)},
87 func inodeFromFile(fd *os.File) (*Inode, error) {
94 err = unix.Fstat(int(fd.Fd()), &stat)
98 ctimeSec, ctimeNsec := stat.Ctim.Unix()
99 mtimeSec := fi.ModTime().Unix()
100 mtimeNsec := fi.ModTime().UnixNano()
103 InodeNum: uint64(stat.Ino),
104 CtimeSec: ctimeSec, CtimeNsec: ctimeNsec,
105 MtimeSec: mtimeSec, MtimeNsec: mtimeNsec,
109 func inodeFromRec(m map[string]string) (*Inode, error) {
111 inodeNum := m["InodeNum"]
112 ctimeSec := m["CtimeSec"]
113 ctimeNsec := m["CtimeNsec"]
114 mtimeSec := m["MtimeSec"]
115 mtimeNsec := m["MtimeNsec"]
117 return nil, errors.New("Size is missing")
120 return nil, errors.New("CtimeSec is missing")
123 return nil, errors.New("CtimeNsec is missing")
127 inode.Size, err = strconv.ParseInt(size, 10, 64)
132 inode.InodeNum, err = strconv.ParseUint(inodeNum, 10, 64)
137 inode.CtimeSec, err = strconv.ParseInt(ctimeSec, 10, 64)
141 inode.CtimeNsec, err = strconv.ParseInt(ctimeNsec, 10, 64)
147 return nil, errors.New("MtimeNsec is missing")
149 inode.MtimeSec, err = strconv.ParseInt(mtimeSec, 10, 64)
153 inode.MtimeNsec, err = strconv.ParseInt(mtimeNsec, 10, 64)