X-Git-Url: https://git.r.bdr.sh/rbdr/mobius/blobdiff_plain/b129b7cbc9fd9a9c11a77e5922861ef08893efa1..3e20402491f013710dfa1c2f3d0cd6ab0e212585:/hotline/file_name_with_info.go diff --git a/hotline/file_name_with_info.go b/hotline/file_name_with_info.go index ac64c74..3324a62 100644 --- a/hotline/file_name_with_info.go +++ b/hotline/file_name_with_info.go @@ -9,7 +9,9 @@ import ( type FileNameWithInfo struct { fileNameWithInfoHeader - name []byte // File name + Name []byte // File Name + + readOffset int // Internal offset to track read progress } // fileNameWithInfoHeader contains the fixed length fields of FileNameWithInfo @@ -19,7 +21,7 @@ type fileNameWithInfoHeader struct { FileSize [4]byte // File Size in bytes RSVD [4]byte NameScript [2]byte // ?? - NameSize [2]byte // Length of name field + NameSize [2]byte // Length of Name field } func (f *fileNameWithInfoHeader) nameLen() int { @@ -27,18 +29,25 @@ func (f *fileNameWithInfoHeader) nameLen() int { } // 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) { @@ -47,7 +56,7 @@ func (f *FileNameWithInfo) Write(p []byte) (int, error) { return 0, err } headerLen := binary.Size(f.fileNameWithInfoHeader) - f.name = p[headerLen : headerLen+f.nameLen()] + f.Name = p[headerLen : headerLen+f.nameLen()] return len(p), nil }