]> git.r.bdr.sh - rbdr/mobius/blame - hotline/file_wrapper.go
Allow for personal ~ folder
[rbdr/mobius] / hotline / file_wrapper.go
CommitLineData
7cd900d6
JH
1package hotline
2
3import (
9cf66aea 4 "bytes"
7cd900d6
JH
5 "encoding/binary"
6 "errors"
7 "fmt"
8 "io"
9 "io/fs"
10 "os"
f22acf38 11 "path/filepath"
7cd900d6
JH
12)
13
14const (
fd740bc4
JH
15 IncompleteFileSuffix = ".incomplete"
16 InfoForkNameTemplate = ".info_%s" // template string for info fork filenames
17 RsrcForkNameTemplate = ".rsrc_%s" // template string for resource fork filenames
7cd900d6
JH
18)
19
20// fileWrapper encapsulates the data, info, and resource forks of a Hotline file and provides methods to manage the files.
21type fileWrapper struct {
22 fs FileStore
fd740bc4 23 Name string // Name of the file
7cd900d6
JH
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
fd740bc4 30 Ffo *flattenedFileObject
7cd900d6
JH
31}
32
fd740bc4 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,
fd740bc4 38 Name: fName,
7cd900d6
JH
39 path: dir,
40 dataPath: path,
41 dataOffset: dataOffset,
fd740bc4
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),
45 Ffo: &flattenedFileObject{},
7cd900d6
JH
46 }
47
48 var err error
fd740bc4 49 f.Ffo, err = f.flattenedFileObject()
7cd900d6
JH
50 if err != nil {
51 return nil, err
52 }
53
54 return &f, nil
55}
56
fd740bc4 57func (f *fileWrapper) TotalSize() []byte {
7cd900d6
JH
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{
fd740bc4
JH
88 ForkType: [4]byte{0x4D, 0x41, 0x43, 0x52}, // "MACR"
89 DataSize: f.rsrcForkSize(),
7cd900d6
JH
90 }
91}
92
93func (f *fileWrapper) incompleteDataName() string {
fd740bc4 94 return f.Name + IncompleteFileSuffix
7cd900d6
JH
95}
96
97func (f *fileWrapper) rsrcForkName() string {
fd740bc4 98 return fmt.Sprintf(RsrcForkNameTemplate, f.Name)
7cd900d6
JH
99}
100
101func (f *fileWrapper) infoForkName() string {
fd740bc4 102 return fmt.Sprintf(InfoForkNameTemplate, f.Name)
7cd900d6
JH
103}
104
df1ade54 105func (f *fileWrapper) rsrcForkWriter() (io.WriteCloser, error) {
7cd900d6
JH
106 file, err := os.OpenFile(f.rsrcPath, os.O_CREATE|os.O_WRONLY, 0644)
107 if err != nil {
108 return nil, err
109 }
110
111 return file, nil
112}
113
fd740bc4 114func (f *fileWrapper) InfoForkWriter() (io.WriteCloser, error) {
3e204024 115 file, err := os.OpenFile(f.infoPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
7cd900d6
JH
116 if err != nil {
117 return nil, err
118 }
119
120 return file, nil
121}
122
df1ade54 123func (f *fileWrapper) incFileWriter() (io.WriteCloser, error) {
7cd900d6
JH
124 file, err := os.OpenFile(f.incompletePath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
125 if err != nil {
126 return nil, err
127 }
128
129 return file, nil
130}
131
132func (f *fileWrapper) dataForkReader() (io.Reader, error) {
133 return f.fs.Open(f.dataPath)
134}
135
136func (f *fileWrapper) rsrcForkFile() (*os.File, error) {
137 return f.fs.Open(f.rsrcPath)
138}
139
fd740bc4 140func (f *fileWrapper) DataFile() (os.FileInfo, error) {
7cd900d6
JH
141 if fi, err := f.fs.Stat(f.dataPath); err == nil {
142 return fi, nil
143 }
144 if fi, err := f.fs.Stat(f.incompletePath); err == nil {
145 return fi, nil
146 }
147
148 return nil, errors.New("file or directory not found")
149}
150
fd740bc4 151// Move a file and its associated meta files to newPath.
a8e89ca4
JH
152// Meta files include:
153// * Partially uploaded file ending with .incomplete
154// * Resource fork starting with .rsrc_
155// * Info fork starting with .info
fd740bc4
JH
156// During Move of the meta files, os.ErrNotExist is ignored as these files may legitimately not exist.
157func (f *fileWrapper) Move(newPath string) error {
158 err := f.fs.Rename(f.dataPath, filepath.Join(newPath, f.Name))
7cd900d6 159 if err != nil {
a8e89ca4 160 return err
7cd900d6
JH
161 }
162
46b48603 163 err = f.fs.Rename(f.incompletePath, filepath.Join(newPath, f.incompleteDataName()))
a8e89ca4
JH
164 if err != nil && !errors.Is(err, os.ErrNotExist) {
165 return err
7cd900d6
JH
166 }
167
46b48603 168 err = f.fs.Rename(f.rsrcPath, filepath.Join(newPath, f.rsrcForkName()))
a8e89ca4
JH
169 if err != nil && !errors.Is(err, os.ErrNotExist) {
170 return err
7cd900d6
JH
171 }
172
46b48603 173 err = f.fs.Rename(f.infoPath, filepath.Join(newPath, f.infoForkName()))
a8e89ca4
JH
174 if err != nil && !errors.Is(err, os.ErrNotExist) {
175 return err
7cd900d6
JH
176 }
177
178 return nil
179}
180
0ee4d86e 181// Delete a file and its associated metadata files if they exist
fd740bc4 182func (f *fileWrapper) Delete() error {
7cd900d6
JH
183 err := f.fs.RemoveAll(f.dataPath)
184 if err != nil {
a8e89ca4 185 return err
7cd900d6
JH
186 }
187
188 err = f.fs.Remove(f.incompletePath)
a8e89ca4
JH
189 if err != nil && !errors.Is(err, os.ErrNotExist) {
190 return err
7cd900d6
JH
191 }
192
193 err = f.fs.Remove(f.rsrcPath)
a8e89ca4
JH
194 if err != nil && !errors.Is(err, os.ErrNotExist) {
195 return err
7cd900d6
JH
196 }
197
198 err = f.fs.Remove(f.infoPath)
a8e89ca4
JH
199 if err != nil && !errors.Is(err, os.ErrNotExist) {
200 return err
7cd900d6
JH
201 }
202
203 return nil
204}
205
206func (f *fileWrapper) flattenedFileObject() (*flattenedFileObject, error) {
207 dataSize := make([]byte, 4)
95159e55 208 mTime := [8]byte{}
7cd900d6
JH
209
210 ft := defaultFileType
211
212 fileInfo, err := f.fs.Stat(f.dataPath)
213 if err != nil && !errors.Is(err, fs.ErrNotExist) {
214 return nil, err
215 }
216 if errors.Is(err, fs.ErrNotExist) {
217 fileInfo, err = f.fs.Stat(f.incompletePath)
218 if err == nil {
fd740bc4 219 mTime = NewTime(fileInfo.ModTime())
7cd900d6
JH
220 binary.BigEndian.PutUint32(dataSize, uint32(fileInfo.Size()-f.dataOffset))
221 ft, _ = fileTypeFromInfo(fileInfo)
222 }
223 } else {
fd740bc4 224 mTime = NewTime(fileInfo.ModTime())
7cd900d6
JH
225 binary.BigEndian.PutUint32(dataSize, uint32(fileInfo.Size()-f.dataOffset))
226 ft, _ = fileTypeFromInfo(fileInfo)
227 }
228
fd740bc4 229 f.Ffo.FlatFileHeader = FlatFileHeader{
7cd900d6
JH
230 Format: [4]byte{0x46, 0x49, 0x4c, 0x50}, // "FILP"
231 Version: [2]byte{0, 1},
7cd900d6
JH
232 ForkCount: [2]byte{0, 2},
233 }
234
235 _, err = f.fs.Stat(f.infoPath)
236 if err == nil {
237 b, err := f.fs.ReadFile(f.infoPath)
238 if err != nil {
239 return nil, err
240 }
241
fd740bc4 242 f.Ffo.FlatFileHeader.ForkCount[1] = 3
7cd900d6 243
fd740bc4 244 _, err = io.Copy(&f.Ffo.FlatFileInformationFork, bytes.NewReader(b))
9cf66aea 245 if err != nil {
45ca5d60 246 return nil, fmt.Errorf("error copying FlatFileInformationFork: %w", err)
7cd900d6
JH
247 }
248 } else {
fd740bc4 249 f.Ffo.FlatFileInformationFork = FlatFileInformationFork{
a55350da
JH
250 Platform: [4]byte{0x41, 0x4D, 0x41, 0x43}, // "AMAC" TODO: Remove hardcode to support "AWIN" Platform (maybe?)
251 TypeSignature: [4]byte([]byte(ft.TypeCode)),
252 CreatorSignature: [4]byte([]byte(ft.CreatorCode)),
253 PlatformFlags: [4]byte{0, 0, 1, 0}, // TODO: What is this?
254 CreateDate: mTime, // some filesystems don't support createTime
255 ModifyDate: mTime,
fd740bc4 256 Name: []byte(f.Name),
7cd900d6
JH
257 Comment: []byte{},
258 }
a55350da
JH
259
260 ns := make([]byte, 2)
fd740bc4
JH
261 binary.BigEndian.PutUint16(ns, uint16(len(f.Name)))
262 f.Ffo.FlatFileInformationFork.NameSize = [2]byte(ns[:])
7cd900d6
JH
263 }
264
fd740bc4
JH
265 f.Ffo.FlatFileInformationForkHeader = FlatFileForkHeader{
266 ForkType: [4]byte{0x49, 0x4E, 0x46, 0x4F}, // "INFO"
267 DataSize: f.Ffo.FlatFileInformationFork.Size(),
7cd900d6
JH
268 }
269
fd740bc4
JH
270 f.Ffo.FlatFileDataForkHeader = FlatFileForkHeader{
271 ForkType: [4]byte{0x44, 0x41, 0x54, 0x41}, // "DATA"
272 DataSize: [4]byte{dataSize[0], dataSize[1], dataSize[2], dataSize[3]},
7cd900d6 273 }
fd740bc4 274 f.Ffo.FlatFileResForkHeader = f.rsrcForkHeader()
7cd900d6 275
fd740bc4 276 return f.Ffo, nil
7cd900d6 277}