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