aboutsummaryrefslogtreecommitdiff
path: root/hotline/file_wrapper.go
diff options
context:
space:
mode:
authorRuben Beltran del Rio <git@r.bdr.sh>2025-11-28 00:39:17 +0100
committerRuben Beltran del Rio <git@r.bdr.sh>2025-11-28 00:39:17 +0100
commit9f67542d8469db45c823e347b1868b3582d9e5a7 (patch)
tree88741b3d8633758e4f6f5cbc292f338bc99602a0 /hotline/file_wrapper.go
parent8f9edf2f3bb18f7ab1a04ead182a1daf2cfd41d9 (diff)
parent8ddb9bb228389b198a76d6df21de005da4fad66b (diff)
Merge branch 'master' of https://github.com/jhalter/mobiusHEADmain
Diffstat (limited to 'hotline/file_wrapper.go')
-rw-r--r--hotline/file_wrapper.go54
1 files changed, 29 insertions, 25 deletions
diff --git a/hotline/file_wrapper.go b/hotline/file_wrapper.go
index b13e0dc..b80bc4b 100644
--- a/hotline/file_wrapper.go
+++ b/hotline/file_wrapper.go
@@ -17,8 +17,8 @@ const (
RsrcForkNameTemplate = ".rsrc_%s" // template string for resource fork filenames
)
-// fileWrapper encapsulates the data, info, and resource forks of a Hotline file and provides methods to manage the files.
-type fileWrapper struct {
+// File encapsulates the data, info, and resource forks of a Hotline file and provides methods to manage the files.
+type File struct {
fs FileStore
Name string // Name of the file
path string // path to file directory
@@ -30,10 +30,10 @@ type fileWrapper struct {
Ffo *flattenedFileObject
}
-func NewFileWrapper(fs FileStore, path string, dataOffset int64) (*fileWrapper, error) {
+func NewFile(fs FileStore, path string, dataOffset int64) (*File, error) {
dir := filepath.Dir(path)
fName := filepath.Base(path)
- f := fileWrapper{
+ f := File{
fs: fs,
Name: fName,
path: dir,
@@ -54,7 +54,7 @@ func NewFileWrapper(fs FileStore, path string, dataOffset int64) (*fileWrapper,
return &f, nil
}
-func (f *fileWrapper) TotalSize() []byte {
+func (f *File) TotalSize() []byte {
var s int64
size := make([]byte, 4)
@@ -73,7 +73,7 @@ func (f *fileWrapper) TotalSize() []byte {
return size
}
-func (f *fileWrapper) rsrcForkSize() (s [4]byte) {
+func (f *File) rsrcForkSize() (s [4]byte) {
info, err := f.fs.Stat(f.rsrcPath)
if err != nil {
return s
@@ -83,26 +83,26 @@ func (f *fileWrapper) rsrcForkSize() (s [4]byte) {
return s
}
-func (f *fileWrapper) rsrcForkHeader() FlatFileForkHeader {
+func (f *File) rsrcForkHeader() FlatFileForkHeader {
return FlatFileForkHeader{
- ForkType: [4]byte{0x4D, 0x41, 0x43, 0x52}, // "MACR"
+ ForkType: ForkTypeMACR,
DataSize: f.rsrcForkSize(),
}
}
-func (f *fileWrapper) incompleteDataName() string {
+func (f *File) incompleteDataName() string {
return f.Name + IncompleteFileSuffix
}
-func (f *fileWrapper) rsrcForkName() string {
+func (f *File) rsrcForkName() string {
return fmt.Sprintf(RsrcForkNameTemplate, f.Name)
}
-func (f *fileWrapper) infoForkName() string {
+func (f *File) infoForkName() string {
return fmt.Sprintf(InfoForkNameTemplate, f.Name)
}
-func (f *fileWrapper) rsrcForkWriter() (io.WriteCloser, error) {
+func (f *File) rsrcForkWriter() (io.WriteCloser, error) {
file, err := os.OpenFile(f.rsrcPath, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return nil, err
@@ -111,7 +111,7 @@ func (f *fileWrapper) rsrcForkWriter() (io.WriteCloser, error) {
return file, nil
}
-func (f *fileWrapper) InfoForkWriter() (io.WriteCloser, error) {
+func (f *File) InfoForkWriter() (io.WriteCloser, error) {
file, err := os.OpenFile(f.infoPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
return nil, err
@@ -120,7 +120,7 @@ func (f *fileWrapper) InfoForkWriter() (io.WriteCloser, error) {
return file, nil
}
-func (f *fileWrapper) incFileWriter() (io.WriteCloser, error) {
+func (f *File) incFileWriter() (io.WriteCloser, error) {
file, err := os.OpenFile(f.incompletePath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
return nil, err
@@ -129,15 +129,19 @@ func (f *fileWrapper) incFileWriter() (io.WriteCloser, error) {
return file, nil
}
-func (f *fileWrapper) dataForkReader() (io.Reader, error) {
- return f.fs.Open(f.dataPath)
+func (f *File) dataForkReader() (io.Reader, error) {
+ if _, err := f.fs.Stat(f.dataPath); err == nil {
+ return f.fs.Open(f.dataPath)
+ }
+
+ return f.fs.Open(f.incompletePath)
}
-func (f *fileWrapper) rsrcForkFile() (*os.File, error) {
+func (f *File) rsrcForkFile() (*os.File, error) {
return f.fs.Open(f.rsrcPath)
}
-func (f *fileWrapper) DataFile() (os.FileInfo, error) {
+func (f *File) DataFile() (os.FileInfo, error) {
if fi, err := f.fs.Stat(f.dataPath); err == nil {
return fi, nil
}
@@ -154,7 +158,7 @@ func (f *fileWrapper) DataFile() (os.FileInfo, error) {
// * Resource fork starting with .rsrc_
// * Info fork starting with .info
// During Move of the meta files, os.ErrNotExist is ignored as these files may legitimately not exist.
-func (f *fileWrapper) Move(newPath string) error {
+func (f *File) Move(newPath string) error {
err := f.fs.Rename(f.dataPath, filepath.Join(newPath, f.Name))
if err != nil {
return err
@@ -179,7 +183,7 @@ func (f *fileWrapper) Move(newPath string) error {
}
// Delete a file and its associated metadata files if they exist
-func (f *fileWrapper) Delete() error {
+func (f *File) Delete() error {
err := f.fs.RemoveAll(f.dataPath)
if err != nil {
return err
@@ -203,7 +207,7 @@ func (f *fileWrapper) Delete() error {
return nil
}
-func (f *fileWrapper) flattenedFileObject() (*flattenedFileObject, error) {
+func (f *File) flattenedFileObject() (*flattenedFileObject, error) {
dataSize := make([]byte, 4)
mTime := [8]byte{}
@@ -227,7 +231,7 @@ func (f *fileWrapper) flattenedFileObject() (*flattenedFileObject, error) {
}
f.Ffo.FlatFileHeader = FlatFileHeader{
- Format: [4]byte{0x46, 0x49, 0x4c, 0x50}, // "FILP"
+ Format: FormatFILP,
Version: [2]byte{0, 1},
ForkCount: [2]byte{0, 2},
}
@@ -247,7 +251,7 @@ func (f *fileWrapper) flattenedFileObject() (*flattenedFileObject, error) {
}
} else {
f.Ffo.FlatFileInformationFork = FlatFileInformationFork{
- Platform: [4]byte{0x41, 0x4D, 0x41, 0x43}, // "AMAC" TODO: Remove hardcode to support "AWIN" Platform (maybe?)
+ Platform: PlatformAMAC, // TODO: Remove hardcode to support "MWIN" Platform (maybe?)
TypeSignature: [4]byte([]byte(ft.TypeCode)),
CreatorSignature: [4]byte([]byte(ft.CreatorCode)),
PlatformFlags: [4]byte{0, 0, 1, 0}, // TODO: What is this?
@@ -263,12 +267,12 @@ func (f *fileWrapper) flattenedFileObject() (*flattenedFileObject, error) {
}
f.Ffo.FlatFileInformationForkHeader = FlatFileForkHeader{
- ForkType: [4]byte{0x49, 0x4E, 0x46, 0x4F}, // "INFO"
+ ForkType: ForkTypeINFO,
DataSize: f.Ffo.FlatFileInformationFork.Size(),
}
f.Ffo.FlatFileDataForkHeader = FlatFileForkHeader{
- ForkType: [4]byte{0x44, 0x41, 0x54, 0x41}, // "DATA"
+ ForkType: ForkTypeDATA,
DataSize: [4]byte{dataSize[0], dataSize[1], dataSize[2], dataSize[3]},
}
f.Ffo.FlatFileResForkHeader = f.rsrcForkHeader()