]> git.r.bdr.sh - rbdr/mobius/blame - hotline/file_header.go
Fix io.Reader implementation for FileNameWithInfo
[rbdr/mobius] / hotline / file_header.go
CommitLineData
6988a057
JH
1package hotline
2
3import (
4 "encoding/binary"
9cf66aea 5 "io"
9c44621e 6 "slices"
6988a057
JH
7)
8
9type FileHeader struct {
9cf66aea
JH
10 Size [2]byte // Total size of FileHeader payload
11 Type [2]byte // 0 for file, 1 for dir
12 FilePath []byte // encoded file path
6988a057
JH
13}
14
15func NewFileHeader(fileName string, isDir bool) FileHeader {
16 fh := FileHeader{
9cf66aea 17 Type: [2]byte{0x00, 0x00},
6988a057
JH
18 FilePath: EncodeFilePath(fileName),
19 }
20 if isDir {
9cf66aea 21 fh.Type = [2]byte{0x00, 0x01}
6988a057
JH
22 }
23
24 encodedPathLen := uint16(len(fh.FilePath) + len(fh.Type))
9cf66aea 25 binary.BigEndian.PutUint16(fh.Size[:], encodedPathLen)
6988a057
JH
26
27 return fh
28}
29
9cf66aea
JH
30func (fh *FileHeader) Read(p []byte) (int, error) {
31 return copy(p, slices.Concat(
32 fh.Size[:],
33 fh.Type[:],
6988a057 34 fh.FilePath,
9cf66aea
JH
35 ),
36 ), io.EOF
6988a057 37}