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