X-Git-Url: https://git.r.bdr.sh/rbdr/mobius/blobdiff_plain/f22acf38da970aa0d865a9978c9499dad01d235f..4a88189f30efac3cd86159b88c1a316a610e1523:/hotline/files.go diff --git a/hotline/files.go b/hotline/files.go index dc19653..9357bdb 100644 --- a/hotline/files.go +++ b/hotline/files.go @@ -4,9 +4,9 @@ import ( "encoding/binary" "errors" "io/fs" - "io/ioutil" "os" "path/filepath" + "regexp" "strings" ) @@ -30,7 +30,9 @@ func fileTypeFromInfo(info fs.FileInfo) (ft fileType, err error) { return ft, nil } -func getFileNameList(path string) (fields []Field, err error) { +const maxFileSize = 4294967296 + +func getFileNameList(path string, ignoreList []string) (fields []Field, err error) { files, err := os.ReadDir(path) if err != nil { return fields, nil @@ -39,7 +41,7 @@ func getFileNameList(path string) (fields []Field, err error) { for _, file := range files { var fnwi FileNameWithInfo - if strings.HasPrefix(file.Name(), ".") { + if ignoreFile(file.Name(), ignoreList) { continue } @@ -50,13 +52,14 @@ func getFileNameList(path string) (fields []Field, err error) { return fields, err } + // Check if path is a symlink. If so, follow it. 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.Join(path, resolvedPath)) + rFile, err := os.Stat(resolvedPath) if errors.Is(err, os.ErrNotExist) { continue } @@ -65,46 +68,45 @@ func getFileNameList(path string) (fields []Field, err error) { } if rFile.IsDir() { - dir, err := ioutil.ReadDir(filepath.Join(path, file.Name())) + dir, err := os.ReadDir(filepath.Join(path, file.Name())) if err != nil { return fields, err } var c uint32 for _, f := range dir { - if !strings.HasPrefix(f.Name(), ".") { + if !ignoreFile(f.Name(), ignoreList) { c += 1 } } binary.BigEndian.PutUint32(fnwi.FileSize[:], c) - copy(fnwi.Type[:], []byte("fldr")[:]) - copy(fnwi.Creator[:], fileCreator[:]) + copy(fnwi.Type[:], "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)[:]) + copy(fnwi.Type[:], fileTypeFromFilename(rFile.Name()).TypeCode) + copy(fnwi.Creator[:], fileTypeFromFilename(rFile.Name()).CreatorCode) } - } else if file.IsDir() { - dir, err := ioutil.ReadDir(filepath.Join(path, file.Name())) + dir, err := os.ReadDir(filepath.Join(path, file.Name())) if err != nil { return fields, err } var c uint32 for _, f := range dir { - if !strings.HasPrefix(f.Name(), ".") { + if !ignoreFile(f.Name(), ignoreList) { c += 1 } } binary.BigEndian.PutUint32(fnwi.FileSize[:], c) - copy(fnwi.Type[:], []byte("fldr")[:]) - copy(fnwi.Creator[:], fileCreator[:]) + copy(fnwi.Type[:], "fldr") + copy(fnwi.Creator[:], fileCreator) } else { - // the Hotline protocol does not support fileWrapper sizes > 4GiB due to the 4 byte field size, so skip them - if fileInfo.Size() > 4294967296 { + // the Hotline protocol does not support file sizes > 4GiB due to the 4 byte field size, so skip them + if fileInfo.Size() > maxFileSize { continue } @@ -113,16 +115,20 @@ func getFileNameList(path string) (fields []Field, err error) { return nil, err } - copy(fnwi.FileSize[:], hlFile.totalSize()[:]) - copy(fnwi.Type[:], hlFile.ffo.FlatFileInformationFork.TypeSignature[:]) - copy(fnwi.Creator[:], hlFile.ffo.FlatFileInformationFork.CreatorSignature[:]) + 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) + strippedName := strings.ReplaceAll(file.Name(), ".incomplete", "") + strippedName, err = txtEncoder.String(strippedName) + if err != nil { + return nil, err + } nameSize := make([]byte, 2) binary.BigEndian.PutUint16(nameSize, uint16(len(strippedName))) - copy(fnwi.NameSize[:], nameSize[:]) + copy(fnwi.NameSize[:], nameSize) fnwi.name = []byte(strippedName) @@ -130,7 +136,7 @@ func getFileNameList(path 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 @@ -201,3 +207,14 @@ func EncodeFilePath(filePath string) []byte { return bytes } + +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 matchIgnoreFilter > 0 +}