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