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