]>
Commit | Line | Data |
---|---|---|
6988a057 JH |
1 | package hotline |
2 | ||
3 | import ( | |
4 | "encoding/binary" | |
16a4ad70 JH |
5 | "errors" |
6 | "io/fs" | |
6988a057 JH |
7 | "io/ioutil" |
8 | "os" | |
9 | "path/filepath" | |
10 | "strings" | |
11 | ) | |
12 | ||
2728d12b JH |
13 | func downcaseFileExtension(filename string) string { |
14 | splitStr := strings.Split(filename, ".") | |
15 | ext := strings.ToLower( | |
16 | splitStr[len(splitStr)-1], | |
17 | ) | |
6988a057 | 18 | |
2728d12b | 19 | return ext |
6988a057 JH |
20 | } |
21 | ||
2728d12b JH |
22 | func fileTypeFromFilename(fn string) fileType { |
23 | ft, ok := fileTypes[downcaseFileExtension(fn)] | |
24 | if ok { | |
25 | return ft | |
6988a057 | 26 | } |
2728d12b | 27 | return defaultFileType |
6988a057 JH |
28 | } |
29 | ||
7cd900d6 | 30 | func fileTypeFromInfo(info fs.FileInfo) (ft fileType, err error) { |
2d52424e JH |
31 | if info.IsDir() { |
32 | ft.CreatorCode = "n/a " | |
33 | ft.TypeCode = "fldr" | |
34 | } else { | |
35 | ft = fileTypeFromFilename(info.Name()) | |
36 | } | |
37 | ||
38 | return ft, nil | |
39 | } | |
40 | ||
c5d9af5a | 41 | func getFileNameList(filePath string) (fields []Field, err error) { |
7cd900d6 | 42 | files, err := os.ReadDir(filePath) |
6988a057 JH |
43 | if err != nil { |
44 | return fields, nil | |
45 | } | |
46 | ||
47 | for _, file := range files { | |
72dd37f1 | 48 | var fnwi FileNameWithInfo |
d7548c16 | 49 | |
7cd900d6 JH |
50 | if strings.HasPrefix(file.Name(), ".") { |
51 | continue | |
52 | } | |
53 | ||
43ecc0f4 | 54 | fileCreator := make([]byte, 4) |
43ecc0f4 | 55 | |
7cd900d6 JH |
56 | fileInfo, err := file.Info() |
57 | if err != nil { | |
58 | return fields, err | |
59 | } | |
60 | ||
61 | if fileInfo.Mode()&os.ModeSymlink != 0 { | |
d7548c16 JH |
62 | resolvedPath, err := os.Readlink(filePath + "/" + file.Name()) |
63 | if err != nil { | |
64 | return fields, err | |
65 | } | |
6988a057 | 66 | |
d7548c16 | 67 | rFile, err := os.Stat(filePath + "/" + resolvedPath) |
dd7aadfa JH |
68 | if errors.Is(err, os.ErrNotExist) { |
69 | continue | |
70 | } | |
d7548c16 JH |
71 | if err != nil { |
72 | return fields, err | |
73 | } | |
74 | ||
75 | if rFile.IsDir() { | |
76 | dir, err := ioutil.ReadDir(filePath + "/" + file.Name()) | |
77 | if err != nil { | |
78 | return fields, err | |
79 | } | |
7cd900d6 JH |
80 | |
81 | var c uint32 | |
82 | for _, f := range dir { | |
83 | if !strings.HasPrefix(f.Name(), ".") { | |
84 | c += 1 | |
85 | } | |
86 | } | |
87 | ||
88 | binary.BigEndian.PutUint32(fnwi.FileSize[:], c) | |
d7548c16 JH |
89 | copy(fnwi.Type[:], []byte("fldr")[:]) |
90 | copy(fnwi.Creator[:], fileCreator[:]) | |
91 | } else { | |
92 | binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(rFile.Size())) | |
93 | copy(fnwi.Type[:], []byte(fileTypeFromFilename(rFile.Name()).TypeCode)[:]) | |
94 | copy(fnwi.Creator[:], []byte(fileTypeFromFilename(rFile.Name()).CreatorCode)[:]) | |
95 | } | |
96 | ||
97 | } else if file.IsDir() { | |
6988a057 JH |
98 | dir, err := ioutil.ReadDir(filePath + "/" + file.Name()) |
99 | if err != nil { | |
100 | return fields, err | |
101 | } | |
7cd900d6 JH |
102 | |
103 | var c uint32 | |
104 | for _, f := range dir { | |
105 | if !strings.HasPrefix(f.Name(), ".") { | |
106 | c += 1 | |
107 | } | |
108 | } | |
109 | ||
110 | binary.BigEndian.PutUint32(fnwi.FileSize[:], c) | |
d7548c16 | 111 | copy(fnwi.Type[:], []byte("fldr")[:]) |
72dd37f1 | 112 | copy(fnwi.Creator[:], fileCreator[:]) |
d7548c16 | 113 | } else { |
7cd900d6 JH |
114 | // the Hotline protocol does not support fileWrapper sizes > 4GiB due to the 4 byte field size, so skip them |
115 | if fileInfo.Size() > 4294967296 { | |
fca1ebde JH |
116 | continue |
117 | } | |
7cd900d6 JH |
118 | |
119 | hlFile, err := newFileWrapper(&OSFileStore{}, filePath+"/"+file.Name(), 0) | |
120 | if err != nil { | |
121 | return nil, err | |
122 | } | |
123 | ||
124 | copy(fnwi.FileSize[:], hlFile.totalSize()[:]) | |
125 | copy(fnwi.Type[:], hlFile.ffo.FlatFileInformationFork.TypeSignature[:]) | |
126 | copy(fnwi.Creator[:], hlFile.ffo.FlatFileInformationFork.CreatorSignature[:]) | |
6988a057 JH |
127 | } |
128 | ||
16a4ad70 JH |
129 | strippedName := strings.Replace(file.Name(), ".incomplete", "", -1) |
130 | ||
72dd37f1 | 131 | nameSize := make([]byte, 2) |
16a4ad70 | 132 | binary.BigEndian.PutUint16(nameSize, uint16(len(strippedName))) |
72dd37f1 JH |
133 | copy(fnwi.NameSize[:], nameSize[:]) |
134 | ||
16a4ad70 | 135 | fnwi.name = []byte(strippedName) |
72dd37f1 JH |
136 | |
137 | b, err := fnwi.MarshalBinary() | |
138 | if err != nil { | |
139 | return nil, err | |
140 | } | |
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 | } |