]> git.r.bdr.sh - rbdr/mobius/blob - hotline/files.go
d07f4455ebe188774a99164a5b033f55c141de7b
[rbdr/mobius] / hotline / files.go
1 package hotline
2
3 import (
4 "encoding/binary"
5 "errors"
6 "io/fs"
7 "io/ioutil"
8 "os"
9 "path/filepath"
10 "strings"
11 )
12
13 func downcaseFileExtension(filename string) string {
14 splitStr := strings.Split(filename, ".")
15 ext := strings.ToLower(
16 splitStr[len(splitStr)-1],
17 )
18
19 return ext
20 }
21
22 func fileTypeFromFilename(fn string) fileType {
23 ft, ok := fileTypes[downcaseFileExtension(fn)]
24 if ok {
25 return ft
26 }
27 return defaultFileType
28 }
29
30 func fileTypeFromInfo(info os.FileInfo) (ft fileType, err error) {
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
41 func getFileNameList(filePath string) (fields []Field, err error) {
42 files, err := ioutil.ReadDir(filePath)
43 if err != nil {
44 return fields, nil
45 }
46
47 for _, file := range files {
48 var fnwi FileNameWithInfo
49
50 fileCreator := make([]byte, 4)
51
52 if file.Mode()&os.ModeSymlink != 0 {
53 resolvedPath, err := os.Readlink(filePath + "/" + file.Name())
54 if err != nil {
55 return fields, err
56 }
57
58 rFile, err := os.Stat(filePath + "/" + resolvedPath)
59 if errors.Is(err, os.ErrNotExist) {
60 continue
61 }
62 if err != nil {
63 return fields, err
64 }
65
66 if rFile.IsDir() {
67 dir, err := ioutil.ReadDir(filePath + "/" + file.Name())
68 if err != nil {
69 return fields, err
70 }
71 binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(len(dir)))
72 copy(fnwi.Type[:], []byte("fldr")[:])
73 copy(fnwi.Creator[:], fileCreator[:])
74 } else {
75 binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(rFile.Size()))
76 copy(fnwi.Type[:], []byte(fileTypeFromFilename(rFile.Name()).TypeCode)[:])
77 copy(fnwi.Creator[:], []byte(fileTypeFromFilename(rFile.Name()).CreatorCode)[:])
78 }
79
80 } else if file.IsDir() {
81 dir, err := ioutil.ReadDir(filePath + "/" + file.Name())
82 if err != nil {
83 return fields, err
84 }
85 binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(len(dir)))
86 copy(fnwi.Type[:], []byte("fldr")[:])
87 copy(fnwi.Creator[:], fileCreator[:])
88 } else {
89 // the Hotline protocol does not support file sizes > 4GiB due to the 4 byte field size, so skip them
90 if file.Size() > 4294967296 {
91 continue
92 }
93 binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(file.Size()))
94 copy(fnwi.Type[:], []byte(fileTypeFromFilename(file.Name()).TypeCode)[:])
95 copy(fnwi.Creator[:], []byte(fileTypeFromFilename(file.Name()).CreatorCode)[:])
96 }
97
98 strippedName := strings.Replace(file.Name(), ".incomplete", "", -1)
99
100 nameSize := make([]byte, 2)
101 binary.BigEndian.PutUint16(nameSize, uint16(len(strippedName)))
102 copy(fnwi.NameSize[:], nameSize[:])
103
104 fnwi.name = []byte(strippedName)
105
106 b, err := fnwi.MarshalBinary()
107 if err != nil {
108 return nil, err
109 }
110 fields = append(fields, NewField(fieldFileNameWithInfo, b))
111 }
112
113 return fields, nil
114 }
115
116 func CalcTotalSize(filePath string) ([]byte, error) {
117 var totalSize uint32
118 err := filepath.Walk(filePath, func(path string, info os.FileInfo, err error) error {
119 if err != nil {
120 return err
121 }
122
123 if info.IsDir() {
124 return nil
125 }
126
127 totalSize += uint32(info.Size())
128
129 return nil
130 })
131 if err != nil {
132 return nil, err
133 }
134
135 bs := make([]byte, 4)
136 binary.BigEndian.PutUint32(bs, totalSize)
137
138 return bs, nil
139 }
140
141 func CalcItemCount(filePath string) ([]byte, error) {
142 var itemcount uint16
143 err := filepath.Walk(filePath, func(path string, info os.FileInfo, err error) error {
144 itemcount += 1
145
146 if err != nil {
147 return err
148 }
149
150 return nil
151 })
152 if err != nil {
153 return nil, err
154 }
155
156 bs := make([]byte, 2)
157 binary.BigEndian.PutUint16(bs, itemcount-1)
158
159 return bs, nil
160 }
161
162 func EncodeFilePath(filePath string) []byte {
163 pathSections := strings.Split(filePath, "/")
164 pathItemCount := make([]byte, 2)
165 binary.BigEndian.PutUint16(pathItemCount, uint16(len(pathSections)))
166
167 bytes := pathItemCount
168
169 for _, section := range pathSections {
170 bytes = append(bytes, []byte{0, 0}...)
171
172 pathStr := []byte(section)
173 bytes = append(bytes, byte(len(pathStr)))
174 bytes = append(bytes, pathStr...)
175 }
176
177 return bytes
178 }
179
180 const incompleteFileSuffix = ".incomplete"
181
182 // effectiveFile wraps os.Open to check for the presence of a partial file transfer as a fallback
183 func effectiveFile(filePath string) (*os.File, error) {
184 file, err := os.Open(filePath)
185 if err != nil && !errors.Is(err, fs.ErrNotExist) {
186 return nil, err
187 }
188
189 if errors.Is(err, fs.ErrNotExist) {
190 file, err = os.OpenFile(filePath+incompleteFileSuffix, os.O_APPEND|os.O_WRONLY, 0644)
191 if err != nil {
192 return nil, err
193 }
194 }
195 return file, nil
196 }