]> git.r.bdr.sh - rbdr/mobius/blame - hotline/file_wrapper.go
Improve third party client compatability
[rbdr/mobius] / hotline / file_wrapper.go
CommitLineData
7cd900d6
JH
1package hotline
2
3import (
4 "encoding/binary"
5 "errors"
6 "fmt"
7 "io"
8 "io/fs"
9 "os"
f22acf38 10 "path/filepath"
7cd900d6
JH
11)
12
13const (
14 incompleteFileSuffix = ".incomplete"
f22acf38
JH
15 infoForkNameTemplate = ".info_%s" // template string for info fork filenames
16 rsrcForkNameTemplate = ".rsrc_%s" // template string for resource fork filenames
7cd900d6
JH
17)
18
19// fileWrapper encapsulates the data, info, and resource forks of a Hotline file and provides methods to manage the files.
20type fileWrapper struct {
21 fs FileStore
22 name string // name of the file
23 path string // path to file directory
24 dataPath string // path to the file data fork
25 dataOffset int64
26 rsrcPath string // path to the file resource fork
27 infoPath string // path to the file information fork
28 incompletePath string // path to partially transferred temp file
7cd900d6
JH
29 infoFork *FlatFileInformationFork
30 ffo *flattenedFileObject
31}
32
33func newFileWrapper(fs FileStore, path string, dataOffset int64) (*fileWrapper, error) {
f22acf38
JH
34 dir := filepath.Dir(path)
35 fName := filepath.Base(path)
7cd900d6
JH
36 f := fileWrapper{
37 fs: fs,
38 name: fName,
39 path: dir,
40 dataPath: path,
41 dataOffset: dataOffset,
f22acf38
JH
42 rsrcPath: filepath.Join(dir, fmt.Sprintf(rsrcForkNameTemplate, fName)),
43 infoPath: filepath.Join(dir, fmt.Sprintf(infoForkNameTemplate, fName)),
44 incompletePath: filepath.Join(dir, fName+incompleteFileSuffix),
7cd900d6
JH
45 ffo: &flattenedFileObject{},
46 }
47
48 var err error
49 f.ffo, err = f.flattenedFileObject()
50 if err != nil {
51 return nil, err
52 }
53
54 return &f, nil
55}
56
57func (f *fileWrapper) totalSize() []byte {
58 var s int64
59 size := make([]byte, 4)
60
61 info, err := f.fs.Stat(f.dataPath)
62 if err == nil {
63 s += info.Size() - f.dataOffset
64 }
65
66 info, err = f.fs.Stat(f.rsrcPath)
67 if err == nil {
68 s += info.Size()
69 }
70
71 binary.BigEndian.PutUint32(size, uint32(s))
72
73 return size
74}
75
76func (f *fileWrapper) rsrcForkSize() (s [4]byte) {
77 info, err := f.fs.Stat(f.rsrcPath)
78 if err != nil {
79 return s
80 }
81
82 binary.BigEndian.PutUint32(s[:], uint32(info.Size()))
83 return s
84}
85
86func (f *fileWrapper) rsrcForkHeader() FlatFileForkHeader {
87 return FlatFileForkHeader{
88 ForkType: [4]byte{0x4D, 0x41, 0x43, 0x52}, // "MACR"
89 CompressionType: [4]byte{},
90 RSVD: [4]byte{},
91 DataSize: f.rsrcForkSize(),
92 }
93}
94
95func (f *fileWrapper) incompleteDataName() string {
96 return f.name + incompleteFileSuffix
97}
98
99func (f *fileWrapper) rsrcForkName() string {
f22acf38 100 return fmt.Sprintf(rsrcForkNameTemplate, f.name)
7cd900d6
JH
101}
102
103func (f *fileWrapper) infoForkName() string {
f22acf38 104 return fmt.Sprintf(infoForkNameTemplate, f.name)
7cd900d6
JH
105}
106
df1ade54 107func (f *fileWrapper) rsrcForkWriter() (io.WriteCloser, error) {
7cd900d6
JH
108 file, err := os.OpenFile(f.rsrcPath, os.O_CREATE|os.O_WRONLY, 0644)
109 if err != nil {
110 return nil, err
111 }
112
113 return file, nil
114}
115
df1ade54 116func (f *fileWrapper) infoForkWriter() (io.WriteCloser, error) {
7cd900d6
JH
117 file, err := os.OpenFile(f.infoPath, os.O_CREATE|os.O_WRONLY, 0644)
118 if err != nil {
119 return nil, err
120 }
121
122 return file, nil
123}
124
df1ade54 125func (f *fileWrapper) incFileWriter() (io.WriteCloser, error) {
7cd900d6
JH
126 file, err := os.OpenFile(f.incompletePath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
127 if err != nil {
128 return nil, err
129 }
130
131 return file, nil
132}
133
134func (f *fileWrapper) dataForkReader() (io.Reader, error) {
135 return f.fs.Open(f.dataPath)
136}
137
138func (f *fileWrapper) rsrcForkFile() (*os.File, error) {
139 return f.fs.Open(f.rsrcPath)
140}
141
142func (f *fileWrapper) dataFile() (os.FileInfo, error) {
143 if fi, err := f.fs.Stat(f.dataPath); err == nil {
144 return fi, nil
145 }
146 if fi, err := f.fs.Stat(f.incompletePath); err == nil {
147 return fi, nil
148 }
149
150 return nil, errors.New("file or directory not found")
151}
152
153// move a fileWrapper and its associated metadata files to newPath
154func (f *fileWrapper) move(newPath string) error {
46b48603 155 err := f.fs.Rename(f.dataPath, filepath.Join(newPath, f.name))
7cd900d6
JH
156 if err != nil {
157 // TODO
158 }
159
46b48603 160 err = f.fs.Rename(f.incompletePath, filepath.Join(newPath, f.incompleteDataName()))
7cd900d6
JH
161 if err != nil {
162 // TODO
163 }
164
46b48603 165 err = f.fs.Rename(f.rsrcPath, filepath.Join(newPath, f.rsrcForkName()))
7cd900d6
JH
166 if err != nil {
167 // TODO
168 }
169
46b48603 170 err = f.fs.Rename(f.infoPath, filepath.Join(newPath, f.infoForkName()))
7cd900d6
JH
171 if err != nil {
172 // TODO
173 }
174
175 return nil
176}
177
178// delete a fileWrapper and its associated metadata files if they exist
179func (f *fileWrapper) delete() error {
180 err := f.fs.RemoveAll(f.dataPath)
181 if err != nil {
182 // TODO
183 }
184
185 err = f.fs.Remove(f.incompletePath)
186 if err != nil {
187 // TODO
188 }
189
190 err = f.fs.Remove(f.rsrcPath)
191 if err != nil {
192 // TODO
193 }
194
195 err = f.fs.Remove(f.infoPath)
196 if err != nil {
197 // TODO
198 }
199
200 return nil
201}
202
203func (f *fileWrapper) flattenedFileObject() (*flattenedFileObject, error) {
204 dataSize := make([]byte, 4)
205 mTime := make([]byte, 8)
206
207 ft := defaultFileType
208
209 fileInfo, err := f.fs.Stat(f.dataPath)
210 if err != nil && !errors.Is(err, fs.ErrNotExist) {
211 return nil, err
212 }
213 if errors.Is(err, fs.ErrNotExist) {
214 fileInfo, err = f.fs.Stat(f.incompletePath)
215 if err == nil {
216 mTime = toHotlineTime(fileInfo.ModTime())
217 binary.BigEndian.PutUint32(dataSize, uint32(fileInfo.Size()-f.dataOffset))
218 ft, _ = fileTypeFromInfo(fileInfo)
219 }
220 } else {
221 mTime = toHotlineTime(fileInfo.ModTime())
222 binary.BigEndian.PutUint32(dataSize, uint32(fileInfo.Size()-f.dataOffset))
223 ft, _ = fileTypeFromInfo(fileInfo)
224 }
225
226 f.ffo.FlatFileHeader = FlatFileHeader{
227 Format: [4]byte{0x46, 0x49, 0x4c, 0x50}, // "FILP"
228 Version: [2]byte{0, 1},
229 RSVD: [16]byte{},
230 ForkCount: [2]byte{0, 2},
231 }
232
233 _, err = f.fs.Stat(f.infoPath)
234 if err == nil {
235 b, err := f.fs.ReadFile(f.infoPath)
236 if err != nil {
237 return nil, err
238 }
239
240 f.ffo.FlatFileHeader.ForkCount[1] = 3
241
242 if err := f.ffo.FlatFileInformationFork.UnmarshalBinary(b); err != nil {
243 return nil, err
244 }
245 } else {
246 f.ffo.FlatFileInformationFork = FlatFileInformationFork{
247 Platform: []byte("AMAC"), // TODO: Remove hardcode to support "AWIN" Platform (maybe?)
248 TypeSignature: []byte(ft.TypeCode),
249 CreatorSignature: []byte(ft.CreatorCode),
250 Flags: []byte{0, 0, 0, 0},
251 PlatformFlags: []byte{0, 0, 1, 0}, // TODO: What is this?
252 RSVD: make([]byte, 32),
253 CreateDate: mTime, // some filesystems don't support createTime
254 ModifyDate: mTime,
255 NameScript: []byte{0, 0},
256 Name: []byte(f.name),
257 NameSize: []byte{0, 0},
258 CommentSize: []byte{0, 0},
259 Comment: []byte{},
260 }
261 binary.BigEndian.PutUint16(f.ffo.FlatFileInformationFork.NameSize, uint16(len(f.name)))
262 }
263
264 f.ffo.FlatFileInformationForkHeader = FlatFileForkHeader{
265 ForkType: [4]byte{0x49, 0x4E, 0x46, 0x4F}, // "INFO"
266 CompressionType: [4]byte{},
267 RSVD: [4]byte{},
268 DataSize: f.ffo.FlatFileInformationFork.Size(),
269 }
270
271 f.ffo.FlatFileDataForkHeader = FlatFileForkHeader{
272 ForkType: [4]byte{0x44, 0x41, 0x54, 0x41}, // "DATA"
273 CompressionType: [4]byte{},
274 RSVD: [4]byte{},
275 DataSize: [4]byte{dataSize[0], dataSize[1], dataSize[2], dataSize[3]},
276 }
277 f.ffo.FlatFileResForkHeader = f.rsrcForkHeader()
278
279 return f.ffo, nil
280}