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