]> git.r.bdr.sh - rbdr/mobius/blobdiff - hotline/files.go
Backfill tests and clean up
[rbdr/mobius] / hotline / files.go
index 19375e9a831a83cfea1baad80d64b78a9a30ade3..4f55eba13f29946a2cec4feed35040271e4276f2 100644 (file)
@@ -2,12 +2,16 @@ package hotline
 
 import (
        "encoding/binary"
 
 import (
        "encoding/binary"
+       "errors"
+       "io/fs"
        "io/ioutil"
        "os"
        "path/filepath"
        "strings"
 )
 
        "io/ioutil"
        "os"
        "path/filepath"
        "strings"
 )
 
+const incompleteFileSuffix = ".incomplete"
+
 func downcaseFileExtension(filename string) string {
        splitStr := strings.Split(filename, ".")
        ext := strings.ToLower(
 func downcaseFileExtension(filename string) string {
        splitStr := strings.Split(filename, ".")
        ext := strings.ToLower(
@@ -25,6 +29,17 @@ func fileTypeFromFilename(fn string) fileType {
        return defaultFileType
 }
 
        return defaultFileType
 }
 
+func fileTypeFromInfo(info os.FileInfo) (ft fileType, err error) {
+       if info.IsDir() {
+               ft.CreatorCode = "n/a "
+               ft.TypeCode = "fldr"
+       } else {
+               ft = fileTypeFromFilename(info.Name())
+       }
+
+       return ft, nil
+}
+
 func getFileNameList(filePath string) (fields []Field, err error) {
        files, err := ioutil.ReadDir(filePath)
        if err != nil {
 func getFileNameList(filePath string) (fields []Field, err error) {
        files, err := ioutil.ReadDir(filePath)
        if err != nil {
@@ -32,33 +47,63 @@ func getFileNameList(filePath string) (fields []Field, err error) {
        }
 
        for _, file := range files {
        }
 
        for _, file := range files {
-               var fileType []byte
                var fnwi FileNameWithInfo
                var fnwi FileNameWithInfo
+
                fileCreator := make([]byte, 4)
                fileCreator := make([]byte, 4)
-               if !file.IsDir() {
-                       fileType = []byte(fileTypeFromFilename(file.Name()).TypeCode)
-                       fileCreator = []byte(fileTypeFromFilename(file.Name()).CreatorCode)
 
 
-                       binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(file.Size()))
-                       copy(fnwi.Type[:], fileType[:])
-                       copy(fnwi.Creator[:], fileCreator[:])
-               } else {
-                       fileType = []byte("fldr")
+               if file.Mode()&os.ModeSymlink != 0 {
+                       resolvedPath, err := os.Readlink(filePath + "/" + file.Name())
+                       if err != nil {
+                               return fields, err
+                       }
+
+                       rFile, err := os.Stat(filePath + "/" + resolvedPath)
+                       if errors.Is(err, os.ErrNotExist) {
+                               continue
+                       }
+                       if err != nil {
+                               return fields, err
+                       }
+
+                       if rFile.IsDir() {
+                               dir, err := ioutil.ReadDir(filePath + "/" + file.Name())
+                               if err != nil {
+                                       return fields, err
+                               }
+                               binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(len(dir)))
+                               copy(fnwi.Type[:], []byte("fldr")[:])
+                               copy(fnwi.Creator[:], fileCreator[:])
+                       } else {
+                               binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(rFile.Size()))
+                               copy(fnwi.Type[:], []byte(fileTypeFromFilename(rFile.Name()).TypeCode)[:])
+                               copy(fnwi.Creator[:], []byte(fileTypeFromFilename(rFile.Name()).CreatorCode)[:])
+                       }
 
 
+               } else if file.IsDir() {
                        dir, err := ioutil.ReadDir(filePath + "/" + file.Name())
                        if err != nil {
                                return fields, err
                        }
                        binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(len(dir)))
                        dir, err := ioutil.ReadDir(filePath + "/" + file.Name())
                        if err != nil {
                                return fields, err
                        }
                        binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(len(dir)))
-                       copy(fnwi.Type[:], fileType[:])
+                       copy(fnwi.Type[:], []byte("fldr")[:])
                        copy(fnwi.Creator[:], fileCreator[:])
                        copy(fnwi.Creator[:], fileCreator[:])
+               } else {
+                       // the Hotline protocol does not support file sizes > 4GiB due to the 4 byte field size, so skip them
+                       if file.Size() > 4294967296 {
+                               continue
+                       }
+                       binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(file.Size()))
+                       copy(fnwi.Type[:], []byte(fileTypeFromFilename(file.Name()).TypeCode)[:])
+                       copy(fnwi.Creator[:], []byte(fileTypeFromFilename(file.Name()).CreatorCode)[:])
                }
 
                }
 
+               strippedName := strings.Replace(file.Name(), ".incomplete", "", -1)
+
                nameSize := make([]byte, 2)
                nameSize := make([]byte, 2)
-               binary.BigEndian.PutUint16(nameSize, uint16(len(file.Name())))
+               binary.BigEndian.PutUint16(nameSize, uint16(len(strippedName)))
                copy(fnwi.NameSize[:], nameSize[:])
 
                copy(fnwi.NameSize[:], nameSize[:])
 
-               fnwi.name = []byte(file.Name())
+               fnwi.name = []byte(strippedName)
 
                b, err := fnwi.MarshalBinary()
                if err != nil {
 
                b, err := fnwi.MarshalBinary()
                if err != nil {
@@ -133,3 +178,19 @@ func EncodeFilePath(filePath string) []byte {
 
        return bytes
 }
 
        return bytes
 }
+
+// effectiveFile wraps os.Open to check for the presence of a partial file transfer as a fallback
+func effectiveFile(filePath string) (*os.File, error) {
+       file, err := os.Open(filePath)
+       if err != nil && !errors.Is(err, fs.ErrNotExist) {
+               return nil, err
+       }
+
+       if errors.Is(err, fs.ErrNotExist) {
+               file, err = os.OpenFile(filePath+incompleteFileSuffix, os.O_APPEND|os.O_WRONLY, 0644)
+               if err != nil {
+                       return nil, err
+               }
+       }
+       return file, nil
+}