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 inodeFromFileStat(fi os.FileInfo, stat unix.Stat_t) *Inode {
88 ctimeSec, ctimeNsec := stat.Ctim.Unix()
89 mtimeSec := fi.ModTime().Unix()
90 mtimeNsec := fi.ModTime().UnixNano()
93 InodeNum: uint64(stat.Ino),
94 CtimeSec: ctimeSec, CtimeNsec: ctimeNsec,
95 MtimeSec: mtimeSec, MtimeNsec: mtimeNsec,
99 func inodeFromFileByFd(fd *os.File) (*Inode, error) {
105 err = unix.Fstat(int(fd.Fd()), &stat)
109 return inodeFromFileStat(fi, stat), nil
112 func inodeFromFileByPath(p string) (*Inode, error) {
113 fi, err := os.Stat(p)
118 err = unix.Stat(p, &stat)
122 return inodeFromFileStat(fi, stat), nil
125 func inodeFromRec(m map[string]string) (*Inode, error) {
127 inodeNum := m["InodeNum"]
128 ctimeSec := m["CtimeSec"]
129 ctimeNsec := m["CtimeNsec"]
130 mtimeSec := m["MtimeSec"]
131 mtimeNsec := m["MtimeNsec"]
133 return nil, errors.New("Size is missing")
136 return nil, errors.New("CtimeSec is missing")
139 return nil, errors.New("CtimeNsec is missing")
143 inode.Size, err = strconv.ParseInt(size, 10, 64)
148 inode.InodeNum, err = strconv.ParseUint(inodeNum, 10, 64)
153 inode.CtimeSec, err = strconv.ParseInt(ctimeSec, 10, 64)
157 inode.CtimeNsec, err = strconv.ParseInt(ctimeNsec, 10, 64)
163 return nil, errors.New("MtimeNsec is missing")
165 inode.MtimeSec, err = strconv.ParseInt(mtimeSec, 10, 64)
169 inode.MtimeNsec, err = strconv.ParseInt(mtimeNsec, 10, 64)