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