]> git.r.bdr.sh - rbdr/mobius/blame_incremental - hotline/files.go
Adopt more fixed size array types for struct fields
[rbdr/mobius] / hotline / files.go
... / ...
CommitLineData
1package hotline
2
3import (
4 "encoding/binary"
5 "errors"
6 "fmt"
7 "io"
8 "io/fs"
9 "os"
10 "path/filepath"
11 "regexp"
12 "strings"
13)
14
15func fileTypeFromFilename(filename string) fileType {
16 fileExt := strings.ToLower(filepath.Ext(filename))
17 ft, ok := fileTypes[fileExt]
18 if ok {
19 return ft
20 }
21 return defaultFileType
22}
23
24func fileTypeFromInfo(info fs.FileInfo) (ft fileType, err error) {
25 if info.IsDir() {
26 ft.CreatorCode = "n/a "
27 ft.TypeCode = "fldr"
28 } else {
29 ft = fileTypeFromFilename(info.Name())
30 }
31
32 return ft, nil
33}
34
35const maxFileSize = 4294967296
36
37func getFileNameList(path string, ignoreList []string) (fields []Field, err error) {
38 files, err := os.ReadDir(path)
39 if err != nil {
40 return fields, fmt.Errorf("error reading path: %s: %w", path, err)
41 }
42
43 for _, file := range files {
44 var fnwi FileNameWithInfo
45
46 if ignoreFile(file.Name(), ignoreList) {
47 continue
48 }
49
50 fileCreator := make([]byte, 4)
51
52 fileInfo, err := file.Info()
53 if err != nil {
54 return fields, fmt.Errorf("error getting file info: %s: %w", file.Name(), err)
55 }
56
57 // Check if path is a symlink. If so, follow it.
58 if fileInfo.Mode()&os.ModeSymlink != 0 {
59 resolvedPath, err := os.Readlink(filepath.Join(path, file.Name()))
60 if err != nil {
61 return fields, fmt.Errorf("error following symlink: %s: %w", resolvedPath, err)
62 }
63
64 rFile, err := os.Stat(resolvedPath)
65 if errors.Is(err, os.ErrNotExist) {
66 continue
67 }
68 if err != nil {
69 return fields, err
70 }
71
72 if rFile.IsDir() {
73 dir, err := os.ReadDir(filepath.Join(path, file.Name()))
74 if err != nil {
75 return fields, err
76 }
77
78 var c uint32
79 for _, f := range dir {
80 if !ignoreFile(f.Name(), ignoreList) {
81 c += 1
82 }
83 }
84
85 binary.BigEndian.PutUint32(fnwi.FileSize[:], c)
86 copy(fnwi.Type[:], "fldr")
87 copy(fnwi.Creator[:], fileCreator)
88 } else {
89 binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(rFile.Size()))
90 copy(fnwi.Type[:], fileTypeFromFilename(rFile.Name()).TypeCode)
91 copy(fnwi.Creator[:], fileTypeFromFilename(rFile.Name()).CreatorCode)
92 }
93 } else if file.IsDir() {
94 dir, err := os.ReadDir(filepath.Join(path, file.Name()))
95 if err != nil {
96 return fields, fmt.Errorf("readDir: %w", err)
97 }
98
99 var c uint32
100 for _, f := range dir {
101 if !ignoreFile(f.Name(), ignoreList) {
102 c += 1
103 }
104 }
105
106 binary.BigEndian.PutUint32(fnwi.FileSize[:], c)
107 copy(fnwi.Type[:], "fldr")
108 copy(fnwi.Creator[:], fileCreator)
109 } else {
110 // the Hotline protocol does not support file sizes > 4GiB due to the 4 byte field size, so skip them
111 if fileInfo.Size() > maxFileSize {
112 continue
113 }
114
115 hlFile, err := newFileWrapper(&OSFileStore{}, path+"/"+file.Name(), 0)
116 if err != nil {
117 return nil, fmt.Errorf("newFileWrapper: %w", err)
118 }
119
120 copy(fnwi.FileSize[:], hlFile.totalSize())
121 copy(fnwi.Type[:], hlFile.ffo.FlatFileInformationFork.TypeSignature[:])
122 copy(fnwi.Creator[:], hlFile.ffo.FlatFileInformationFork.CreatorSignature[:])
123 }
124
125 strippedName := strings.ReplaceAll(file.Name(), ".incomplete", "")
126 strippedName, err = txtEncoder.String(strippedName)
127 if err != nil {
128 continue
129 }
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 := io.ReadAll(&fnwi)
138 if err != nil {
139 return nil, fmt.Errorf("error io.ReadAll: %w", err)
140 }
141 fields = append(fields, NewField(FieldFileNameWithInfo, b))
142 }
143
144 return fields, nil
145}
146
147func 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
172func 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
195func 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}
212
213func ignoreFile(fileName string, ignoreList []string) bool {
214 // skip files that match any regular expression present in the IgnoreFiles list
215 matchIgnoreFilter := 0
216 for _, pattern := range ignoreList {
217 if match, _ := regexp.MatchString(pattern, fileName); match {
218 matchIgnoreFilter += 1
219 }
220 }
221 return matchIgnoreFilter > 0
222}