]>
Commit | Line | Data |
---|---|---|
1 | package hotline | |
2 | ||
3 | import ( | |
4 | "encoding/binary" | |
5 | "io" | |
6 | "slices" | |
7 | ) | |
8 | ||
9 | type FileHeader struct { | |
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 | |
13 | } | |
14 | ||
15 | func NewFileHeader(fileName string, isDir bool) FileHeader { | |
16 | fh := FileHeader{ | |
17 | Type: [2]byte{0x00, 0x00}, | |
18 | FilePath: EncodeFilePath(fileName), | |
19 | } | |
20 | if isDir { | |
21 | fh.Type = [2]byte{0x00, 0x01} | |
22 | } | |
23 | ||
24 | encodedPathLen := uint16(len(fh.FilePath) + len(fh.Type)) | |
25 | binary.BigEndian.PutUint16(fh.Size[:], encodedPathLen) | |
26 | ||
27 | return fh | |
28 | } | |
29 | ||
30 | func (fh *FileHeader) Read(p []byte) (int, error) { | |
31 | return copy(p, slices.Concat( | |
32 | fh.Size[:], | |
33 | fh.Type[:], | |
34 | fh.FilePath, | |
35 | ), | |
36 | ), io.EOF | |
37 | } |