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