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