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