X-Git-Url: https://git.r.bdr.sh/rbdr/mobius/blobdiff_plain/7cd900d61edbd6d322db3cecb913adf574389320..548d4bb93fe32d24b3fbc491a01e5c6127be9289:/hotline/files.go diff --git a/hotline/files.go b/hotline/files.go index b66580f..1eeef59 100644 --- a/hotline/files.go +++ b/hotline/files.go @@ -4,23 +4,15 @@ import ( "encoding/binary" "errors" "io/fs" - "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 } @@ -38,8 +30,8 @@ func fileTypeFromInfo(info fs.FileInfo) (ft fileType, err error) { return ft, nil } -func getFileNameList(filePath string) (fields []Field, err error) { - files, err := os.ReadDir(filePath) +func getFileNameList(path string, ignoreList []string) (fields []Field, err error) { + files, err := os.ReadDir(path) if err != nil { return fields, nil } @@ -47,7 +39,7 @@ func getFileNameList(filePath string) (fields []Field, err error) { for _, file := range files { var fnwi FileNameWithInfo - if strings.HasPrefix(file.Name(), ".") { + if ignoreFile(file.Name(), ignoreList) { continue } @@ -59,12 +51,12 @@ func getFileNameList(filePath string) (fields []Field, err error) { } if fileInfo.Mode()&os.ModeSymlink != 0 { - resolvedPath, err := os.Readlink(filePath + "/" + file.Name()) + 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 } @@ -73,14 +65,14 @@ func getFileNameList(filePath string) (fields []Field, err error) { } if rFile.IsDir() { - dir, err := ioutil.ReadDir(filePath + "/" + 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 } } @@ -95,14 +87,14 @@ func getFileNameList(filePath string) (fields []Field, err error) { } } else if file.IsDir() { - dir, err := ioutil.ReadDir(filePath + "/" + 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 } } @@ -116,7 +108,7 @@ func getFileNameList(filePath string) (fields []Field, err error) { continue } - hlFile, err := newFileWrapper(&OSFileStore{}, filePath+"/"+file.Name(), 0) + hlFile, err := newFileWrapper(&OSFileStore{}, path+"/"+file.Name(), 0) if err != nil { return nil, err } @@ -138,7 +130,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 @@ -209,3 +201,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 +}