]>
Commit | Line | Data |
---|---|---|
6988a057 JH |
1 | package hotline |
2 | ||
3 | import ( | |
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 |
13 | func 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 |
22 | func 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 |
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 | ||
c5d9af5a | 41 | func 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 { | |
72dd37f1 | 48 | var fnwi FileNameWithInfo |
d7548c16 | 49 | |
43ecc0f4 | 50 | fileCreator := make([]byte, 4) |
43ecc0f4 | 51 | |
d7548c16 JH |
52 | if file.Mode()&os.ModeSymlink != 0 { |
53 | resolvedPath, err := os.Readlink(filePath + "/" + file.Name()) | |
54 | if err != nil { | |
55 | return fields, err | |
56 | } | |
6988a057 | 57 | |
d7548c16 | 58 | rFile, err := os.Stat(filePath + "/" + resolvedPath) |
dd7aadfa JH |
59 | if errors.Is(err, os.ErrNotExist) { |
60 | continue | |
61 | } | |
d7548c16 JH |
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() { | |
6988a057 JH |
81 | dir, err := ioutil.ReadDir(filePath + "/" + file.Name()) |
82 | if err != nil { | |
83 | return fields, err | |
84 | } | |
72dd37f1 | 85 | binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(len(dir))) |
d7548c16 | 86 | copy(fnwi.Type[:], []byte("fldr")[:]) |
72dd37f1 | 87 | copy(fnwi.Creator[:], fileCreator[:]) |
d7548c16 JH |
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)[:]) | |
6988a057 JH |
92 | } |
93 | ||
16a4ad70 JH |
94 | strippedName := strings.Replace(file.Name(), ".incomplete", "", -1) |
95 | ||
72dd37f1 | 96 | nameSize := make([]byte, 2) |
16a4ad70 | 97 | binary.BigEndian.PutUint16(nameSize, uint16(len(strippedName))) |
72dd37f1 JH |
98 | copy(fnwi.NameSize[:], nameSize[:]) |
99 | ||
16a4ad70 | 100 | fnwi.name = []byte(strippedName) |
72dd37f1 JH |
101 | |
102 | b, err := fnwi.MarshalBinary() | |
103 | if err != nil { | |
104 | return nil, err | |
105 | } | |
106 | fields = append(fields, NewField(fieldFileNameWithInfo, b)) | |
6988a057 JH |
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 | } | |
16a4ad70 JH |
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) { | |
c26c20e3 | 186 | file, err = os.OpenFile(filePath+incompleteFileSuffix, os.O_APPEND|os.O_WRONLY, 0644) |
16a4ad70 JH |
187 | if err != nil { |
188 | return nil, err | |
189 | } | |
190 | } | |
191 | return file, nil | |
192 | } |