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
52 func (our *Inode) Equals(their *Inode) bool {
53 if our.Size != their.Size {
58 if our.CtimeSec != their.CtimeSec || our.CtimeNsec != their.CtimeNsec {
62 if our.MtimeSec == 0 || our.MtimeNsec == 0 {
65 if our.MtimeSec != their.MtimeSec || our.MtimeNsec != their.MtimeNsec {
72 func (inode *Inode) RecfileFields() []recfile.Field {
73 return []recfile.Field{
74 {Name: "Size", Value: strconv.FormatInt(inode.Size, 10)},
75 {Name: "CtimeSec", Value: strconv.FormatInt(inode.CtimeSec, 10)},
76 {Name: "CtimeNsec", Value: strconv.FormatInt(inode.CtimeNsec, 10)},
77 {Name: "MtimeSec", Value: strconv.FormatInt(inode.MtimeSec, 10)},
78 {Name: "MtimeNsec", Value: strconv.FormatInt(inode.MtimeNsec, 10)},
82 func inodeFromFile(fd *os.File) (*Inode, error) {
89 err = unix.Fstat(int(fd.Fd()), &stat)
93 ctimeSec, ctimeNsec := stat.Ctim.Unix()
94 mtimeSec := fi.ModTime().Unix()
95 mtimeNsec := fi.ModTime().UnixNano()
98 CtimeSec: ctimeSec, CtimeNsec: ctimeNsec,
99 MtimeSec: mtimeSec, MtimeNsec: mtimeNsec,
103 func inodeFromRec(m map[string]string) (*Inode, error) {
105 ctimeSec := m["CtimeSec"]
106 ctimeNsec := m["CtimeNsec"]
107 mtimeSec := m["MtimeSec"]
108 mtimeNsec := m["MtimeNsec"]
110 return nil, errors.New("Size is missing")
113 return nil, errors.New("CtimeSec is missing")
116 return nil, errors.New("CtimeNsec is missing")
120 inode.Size, err = strconv.ParseInt(size, 10, 64)
124 inode.CtimeSec, err = strconv.ParseInt(ctimeSec, 10, 64)
128 inode.CtimeNsec, err = strconv.ParseInt(ctimeNsec, 10, 64)
134 return nil, errors.New("MtimeNsec is missing")
136 inode.MtimeSec, err = strconv.ParseInt(mtimeSec, 10, 64)
140 inode.MtimeNsec, err = strconv.ParseInt(mtimeNsec, 10, 64)