]> git.r.bdr.sh - rbdr/mobius/blob - hotline/files.go
Minor cleanup
[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.Replace(file.Name(), ".incomplete", "", -1)
121
122 nameSize := make([]byte, 2)
123 binary.BigEndian.PutUint16(nameSize, uint16(len(strippedName)))
124 copy(fnwi.NameSize[:], nameSize)
125
126 fnwi.name = []byte(strippedName)
127
128 b, err := fnwi.MarshalBinary()
129 if err != nil {
130 return nil, err
131 }
132 fields = append(fields, NewField(FieldFileNameWithInfo, b))
133 }
134
135 return fields, nil
136 }
137
138 func CalcTotalSize(filePath string) ([]byte, error) {
139 var totalSize uint32
140 err := filepath.Walk(filePath, func(path string, info os.FileInfo, err error) error {
141 if err != nil {
142 return err
143 }
144
145 if info.IsDir() {
146 return nil
147 }
148
149 totalSize += uint32(info.Size())
150
151 return nil
152 })
153 if err != nil {
154 return nil, err
155 }
156
157 bs := make([]byte, 4)
158 binary.BigEndian.PutUint32(bs, totalSize)
159
160 return bs, nil
161 }
162
163 func CalcItemCount(filePath string) ([]byte, error) {
164 var itemcount uint16
165 err := filepath.Walk(filePath, func(path string, info os.FileInfo, err error) error {
166 if err != nil {
167 return err
168 }
169
170 if !strings.HasPrefix(info.Name(), ".") {
171 itemcount += 1
172 }
173
174 return nil
175 })
176 if err != nil {
177 return nil, err
178 }
179
180 bs := make([]byte, 2)
181 binary.BigEndian.PutUint16(bs, itemcount-1)
182
183 return bs, nil
184 }
185
186 func EncodeFilePath(filePath string) []byte {
187 pathSections := strings.Split(filePath, "/")
188 pathItemCount := make([]byte, 2)
189 binary.BigEndian.PutUint16(pathItemCount, uint16(len(pathSections)))
190
191 bytes := pathItemCount
192
193 for _, section := range pathSections {
194 bytes = append(bytes, []byte{0, 0}...)
195
196 pathStr := []byte(section)
197 bytes = append(bytes, byte(len(pathStr)))
198 bytes = append(bytes, pathStr...)
199 }
200
201 return bytes
202 }
203
204 func ignoreFile(fileName string, ignoreList []string) bool {
205 // skip files that match any regular expression present in the IgnoreFiles list
206 matchIgnoreFilter := 0
207 for _, pattern := range ignoreList {
208 if match, _ := regexp.MatchString(pattern, fileName); match {
209 matchIgnoreFilter += 1
210 }
211 }
212 return matchIgnoreFilter > 0
213 }