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