]> git.r.bdr.sh - rbdr/mobius/blobdiff - hotline/file_name_with_info.go
Fix io.Reader implementation for FileNameWithInfo
[rbdr/mobius] / hotline / file_name_with_info.go
index ac64c74567493646f60357ca4b7e75616c488413..3324a6252e837be1b2619bfe24c3c036efd36462 100644 (file)
@@ -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
 }