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