| 1 | package send |
| 2 | |
| 3 | import ( |
| 4 | "encoding/binary" |
| 5 | "io" |
| 6 | "strconv" |
| 7 | ) |
| 8 | |
| 9 | var sendEndianess = binary.LittleEndian |
| 10 | |
| 11 | const ( |
| 12 | sendStreamMagic = "btrfs-stream\x00" |
| 13 | sendStreamMagicSize = len(sendStreamMagic) |
| 14 | sendStreamVersion = 1 |
| 15 | ) |
| 16 | |
| 17 | const ( |
| 18 | sendBufSize = 64 * 1024 |
| 19 | sendReadSize = 48 * 1024 |
| 20 | ) |
| 21 | |
| 22 | const cmdHeaderSize = 10 |
| 23 | |
| 24 | type cmdHeader struct { |
| 25 | Len uint32 // len excluding the header |
| 26 | Cmd CmdType |
| 27 | Crc uint32 // crc including the header with zero crc field |
| 28 | } |
| 29 | |
| 30 | func (h *cmdHeader) Size() int { return cmdHeaderSize } |
| 31 | func (h *cmdHeader) Unmarshal(p []byte) error { |
| 32 | if len(p) < cmdHeaderSize { |
| 33 | return io.ErrUnexpectedEOF |
| 34 | } |
| 35 | h.Len = sendEndianess.Uint32(p[0:]) |
| 36 | h.Cmd = CmdType(sendEndianess.Uint16(p[4:])) |
| 37 | h.Crc = sendEndianess.Uint32(p[6:]) |
| 38 | return nil |
| 39 | } |
| 40 | |
| 41 | const tlvHeaderSize = 4 |
| 42 | |
| 43 | type tlvHeader struct { |
| 44 | Type uint16 |
| 45 | Len uint16 // len excluding the header |
| 46 | } |
| 47 | |
| 48 | func (h *tlvHeader) Size() int { return tlvHeaderSize } |
| 49 | func (h *tlvHeader) Unmarshal(p []byte) error { |
| 50 | if len(p) < tlvHeaderSize { |
| 51 | return io.ErrUnexpectedEOF |
| 52 | } |
| 53 | h.Type = sendEndianess.Uint16(p[0:]) |
| 54 | h.Len = sendEndianess.Uint16(p[2:]) |
| 55 | return nil |
| 56 | } |
| 57 | |
| 58 | type CmdType uint16 |
| 59 | |
| 60 | func (c CmdType) String() string { |
| 61 | var name string |
| 62 | if int(c) < len(cmdTypeNames) { |
| 63 | name = cmdTypeNames[int(c)] |
| 64 | } |
| 65 | if name != "" { |
| 66 | return name |
| 67 | } |
| 68 | return strconv.FormatInt(int64(c), 16) |
| 69 | } |
| 70 | |
| 71 | var cmdTypeNames = []string{ |
| 72 | "<zero>", |
| 73 | |
| 74 | "subvol", |
| 75 | "snapshot", |
| 76 | |
| 77 | "mkfile", |
| 78 | "mkdir", |
| 79 | "mknod", |
| 80 | "mkfifo", |
| 81 | "mksock", |
| 82 | "symlink", |
| 83 | |
| 84 | "rename", |
| 85 | "link", |
| 86 | "unlink", |
| 87 | "rmdir", |
| 88 | |
| 89 | "set_xattr", |
| 90 | "remove_xattr", |
| 91 | |
| 92 | "write", |
| 93 | "clone", |
| 94 | |
| 95 | "truncate", |
| 96 | "chmod", |
| 97 | "chown", |
| 98 | "utimes", |
| 99 | |
| 100 | "end", |
| 101 | "update_extent", |
| 102 | "<max>", |
| 103 | } |
| 104 | |
| 105 | const ( |
| 106 | sendCmdUnspec = CmdType(iota) |
| 107 | |
| 108 | sendCmdSubvol |
| 109 | sendCmdSnapshot |
| 110 | |
| 111 | sendCmdMkfile |
| 112 | sendCmdMkdir |
| 113 | sendCmdMknod |
| 114 | sendCmdMkfifo |
| 115 | sendCmdMksock |
| 116 | sendCmdSymlink |
| 117 | |
| 118 | sendCmdRename |
| 119 | sendCmdLink |
| 120 | sendCmdUnlink |
| 121 | sendCmdRmdir |
| 122 | |
| 123 | sendCmdSetXattr |
| 124 | sendCmdRemoveXattr |
| 125 | |
| 126 | sendCmdWrite |
| 127 | sendCmdClone |
| 128 | |
| 129 | sendCmdTruncate |
| 130 | sendCmdChmod |
| 131 | sendCmdChown |
| 132 | sendCmdUtimes |
| 133 | |
| 134 | sendCmdEnd |
| 135 | sendCmdUpdateExtent |
| 136 | _sendCmdMax |
| 137 | ) |
| 138 | |
| 139 | const sendCmdMax = _sendCmdMax - 1 |
| 140 | |
| 141 | type sendCmdAttr uint16 |
| 142 | |
| 143 | func (c sendCmdAttr) String() string { |
| 144 | var name string |
| 145 | if int(c) < len(sendAttrNames) { |
| 146 | name = sendAttrNames[int(c)] |
| 147 | } |
| 148 | if name != "" { |
| 149 | return name |
| 150 | } |
| 151 | return strconv.FormatInt(int64(c), 16) |
| 152 | } |
| 153 | |
| 154 | const ( |
| 155 | sendAttrUnspec = sendCmdAttr(iota) |
| 156 | |
| 157 | sendAttrUuid |
| 158 | sendAttrCtransid |
| 159 | |
| 160 | sendAttrIno |
| 161 | sendAttrSize |
| 162 | sendAttrMode |
| 163 | sendAttrUid |
| 164 | sendAttrGid |
| 165 | sendAttrRdev |
| 166 | sendAttrCtime |
| 167 | sendAttrMtime |
| 168 | sendAttrAtime |
| 169 | sendAttrOtime |
| 170 | |
| 171 | sendAttrXattrName |
| 172 | sendAttrXattrData |
| 173 | |
| 174 | sendAttrPath |
| 175 | sendAttrPathTo |
| 176 | sendAttrPathLink |
| 177 | |
| 178 | sendAttrFileOffset |
| 179 | sendAttrData |
| 180 | |
| 181 | sendAttrCloneUuid |
| 182 | sendAttrCloneCtransid |
| 183 | sendAttrClonePath |
| 184 | sendAttrCloneOffset |
| 185 | sendAttrCloneLen |
| 186 | |
| 187 | _sendAttrMax |
| 188 | ) |
| 189 | const sendAttrMax = _sendAttrMax - 1 |
| 190 | |
| 191 | var sendAttrNames = []string{ |
| 192 | "<zero>", |
| 193 | |
| 194 | "uuid", |
| 195 | "ctransid", |
| 196 | |
| 197 | "ino", |
| 198 | "size", |
| 199 | "mode", |
| 200 | "uid", |
| 201 | "gid", |
| 202 | "rdev", |
| 203 | "ctime", |
| 204 | "mtime", |
| 205 | "atime", |
| 206 | "otime", |
| 207 | |
| 208 | "xattrname", |
| 209 | "xattrdata", |
| 210 | |
| 211 | "path", |
| 212 | "pathto", |
| 213 | "pathlink", |
| 214 | |
| 215 | "fileoffset", |
| 216 | "data", |
| 217 | |
| 218 | "cloneuuid", |
| 219 | "clonectransid", |
| 220 | "clonepath", |
| 221 | "cloneoffset", |
| 222 | "clonelen", |
| 223 | |
| 224 | "<max>", |
| 225 | } |