X-Git-Url: https://git.r.bdr.sh/rbdr/mobius/blobdiff_plain/95159e5585762c06c654945070ba54262b7dcec9..9f89cd9fbbcec9f7f42c87ee0adc21427aab9f1c:/hotline/file_name_with_info.go?ds=inline diff --git a/hotline/file_name_with_info.go b/hotline/file_name_with_info.go index 4d59dd0..3a4a795 100644 --- a/hotline/file_name_with_info.go +++ b/hotline/file_name_with_info.go @@ -8,12 +8,14 @@ import ( ) type FileNameWithInfo struct { - fileNameWithInfoHeader + FileNameWithInfoHeader Name []byte // File Name + + readOffset int // Internal offset to track read progress } -// fileNameWithInfoHeader contains the fixed length fields of FileNameWithInfo -type fileNameWithInfoHeader struct { +// FileNameWithInfoHeader contains the fixed length fields of FileNameWithInfo +type FileNameWithInfoHeader struct { Type [4]byte // File type code Creator [4]byte // File creator code FileSize [4]byte // File Size in bytes @@ -22,31 +24,38 @@ type fileNameWithInfoHeader struct { NameSize [2]byte // Length of Name field } -func (f *fileNameWithInfoHeader) nameLen() int { +func (f *FileNameWithInfoHeader) nameLen() int { return int(binary.BigEndian.Uint16(f.NameSize[:])) } // Read implements io.Reader for FileNameWithInfo -func (f *FileNameWithInfo) Read(b []byte) (int, error) { - return copy(b, - slices.Concat( - f.Type[:], - f.Creator[:], - f.FileSize[:], - f.RSVD[:], - f.NameScript[:], - f.NameSize[:], - f.Name, - ), - ), io.EOF +func (f *FileNameWithInfo) Read(p []byte) (int, error) { + buf := slices.Concat( + f.Type[:], + f.Creator[:], + f.FileSize[:], + f.RSVD[:], + f.NameScript[:], + f.NameSize[:], + f.Name, + ) + + if f.readOffset >= len(buf) { + return 0, io.EOF // All bytes have been read + } + + n := copy(p, buf[f.readOffset:]) + f.readOffset += n + + return n, nil } func (f *FileNameWithInfo) Write(p []byte) (int, error) { - err := binary.Read(bytes.NewReader(p), binary.BigEndian, &f.fileNameWithInfoHeader) + err := binary.Read(bytes.NewReader(p), binary.BigEndian, &f.FileNameWithInfoHeader) if err != nil { return 0, err } - headerLen := binary.Size(f.fileNameWithInfoHeader) + headerLen := binary.Size(f.FileNameWithInfoHeader) f.Name = p[headerLen : headerLen+f.nameLen()] return len(p), nil