X-Git-Url: https://git.r.bdr.sh/rbdr/mobius/blobdiff_plain/f22acf38da970aa0d865a9978c9499dad01d235f..7de788aa3d713ae561f076ff28a9b5ec4aa7021c:/hotline/files.go diff --git a/hotline/files.go b/hotline/files.go index dc19653..a1db329 100644 --- a/hotline/files.go +++ b/hotline/files.go @@ -3,10 +3,12 @@ package hotline import ( "encoding/binary" "errors" + "fmt" + "io" "io/fs" - "io/ioutil" "os" "path/filepath" + "regexp" "strings" ) @@ -30,16 +32,18 @@ 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 + return fields, fmt.Errorf("error reading path: %s: %w", path, err) } for _, file := range files { var fnwi FileNameWithInfo - if strings.HasPrefix(file.Name(), ".") { + if ignoreFile(file.Name(), ignoreList) { continue } @@ -47,16 +51,17 @@ func getFileNameList(path string) (fields []Field, err error) { fileInfo, err := file.Info() if err != nil { - return fields, err + return fields, fmt.Errorf("error getting file info: %s: %w", file.Name(), 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 + return fields, fmt.Errorf("error following symlink: %s: %w", resolvedPath, err) } - rFile, err := os.Stat(filepath.Join(path, resolvedPath)) + rFile, err := os.Stat(resolvedPath) if errors.Is(err, os.ErrNotExist) { continue } @@ -65,72 +70,75 @@ 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 + return fields, fmt.Errorf("readDir: %w", 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 } - hlFile, err := newFileWrapper(&OSFileStore{}, path+"/"+file.Name(), 0) + hlFile, err := NewFileWrapper(&OSFileStore{}, path+"/"+file.Name(), 0) if err != nil { - return nil, err + return nil, fmt.Errorf("NewFileWrapper: %w", 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 { + continue + } nameSize := make([]byte, 2) binary.BigEndian.PutUint16(nameSize, uint16(len(strippedName))) - copy(fnwi.NameSize[:], nameSize[:]) + copy(fnwi.NameSize[:], nameSize) - fnwi.name = []byte(strippedName) + fnwi.Name = []byte(strippedName) - b, err := fnwi.MarshalBinary() + b, err := io.ReadAll(&fnwi) if err != nil { - return nil, err + return nil, fmt.Errorf("error io.ReadAll: %w", err) } - fields = append(fields, NewField(fieldFileNameWithInfo, b)) + fields = append(fields, NewField(FieldFileNameWithInfo, b)) } return fields, nil @@ -161,15 +169,19 @@ func CalcTotalSize(filePath string) ([]byte, error) { return bs, nil } +// CalcItemCount recurses through a file path and counts the number of non-hidden files. func CalcItemCount(filePath string) ([]byte, error) { - var itemcount uint16 + var itemCount uint16 + + // Walk the directory and count items err := filepath.Walk(filePath, func(path string, info os.FileInfo, err error) error { if err != nil { return err } + // Skip hidden files if !strings.HasPrefix(info.Name(), ".") { - itemcount += 1 + itemCount++ } return nil @@ -179,7 +191,7 @@ func CalcItemCount(filePath string) ([]byte, error) { } bs := make([]byte, 2) - binary.BigEndian.PutUint16(bs, itemcount-1) + binary.BigEndian.PutUint16(bs, itemCount-1) return bs, nil } @@ -201,3 +213,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 +}