]> git.r.bdr.sh - rbdr/mobius/blobdiff - hotline/file_wrapper.go
build(deps): bump golang.org/x/crypto (#110)
[rbdr/mobius] / hotline / file_wrapper.go
index 3773e79812204343e24e6e90958517b085218895..2c796ca05b2254c64728b74f2dca7f2ddcd1f6da 100644 (file)
@@ -7,14 +7,13 @@ import (
        "io"
        "io/fs"
        "os"
        "io"
        "io/fs"
        "os"
-       "path"
-       "strings"
+       "path/filepath"
 )
 
 const (
        incompleteFileSuffix = ".incomplete"
 )
 
 const (
        incompleteFileSuffix = ".incomplete"
-       infoForkNameTemplate = "%s.info_%s" // template string for info fork filenames
-       rsrcForkNameTemplate = "%s.rsrc_%s" // template string for resource fork filenames
+       infoForkNameTemplate = ".info_%s" // template string for info fork filenames
+       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.
 )
 
 // fileWrapper encapsulates the data, info, and resource forks of a Hotline file and provides methods to manage the files.
@@ -27,24 +26,21 @@ type fileWrapper struct {
        rsrcPath       string // path to the file resource fork
        infoPath       string // path to the file information fork
        incompletePath string // path to partially transferred temp file
        rsrcPath       string // path to the file resource fork
        infoPath       string // path to the file information fork
        incompletePath string // path to partially transferred temp file
-       saveMetaData   bool   // if true, enables saving of info and resource forks in sidecar files
-       infoFork       *FlatFileInformationFork
        ffo            *flattenedFileObject
 }
 
 func newFileWrapper(fs FileStore, path string, dataOffset int64) (*fileWrapper, error) {
        ffo            *flattenedFileObject
 }
 
 func newFileWrapper(fs FileStore, path string, dataOffset int64) (*fileWrapper, error) {
-       pathSegs := strings.Split(path, pathSeparator)
-       dir := strings.Join(pathSegs[:len(pathSegs)-1], pathSeparator)
-       fName := pathSegs[len(pathSegs)-1]
+       dir := filepath.Dir(path)
+       fName := filepath.Base(path)
        f := fileWrapper{
                fs:             fs,
                name:           fName,
                path:           dir,
                dataPath:       path,
                dataOffset:     dataOffset,
        f := fileWrapper{
                fs:             fs,
                name:           fName,
                path:           dir,
                dataPath:       path,
                dataOffset:     dataOffset,
-               rsrcPath:       fmt.Sprintf(rsrcForkNameTemplate, dir+"/", fName),
-               infoPath:       fmt.Sprintf(infoForkNameTemplate, dir+"/", fName),
-               incompletePath: dir + "/" + fName + incompleteFileSuffix,
+               rsrcPath:       filepath.Join(dir, fmt.Sprintf(rsrcForkNameTemplate, fName)),
+               infoPath:       filepath.Join(dir, fmt.Sprintf(infoForkNameTemplate, fName)),
+               incompletePath: filepath.Join(dir, fName+incompleteFileSuffix),
                ffo:            &flattenedFileObject{},
        }
 
                ffo:            &flattenedFileObject{},
        }
 
@@ -100,28 +96,14 @@ func (f *fileWrapper) incompleteDataName() string {
 }
 
 func (f *fileWrapper) rsrcForkName() string {
 }
 
 func (f *fileWrapper) rsrcForkName() string {
-       return fmt.Sprintf(rsrcForkNameTemplate, "", f.name)
+       return fmt.Sprintf(rsrcForkNameTemplate, f.name)
 }
 
 func (f *fileWrapper) infoForkName() string {
 }
 
 func (f *fileWrapper) infoForkName() string {
-       return fmt.Sprintf(infoForkNameTemplate, "", f.name)
+       return fmt.Sprintf(infoForkNameTemplate, f.name)
 }
 
 }
 
-func (f *fileWrapper) creatorCode() []byte {
-       if f.ffo.FlatFileInformationFork.CreatorSignature != nil {
-               return f.infoFork.CreatorSignature
-       }
-       return []byte(fileTypeFromFilename(f.name).CreatorCode)
-}
-
-func (f *fileWrapper) typeCode() []byte {
-       if f.infoFork != nil {
-               return f.infoFork.TypeSignature
-       }
-       return []byte(fileTypeFromFilename(f.name).TypeCode)
-}
-
-func (f *fileWrapper) rsrcForkWriter() (io.Writer, error) {
+func (f *fileWrapper) rsrcForkWriter() (io.WriteCloser, error) {
        file, err := os.OpenFile(f.rsrcPath, os.O_CREATE|os.O_WRONLY, 0644)
        if err != nil {
                return nil, err
        file, err := os.OpenFile(f.rsrcPath, os.O_CREATE|os.O_WRONLY, 0644)
        if err != nil {
                return nil, err
@@ -130,7 +112,7 @@ func (f *fileWrapper) rsrcForkWriter() (io.Writer, error) {
        return file, nil
 }
 
        return file, nil
 }
 
-func (f *fileWrapper) infoForkWriter() (io.Writer, error) {
+func (f *fileWrapper) infoForkWriter() (io.WriteCloser, error) {
        file, err := os.OpenFile(f.infoPath, os.O_CREATE|os.O_WRONLY, 0644)
        if err != nil {
                return nil, err
        file, err := os.OpenFile(f.infoPath, os.O_CREATE|os.O_WRONLY, 0644)
        if err != nil {
                return nil, err
@@ -139,7 +121,7 @@ func (f *fileWrapper) infoForkWriter() (io.Writer, error) {
        return file, nil
 }
 
        return file, nil
 }
 
-func (f *fileWrapper) incFileWriter() (io.Writer, error) {
+func (f *fileWrapper) 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
        file, err := os.OpenFile(f.incompletePath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
        if err != nil {
                return nil, err
@@ -167,26 +149,31 @@ func (f *fileWrapper) dataFile() (os.FileInfo, error) {
        return nil, errors.New("file or directory not found")
 }
 
        return nil, errors.New("file or directory not found")
 }
 
-// move a fileWrapper and its associated metadata files to newPath
+// move a fileWrapper and its associated meta files to newPath.
+// Meta files include:
+// * Partially uploaded file ending with .incomplete
+// * 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 *fileWrapper) move(newPath string) error {
-       err := f.fs.Rename(f.dataPath, path.Join(newPath, f.name))
+       err := f.fs.Rename(f.dataPath, filepath.Join(newPath, f.name))
        if err != nil {
        if err != nil {
-               // TODO
+               return err
        }
 
        }
 
-       err = f.fs.Rename(f.incompletePath, path.Join(newPath, f.incompleteDataName()))
-       if err != nil {
-               // TODO
+       err = f.fs.Rename(f.incompletePath, filepath.Join(newPath, f.incompleteDataName()))
+       if err != nil && !errors.Is(err, os.ErrNotExist) {
+               return err
        }
 
        }
 
-       err = f.fs.Rename(f.rsrcPath, path.Join(newPath, f.rsrcForkName()))
-       if err != nil {
-               // TODO
+       err = f.fs.Rename(f.rsrcPath, filepath.Join(newPath, f.rsrcForkName()))
+       if err != nil && !errors.Is(err, os.ErrNotExist) {
+               return err
        }
 
        }
 
-       err = f.fs.Rename(f.infoPath, path.Join(newPath, f.infoForkName()))
-       if err != nil {
-               // TODO
+       err = f.fs.Rename(f.infoPath, filepath.Join(newPath, f.infoForkName()))
+       if err != nil && !errors.Is(err, os.ErrNotExist) {
+               return err
        }
 
        return nil
        }
 
        return nil
@@ -196,22 +183,22 @@ func (f *fileWrapper) move(newPath string) error {
 func (f *fileWrapper) delete() error {
        err := f.fs.RemoveAll(f.dataPath)
        if err != nil {
 func (f *fileWrapper) delete() error {
        err := f.fs.RemoveAll(f.dataPath)
        if err != nil {
-               // TODO
+               return err
        }
 
        err = f.fs.Remove(f.incompletePath)
        }
 
        err = f.fs.Remove(f.incompletePath)
-       if err != nil {
-               // TODO
+       if err != nil && !errors.Is(err, os.ErrNotExist) {
+               return err
        }
 
        err = f.fs.Remove(f.rsrcPath)
        }
 
        err = f.fs.Remove(f.rsrcPath)
-       if err != nil {
-               // TODO
+       if err != nil && !errors.Is(err, os.ErrNotExist) {
+               return err
        }
 
        err = f.fs.Remove(f.infoPath)
        }
 
        err = f.fs.Remove(f.infoPath)
-       if err != nil {
-               // TODO
+       if err != nil && !errors.Is(err, os.ErrNotExist) {
+               return err
        }
 
        return nil
        }
 
        return nil