btrfs/send/send_h.go

226 lines
3.1 KiB
Go
Raw Normal View History

2017-11-27 18:43:55 +00:00
package send
2016-09-21 12:31:08 +00:00
import (
"encoding/binary"
"io"
2017-11-13 12:31:53 +00:00
"strconv"
2016-09-21 12:31:08 +00:00
)
2017-11-13 12:31:53 +00:00
var sendEndianess = binary.LittleEndian
2016-09-21 12:31:08 +00:00
const (
2017-11-13 12:31:53 +00:00
sendStreamMagic = "btrfs-stream\x00"
sendStreamMagicSize = len(sendStreamMagic)
sendStreamVersion = 1
2016-09-21 12:31:08 +00:00
)
const (
2017-11-13 12:31:53 +00:00
sendBufSize = 64 * 1024
sendReadSize = 48 * 1024
2016-09-21 12:31:08 +00:00
)
2017-11-13 12:31:53 +00:00
const cmdHeaderSize = 10
2016-09-21 12:31:08 +00:00
type cmdHeader struct {
Len uint32 // len excluding the header
2017-11-27 18:43:55 +00:00
Cmd CmdType
2016-09-21 12:31:08 +00:00
Crc uint32 // crc including the header with zero crc field
}
2017-11-13 12:31:53 +00:00
func (h *cmdHeader) Size() int { return cmdHeaderSize }
2016-09-21 12:31:08 +00:00
func (h *cmdHeader) Unmarshal(p []byte) error {
2017-11-13 12:31:53 +00:00
if len(p) < cmdHeaderSize {
2016-09-21 12:31:08 +00:00
return io.ErrUnexpectedEOF
}
2017-11-13 12:31:53 +00:00
h.Len = sendEndianess.Uint32(p[0:])
2017-11-27 18:43:55 +00:00
h.Cmd = CmdType(sendEndianess.Uint16(p[4:]))
2017-11-13 12:31:53 +00:00
h.Crc = sendEndianess.Uint32(p[6:])
2016-09-21 12:31:08 +00:00
return nil
}
2017-11-13 12:31:53 +00:00
const tlvHeaderSize = 4
2016-09-21 12:31:08 +00:00
type tlvHeader struct {
2017-11-13 12:31:53 +00:00
Type uint16
2016-09-21 12:31:08 +00:00
Len uint16 // len excluding the header
}
2017-11-13 12:31:53 +00:00
func (h *tlvHeader) Size() int { return tlvHeaderSize }
2016-09-21 12:31:08 +00:00
func (h *tlvHeader) Unmarshal(p []byte) error {
2017-11-13 12:31:53 +00:00
if len(p) < tlvHeaderSize {
2016-09-21 12:31:08 +00:00
return io.ErrUnexpectedEOF
}
2017-11-13 12:31:53 +00:00
h.Type = sendEndianess.Uint16(p[0:])
h.Len = sendEndianess.Uint16(p[2:])
2016-09-21 12:31:08 +00:00
return nil
}
2017-11-27 18:43:55 +00:00
type CmdType uint16
2016-09-21 12:31:08 +00:00
2017-11-27 18:43:55 +00:00
func (c CmdType) String() string {
2017-11-13 12:31:53 +00:00
var name string
2017-11-27 18:43:55 +00:00
if int(c) < len(cmdTypeNames) {
name = cmdTypeNames[int(c)]
2017-11-13 12:31:53 +00:00
}
if name != "" {
return name
}
return strconv.FormatInt(int64(c), 16)
}
2017-11-27 18:43:55 +00:00
var cmdTypeNames = []string{
2017-11-13 12:31:53 +00:00
"<zero>",
"subvol",
"snapshot",
"mkfile",
"mkdir",
"mknod",
"mkfifo",
"mksock",
"symlink",
"rename",
"link",
"unlink",
"rmdir",
"set_xattr",
"remove_xattr",
"write",
"clone",
"truncate",
"chmod",
"chown",
"utimes",
"end",
"update_extent",
"<max>",
}
2016-09-21 12:31:08 +00:00
const (
2017-11-27 18:43:55 +00:00
sendCmdUnspec = CmdType(iota)
2016-09-21 12:31:08 +00:00
2017-11-13 12:31:53 +00:00
sendCmdSubvol
sendCmdSnapshot
2016-09-21 12:31:08 +00:00
2017-11-13 12:31:53 +00:00
sendCmdMkfile
sendCmdMkdir
sendCmdMknod
sendCmdMkfifo
sendCmdMksock
sendCmdSymlink
2016-09-21 12:31:08 +00:00
2017-11-13 12:31:53 +00:00
sendCmdRename
sendCmdLink
sendCmdUnlink
sendCmdRmdir
2016-09-21 12:31:08 +00:00
2017-11-13 12:31:53 +00:00
sendCmdSetXattr
sendCmdRemoveXattr
2016-09-21 12:31:08 +00:00
2017-11-13 12:31:53 +00:00
sendCmdWrite
sendCmdClone
2016-09-21 12:31:08 +00:00
2017-11-13 12:31:53 +00:00
sendCmdTruncate
sendCmdChmod
sendCmdChown
sendCmdUtimes
2016-09-21 12:31:08 +00:00
2017-11-13 12:31:53 +00:00
sendCmdEnd
sendCmdUpdateExtent
_sendCmdMax
2016-09-21 12:31:08 +00:00
)
2017-11-13 12:31:53 +00:00
const sendCmdMax = _sendCmdMax - 1
2016-09-21 12:31:08 +00:00
type sendCmdAttr uint16
2017-11-13 12:31:53 +00:00
func (c sendCmdAttr) String() string {
var name string
if int(c) < len(sendAttrNames) {
name = sendAttrNames[int(c)]
}
if name != "" {
return name
}
return strconv.FormatInt(int64(c), 16)
}
2016-09-21 12:31:08 +00:00
const (
2017-11-13 12:31:53 +00:00
sendAttrUnspec = sendCmdAttr(iota)
sendAttrUuid
sendAttrCtransid
sendAttrIno
sendAttrSize
sendAttrMode
sendAttrUid
sendAttrGid
sendAttrRdev
sendAttrCtime
sendAttrMtime
sendAttrAtime
sendAttrOtime
sendAttrXattrName
sendAttrXattrData
sendAttrPath
sendAttrPathTo
sendAttrPathLink
sendAttrFileOffset
sendAttrData
sendAttrCloneUuid
sendAttrCloneCtransid
sendAttrClonePath
sendAttrCloneOffset
sendAttrCloneLen
_sendAttrMax
2016-09-21 12:31:08 +00:00
)
2017-11-13 12:31:53 +00:00
const sendAttrMax = _sendAttrMax - 1
var sendAttrNames = []string{
"<zero>",
"uuid",
"ctransid",
"ino",
"size",
"mode",
"uid",
"gid",
"rdev",
"ctime",
"mtime",
"atime",
"otime",
"xattrname",
"xattrdata",
"path",
"pathto",
"pathlink",
"fileoffset",
"data",
"cloneuuid",
"clonectransid",
"clonepath",
"cloneoffset",
"clonelen",
"<max>",
}