]> git.r.bdr.sh - rbdr/mobius/blobdiff - hotline/file_path.go
patch: v0.11.1
[rbdr/mobius] / hotline / file_path.go
index 3ff243557f9b96a69a2d5be6a952fc48f1f03614..97ce417e484550202cfb1b4da284ce57ef7c2ef7 100644 (file)
@@ -1,63 +1,86 @@
 package hotline
 
 import (
+       "bufio"
        "bytes"
        "encoding/binary"
        "errors"
        "io"
-       "path"
+       "path/filepath"
        "strings"
 )
 
-const pathSeparator = "/" // File path separator TODO: make configurable to support Windows
-
 // FilePathItem represents the file or directory portion of a delimited file path (e.g. foo and bar in "/foo/bar")
+// Example bytes:
 // 00 00
 // 09
-// 73 75 62 66 6f 6c 64 65 72 // "subfolder"
+// 73 75 62 66 6f 6c 64 65 72  "subfolder"
 type FilePathItem struct {
        Len  byte
        Name []byte
 }
 
+const fileItemMinLen = 3
+
+// fileItemScanner implements bufio.SplitFunc for parsing incoming byte slices into complete tokens
+func fileItemScanner(data []byte, _ bool) (advance int, token []byte, err error) {
+       if len(data) < fileItemMinLen {
+               return 0, nil, nil
+       }
+
+       advance = fileItemMinLen + int(data[2])
+       return advance, data[0:advance], nil
+}
+
+// Write implements the io.Writer interface for FilePathItem
+func (fpi *FilePathItem) Write(b []byte) (n int, err error) {
+       if len(b) < 3 {
+               return n, errors.New("buflen too small")
+       }
+       fpi.Len = b[2]
+       fpi.Name = b[fileItemMinLen : fpi.Len+fileItemMinLen]
+
+       return int(fpi.Len) + fileItemMinLen, nil
+}
+
 type FilePath struct {
        ItemCount [2]byte
        Items     []FilePathItem
 }
 
-func (fp *FilePath) UnmarshalBinary(b []byte) error {
+// Write implements io.Writer interface for FilePath
+func (fp *FilePath) Write(b []byte) (n int, err error) {
        reader := bytes.NewReader(b)
-       err := binary.Read(reader, binary.BigEndian, &fp.ItemCount)
+       err = binary.Read(reader, binary.BigEndian, &fp.ItemCount)
        if err != nil && !errors.Is(err, io.EOF) {
-               return err
+               return n, err
        }
        if errors.Is(err, io.EOF) {
-               return nil
+               return n, nil
        }
 
-       for i := uint16(0); i < fp.Len(); i++ {
-               // skip two bytes for the file path delimiter
-               _, _ = reader.Seek(2, io.SeekCurrent)
+       scanner := bufio.NewScanner(reader)
+       scanner.Split(fileItemScanner)
 
-               // read the length of the next pathItem
-               segLen, err := reader.ReadByte()
-               if err != nil {
-                       return err
-               }
+       for i := 0; i < int(binary.BigEndian.Uint16(fp.ItemCount[:])); i++ {
+               var fpi FilePathItem
+               scanner.Scan()
 
-               pBytes := make([]byte, segLen)
+               // Make a new []byte slice and copy the scanner bytes to it.  This is critical to avoid a data race as the
+               // scanner re-uses the buffer for subsequent scans.
+               buf := make([]byte, len(scanner.Bytes()))
+               copy(buf, scanner.Bytes())
 
-               _, err = reader.Read(pBytes)
-               if err != nil && !errors.Is(err, io.EOF) {
-                       return err
+               if _, err := fpi.Write(buf); err != nil {
+                       return n, err
                }
-
-               fp.Items = append(fp.Items, FilePathItem{Len: segLen, Name: pBytes})
+               fp.Items = append(fp.Items, fpi)
        }
 
-       return nil
+       return n, nil
 }
 
+// IsDropbox checks if a FilePath matches the special drop box folder type
 func (fp *FilePath) IsDropbox() bool {
        if fp.Len() == 0 {
                return false
@@ -78,37 +101,23 @@ func (fp *FilePath) Len() uint16 {
        return binary.BigEndian.Uint16(fp.ItemCount[:])
 }
 
-func (fp *FilePath) String() string {
-       out := []string{"/"}
-       for _, i := range fp.Items {
-               out = append(out, string(i.Name))
-       }
-
-       return path.Join(out...)
-}
-
-func ReadFilePath(filePathFieldData []byte) string {
-       var fp FilePath
-       err := fp.UnmarshalBinary(filePathFieldData)
-       if err != nil {
-               // TODO
-       }
-       return fp.String()
-}
-
 func readPath(fileRoot string, filePath, fileName []byte) (fullPath string, err error) {
        var fp FilePath
        if filePath != nil {
-               if err = fp.UnmarshalBinary(filePath); err != nil {
+               if _, err = fp.Write(filePath); err != nil {
                        return "", err
                }
        }
 
-       fullPath = path.Join(
-               "/",
+       var subPath string
+       for _, pathItem := range fp.Items {
+               subPath = filepath.Join("/", subPath, string(pathItem.Name))
+       }
+
+       fullPath = filepath.Join(
                fileRoot,
-               fp.String(),
-               path.Join("/", string(fileName)),
+               subPath,
+               filepath.Join("/", string(fileName)),
        )
 
        return fullPath, nil