diff options
| author | Jeff Halter <868228+jhalter@users.noreply.github.com> | 2025-07-06 16:02:53 -0700 |
|---|---|---|
| committer | Jeff Halter <868228+jhalter@users.noreply.github.com> | 2025-07-06 16:02:53 -0700 |
| commit | e61b3160bc8936b7ffb0af5c89b13742a7081826 (patch) | |
| tree | db3ba0e6cf0add99da6fa806709670d4b39d4bbf /hotline | |
| parent | c0ec79dcfd267f7495274a9186b36a1b0669f000 (diff) | |
Add ForkType type alias for improved type safety
- Replace [4]byte with ForkType in ForkInfoList struct
- Update constants ForkTypeDATA and ForkTypeMACR to use ForkType
- Add String() method to ForkType for better debugging
- Improve consistency with existing type patterns (FieldType, TranType)
- Clean up unused code and improve documentation
Diffstat (limited to 'hotline')
| -rw-r--r-- | hotline/file_resume_data.go | 57 | ||||
| -rw-r--r-- | hotline/file_transfer.go | 27 | ||||
| -rw-r--r-- | hotline/file_wrapper.go | 2 |
3 files changed, 26 insertions, 60 deletions
diff --git a/hotline/file_resume_data.go b/hotline/file_resume_data.go index 5dfd2d8..80f5fe5 100644 --- a/hotline/file_resume_data.go +++ b/hotline/file_resume_data.go @@ -3,38 +3,54 @@ package hotline import ( "bytes" "encoding/binary" + "fmt" ) // FileResumeData is sent when a client or server would like to resume a transfer from an offset type FileResumeData struct { Format [4]byte // "RFLT" Version [2]byte // Always 1 - RSVD [34]byte // Unused + RSVD [34]byte // Present in the Hotline protocol docs, but unused. Left here for documentation purposes. ForkCount [2]byte // Length of ForkInfoList. Either 2 or 3 depending on whether file has a resource fork ForkInfoList []ForkInfoList +} + +// ForkType represents a 4-byte fork type identifier +type ForkType [4]byte - //readOffset int // TODO +// String returns a string representation of the fork type +func (ft ForkType) String() string { + return fmt.Sprintf("%c%c%c%c", ft[0], ft[1], ft[2], ft[3]) } type ForkInfoList struct { - Fork [4]byte // "DATA" or "MACR" - DataSize [4]byte // offset from which to resume the transfer of data - RSVDA [4]byte // Unused - RSVDB [4]byte // Unused + Fork ForkType // "DATA" or "MACR" + DataSize [4]byte // offset from which to resume the transfer of data + RSVDA [4]byte // Present in the Hotline protocol docs, but unused. Left here for documentation purposes. + RSVDB [4]byte // Present in the Hotline protocol docs, but unused. Left here for documentation purposes. } +var ( + ForkTypeDATA = ForkType{0x44, 0x41, 0x54, 0x41} // DATA: Data fork + ForkTypeMACR = ForkType{0x4d, 0x41, 0x43, 0x52} // MACR: Mac resource fork +) + func NewForkInfoList(b []byte) *ForkInfoList { return &ForkInfoList{ - Fork: [4]byte{0x44, 0x41, 0x54, 0x41}, + Fork: ForkTypeDATA, DataSize: [4]byte{b[0], b[1], b[2], b[3]}, RSVDA: [4]byte{}, RSVDB: [4]byte{}, } } +var ( + FormatRFLT = [4]byte{0x52, 0x46, 0x4C, 0x54} // File resume format: "RFLT" (?) +) + func NewFileResumeData(list []ForkInfoList) *FileResumeData { return &FileResumeData{ - Format: [4]byte{0x52, 0x46, 0x4C, 0x54}, // RFLT + Format: FormatRFLT, Version: [2]byte{0, 1}, RSVD: [34]byte{}, ForkCount: [2]byte{0, uint8(len(list))}, @@ -42,31 +58,6 @@ func NewFileResumeData(list []ForkInfoList) *FileResumeData { } } -// -//func (frd *FileResumeData) Read(p []byte) (int, error) { -// buf := slices.Concat( -// frd.Format[:], -// frd.Version[:], -// frd.RSVD[:], -// frd.ForkCount[:], -// ) -// for _, fil := range frd.ForkInfoList { -// buf = append(buf, fil...) -// _ = binary.Write(&buf, binary.LittleEndian, fil) -// } -// -// var buf bytes.Buffer -// _ = binary.Write(&buf, binary.LittleEndian, frd.Format) -// _ = binary.Write(&buf, binary.LittleEndian, frd.Version) -// _ = binary.Write(&buf, binary.LittleEndian, frd.RSVD) -// _ = binary.Write(&buf, binary.LittleEndian, frd.ForkCount) -// for _, fil := range frd.ForkInfoList { -// _ = binary.Write(&buf, binary.LittleEndian, fil) -// } -// -// return buf.Bytes(), nil -//} - func (frd *FileResumeData) BinaryMarshal() ([]byte, error) { var buf bytes.Buffer _ = binary.Write(&buf, binary.LittleEndian, frd.Format) diff --git a/hotline/file_transfer.go b/hotline/file_transfer.go index 701259a..521b963 100644 --- a/hotline/file_transfer.go +++ b/hotline/file_transfer.go @@ -172,23 +172,6 @@ type folderUpload struct { FileNamePath []byte } -//func (fu *folderUpload) Write(p []byte) (int, error) { -// if len(p) < 7 { -// return 0, errors.New("buflen too short") -// } -// copy(fu.DataSize[:], p[0:2]) -// copy(fu.IsFolder[:], p[2:4]) -// copy(fu.PathItemCount[:], p[4:6]) -// -// fu.FileNamePath = make([]byte, binary.BigEndian.Uint16(fu.DataSize[:])-4) // -4 to subtract the path separator bytes TODO: wat -// n, err := io.ReadFull(rwc, fu.FileNamePath) -// if err != nil { -// return 0, err -// } -// -// return n + 6, nil -//} - // pathSegmentScanner implements bufio.SplitFunc for parsing path segments func pathSegmentScanner(data []byte, _ bool) (advance int, token []byte, err error) { if len(data) < 3 { @@ -269,12 +252,6 @@ func (fh *FileHeader) Read(p []byte) (int, error) { } func DownloadHandler(w io.Writer, fullPath string, fileTransfer *FileTransfer, fs FileStore, rLogger *slog.Logger, preserveForks bool) error { - //s.Stats.DownloadCounter += 1 - //s.Stats.DownloadsInProgress += 1 - //defer func() { - // s.Stats.DownloadsInProgress -= 1 - //}() - var dataOffset int64 if fileTransfer.FileResumeData != nil { dataOffset = int64(binary.BigEndian.Uint32(fileTransfer.FileResumeData.ForkInfoList[0].DataSize[:])) @@ -520,7 +497,6 @@ func DownloadFolderHandler(rwc io.ReadWriter, fullPath string, fileTransfer *Fil return fmt.Errorf("error opening file: %w", err) } - // wr := bufio.NewWriterSize(rwc, 1460) if _, err = io.Copy(rwc, io.TeeReader(file, fileTransfer.bytesSentCounter)); err != nil { return fmt.Errorf("error sending file: %w", err) } @@ -576,7 +552,6 @@ func UploadFolderHandler(rwc io.ReadWriter, fullPath string, fileTransfer *FileT //s.Stats.UploadCounter += 1 var fu folderUpload - // TODO: implement io.Writer on folderUpload and replace this if _, err := io.ReadFull(rwc, fu.DataSize[:]); err != nil { return err } @@ -586,7 +561,7 @@ func UploadFolderHandler(rwc io.ReadWriter, fullPath string, fileTransfer *FileT if _, err := io.ReadFull(rwc, fu.PathItemCount[:]); err != nil { return err } - fu.FileNamePath = make([]byte, binary.BigEndian.Uint16(fu.DataSize[:])-4) // -4 to subtract the path separator bytes TODO: wat + fu.FileNamePath = make([]byte, binary.BigEndian.Uint16(fu.DataSize[:])-4) // -4 to subtract the length of the DataSize and IsFolder fields if _, err := io.ReadFull(rwc, fu.FileNamePath); err != nil { return err } diff --git a/hotline/file_wrapper.go b/hotline/file_wrapper.go index b13e0dc..b6aff94 100644 --- a/hotline/file_wrapper.go +++ b/hotline/file_wrapper.go @@ -85,7 +85,7 @@ func (f *fileWrapper) rsrcForkSize() (s [4]byte) { func (f *fileWrapper) rsrcForkHeader() FlatFileForkHeader { return FlatFileForkHeader{ - ForkType: [4]byte{0x4D, 0x41, 0x43, 0x52}, // "MACR" + ForkType: ForkTypeMACR, DataSize: f.rsrcForkSize(), } } |