]> Cypherpunks.ru repositories - goredo.git/blob - inode.go
Download link for 2.6.2 release
[goredo.git] / inode.go
1 // goredo -- djb's redo implementation on pure Go
2 // Copyright (C) 2020-2024 Sergey Matveev <stargrave@stargrave.org>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, version 3 of the License.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 // Inode metainformation
17
18 package main
19
20 import (
21         "bytes"
22         "encoding/binary"
23         "os"
24         "strconv"
25
26         "go.cypherpunks.ru/recfile"
27         "golang.org/x/sys/unix"
28 )
29
30 const InodeLen = 6 * 8
31
32 type InodeTrustType int
33
34 //go:generate stringer -type=InodeTrustType
35 const (
36         EnvInodeTrust = "REDO_INODE_TRUST"
37
38         InodeTrustNone InodeTrustType = iota
39         InodeTrustCtime
40         InodeTrustMtime
41 )
42
43 var InodeTrust InodeTrustType
44
45 // It is big-endian 64-bit unsigned integers: size, inodeNum,
46 // ctime sec, ctime nsec, mtime sec, mtime nsec.
47 type Inode [InodeLen]byte
48
49 func (our *Inode) Equals(their *Inode) bool {
50         if !bytes.Equal(our[:2*8], their[:2*8]) {
51                 return false
52         }
53         switch InodeTrust {
54         case InodeTrustCtime:
55                 if !bytes.Equal(our[2*8:4*8], their[2*8:4*8]) {
56                         return false
57                 }
58         case InodeTrustMtime:
59                 if !bytes.Equal(our[4*8:6*8], their[4*8:6*8]) {
60                         return false
61                 }
62         }
63         return true
64 }
65
66 func (inode *Inode) RecfileFields() []recfile.Field {
67         return []recfile.Field{
68                 {Name: "Size", Value: strconv.FormatUint(binary.BigEndian.Uint64(
69                         []byte(inode[0*8:1*8])), 10)},
70                 {Name: "InodeNum", Value: strconv.FormatUint(binary.BigEndian.Uint64(
71                         []byte(inode[1*8:2*8])), 10)},
72                 {Name: "CtimeSec", Value: strconv.FormatUint(binary.BigEndian.Uint64(
73                         []byte(inode[2*8:3*8])), 10)},
74                 {Name: "CtimeNsec", Value: strconv.FormatUint(binary.BigEndian.Uint64(
75                         []byte(inode[3*8:4*8])), 10)},
76                 {Name: "MtimeSec", Value: strconv.FormatUint(binary.BigEndian.Uint64(
77                         []byte(inode[4*8:5*8])), 10)},
78                 {Name: "MtimeNsec", Value: strconv.FormatUint(binary.BigEndian.Uint64(
79                         []byte(inode[5*8:6*8])), 10)},
80         }
81 }
82
83 func inodeFromFileStat(fi os.FileInfo, stat unix.Stat_t) *Inode {
84         ctimeSec, ctimeNsec := stat.Ctim.Unix()
85         mtimeSec := fi.ModTime().Unix()
86         mtimeNsec := fi.ModTime().UnixNano()
87         inode := new(Inode)
88         binary.BigEndian.PutUint64(inode[0*8:1*8], uint64(fi.Size()))
89         binary.BigEndian.PutUint64(inode[1*8:2*8], uint64(stat.Ino))
90         binary.BigEndian.PutUint64(inode[2*8:3*8], uint64(ctimeSec))
91         binary.BigEndian.PutUint64(inode[3*8:4*8], uint64(ctimeNsec))
92         binary.BigEndian.PutUint64(inode[4*8:5*8], uint64(mtimeSec))
93         binary.BigEndian.PutUint64(inode[5*8:6*8], uint64(mtimeNsec))
94         return inode
95 }
96
97 func inodeFromFileByFd(fd *os.File) (inode *Inode, isDir bool, err error) {
98         fi, err := fd.Stat()
99         if err != nil {
100                 return
101         }
102         if fi.IsDir() {
103                 isDir = true
104                 return
105         }
106         var stat unix.Stat_t
107         err = unix.Fstat(int(fd.Fd()), &stat)
108         if err != nil {
109                 return
110         }
111         inode = inodeFromFileStat(fi, stat)
112         return
113 }
114
115 func inodeFromFileByPath(p string) (*Inode, error) {
116         fi, err := os.Stat(p)
117         if err != nil {
118                 return nil, err
119         }
120         var stat unix.Stat_t
121         err = unix.Stat(p, &stat)
122         if err != nil {
123                 return nil, err
124         }
125         return inodeFromFileStat(fi, stat), nil
126 }