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