]> git.r.bdr.sh - rbdr/mobius/blob - hotline/files.go
Follow symlinks in Files dir
[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 err != nil {
60 return fields, err
61 }
62
63 if rFile.IsDir() {
64 dir, err := ioutil.ReadDir(filePath + "/" + file.Name())
65 if err != nil {
66 return fields, err
67 }
68 binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(len(dir)))
69 copy(fnwi.Type[:], []byte("fldr")[:])
70 copy(fnwi.Creator[:], fileCreator[:])
71 } else {
72 binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(rFile.Size()))
73 copy(fnwi.Type[:], []byte(fileTypeFromFilename(rFile.Name()).TypeCode)[:])
74 copy(fnwi.Creator[:], []byte(fileTypeFromFilename(rFile.Name()).CreatorCode)[:])
75 }
76
77 } else if file.IsDir() {
78 dir, err := ioutil.ReadDir(filePath + "/" + file.Name())
79 if err != nil {
80 return fields, err
81 }
82 binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(len(dir)))
83 copy(fnwi.Type[:], []byte("fldr")[:])
84 copy(fnwi.Creator[:], fileCreator[:])
85 } else {
86 binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(file.Size()))
87 copy(fnwi.Type[:], []byte(fileTypeFromFilename(file.Name()).TypeCode)[:])
88 copy(fnwi.Creator[:], []byte(fileTypeFromFilename(file.Name()).CreatorCode)[:])
89 }
90
91 strippedName := strings.Replace(file.Name(), ".incomplete", "", -1)
92
93 nameSize := make([]byte, 2)
94 binary.BigEndian.PutUint16(nameSize, uint16(len(strippedName)))
95 copy(fnwi.NameSize[:], nameSize[:])
96
97 fnwi.name = []byte(strippedName)
98
99 b, err := fnwi.MarshalBinary()
100 if err != nil {
101 return nil, err
102 }
103 fields = append(fields, NewField(fieldFileNameWithInfo, b))
104 }
105
106 return fields, nil
107 }
108
109 func CalcTotalSize(filePath string) ([]byte, error) {
110 var totalSize uint32
111 err := filepath.Walk(filePath, func(path string, info os.FileInfo, err error) error {
112 if err != nil {
113 return err
114 }
115
116 if info.IsDir() {
117 return nil
118 }
119
120 totalSize += uint32(info.Size())
121
122 return nil
123 })
124 if err != nil {
125 return nil, err
126 }
127
128 bs := make([]byte, 4)
129 binary.BigEndian.PutUint32(bs, totalSize)
130
131 return bs, nil
132 }
133
134 func CalcItemCount(filePath string) ([]byte, error) {
135 var itemcount uint16
136 err := filepath.Walk(filePath, func(path string, info os.FileInfo, err error) error {
137 itemcount += 1
138
139 if err != nil {
140 return err
141 }
142
143 return nil
144 })
145 if err != nil {
146 return nil, err
147 }
148
149 bs := make([]byte, 2)
150 binary.BigEndian.PutUint16(bs, itemcount-1)
151
152 return bs, nil
153 }
154
155 func EncodeFilePath(filePath string) []byte {
156 pathSections := strings.Split(filePath, "/")
157 pathItemCount := make([]byte, 2)
158 binary.BigEndian.PutUint16(pathItemCount, uint16(len(pathSections)))
159
160 bytes := pathItemCount
161
162 for _, section := range pathSections {
163 bytes = append(bytes, []byte{0, 0}...)
164
165 pathStr := []byte(section)
166 bytes = append(bytes, byte(len(pathStr)))
167 bytes = append(bytes, pathStr...)
168 }
169
170 return bytes
171 }
172
173 const incompleteFileSuffix = ".incomplete"
174
175 // effectiveFile wraps os.Open to check for the presence of a partial file transfer as a fallback
176 func effectiveFile(filePath string) (*os.File, error) {
177 file, err := os.Open(filePath)
178 if err != nil && !errors.Is(err, fs.ErrNotExist) {
179 return nil, err
180 }
181
182 if errors.Is(err, fs.ErrNotExist) {
183 file, err = os.OpenFile(filePath+incompleteFileSuffix, os.O_APPEND|os.O_WRONLY, 0644)
184 if err != nil {
185 return nil, err
186 }
187 }
188 return file, nil
189 }