]> git.r.bdr.sh - rbdr/mobius/blame - hotline/files.go
Account for the root
[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
fd740bc4 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
f8e4cd54 43 for _, file := range files {
72dd37f1 44 var fnwi FileNameWithInfo
d7548c16 45
b8c0a83a 46 if ignoreFile(file.Name(), ignoreList) {
7cd900d6
JH
47 continue
48 }
49
43ecc0f4 50 fileCreator := make([]byte, 4)
43ecc0f4 51
7cd900d6
JH
52 fileInfo, err := file.Info()
53 if err != nil {
45ca5d60 54 return fields, fmt.Errorf("error getting file info: %s: %w", file.Name(), err)
7cd900d6
JH
55 }
56
9ff82933 57 // Check if path is a symlink. If so, follow it.
7cd900d6 58 if fileInfo.Mode()&os.ModeSymlink != 0 {
f22acf38 59 resolvedPath, err := os.Readlink(filepath.Join(path, file.Name()))
d7548c16 60 if err != nil {
45ca5d60 61 return fields, fmt.Errorf("error following symlink: %s: %w", resolvedPath, err)
d7548c16 62 }
6988a057 63
9ff82933 64 rFile, err := os.Stat(resolvedPath)
dd7aadfa
JH
65 if errors.Is(err, os.ErrNotExist) {
66 continue
67 }
d7548c16
JH
68 if err != nil {
69 return fields, err
70 }
71
72 if rFile.IsDir() {
548d4bb9 73 dir, err := os.ReadDir(filepath.Join(path, file.Name()))
d7548c16
JH
74 if err != nil {
75 return fields, err
76 }
7cd900d6
JH
77
78 var c uint32
79 for _, f := range dir {
b8c0a83a 80 if !ignoreFile(f.Name(), ignoreList) {
7cd900d6
JH
81 c += 1
82 }
83 }
84
85 binary.BigEndian.PutUint32(fnwi.FileSize[:], c)
1c7401b1 86 copy(fnwi.Type[:], "fldr")
aeb97482 87 copy(fnwi.Creator[:], fileCreator)
d7548c16
JH
88 } else {
89 binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(rFile.Size()))
1c7401b1
JH
90 copy(fnwi.Type[:], fileTypeFromFilename(rFile.Name()).TypeCode)
91 copy(fnwi.Creator[:], fileTypeFromFilename(rFile.Name()).CreatorCode)
d7548c16 92 }
d7548c16 93 } else if file.IsDir() {
548d4bb9 94 dir, err := os.ReadDir(filepath.Join(path, file.Name()))
6988a057 95 if err != nil {
45ca5d60 96 return fields, fmt.Errorf("readDir: %w", err)
6988a057 97 }
7cd900d6
JH
98
99 var c uint32
100 for _, f := range dir {
b8c0a83a 101 if !ignoreFile(f.Name(), ignoreList) {
7cd900d6
JH
102 c += 1
103 }
104 }
105
106 binary.BigEndian.PutUint32(fnwi.FileSize[:], c)
1c7401b1 107 copy(fnwi.Type[:], "fldr")
aeb97482 108 copy(fnwi.Creator[:], fileCreator)
d7548c16 109 } else {
1c7401b1
JH
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 {
fca1ebde
JH
112 continue
113 }
7cd900d6 114
fd740bc4 115 hlFile, err := NewFileWrapper(&OSFileStore{}, path+"/"+file.Name(), 0)
7cd900d6 116 if err != nil {
fd740bc4 117 return nil, fmt.Errorf("NewFileWrapper: %w", err)
7cd900d6
JH
118 }
119
fd740bc4
JH
120 copy(fnwi.FileSize[:], hlFile.TotalSize())
121 copy(fnwi.Type[:], hlFile.Ffo.FlatFileInformationFork.TypeSignature[:])
122 copy(fnwi.Creator[:], hlFile.Ffo.FlatFileInformationFork.CreatorSignature[:])
6988a057
JH
123 }
124
c8bfd606 125 strippedName := strings.ReplaceAll(file.Name(), ".incomplete", "")
2e1aec0f
JH
126 strippedName, err = txtEncoder.String(strippedName)
127 if err != nil {
2b7fabb5 128 continue
2e1aec0f 129 }
16a4ad70 130
72dd37f1 131 nameSize := make([]byte, 2)
16a4ad70 132 binary.BigEndian.PutUint16(nameSize, uint16(len(strippedName)))
aeb97482 133 copy(fnwi.NameSize[:], nameSize)
72dd37f1 134
95159e55 135 fnwi.Name = []byte(strippedName)
72dd37f1 136
b129b7cb 137 b, err := io.ReadAll(&fnwi)
72dd37f1 138 if err != nil {
45ca5d60 139 return nil, fmt.Errorf("error io.ReadAll: %w", err)
72dd37f1 140 }
d005ef04 141 fields = append(fields, NewField(FieldFileNameWithInfo, b))
6988a057
JH
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
a2ef262a 172// CalcItemCount recurses through a file path and counts the number of non-hidden files.
6988a057 173func CalcItemCount(filePath string) ([]byte, error) {
a2ef262a
JH
174 var itemCount uint16
175
176 // Walk the directory and count items
6988a057 177 err := filepath.Walk(filePath, func(path string, info os.FileInfo, err error) error {
6988a057
JH
178 if err != nil {
179 return err
180 }
181
a2ef262a 182 // Skip hidden files
7cd900d6 183 if !strings.HasPrefix(info.Name(), ".") {
a2ef262a 184 itemCount++
7cd900d6
JH
185 }
186
6988a057
JH
187 return nil
188 })
189 if err != nil {
190 return nil, err
191 }
192
193 bs := make([]byte, 2)
a2ef262a 194 binary.BigEndian.PutUint16(bs, itemCount-1)
6988a057
JH
195
196 return bs, nil
197}
198
199func EncodeFilePath(filePath string) []byte {
200 pathSections := strings.Split(filePath, "/")
201 pathItemCount := make([]byte, 2)
202 binary.BigEndian.PutUint16(pathItemCount, uint16(len(pathSections)))
203
204 bytes := pathItemCount
205
206 for _, section := range pathSections {
207 bytes = append(bytes, []byte{0, 0}...)
208
209 pathStr := []byte(section)
210 bytes = append(bytes, byte(len(pathStr)))
211 bytes = append(bytes, pathStr...)
212 }
213
214 return bytes
215}
b8c0a83a
JH
216
217func ignoreFile(fileName string, ignoreList []string) bool {
218 // skip files that match any regular expression present in the IgnoreFiles list
219 matchIgnoreFilter := 0
220 for _, pattern := range ignoreList {
221 if match, _ := regexp.MatchString(pattern, fileName); match {
222 matchIgnoreFilter += 1
223 }
224 }
225 return matchIgnoreFilter > 0
226}