]> git.r.bdr.sh - rbdr/mobius/blobdiff - hotline/flattened_file_object.go
Include tracker.preterhuman.net in default config
[rbdr/mobius] / hotline / flattened_file_object.go
index ba6b0633160c9ec8728d81fcff5142e8a1c75b56..6b8d3d9f788a948416eda05d0a8f1fd7c5b6801d 100644 (file)
@@ -55,15 +55,15 @@ type FlatFileInformationFork struct {
        Comment          []byte // File comment
 }
 
-func NewFlatFileInformationFork(fileName string, modifyTime []byte) FlatFileInformationFork {
+func NewFlatFileInformationFork(fileName string, modifyTime []byte, typeSignature string, creatorSignature string) FlatFileInformationFork {
        return FlatFileInformationFork{
-               Platform:         []byte("AMAC"),                                     // TODO: Remove hardcode to support "AWIN" Platform (maybe?)
-               TypeSignature:    []byte(fileTypeFromFilename(fileName).TypeCode),    // TODO: Don't infer types from filename
-               CreatorSignature: []byte(fileTypeFromFilename(fileName).CreatorCode), // TODO: Don't infer types from filename
-               Flags:            []byte{0, 0, 0, 0},                                 // TODO: What is this?
-               PlatformFlags:    []byte{0, 0, 1, 0},                                 // TODO: What is this?
-               RSVD:             make([]byte, 32),                                   // Unimplemented in Hotline Protocol
-               CreateDate:       modifyTime,                                         // some filesystems don't support createTime
+               Platform:         []byte("AMAC"),           // TODO: Remove hardcode to support "AWIN" Platform (maybe?)
+               TypeSignature:    []byte(typeSignature),    // TODO: Don't infer types from filename
+               CreatorSignature: []byte(creatorSignature), // TODO: Don't infer types from filename
+               Flags:            []byte{0, 0, 0, 0},       // TODO: What is this?
+               PlatformFlags:    []byte{0, 0, 1, 0},       // TODO: What is this?
+               RSVD:             make([]byte, 32),         // Unimplemented in Hotline Protocol
+               CreateDate:       modifyTime,               // some filesystems don't support createTime
                ModifyDate:       modifyTime,
                NameScript:       make([]byte, 2), // TODO: What is this?
                Name:             []byte(fileName),
@@ -72,6 +72,14 @@ func NewFlatFileInformationFork(fileName string, modifyTime []byte) FlatFileInfo
        }
 }
 
+func (ffif *FlatFileInformationFork) friendlyType() []byte {
+
+       if name, ok := friendlyCreatorNames[string(ffif.TypeSignature)]; ok {
+               return []byte(name)
+       }
+       return ffif.CreatorSignature
+}
+
 // DataSize calculates the size of the flat file information fork, which is
 // 72 bytes for the fixed length fields plus the length of the Name + Comment
 func (ffif *FlatFileInformationFork) DataSize() []byte {
@@ -110,20 +118,10 @@ type FlatFileDataForkHeader struct {
 }
 
 func (ffif *FlatFileInformationFork) UnmarshalBinary(b []byte) error {
-
        nameSize := b[70:72]
        bs := binary.BigEndian.Uint16(nameSize)
-
        nameEnd := 72 + bs
 
-       commentSize := b[nameEnd : nameEnd+2]
-       commentLen := binary.BigEndian.Uint16(commentSize)
-
-       commentStartPos := int(nameEnd) + 2
-       commentEndPos := int(nameEnd) + 2 + int(commentLen)
-
-       comment := b[commentStartPos:commentEndPos]
-
        ffif.Platform = b[0:4]
        ffif.TypeSignature = b[4:8]
        ffif.CreatorSignature = b[8:12]
@@ -135,8 +133,16 @@ func (ffif *FlatFileInformationFork) UnmarshalBinary(b []byte) error {
        ffif.NameScript = b[68:70]
        ffif.NameSize = b[70:72]
        ffif.Name = b[72:nameEnd]
-       ffif.CommentSize = b[nameEnd : nameEnd+2]
-       ffif.Comment = comment
+
+       if len(b) > int(nameEnd) {
+               ffif.CommentSize = b[nameEnd : nameEnd+2]
+               commentLen := binary.BigEndian.Uint16(ffif.CommentSize)
+
+               commentStartPos := int(nameEnd) + 2
+               commentEndPos := int(nameEnd) + 2 + int(commentLen)
+
+               ffif.Comment = b[commentStartPos:commentEndPos]
+       }
 
        return nil
 }
@@ -175,15 +181,16 @@ func (f *flattenedFileObject) BinaryMarshal() []byte {
        return out
 }
 
-func NewFlattenedFileObject(fileRoot string, filePath, fileName []byte) (*flattenedFileObject, error) {
+func NewFlattenedFileObject(fileRoot string, filePath, fileName []byte, dataOffset int64) (*flattenedFileObject, error) {
        fullFilePath, err := readPath(fileRoot, filePath, fileName)
        if err != nil {
                return nil, err
        }
-       file, err := os.Open(fullFilePath)
+       file, err := effectiveFile(fullFilePath)
        if err != nil {
                return nil, err
        }
+
        defer func(file *os.File) { _ = file.Close() }(file)
 
        fileInfo, err := file.Stat()
@@ -192,13 +199,15 @@ func NewFlattenedFileObject(fileRoot string, filePath, fileName []byte) (*flatte
        }
 
        dataSize := make([]byte, 4)
-       binary.BigEndian.PutUint32(dataSize, uint32(fileInfo.Size()))
+       binary.BigEndian.PutUint32(dataSize, uint32(fileInfo.Size()-dataOffset))
 
        mTime := toHotlineTime(fileInfo.ModTime())
 
+       ft, _ := fileTypeFromInfo(fileInfo)
+
        return &flattenedFileObject{
                FlatFileHeader:          NewFlatFileHeader(),
-               FlatFileInformationFork: NewFlatFileInformationFork(string(fileName), mTime),
+               FlatFileInformationFork: NewFlatFileInformationFork(string(fileName), mTime, ft.TypeCode, ft.CreatorCode),
                FlatFileDataForkHeader: FlatFileDataForkHeader{
                        ForkType:        [4]byte{0x44, 0x41, 0x54, 0x41}, // "DATA"
                        CompressionType: [4]byte{},