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