]> Cypherpunks.ru repositories - goredo.git/blob - inode.go
Ability to rely on mtime, instead of ctime
[goredo.git] / inode.go
1 /*
2 goredo -- djb's redo implementation on pure Go
3 Copyright (C) 2020-2021 Sergey Matveev <stargrave@stargrave.org>
4
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.
8
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.
13
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/>.
16 */
17
18 // Inode metainformation
19
20 package main
21
22 import (
23         "errors"
24         "os"
25         "strconv"
26
27         "go.cypherpunks.ru/recfile"
28         "golang.org/x/sys/unix"
29 )
30
31 type InodeTrustType int
32
33 //go:generate stringer -type=InodeTrustType
34 const (
35         EnvInodeTrust = "REDO_INODE_TRUST"
36
37         InodeTrustNone InodeTrustType = iota
38         InodeTrustCtime
39         InodeTrustMtime
40 )
41
42 var InodeTrust InodeTrustType
43
44 type Inode struct {
45         Size      int64
46         CtimeSec  int64
47         CtimeNsec int64
48         MtimeSec  int64
49         MtimeNsec int64
50 }
51
52 func (our *Inode) Equals(their *Inode) bool {
53         if our.Size != their.Size {
54                 return false
55         }
56         switch InodeTrust {
57         case InodeTrustCtime:
58                 if our.CtimeSec != their.CtimeSec || our.CtimeNsec != their.CtimeNsec {
59                         return false
60                 }
61         case InodeTrustMtime:
62                 if our.MtimeSec == 0 || our.MtimeNsec == 0 {
63                         return false
64                 }
65                 if our.MtimeSec != their.MtimeSec || our.MtimeNsec != their.MtimeNsec {
66                         return false
67                 }
68         }
69         return true
70 }
71
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)},
79         }
80 }
81
82 func inodeFromFile(fd *os.File) (*Inode, error) {
83         var fi os.FileInfo
84         fi, err := fd.Stat()
85         if err != nil {
86                 return nil, err
87         }
88         var stat unix.Stat_t
89         err = unix.Fstat(int(fd.Fd()), &stat)
90         if err != nil {
91                 return nil, err
92         }
93         ctimeSec, ctimeNsec := stat.Ctim.Unix()
94         mtimeSec := fi.ModTime().Unix()
95         mtimeNsec := fi.ModTime().UnixNano()
96         return &Inode{
97                 Size:     fi.Size(),
98                 CtimeSec: ctimeSec, CtimeNsec: ctimeNsec,
99                 MtimeSec: mtimeSec, MtimeNsec: mtimeNsec,
100         }, nil
101 }
102
103 func inodeFromRec(m map[string]string) (*Inode, error) {
104         size := m["Size"]
105         ctimeSec := m["CtimeSec"]
106         ctimeNsec := m["CtimeNsec"]
107         mtimeSec := m["MtimeSec"]
108         mtimeNsec := m["MtimeNsec"]
109         if size == "" {
110                 return nil, errors.New("Size is missing")
111         }
112         if ctimeSec == "" {
113                 return nil, errors.New("CtimeSec is missing")
114         }
115         if ctimeNsec == "" {
116                 return nil, errors.New("CtimeNsec is missing")
117         }
118         inode := Inode{}
119         var err error
120         inode.Size, err = strconv.ParseInt(size, 10, 64)
121         if err != nil {
122                 return nil, err
123         }
124         inode.CtimeSec, err = strconv.ParseInt(ctimeSec, 10, 64)
125         if err != nil {
126                 return nil, err
127         }
128         inode.CtimeNsec, err = strconv.ParseInt(ctimeNsec, 10, 64)
129         if err != nil {
130                 return nil, err
131         }
132         if mtimeSec != "" {
133                 if mtimeNsec == "" {
134                         return nil, errors.New("MtimeNsec is missing")
135                 }
136                 inode.MtimeSec, err = strconv.ParseInt(mtimeSec, 10, 64)
137                 if err != nil {
138                         return nil, err
139                 }
140                 inode.MtimeNsec, err = strconv.ParseInt(mtimeNsec, 10, 64)
141                 if err != nil {
142                         return nil, err
143                 }
144         }
145         return &inode, nil
146 }