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