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