X-Git-Url: http://www.git.cypherpunks.ru/?a=blobdiff_plain;f=inode.go;h=27cfaa72078a0b3c9c67c5cd0cd31265a2c9b110;hb=HEAD;hp=50db586cb4cc6d1eaec726eafa231f56d7daaf34;hpb=b69d2ab11a495413ecc16a9bfea0d105b030c606;p=goredo.git diff --git a/inode.go b/inode.go index 50db586..27cfaa7 100644 --- a/inode.go +++ b/inode.go @@ -1,26 +1,25 @@ -/* -goredo -- djb's redo implementation on pure Go -Copyright (C) 2020-2023 Sergey Matveev - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, version 3 of the License. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see . -*/ +// goredo -- djb's redo implementation on pure Go +// Copyright (C) 2020-2024 Sergey Matveev +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, version 3 of the License. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . // Inode metainformation package main import ( - "errors" + "bytes" + "encoding/binary" "os" "strconv" @@ -28,6 +27,8 @@ import ( "golang.org/x/sys/unix" ) +const InodeLen = 6 * 8 + type InodeTrustType int //go:generate stringer -type=InodeTrustType @@ -41,32 +42,21 @@ const ( var InodeTrust InodeTrustType -type Inode struct { - Size int64 - InodeNum uint64 - CtimeSec int64 - CtimeNsec int64 - MtimeSec int64 - MtimeNsec int64 -} +// It is big-endian 64-bit unsigned integers: size, inodeNum, +// ctime sec, ctime nsec, mtime sec, mtime nsec. +type Inode [InodeLen]byte func (our *Inode) Equals(their *Inode) bool { - if our.Size != their.Size { - return false - } - if our.InodeNum != their.InodeNum { + if !bytes.Equal(our[:2*8], their[:2*8]) { return false } switch InodeTrust { case InodeTrustCtime: - if our.CtimeSec != their.CtimeSec || our.CtimeNsec != their.CtimeNsec { + if !bytes.Equal(our[2*8:4*8], their[2*8:4*8]) { return false } case InodeTrustMtime: - if our.MtimeSec == 0 || our.MtimeNsec == 0 { - return false - } - if our.MtimeSec != their.MtimeSec || our.MtimeNsec != their.MtimeNsec { + if !bytes.Equal(our[4*8:6*8], their[4*8:6*8]) { return false } } @@ -75,12 +65,18 @@ func (our *Inode) Equals(their *Inode) bool { func (inode *Inode) RecfileFields() []recfile.Field { return []recfile.Field{ - {Name: "Size", Value: strconv.FormatInt(inode.Size, 10)}, - {Name: "InodeNum", Value: strconv.FormatUint(inode.InodeNum, 10)}, - {Name: "CtimeSec", Value: strconv.FormatInt(inode.CtimeSec, 10)}, - {Name: "CtimeNsec", Value: strconv.FormatInt(inode.CtimeNsec, 10)}, - {Name: "MtimeSec", Value: strconv.FormatInt(inode.MtimeSec, 10)}, - {Name: "MtimeNsec", Value: strconv.FormatInt(inode.MtimeNsec, 10)}, + {Name: "Size", Value: strconv.FormatUint(binary.BigEndian.Uint64( + []byte(inode[0*8:1*8])), 10)}, + {Name: "InodeNum", Value: strconv.FormatUint(binary.BigEndian.Uint64( + []byte(inode[1*8:2*8])), 10)}, + {Name: "CtimeSec", Value: strconv.FormatUint(binary.BigEndian.Uint64( + []byte(inode[2*8:3*8])), 10)}, + {Name: "CtimeNsec", Value: strconv.FormatUint(binary.BigEndian.Uint64( + []byte(inode[3*8:4*8])), 10)}, + {Name: "MtimeSec", Value: strconv.FormatUint(binary.BigEndian.Uint64( + []byte(inode[4*8:5*8])), 10)}, + {Name: "MtimeNsec", Value: strconv.FormatUint(binary.BigEndian.Uint64( + []byte(inode[5*8:6*8])), 10)}, } } @@ -88,25 +84,32 @@ func inodeFromFileStat(fi os.FileInfo, stat unix.Stat_t) *Inode { ctimeSec, ctimeNsec := stat.Ctim.Unix() mtimeSec := fi.ModTime().Unix() mtimeNsec := fi.ModTime().UnixNano() - return &Inode{ - Size: fi.Size(), - InodeNum: uint64(stat.Ino), - CtimeSec: ctimeSec, CtimeNsec: ctimeNsec, - MtimeSec: mtimeSec, MtimeNsec: mtimeNsec, - } + inode := new(Inode) + binary.BigEndian.PutUint64(inode[0*8:1*8], uint64(fi.Size())) + binary.BigEndian.PutUint64(inode[1*8:2*8], uint64(stat.Ino)) + binary.BigEndian.PutUint64(inode[2*8:3*8], uint64(ctimeSec)) + binary.BigEndian.PutUint64(inode[3*8:4*8], uint64(ctimeNsec)) + binary.BigEndian.PutUint64(inode[4*8:5*8], uint64(mtimeSec)) + binary.BigEndian.PutUint64(inode[5*8:6*8], uint64(mtimeNsec)) + return inode } -func inodeFromFileByFd(fd *os.File) (*Inode, error) { +func inodeFromFileByFd(fd *os.File) (inode *Inode, isDir bool, err error) { fi, err := fd.Stat() if err != nil { - return nil, err + return + } + if fi.IsDir() { + isDir = true + return } var stat unix.Stat_t err = unix.Fstat(int(fd.Fd()), &stat) if err != nil { - return nil, err + return } - return inodeFromFileStat(fi, stat), nil + inode = inodeFromFileStat(fi, stat) + return } func inodeFromFileByPath(p string) (*Inode, error) { @@ -121,55 +124,3 @@ func inodeFromFileByPath(p string) (*Inode, error) { } return inodeFromFileStat(fi, stat), nil } - -func inodeFromRec(m map[string]string) (*Inode, error) { - size := m["Size"] - inodeNum := m["InodeNum"] - ctimeSec := m["CtimeSec"] - ctimeNsec := m["CtimeNsec"] - mtimeSec := m["MtimeSec"] - mtimeNsec := m["MtimeNsec"] - if size == "" { - return nil, errors.New("Size is missing") - } - if ctimeSec == "" { - return nil, errors.New("CtimeSec is missing") - } - if ctimeNsec == "" { - return nil, errors.New("CtimeNsec is missing") - } - inode := Inode{} - var err error - inode.Size, err = strconv.ParseInt(size, 10, 64) - if err != nil { - return nil, err - } - if inodeNum != "" { - inode.InodeNum, err = strconv.ParseUint(inodeNum, 10, 64) - if err != nil { - return nil, err - } - } - inode.CtimeSec, err = strconv.ParseInt(ctimeSec, 10, 64) - if err != nil { - return nil, err - } - inode.CtimeNsec, err = strconv.ParseInt(ctimeNsec, 10, 64) - if err != nil { - return nil, err - } - if mtimeSec != "" { - if mtimeNsec == "" { - return nil, errors.New("MtimeNsec is missing") - } - inode.MtimeSec, err = strconv.ParseInt(mtimeSec, 10, 64) - if err != nil { - return nil, err - } - inode.MtimeNsec, err = strconv.ParseInt(mtimeNsec, 10, 64) - if err != nil { - return nil, err - } - } - return &inode, nil -}