]> git.r.bdr.sh - rbdr/mobius/blob - hotline/files.go
Fix handling of symlinks in Files dir
[rbdr/mobius] / hotline / files.go
1 package hotline
2
3 import (
4 "encoding/binary"
5 "errors"
6 "io/fs"
7 "os"
8 "path/filepath"
9 "regexp"
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, ignoreList []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 ignoreFile(file.Name(), ignoreList) {
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 // Check if path is a symlink. If so, follow it.
54 if fileInfo.Mode()&os.ModeSymlink != 0 {
55 resolvedPath, err := os.Readlink(filepath.Join(path, file.Name()))
56 if err != nil {
57 return fields, err
58 }
59
60 rFile, err := os.Stat(resolvedPath)
61 if errors.Is(err, os.ErrNotExist) {
62 continue
63 }
64 if err != nil {
65 return fields, err
66 }
67
68 if rFile.IsDir() {
69 dir, err := os.ReadDir(filepath.Join(path, file.Name()))
70 if err != nil {
71 return fields, err
72 }
73
74 var c uint32
75 for _, f := range dir {
76 if !ignoreFile(f.Name(), ignoreList) {
77 c += 1
78 }
79 }
80
81 binary.BigEndian.PutUint32(fnwi.FileSize[:], c)
82 copy(fnwi.Type[:], []byte("fldr"))
83 copy(fnwi.Creator[:], fileCreator)
84 } else {
85 binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(rFile.Size()))
86 copy(fnwi.Type[:], []byte(fileTypeFromFilename(rFile.Name()).TypeCode))
87 copy(fnwi.Creator[:], []byte(fileTypeFromFilename(rFile.Name()).CreatorCode))
88 }
89 } else if file.IsDir() {
90 dir, err := os.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 !ignoreFile(f.Name(), ignoreList) {
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.ReplaceAll(file.Name(), ".incomplete", "")
122 strippedName, err = txtEncoder.String(strippedName)
123 if err != nil {
124 return nil, err
125 }
126
127 nameSize := make([]byte, 2)
128 binary.BigEndian.PutUint16(nameSize, uint16(len(strippedName)))
129 copy(fnwi.NameSize[:], nameSize)
130
131 fnwi.name = []byte(strippedName)
132
133 b, err := fnwi.MarshalBinary()
134 if err != nil {
135 return nil, err
136 }
137 fields = append(fields, NewField(FieldFileNameWithInfo, b))
138 }
139
140 return fields, nil
141 }
142
143 func CalcTotalSize(filePath string) ([]byte, error) {
144 var totalSize uint32
145 err := filepath.Walk(filePath, func(path string, info os.FileInfo, err error) error {
146 if err != nil {
147 return err
148 }
149
150 if info.IsDir() {
151 return nil
152 }
153
154 totalSize += uint32(info.Size())
155
156 return nil
157 })
158 if err != nil {
159 return nil, err
160 }
161
162 bs := make([]byte, 4)
163 binary.BigEndian.PutUint32(bs, totalSize)
164
165 return bs, nil
166 }
167
168 func CalcItemCount(filePath string) ([]byte, error) {
169 var itemcount uint16
170 err := filepath.Walk(filePath, func(path string, info os.FileInfo, err error) error {
171 if err != nil {
172 return err
173 }
174
175 if !strings.HasPrefix(info.Name(), ".") {
176 itemcount += 1
177 }
178
179 return nil
180 })
181 if err != nil {
182 return nil, err
183 }
184
185 bs := make([]byte, 2)
186 binary.BigEndian.PutUint16(bs, itemcount-1)
187
188 return bs, nil
189 }
190
191 func EncodeFilePath(filePath string) []byte {
192 pathSections := strings.Split(filePath, "/")
193 pathItemCount := make([]byte, 2)
194 binary.BigEndian.PutUint16(pathItemCount, uint16(len(pathSections)))
195
196 bytes := pathItemCount
197
198 for _, section := range pathSections {
199 bytes = append(bytes, []byte{0, 0}...)
200
201 pathStr := []byte(section)
202 bytes = append(bytes, byte(len(pathStr)))
203 bytes = append(bytes, pathStr...)
204 }
205
206 return bytes
207 }
208
209 func ignoreFile(fileName string, ignoreList []string) bool {
210 // skip files that match any regular expression present in the IgnoreFiles list
211 matchIgnoreFilter := 0
212 for _, pattern := range ignoreList {
213 if match, _ := regexp.MatchString(pattern, fileName); match {
214 matchIgnoreFilter += 1
215 }
216 }
217 return matchIgnoreFilter > 0
218 }