]>
Commit | Line | Data |
---|---|---|
1 | package hotline | |
2 | ||
3 | import ( | |
4 | "encoding/binary" | |
5 | "errors" | |
6 | "io" | |
7 | "io/fs" | |
8 | "os" | |
9 | "path/filepath" | |
10 | "regexp" | |
11 | "strings" | |
12 | ) | |
13 | ||
14 | func fileTypeFromFilename(filename string) fileType { | |
15 | fileExt := strings.ToLower(filepath.Ext(filename)) | |
16 | ft, ok := fileTypes[fileExt] | |
17 | if ok { | |
18 | return ft | |
19 | } | |
20 | return defaultFileType | |
21 | } | |
22 | ||
23 | func fileTypeFromInfo(info fs.FileInfo) (ft fileType, err error) { | |
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 | ||
34 | const maxFileSize = 4294967296 | |
35 | ||
36 | func getFileNameList(path string, ignoreList []string) (fields []Field, err error) { | |
37 | files, err := os.ReadDir(path) | |
38 | if err != nil { | |
39 | return fields, nil | |
40 | } | |
41 | ||
42 | for _, file := range files { | |
43 | var fnwi FileNameWithInfo | |
44 | ||
45 | if ignoreFile(file.Name(), ignoreList) { | |
46 | continue | |
47 | } | |
48 | ||
49 | fileCreator := make([]byte, 4) | |
50 | ||
51 | fileInfo, err := file.Info() | |
52 | if err != nil { | |
53 | return fields, err | |
54 | } | |
55 | ||
56 | // Check if path is a symlink. If so, follow it. | |
57 | if fileInfo.Mode()&os.ModeSymlink != 0 { | |
58 | resolvedPath, err := os.Readlink(filepath.Join(path, file.Name())) | |
59 | if err != nil { | |
60 | return fields, err | |
61 | } | |
62 | ||
63 | rFile, err := os.Stat(resolvedPath) | |
64 | if errors.Is(err, os.ErrNotExist) { | |
65 | continue | |
66 | } | |
67 | if err != nil { | |
68 | return fields, err | |
69 | } | |
70 | ||
71 | if rFile.IsDir() { | |
72 | dir, err := os.ReadDir(filepath.Join(path, file.Name())) | |
73 | if err != nil { | |
74 | return fields, err | |
75 | } | |
76 | ||
77 | var c uint32 | |
78 | for _, f := range dir { | |
79 | if !ignoreFile(f.Name(), ignoreList) { | |
80 | c += 1 | |
81 | } | |
82 | } | |
83 | ||
84 | binary.BigEndian.PutUint32(fnwi.FileSize[:], c) | |
85 | copy(fnwi.Type[:], "fldr") | |
86 | copy(fnwi.Creator[:], fileCreator) | |
87 | } else { | |
88 | binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(rFile.Size())) | |
89 | copy(fnwi.Type[:], fileTypeFromFilename(rFile.Name()).TypeCode) | |
90 | copy(fnwi.Creator[:], fileTypeFromFilename(rFile.Name()).CreatorCode) | |
91 | } | |
92 | } else if file.IsDir() { | |
93 | dir, err := os.ReadDir(filepath.Join(path, file.Name())) | |
94 | if err != nil { | |
95 | return fields, err | |
96 | } | |
97 | ||
98 | var c uint32 | |
99 | for _, f := range dir { | |
100 | if !ignoreFile(f.Name(), ignoreList) { | |
101 | c += 1 | |
102 | } | |
103 | } | |
104 | ||
105 | binary.BigEndian.PutUint32(fnwi.FileSize[:], c) | |
106 | copy(fnwi.Type[:], "fldr") | |
107 | copy(fnwi.Creator[:], fileCreator) | |
108 | } else { | |
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 { | |
111 | continue | |
112 | } | |
113 | ||
114 | hlFile, err := newFileWrapper(&OSFileStore{}, path+"/"+file.Name(), 0) | |
115 | if err != nil { | |
116 | return nil, err | |
117 | } | |
118 | ||
119 | copy(fnwi.FileSize[:], hlFile.totalSize()) | |
120 | copy(fnwi.Type[:], hlFile.ffo.FlatFileInformationFork.TypeSignature) | |
121 | copy(fnwi.Creator[:], hlFile.ffo.FlatFileInformationFork.CreatorSignature) | |
122 | } | |
123 | ||
124 | strippedName := strings.ReplaceAll(file.Name(), ".incomplete", "") | |
125 | strippedName, err = txtEncoder.String(strippedName) | |
126 | if err != nil { | |
127 | continue | |
128 | } | |
129 | ||
130 | nameSize := make([]byte, 2) | |
131 | binary.BigEndian.PutUint16(nameSize, uint16(len(strippedName))) | |
132 | copy(fnwi.NameSize[:], nameSize) | |
133 | ||
134 | fnwi.Name = []byte(strippedName) | |
135 | ||
136 | b, err := io.ReadAll(&fnwi) | |
137 | if err != nil { | |
138 | return nil, err | |
139 | } | |
140 | fields = append(fields, NewField(FieldFileNameWithInfo, b)) | |
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 { | |
174 | if err != nil { | |
175 | return err | |
176 | } | |
177 | ||
178 | if !strings.HasPrefix(info.Name(), ".") { | |
179 | itemcount += 1 | |
180 | } | |
181 | ||
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 | } | |
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 | } |