15 IncompleteFileSuffix = ".incomplete"
16 InfoForkNameTemplate = ".info_%s" // template string for info fork filenames
17 RsrcForkNameTemplate = ".rsrc_%s" // template string for resource fork filenames
20 // fileWrapper encapsulates the data, info, and resource forks of a Hotline file and provides methods to manage the files.
21 type fileWrapper struct {
23 Name string // Name of the file
24 path string // path to file directory
25 dataPath string // path to the file data fork
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 Ffo *flattenedFileObject
33 func NewFileWrapper(fs FileStore, path string, dataOffset int64) (*fileWrapper, error) {
34 dir := filepath.Dir(path)
35 fName := filepath.Base(path)
41 dataOffset: dataOffset,
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{},
49 f.Ffo, err = f.flattenedFileObject()
57 func (f *fileWrapper) TotalSize() []byte {
59 size := make([]byte, 4)
61 info, err := f.fs.Stat(f.dataPath)
63 s += info.Size() - f.dataOffset
66 info, err = f.fs.Stat(f.rsrcPath)
71 binary.BigEndian.PutUint32(size, uint32(s))
76 func (f *fileWrapper) rsrcForkSize() (s [4]byte) {
77 info, err := f.fs.Stat(f.rsrcPath)
82 binary.BigEndian.PutUint32(s[:], uint32(info.Size()))
86 func (f *fileWrapper) rsrcForkHeader() FlatFileForkHeader {
87 return FlatFileForkHeader{
88 ForkType: [4]byte{0x4D, 0x41, 0x43, 0x52}, // "MACR"
89 DataSize: f.rsrcForkSize(),
93 func (f *fileWrapper) incompleteDataName() string {
94 return f.Name + IncompleteFileSuffix
97 func (f *fileWrapper) rsrcForkName() string {
98 return fmt.Sprintf(RsrcForkNameTemplate, f.Name)
101 func (f *fileWrapper) infoForkName() string {
102 return fmt.Sprintf(InfoForkNameTemplate, f.Name)
105 func (f *fileWrapper) rsrcForkWriter() (io.WriteCloser, error) {
106 file, err := os.OpenFile(f.rsrcPath, os.O_CREATE|os.O_WRONLY, 0644)
114 func (f *fileWrapper) InfoForkWriter() (io.WriteCloser, error) {
115 file, err := os.OpenFile(f.infoPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
123 func (f *fileWrapper) incFileWriter() (io.WriteCloser, error) {
124 file, err := os.OpenFile(f.incompletePath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
132 func (f *fileWrapper) dataForkReader() (io.Reader, error) {
133 return f.fs.Open(f.dataPath)
136 func (f *fileWrapper) rsrcForkFile() (*os.File, error) {
137 return f.fs.Open(f.rsrcPath)
140 func (f *fileWrapper) DataFile() (os.FileInfo, error) {
141 if fi, err := f.fs.Stat(f.dataPath); err == nil {
144 if fi, err := f.fs.Stat(f.incompletePath); err == nil {
148 return nil, errors.New("file or directory not found")
151 // Move a file and its associated meta files to newPath.
152 // Meta files include:
153 // * Partially uploaded file ending with .incomplete
154 // * Resource fork starting with .rsrc_
155 // * Info fork starting with .info
156 // During Move of the meta files, os.ErrNotExist is ignored as these files may legitimately not exist.
157 func (f *fileWrapper) Move(newPath string) error {
158 err := f.fs.Rename(f.dataPath, filepath.Join(newPath, f.Name))
163 err = f.fs.Rename(f.incompletePath, filepath.Join(newPath, f.incompleteDataName()))
164 if err != nil && !errors.Is(err, os.ErrNotExist) {
168 err = f.fs.Rename(f.rsrcPath, filepath.Join(newPath, f.rsrcForkName()))
169 if err != nil && !errors.Is(err, os.ErrNotExist) {
173 err = f.fs.Rename(f.infoPath, filepath.Join(newPath, f.infoForkName()))
174 if err != nil && !errors.Is(err, os.ErrNotExist) {
181 // Delete a file and its associated metadata files if they exist
182 func (f *fileWrapper) Delete() error {
183 err := f.fs.RemoveAll(f.dataPath)
188 err = f.fs.Remove(f.incompletePath)
189 if err != nil && !errors.Is(err, os.ErrNotExist) {
193 err = f.fs.Remove(f.rsrcPath)
194 if err != nil && !errors.Is(err, os.ErrNotExist) {
198 err = f.fs.Remove(f.infoPath)
199 if err != nil && !errors.Is(err, os.ErrNotExist) {
206 func (f *fileWrapper) flattenedFileObject() (*flattenedFileObject, error) {
207 dataSize := make([]byte, 4)
210 ft := defaultFileType
212 fileInfo, err := f.fs.Stat(f.dataPath)
213 if err != nil && !errors.Is(err, fs.ErrNotExist) {
216 if errors.Is(err, fs.ErrNotExist) {
217 fileInfo, err = f.fs.Stat(f.incompletePath)
219 mTime = NewTime(fileInfo.ModTime())
220 binary.BigEndian.PutUint32(dataSize, uint32(fileInfo.Size()-f.dataOffset))
221 ft, _ = fileTypeFromInfo(fileInfo)
224 mTime = NewTime(fileInfo.ModTime())
225 binary.BigEndian.PutUint32(dataSize, uint32(fileInfo.Size()-f.dataOffset))
226 ft, _ = fileTypeFromInfo(fileInfo)
229 f.Ffo.FlatFileHeader = FlatFileHeader{
230 Format: [4]byte{0x46, 0x49, 0x4c, 0x50}, // "FILP"
231 Version: [2]byte{0, 1},
232 ForkCount: [2]byte{0, 2},
235 _, err = f.fs.Stat(f.infoPath)
237 b, err := f.fs.ReadFile(f.infoPath)
242 f.Ffo.FlatFileHeader.ForkCount[1] = 3
244 _, err = io.Copy(&f.Ffo.FlatFileInformationFork, bytes.NewReader(b))
246 return nil, fmt.Errorf("error copying FlatFileInformationFork: %w", err)
249 f.Ffo.FlatFileInformationFork = FlatFileInformationFork{
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
256 Name: []byte(f.Name),
260 ns := make([]byte, 2)
261 binary.BigEndian.PutUint16(ns, uint16(len(f.Name)))
262 f.Ffo.FlatFileInformationFork.NameSize = [2]byte(ns[:])
265 f.Ffo.FlatFileInformationForkHeader = FlatFileForkHeader{
266 ForkType: [4]byte{0x49, 0x4E, 0x46, 0x4F}, // "INFO"
267 DataSize: f.Ffo.FlatFileInformationFork.Size(),
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]},
274 f.Ffo.FlatFileResForkHeader = f.rsrcForkHeader()