]> git.r.bdr.sh - rbdr/mobius/blame - hotline/files.go
Handle zero length comment and file paths for Nostalgia compatibility
[rbdr/mobius] / hotline / files.go
CommitLineData
6988a057
JH
1package hotline
2
3import (
4 "encoding/binary"
16a4ad70
JH
5 "errors"
6 "io/fs"
6988a057
JH
7 "io/ioutil"
8 "os"
9 "path/filepath"
10 "strings"
11)
12
2728d12b
JH
13func downcaseFileExtension(filename string) string {
14 splitStr := strings.Split(filename, ".")
15 ext := strings.ToLower(
16 splitStr[len(splitStr)-1],
17 )
6988a057 18
2728d12b 19 return ext
6988a057
JH
20}
21
2728d12b
JH
22func fileTypeFromFilename(fn string) fileType {
23 ft, ok := fileTypes[downcaseFileExtension(fn)]
24 if ok {
25 return ft
6988a057 26 }
2728d12b 27 return defaultFileType
6988a057
JH
28}
29
c5d9af5a 30func getFileNameList(filePath string) (fields []Field, err error) {
6988a057
JH
31 files, err := ioutil.ReadDir(filePath)
32 if err != nil {
33 return fields, nil
34 }
35
36 for _, file := range files {
43ecc0f4 37 var fileType []byte
72dd37f1 38 var fnwi FileNameWithInfo
43ecc0f4 39 fileCreator := make([]byte, 4)
6988a057 40 if !file.IsDir() {
2728d12b
JH
41 fileType = []byte(fileTypeFromFilename(file.Name()).TypeCode)
42 fileCreator = []byte(fileTypeFromFilename(file.Name()).CreatorCode)
43ecc0f4 43
72dd37f1
JH
44 binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(file.Size()))
45 copy(fnwi.Type[:], fileType[:])
46 copy(fnwi.Creator[:], fileCreator[:])
6988a057 47 } else {
43ecc0f4 48 fileType = []byte("fldr")
6988a057
JH
49
50 dir, err := ioutil.ReadDir(filePath + "/" + file.Name())
51 if err != nil {
52 return fields, err
53 }
72dd37f1
JH
54 binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(len(dir)))
55 copy(fnwi.Type[:], fileType[:])
56 copy(fnwi.Creator[:], fileCreator[:])
6988a057
JH
57 }
58
16a4ad70
JH
59 strippedName := strings.Replace(file.Name(), ".incomplete", "", -1)
60
72dd37f1 61 nameSize := make([]byte, 2)
16a4ad70 62 binary.BigEndian.PutUint16(nameSize, uint16(len(strippedName)))
72dd37f1
JH
63 copy(fnwi.NameSize[:], nameSize[:])
64
16a4ad70 65 fnwi.name = []byte(strippedName)
72dd37f1
JH
66
67 b, err := fnwi.MarshalBinary()
68 if err != nil {
69 return nil, err
70 }
71 fields = append(fields, NewField(fieldFileNameWithInfo, b))
6988a057
JH
72 }
73
74 return fields, nil
75}
76
77func CalcTotalSize(filePath string) ([]byte, error) {
78 var totalSize uint32
79 err := filepath.Walk(filePath, func(path string, info os.FileInfo, err error) error {
80 if err != nil {
81 return err
82 }
83
84 if info.IsDir() {
85 return nil
86 }
87
88 totalSize += uint32(info.Size())
89
90 return nil
91 })
92 if err != nil {
93 return nil, err
94 }
95
96 bs := make([]byte, 4)
97 binary.BigEndian.PutUint32(bs, totalSize)
98
99 return bs, nil
100}
101
102func CalcItemCount(filePath string) ([]byte, error) {
103 var itemcount uint16
104 err := filepath.Walk(filePath, func(path string, info os.FileInfo, err error) error {
105 itemcount += 1
106
107 if err != nil {
108 return err
109 }
110
111 return nil
112 })
113 if err != nil {
114 return nil, err
115 }
116
117 bs := make([]byte, 2)
118 binary.BigEndian.PutUint16(bs, itemcount-1)
119
120 return bs, nil
121}
122
123func EncodeFilePath(filePath string) []byte {
124 pathSections := strings.Split(filePath, "/")
125 pathItemCount := make([]byte, 2)
126 binary.BigEndian.PutUint16(pathItemCount, uint16(len(pathSections)))
127
128 bytes := pathItemCount
129
130 for _, section := range pathSections {
131 bytes = append(bytes, []byte{0, 0}...)
132
133 pathStr := []byte(section)
134 bytes = append(bytes, byte(len(pathStr)))
135 bytes = append(bytes, pathStr...)
136 }
137
138 return bytes
139}
16a4ad70
JH
140
141const incompleteFileSuffix = ".incomplete"
142
143// effectiveFile wraps os.Open to check for the presence of a partial file transfer as a fallback
144func effectiveFile(filePath string) (*os.File, error) {
145 file, err := os.Open(filePath)
146 if err != nil && !errors.Is(err, fs.ErrNotExist) {
147 return nil, err
148 }
149
150 if errors.Is(err, fs.ErrNotExist) {
151 file, err = os.OpenFile(filePath+incompleteFileSuffix, os.O_APPEND|os.O_WRONLY, 0644)
152 if err != nil {
153 return nil, err
154 }
155 }
156 return file, nil
157}