]> git.r.bdr.sh - rbdr/mobius/blob - hotline/files.go
Implement bufio.Scanner for transaction parsing
[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 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) (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 strings.HasPrefix(file.Name(), ".") {
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 := ioutil.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 !strings.HasPrefix(f.Name(), ".") {
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
89 } else if file.IsDir() {
90 dir, err := ioutil.ReadDir(filepath.Join(path, file.Name()))
91 if err != nil {
92 return fields, err
93 }
94
95 var c uint32
96 for _, f := range dir {
97 if !strings.HasPrefix(f.Name(), ".") {
98 c += 1
99 }
100 }
101
102 binary.BigEndian.PutUint32(fnwi.FileSize[:], c)
103 copy(fnwi.Type[:], []byte("fldr")[:])
104 copy(fnwi.Creator[:], fileCreator[:])
105 } else {
106 // the Hotline protocol does not support fileWrapper sizes > 4GiB due to the 4 byte field size, so skip them
107 if fileInfo.Size() > 4294967296 {
108 continue
109 }
110
111 hlFile, err := newFileWrapper(&OSFileStore{}, path+"/"+file.Name(), 0)
112 if err != nil {
113 return nil, err
114 }
115
116 copy(fnwi.FileSize[:], hlFile.totalSize()[:])
117 copy(fnwi.Type[:], hlFile.ffo.FlatFileInformationFork.TypeSignature[:])
118 copy(fnwi.Creator[:], hlFile.ffo.FlatFileInformationFork.CreatorSignature[:])
119 }
120
121 strippedName := strings.Replace(file.Name(), ".incomplete", "", -1)
122
123 nameSize := make([]byte, 2)
124 binary.BigEndian.PutUint16(nameSize, uint16(len(strippedName)))
125 copy(fnwi.NameSize[:], nameSize[:])
126
127 fnwi.name = []byte(strippedName)
128
129 b, err := fnwi.MarshalBinary()
130 if err != nil {
131 return nil, err
132 }
133 fields = append(fields, NewField(fieldFileNameWithInfo, b))
134 }
135
136 return fields, nil
137 }
138
139 func CalcTotalSize(filePath string) ([]byte, error) {
140 var totalSize uint32
141 err := filepath.Walk(filePath, func(path string, info os.FileInfo, err error) error {
142 if err != nil {
143 return err
144 }
145
146 if info.IsDir() {
147 return nil
148 }
149
150 totalSize += uint32(info.Size())
151
152 return nil
153 })
154 if err != nil {
155 return nil, err
156 }
157
158 bs := make([]byte, 4)
159 binary.BigEndian.PutUint32(bs, totalSize)
160
161 return bs, nil
162 }
163
164 func CalcItemCount(filePath string) ([]byte, error) {
165 var itemcount uint16
166 err := filepath.Walk(filePath, func(path string, info os.FileInfo, err error) error {
167 if err != nil {
168 return err
169 }
170
171 if !strings.HasPrefix(info.Name(), ".") {
172 itemcount += 1
173 }
174
175 return nil
176 })
177 if err != nil {
178 return nil, err
179 }
180
181 bs := make([]byte, 2)
182 binary.BigEndian.PutUint16(bs, itemcount-1)
183
184 return bs, nil
185 }
186
187 func EncodeFilePath(filePath string) []byte {
188 pathSections := strings.Split(filePath, "/")
189 pathItemCount := make([]byte, 2)
190 binary.BigEndian.PutUint16(pathItemCount, uint16(len(pathSections)))
191
192 bytes := pathItemCount
193
194 for _, section := range pathSections {
195 bytes = append(bytes, []byte{0, 0}...)
196
197 pathStr := []byte(section)
198 bytes = append(bytes, byte(len(pathStr)))
199 bytes = append(bytes, pathStr...)
200 }
201
202 return bytes
203 }