]>
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 | ||
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 | ||
f22acf38 JH |
33 | func getFileNameList(path string) (fields []Field, err error) { |
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 | |
7cd900d6 JH |
42 | if strings.HasPrefix(file.Name(), ".") { |
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() { | |
f22acf38 | 68 | dir, err := ioutil.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 { | |
75 | if !strings.HasPrefix(f.Name(), ".") { | |
76 | c += 1 | |
77 | } | |
78 | } | |
79 | ||
80 | binary.BigEndian.PutUint32(fnwi.FileSize[:], c) | |
d7548c16 JH |
81 | copy(fnwi.Type[:], []byte("fldr")[:]) |
82 | copy(fnwi.Creator[:], fileCreator[:]) | |
83 | } else { | |
84 | binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(rFile.Size())) | |
85 | copy(fnwi.Type[:], []byte(fileTypeFromFilename(rFile.Name()).TypeCode)[:]) | |
86 | copy(fnwi.Creator[:], []byte(fileTypeFromFilename(rFile.Name()).CreatorCode)[:]) | |
87 | } | |
88 | ||
89 | } else if file.IsDir() { | |
f22acf38 | 90 | dir, err := ioutil.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 { | |
97 | if !strings.HasPrefix(f.Name(), ".") { | |
98 | c += 1 | |
99 | } | |
100 | } | |
101 | ||
102 | binary.BigEndian.PutUint32(fnwi.FileSize[:], c) | |
d7548c16 | 103 | copy(fnwi.Type[:], []byte("fldr")[:]) |
72dd37f1 | 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 | ||
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 | ||
16a4ad70 JH |
121 | strippedName := strings.Replace(file.Name(), ".incomplete", "", -1) |
122 | ||
72dd37f1 | 123 | nameSize := make([]byte, 2) |
16a4ad70 | 124 | binary.BigEndian.PutUint16(nameSize, uint16(len(strippedName))) |
72dd37f1 JH |
125 | copy(fnwi.NameSize[:], nameSize[:]) |
126 | ||
16a4ad70 | 127 | fnwi.name = []byte(strippedName) |
72dd37f1 JH |
128 | |
129 | b, err := fnwi.MarshalBinary() | |
130 | if err != nil { | |
131 | return nil, err | |
132 | } | |
133 | fields = append(fields, NewField(fieldFileNameWithInfo, b)) | |
6988a057 JH |
134 | } |
135 | ||
136 | return fields, nil | |
137 | } | |
138 | ||
139 | func CalcTotalSize(filePath string) ([]byte, error) { | |
140 | var totalSize uint32 | |
141 | err := filepath.Walk(filePath, func(path string, info os.FileInfo, err error) error { | |
142 | if err != nil { | |
143 | return err | |
144 | } | |
145 | ||
146 | if info.IsDir() { | |
147 | return nil | |
148 | } | |
149 | ||
150 | totalSize += uint32(info.Size()) | |
151 | ||
152 | return nil | |
153 | }) | |
154 | if err != nil { | |
155 | return nil, err | |
156 | } | |
157 | ||
158 | bs := make([]byte, 4) | |
159 | binary.BigEndian.PutUint32(bs, totalSize) | |
160 | ||
161 | return bs, nil | |
162 | } | |
163 | ||
164 | func CalcItemCount(filePath string) ([]byte, error) { | |
165 | var itemcount uint16 | |
166 | err := filepath.Walk(filePath, func(path string, info os.FileInfo, err error) error { | |
6988a057 JH |
167 | if err != nil { |
168 | return err | |
169 | } | |
170 | ||
7cd900d6 JH |
171 | if !strings.HasPrefix(info.Name(), ".") { |
172 | itemcount += 1 | |
173 | } | |
174 | ||
6988a057 JH |
175 | return nil |
176 | }) | |
177 | if err != nil { | |
178 | return nil, err | |
179 | } | |
180 | ||
181 | bs := make([]byte, 2) | |
182 | binary.BigEndian.PutUint16(bs, itemcount-1) | |
183 | ||
184 | return bs, nil | |
185 | } | |
186 | ||
187 | func EncodeFilePath(filePath string) []byte { | |
188 | pathSections := strings.Split(filePath, "/") | |
189 | pathItemCount := make([]byte, 2) | |
190 | binary.BigEndian.PutUint16(pathItemCount, uint16(len(pathSections))) | |
191 | ||
192 | bytes := pathItemCount | |
193 | ||
194 | for _, section := range pathSections { | |
195 | bytes = append(bytes, []byte{0, 0}...) | |
196 | ||
197 | pathStr := []byte(section) | |
198 | bytes = append(bytes, byte(len(pathStr))) | |
199 | bytes = append(bytes, pathStr...) | |
200 | } | |
201 | ||
202 | return bytes | |
203 | } |