]> Cypherpunks.ru repositories - goredo.git/blob - inode.go
Binary format and many optimisations
[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         "encoding/binary"
24         "os"
25         "strconv"
26
27         "go.cypherpunks.ru/recfile"
28         "golang.org/x/sys/unix"
29 )
30
31 const InodeLen = 6 * 8
32
33 type InodeTrustType int
34
35 //go:generate stringer -type=InodeTrustType
36 const (
37         EnvInodeTrust = "REDO_INODE_TRUST"
38
39         InodeTrustNone InodeTrustType = iota
40         InodeTrustCtime
41         InodeTrustMtime
42 )
43
44 var InodeTrust InodeTrustType
45
46 // It is big-endian 64-bit unsigned integers: size, inodeNum,
47 // ctime sec, ctime nsec, mtime sec, mtime nsec.
48 type Inode string
49
50 func (our Inode) Equals(their Inode) bool {
51         if our[:2*8] != their[:2*8] {
52                 return false
53         }
54         switch InodeTrust {
55         case InodeTrustCtime:
56                 if our[2*8:4*8] != their[2*8:4*8] {
57                         return false
58                 }
59         case InodeTrustMtime:
60                 if our[4*8:6*8] != their[4*8:6*8] {
61                         return false
62                 }
63         }
64         return true
65 }
66
67 func (inode Inode) RecfileFields() []recfile.Field {
68         return []recfile.Field{
69                 {Name: "Size", Value: strconv.FormatUint(binary.BigEndian.Uint64(
70                         []byte(inode[0*8:1*8])), 10)},
71                 {Name: "InodeNum", Value: strconv.FormatUint(binary.BigEndian.Uint64(
72                         []byte(inode[1*8:2*8])), 10)},
73                 {Name: "CtimeSec", Value: strconv.FormatUint(binary.BigEndian.Uint64(
74                         []byte(inode[2*8:3*8])), 10)},
75                 {Name: "CtimeNsec", Value: strconv.FormatUint(binary.BigEndian.Uint64(
76                         []byte(inode[3*8:4*8])), 10)},
77                 {Name: "MtimeSec", Value: strconv.FormatUint(binary.BigEndian.Uint64(
78                         []byte(inode[4*8:5*8])), 10)},
79                 {Name: "MtimeNsec", Value: strconv.FormatUint(binary.BigEndian.Uint64(
80                         []byte(inode[5*8:6*8])), 10)},
81         }
82 }
83
84 func inodeFromFileStat(fi os.FileInfo, stat unix.Stat_t) Inode {
85         ctimeSec, ctimeNsec := stat.Ctim.Unix()
86         mtimeSec := fi.ModTime().Unix()
87         mtimeNsec := fi.ModTime().UnixNano()
88         buf := make([]byte, InodeLen)
89         binary.BigEndian.PutUint64(buf[0*8:1*8], uint64(fi.Size()))
90         binary.BigEndian.PutUint64(buf[1*8:2*8], uint64(stat.Ino))
91         binary.BigEndian.PutUint64(buf[2*8:3*8], uint64(ctimeSec))
92         binary.BigEndian.PutUint64(buf[3*8:4*8], uint64(ctimeNsec))
93         binary.BigEndian.PutUint64(buf[4*8:5*8], uint64(mtimeSec))
94         binary.BigEndian.PutUint64(buf[5*8:6*8], uint64(mtimeNsec))
95         return Inode(buf)
96 }
97
98 func inodeFromFileByFd(fd *os.File) (inode Inode, isDir bool, err error) {
99         fi, err := fd.Stat()
100         if err != nil {
101                 return
102         }
103         if fi.IsDir() {
104                 isDir = true
105                 return
106         }
107         var stat unix.Stat_t
108         err = unix.Fstat(int(fd.Fd()), &stat)
109         if err != nil {
110                 return
111         }
112         inode = inodeFromFileStat(fi, stat)
113         return
114 }
115
116 func inodeFromFileByPath(p string) (Inode, error) {
117         fi, err := os.Stat(p)
118         if err != nil {
119                 return "", err
120         }
121         var stat unix.Stat_t
122         err = unix.Stat(p, &stat)
123         if err != nil {
124                 return "", err
125         }
126         return inodeFromFileStat(fi, stat), nil
127 }