]>
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", "") |
16a4ad70 | 121 | |
72dd37f1 | 122 | nameSize := make([]byte, 2) |
16a4ad70 | 123 | binary.BigEndian.PutUint16(nameSize, uint16(len(strippedName))) |
aeb97482 | 124 | copy(fnwi.NameSize[:], nameSize) |
72dd37f1 | 125 | |
16a4ad70 | 126 | fnwi.name = []byte(strippedName) |
72dd37f1 JH |
127 | |
128 | b, err := fnwi.MarshalBinary() | |
129 | if err != nil { | |
130 | return nil, err | |
131 | } | |
d005ef04 | 132 | fields = append(fields, NewField(FieldFileNameWithInfo, b)) |
6988a057 JH |
133 | } |
134 | ||
135 | return fields, nil | |
136 | } | |
137 | ||
138 | func CalcTotalSize(filePath string) ([]byte, error) { | |
139 | var totalSize uint32 | |
140 | err := filepath.Walk(filePath, func(path string, info os.FileInfo, err error) error { | |
141 | if err != nil { | |
142 | return err | |
143 | } | |
144 | ||
145 | if info.IsDir() { | |
146 | return nil | |
147 | } | |
148 | ||
149 | totalSize += uint32(info.Size()) | |
150 | ||
151 | return nil | |
152 | }) | |
153 | if err != nil { | |
154 | return nil, err | |
155 | } | |
156 | ||
157 | bs := make([]byte, 4) | |
158 | binary.BigEndian.PutUint32(bs, totalSize) | |
159 | ||
160 | return bs, nil | |
161 | } | |
162 | ||
163 | func CalcItemCount(filePath string) ([]byte, error) { | |
164 | var itemcount uint16 | |
165 | err := filepath.Walk(filePath, func(path string, info os.FileInfo, err error) error { | |
6988a057 JH |
166 | if err != nil { |
167 | return err | |
168 | } | |
169 | ||
7cd900d6 JH |
170 | if !strings.HasPrefix(info.Name(), ".") { |
171 | itemcount += 1 | |
172 | } | |
173 | ||
6988a057 JH |
174 | return nil |
175 | }) | |
176 | if err != nil { | |
177 | return nil, err | |
178 | } | |
179 | ||
180 | bs := make([]byte, 2) | |
181 | binary.BigEndian.PutUint16(bs, itemcount-1) | |
182 | ||
183 | return bs, nil | |
184 | } | |
185 | ||
186 | func EncodeFilePath(filePath string) []byte { | |
187 | pathSections := strings.Split(filePath, "/") | |
188 | pathItemCount := make([]byte, 2) | |
189 | binary.BigEndian.PutUint16(pathItemCount, uint16(len(pathSections))) | |
190 | ||
191 | bytes := pathItemCount | |
192 | ||
193 | for _, section := range pathSections { | |
194 | bytes = append(bytes, []byte{0, 0}...) | |
195 | ||
196 | pathStr := []byte(section) | |
197 | bytes = append(bytes, byte(len(pathStr))) | |
198 | bytes = append(bytes, pathStr...) | |
199 | } | |
200 | ||
201 | return bytes | |
202 | } | |
b8c0a83a JH |
203 | |
204 | func ignoreFile(fileName string, ignoreList []string) bool { | |
205 | // skip files that match any regular expression present in the IgnoreFiles list | |
206 | matchIgnoreFilter := 0 | |
207 | for _, pattern := range ignoreList { | |
208 | if match, _ := regexp.MatchString(pattern, fileName); match { | |
209 | matchIgnoreFilter += 1 | |
210 | } | |
211 | } | |
212 | return matchIgnoreFilter > 0 | |
213 | } |