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