]> Cypherpunks.ru repositories - goredo.git/blob - inode.go
More general Inode information tracking, explicit size check
[goredo.git] / inode.go
1 /*
2 goredo -- 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 Inode struct {
32         Size      int64
33         CtimeSec  int64
34         CtimeNsec int64
35 }
36
37 func (our *Inode) Equals(their *Inode) bool {
38         return (our.Size == their.Size) &&
39                 (our.CtimeSec == their.CtimeSec) &&
40                 (our.CtimeNsec == their.CtimeNsec)
41 }
42
43 func (inode *Inode) RecfileFields() []recfile.Field {
44         return []recfile.Field{
45                 {Name: "Size", Value: strconv.FormatInt(inode.Size, 10)},
46                 {Name: "CtimeSec", Value: strconv.FormatInt(inode.CtimeSec, 10)},
47                 {Name: "CtimeNsec", Value: strconv.FormatInt(inode.CtimeNsec, 10)},
48         }
49 }
50
51 func inodeFromFile(fd *os.File) (*Inode, error) {
52         var fi os.FileInfo
53         fi, err := fd.Stat()
54         if err != nil {
55                 return nil, err
56         }
57         var stat unix.Stat_t
58         err = unix.Fstat(int(fd.Fd()), &stat)
59         if err != nil {
60                 return nil, err
61         }
62         sec, nsec := stat.Ctim.Unix()
63         return &Inode{Size: fi.Size(), CtimeSec: sec, CtimeNsec: nsec}, nil
64 }
65
66 func inodeFromRec(m map[string]string) (*Inode, error) {
67         size := m["Size"]
68         ctimeSec := m["CtimeSec"]
69         ctimeNsec := m["CtimeNsec"]
70         if size == "" {
71                 return nil, errors.New("Size is missing")
72         }
73         if ctimeSec == "" {
74                 return nil, errors.New("CtimeSec is missing")
75         }
76         if ctimeNsec == "" {
77                 return nil, errors.New("CtimeNsec is missing")
78         }
79         inode := Inode{}
80         var err error
81         inode.Size, err = strconv.ParseInt(size, 10, 64)
82         if err != nil {
83                 return nil, err
84         }
85         inode.CtimeSec, err = strconv.ParseInt(ctimeSec, 10, 64)
86         if err != nil {
87                 return nil, err
88         }
89         inode.CtimeNsec, err = strconv.ParseInt(ctimeNsec, 10, 64)
90         if err != nil {
91                 return nil, err
92         }
93         return &inode, nil
94 }