]> Cypherpunks.ru repositories - goredo.git/blob - inode.go
2f2285d407de55eb3b1bfd3cdea1f8b53bbb44d4
[goredo.git] / inode.go
1 /*
2 goredo -- djb's redo implementation on pure Go
3 Copyright (C) 2020-2023 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         InodeNum  uint64
47         CtimeSec  int64
48         CtimeNsec int64
49         MtimeSec  int64
50         MtimeNsec int64
51 }
52
53 func (our *Inode) Equals(their *Inode) bool {
54         if our.Size != their.Size {
55                 return false
56         }
57         if our.InodeNum != their.InodeNum {
58                 return false
59         }
60         switch InodeTrust {
61         case InodeTrustCtime:
62                 if our.CtimeSec != their.CtimeSec || our.CtimeNsec != their.CtimeNsec {
63                         return false
64                 }
65         case InodeTrustMtime:
66                 if our.MtimeSec == 0 || our.MtimeNsec == 0 {
67                         return false
68                 }
69                 if our.MtimeSec != their.MtimeSec || our.MtimeNsec != their.MtimeNsec {
70                         return false
71                 }
72         }
73         return true
74 }
75
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)},
84         }
85 }
86
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()
91         return &Inode{
92                 Size:     fi.Size(),
93                 InodeNum: uint64(stat.Ino),
94                 CtimeSec: ctimeSec, CtimeNsec: ctimeNsec,
95                 MtimeSec: mtimeSec, MtimeNsec: mtimeNsec,
96         }
97 }
98
99 func inodeFromFileByFd(fd *os.File) (inode *Inode, isDir bool, err error) {
100         fi, err := fd.Stat()
101         if err != nil {
102                 return
103         }
104         if fi.IsDir() {
105                 isDir = true
106                 return
107         }
108         var stat unix.Stat_t
109         err = unix.Fstat(int(fd.Fd()), &stat)
110         if err != nil {
111                 return
112         }
113         inode = inodeFromFileStat(fi, stat)
114         return
115 }
116
117 func inodeFromFileByPath(p string) (*Inode, error) {
118         fi, err := os.Stat(p)
119         if err != nil {
120                 return nil, err
121         }
122         var stat unix.Stat_t
123         err = unix.Stat(p, &stat)
124         if err != nil {
125                 return nil, err
126         }
127         return inodeFromFileStat(fi, stat), nil
128 }
129
130 func inodeFromRec(m map[string]string) (*Inode, error) {
131         size := m["Size"]
132         inodeNum := m["InodeNum"]
133         ctimeSec := m["CtimeSec"]
134         ctimeNsec := m["CtimeNsec"]
135         mtimeSec := m["MtimeSec"]
136         mtimeNsec := m["MtimeNsec"]
137         if size == "" {
138                 return nil, errors.New("Size is missing")
139         }
140         if ctimeSec == "" {
141                 return nil, errors.New("CtimeSec is missing")
142         }
143         if ctimeNsec == "" {
144                 return nil, errors.New("CtimeNsec is missing")
145         }
146         inode := Inode{}
147         var err error
148         inode.Size, err = strconv.ParseInt(size, 10, 64)
149         if err != nil {
150                 return nil, err
151         }
152         if inodeNum != "" {
153                 inode.InodeNum, err = strconv.ParseUint(inodeNum, 10, 64)
154                 if err != nil {
155                         return nil, err
156                 }
157         }
158         inode.CtimeSec, err = strconv.ParseInt(ctimeSec, 10, 64)
159         if err != nil {
160                 return nil, err
161         }
162         inode.CtimeNsec, err = strconv.ParseInt(ctimeNsec, 10, 64)
163         if err != nil {
164                 return nil, err
165         }
166         if mtimeSec != "" {
167                 if mtimeNsec == "" {
168                         return nil, errors.New("MtimeNsec is missing")
169                 }
170                 inode.MtimeSec, err = strconv.ParseInt(mtimeSec, 10, 64)
171                 if err != nil {
172                         return nil, err
173                 }
174                 inode.MtimeNsec, err = strconv.ParseInt(mtimeNsec, 10, 64)
175                 if err != nil {
176                         return nil, err
177                 }
178         }
179         return &inode, nil
180 }