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