]> git.r.bdr.sh - rbdr/mobius/blob - hotline/files.go
0746b1f6ef51d1f5f26bdf8fad34cd386ce11ea6
[rbdr/mobius] / hotline / files.go
1 package hotline
2
3 import (
4 "encoding/binary"
5 "errors"
6 "io/fs"
7 "os"
8 "path/filepath"
9 "regexp"
10 "strings"
11 )
12
13 func fileTypeFromFilename(filename string) fileType {
14 fileExt := strings.ToLower(filepath.Ext(filename))
15 ft, ok := fileTypes[fileExt]
16 if ok {
17 return ft
18 }
19 return defaultFileType
20 }
21
22 func fileTypeFromInfo(info fs.FileInfo) (ft fileType, err error) {
23 if info.IsDir() {
24 ft.CreatorCode = "n/a "
25 ft.TypeCode = "fldr"
26 } else {
27 ft = fileTypeFromFilename(info.Name())
28 }
29
30 return ft, nil
31 }
32
33 func getFileNameList(path string, ignoreList []string) (fields []Field, err error) {
34 files, err := os.ReadDir(path)
35 if err != nil {
36 return fields, nil
37 }
38
39 for _, file := range files {
40 var fnwi FileNameWithInfo
41
42 if ignoreFile(file.Name(), ignoreList) {
43 continue
44 }
45
46 fileCreator := make([]byte, 4)
47
48 fileInfo, err := file.Info()
49 if err != nil {
50 return fields, err
51 }
52
53 if fileInfo.Mode()&os.ModeSymlink != 0 {
54 resolvedPath, err := os.Readlink(filepath.Join(path, file.Name()))
55 if err != nil {
56 return fields, err
57 }
58
59 rFile, err := os.Stat(filepath.Join(path, resolvedPath))
60 if errors.Is(err, os.ErrNotExist) {
61 continue
62 }
63 if err != nil {
64 return fields, err
65 }
66
67 if rFile.IsDir() {
68 dir, err := os.ReadDir(filepath.Join(path, file.Name()))
69 if err != nil {
70 return fields, err
71 }
72
73 var c uint32
74 for _, f := range dir {
75 if !ignoreFile(f.Name(), ignoreList) {
76 c += 1
77 }
78 }
79
80 binary.BigEndian.PutUint32(fnwi.FileSize[:], c)
81 copy(fnwi.Type[:], []byte("fldr"))
82 copy(fnwi.Creator[:], fileCreator)
83 } else {
84 binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(rFile.Size()))
85 copy(fnwi.Type[:], []byte(fileTypeFromFilename(rFile.Name()).TypeCode))
86 copy(fnwi.Creator[:], []byte(fileTypeFromFilename(rFile.Name()).CreatorCode))
87 }
88 } else if file.IsDir() {
89 dir, err := os.ReadDir(filepath.Join(path, file.Name()))
90 if err != nil {
91 return fields, err
92 }
93
94 var c uint32
95 for _, f := range dir {
96 if !ignoreFile(f.Name(), ignoreList) {
97 c += 1
98 }
99 }
100
101 binary.BigEndian.PutUint32(fnwi.FileSize[:], c)
102 copy(fnwi.Type[:], []byte("fldr"))
103 copy(fnwi.Creator[:], fileCreator)
104 } else {
105 // the Hotline protocol does not support fileWrapper sizes > 4GiB due to the 4 byte field size, so skip them
106 if fileInfo.Size() > 4294967296 {
107 continue
108 }
109
110 hlFile, err := newFileWrapper(&OSFileStore{}, path+"/"+file.Name(), 0)
111 if err != nil {
112 return nil, err
113 }
114
115 copy(fnwi.FileSize[:], hlFile.totalSize())
116 copy(fnwi.Type[:], hlFile.ffo.FlatFileInformationFork.TypeSignature)
117 copy(fnwi.Creator[:], hlFile.ffo.FlatFileInformationFork.CreatorSignature)
118 }
119
120 strippedName := strings.ReplaceAll(file.Name(), ".incomplete", "")
121 strippedName, err = txtEncoder.String(strippedName)
122 if err != nil {
123 return nil, err
124 }
125
126 nameSize := make([]byte, 2)
127 binary.BigEndian.PutUint16(nameSize, uint16(len(strippedName)))
128 copy(fnwi.NameSize[:], nameSize)
129
130 fnwi.name = []byte(strippedName)
131
132 b, err := fnwi.MarshalBinary()
133 if err != nil {
134 return nil, err
135 }
136 fields = append(fields, NewField(FieldFileNameWithInfo, b))
137 }
138
139 return fields, nil
140 }
141
142 func CalcTotalSize(filePath string) ([]byte, error) {
143 var totalSize uint32
144 err := filepath.Walk(filePath, func(path string, info os.FileInfo, err error) error {
145 if err != nil {
146 return err
147 }
148
149 if info.IsDir() {
150 return nil
151 }
152
153 totalSize += uint32(info.Size())
154
155 return nil
156 })
157 if err != nil {
158 return nil, err
159 }
160
161 bs := make([]byte, 4)
162 binary.BigEndian.PutUint32(bs, totalSize)
163
164 return bs, nil
165 }
166
167 func CalcItemCount(filePath string) ([]byte, error) {
168 var itemcount uint16
169 err := filepath.Walk(filePath, func(path string, info os.FileInfo, err error) error {
170 if err != nil {
171 return err
172 }
173
174 if !strings.HasPrefix(info.Name(), ".") {
175 itemcount += 1
176 }
177
178 return nil
179 })
180 if err != nil {
181 return nil, err
182 }
183
184 bs := make([]byte, 2)
185 binary.BigEndian.PutUint16(bs, itemcount-1)
186
187 return bs, nil
188 }
189
190 func EncodeFilePath(filePath string) []byte {
191 pathSections := strings.Split(filePath, "/")
192 pathItemCount := make([]byte, 2)
193 binary.BigEndian.PutUint16(pathItemCount, uint16(len(pathSections)))
194
195 bytes := pathItemCount
196
197 for _, section := range pathSections {
198 bytes = append(bytes, []byte{0, 0}...)
199
200 pathStr := []byte(section)
201 bytes = append(bytes, byte(len(pathStr)))
202 bytes = append(bytes, pathStr...)
203 }
204
205 return bytes
206 }
207
208 func ignoreFile(fileName string, ignoreList []string) bool {
209 // skip files that match any regular expression present in the IgnoreFiles list
210 matchIgnoreFilter := 0
211 for _, pattern := range ignoreList {
212 if match, _ := regexp.MatchString(pattern, fileName); match {
213 matchIgnoreFilter += 1
214 }
215 }
216 return matchIgnoreFilter > 0
217 }