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