]> Cypherpunks.ru repositories - goredo.git/blob - inode.go
Redundant @documentencoding
[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, error) {
100         fi, err := fd.Stat()
101         if err != nil {
102                 return nil, err
103         }
104         var stat unix.Stat_t
105         err = unix.Fstat(int(fd.Fd()), &stat)
106         if err != nil {
107                 return nil, err
108         }
109         return inodeFromFileStat(fi, stat), nil
110 }
111
112 func inodeFromFileByPath(p string) (*Inode, error) {
113         fi, err := os.Stat(p)
114         if err != nil {
115                 return nil, err
116         }
117         var stat unix.Stat_t
118         err = unix.Stat(p, &stat)
119         if err != nil {
120                 return nil, err
121         }
122         return inodeFromFileStat(fi, stat), nil
123 }
124
125 func inodeFromRec(m map[string]string) (*Inode, error) {
126         size := m["Size"]
127         inodeNum := m["InodeNum"]
128         ctimeSec := m["CtimeSec"]
129         ctimeNsec := m["CtimeNsec"]
130         mtimeSec := m["MtimeSec"]
131         mtimeNsec := m["MtimeNsec"]
132         if size == "" {
133                 return nil, errors.New("Size is missing")
134         }
135         if ctimeSec == "" {
136                 return nil, errors.New("CtimeSec is missing")
137         }
138         if ctimeNsec == "" {
139                 return nil, errors.New("CtimeNsec is missing")
140         }
141         inode := Inode{}
142         var err error
143         inode.Size, err = strconv.ParseInt(size, 10, 64)
144         if err != nil {
145                 return nil, err
146         }
147         if inodeNum != "" {
148                 inode.InodeNum, err = strconv.ParseUint(inodeNum, 10, 64)
149                 if err != nil {
150                         return nil, err
151                 }
152         }
153         inode.CtimeSec, err = strconv.ParseInt(ctimeSec, 10, 64)
154         if err != nil {
155                 return nil, err
156         }
157         inode.CtimeNsec, err = strconv.ParseInt(ctimeNsec, 10, 64)
158         if err != nil {
159                 return nil, err
160         }
161         if mtimeSec != "" {
162                 if mtimeNsec == "" {
163                         return nil, errors.New("MtimeNsec is missing")
164                 }
165                 inode.MtimeSec, err = strconv.ParseInt(mtimeSec, 10, 64)
166                 if err != nil {
167                         return nil, err
168                 }
169                 inode.MtimeNsec, err = strconv.ParseInt(mtimeNsec, 10, 64)
170                 if err != nil {
171                         return nil, err
172                 }
173         }
174         return &inode, nil
175 }