]> git.r.bdr.sh - rbdr/mobius/blobdiff - hotline/files.go
patch: v0.10.20
[rbdr/mobius] / hotline / files.go
index 04334e951410cb06d62a9ee4c92349afa94dfbd1..c4ba718b8fd23cf1319c39f6833fd71887646878 100644 (file)
@@ -7,27 +7,20 @@ import (
        "io/ioutil"
        "os"
        "path/filepath"
+       "regexp"
        "strings"
 )
 
-func downcaseFileExtension(filename string) string {
-       splitStr := strings.Split(filename, ".")
-       ext := strings.ToLower(
-               splitStr[len(splitStr)-1],
-       )
-
-       return ext
-}
-
-func fileTypeFromFilename(fn string) fileType {
-       ft, ok := fileTypes[downcaseFileExtension(fn)]
+func fileTypeFromFilename(filename string) fileType {
+       fileExt := strings.ToLower(filepath.Ext(filename))
+       ft, ok := fileTypes[fileExt]
        if ok {
                return ft
        }
        return defaultFileType
 }
 
-func fileTypeFromInfo(info os.FileInfo) (ft fileType, err error) {
+func fileTypeFromInfo(info fs.FileInfo) (ft fileType, err error) {
        if info.IsDir() {
                ft.CreatorCode = "n/a "
                ft.TypeCode = "fldr"
@@ -38,8 +31,8 @@ func fileTypeFromInfo(info os.FileInfo) (ft fileType, err error) {
        return ft, nil
 }
 
-func getFileNameList(filePath string) (fields []Field, err error) {
-       files, err := ioutil.ReadDir(filePath)
+func getFileNameList(path string, ignoreList []string) (fields []Field, err error) {
+       files, err := os.ReadDir(path)
        if err != nil {
                return fields, nil
        }
@@ -47,25 +40,45 @@ func getFileNameList(filePath string) (fields []Field, err error) {
        for _, file := range files {
                var fnwi FileNameWithInfo
 
+               if ignoreFile(file.Name(), ignoreList) {
+                       continue
+               }
+
                fileCreator := make([]byte, 4)
 
-               if file.Mode()&os.ModeSymlink != 0 {
-                       resolvedPath, err := os.Readlink(filePath + "/" + file.Name())
+               fileInfo, err := file.Info()
+               if err != nil {
+                       return fields, err
+               }
+
+               if fileInfo.Mode()&os.ModeSymlink != 0 {
+                       resolvedPath, err := os.Readlink(filepath.Join(path, file.Name()))
                        if err != nil {
                                return fields, err
                        }
 
-                       rFile, err := os.Stat(filePath + "/" + resolvedPath)
+                       rFile, err := os.Stat(filepath.Join(path, resolvedPath))
+                       if errors.Is(err, os.ErrNotExist) {
+                               continue
+                       }
                        if err != nil {
                                return fields, err
                        }
 
                        if rFile.IsDir() {
-                               dir, err := ioutil.ReadDir(filePath + "/" + file.Name())
+                               dir, err := ioutil.ReadDir(filepath.Join(path, file.Name()))
                                if err != nil {
                                        return fields, err
                                }
-                               binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(len(dir)))
+
+                               var c uint32
+                               for _, f := range dir {
+                                       if !ignoreFile(f.Name(), ignoreList) {
+                                               c += 1
+                                       }
+                               }
+
+                               binary.BigEndian.PutUint32(fnwi.FileSize[:], c)
                                copy(fnwi.Type[:], []byte("fldr")[:])
                                copy(fnwi.Creator[:], fileCreator[:])
                        } else {
@@ -75,17 +88,35 @@ func getFileNameList(filePath string) (fields []Field, err error) {
                        }
 
                } else if file.IsDir() {
-                       dir, err := ioutil.ReadDir(filePath + "/" + file.Name())
+                       dir, err := ioutil.ReadDir(filepath.Join(path, file.Name()))
                        if err != nil {
                                return fields, err
                        }
-                       binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(len(dir)))
+
+                       var c uint32
+                       for _, f := range dir {
+                               if !ignoreFile(f.Name(), ignoreList) {
+                                       c += 1
+                               }
+                       }
+
+                       binary.BigEndian.PutUint32(fnwi.FileSize[:], c)
                        copy(fnwi.Type[:], []byte("fldr")[:])
                        copy(fnwi.Creator[:], fileCreator[:])
                } else {
-                       binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(file.Size()))
-                       copy(fnwi.Type[:], []byte(fileTypeFromFilename(file.Name()).TypeCode)[:])
-                       copy(fnwi.Creator[:], []byte(fileTypeFromFilename(file.Name()).CreatorCode)[:])
+                       // the Hotline protocol does not support fileWrapper sizes > 4GiB due to the 4 byte field size, so skip them
+                       if fileInfo.Size() > 4294967296 {
+                               continue
+                       }
+
+                       hlFile, err := newFileWrapper(&OSFileStore{}, path+"/"+file.Name(), 0)
+                       if err != nil {
+                               return nil, err
+                       }
+
+                       copy(fnwi.FileSize[:], hlFile.totalSize()[:])
+                       copy(fnwi.Type[:], hlFile.ffo.FlatFileInformationFork.TypeSignature[:])
+                       copy(fnwi.Creator[:], hlFile.ffo.FlatFileInformationFork.CreatorSignature[:])
                }
 
                strippedName := strings.Replace(file.Name(), ".incomplete", "", -1)
@@ -100,7 +131,7 @@ func getFileNameList(filePath string) (fields []Field, err error) {
                if err != nil {
                        return nil, err
                }
-               fields = append(fields, NewField(fieldFileNameWithInfo, b))
+               fields = append(fields, NewField(FieldFileNameWithInfo, b))
        }
 
        return fields, nil
@@ -134,12 +165,14 @@ func CalcTotalSize(filePath string) ([]byte, error) {
 func CalcItemCount(filePath string) ([]byte, error) {
        var itemcount uint16
        err := filepath.Walk(filePath, func(path string, info os.FileInfo, err error) error {
-               itemcount += 1
-
                if err != nil {
                        return err
                }
 
+               if !strings.HasPrefix(info.Name(), ".") {
+                       itemcount += 1
+               }
+
                return nil
        })
        if err != nil {
@@ -170,20 +203,13 @@ func EncodeFilePath(filePath string) []byte {
        return bytes
 }
 
-const incompleteFileSuffix = ".incomplete"
-
-// 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
+func ignoreFile(fileName string, ignoreList []string) bool {
+       // skip files that match any regular expression present in the IgnoreFiles list
+       matchIgnoreFilter := 0
+       for _, pattern := range ignoreList {
+               if match, _ := regexp.MatchString(pattern, fileName); match {
+                       matchIgnoreFilter += 1
                }
        }
-       return file, nil
+       return matchIgnoreFilter > 0
 }