X-Git-Url: https://git.r.bdr.sh/rbdr/mobius/blobdiff_plain/11843c8da176b40093972e9037df3e04b077890b..32d7bb7e1d8c96f93c0d24884fe038c477c4df02:/hotline/files.go?ds=inline diff --git a/hotline/files.go b/hotline/files.go index 19375e9..aa248f8 100644 --- a/hotline/files.go +++ b/hotline/files.go @@ -2,63 +2,130 @@ package hotline 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 } return defaultFileType } -func getFileNameList(filePath string) (fields []Field, err error) { - files, err := ioutil.ReadDir(filePath) +func fileTypeFromInfo(info fs.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(path string, ignoreList []string) (fields []Field, err error) { + files, err := os.ReadDir(path) if err != nil { return fields, nil } for _, file := range files { - var fileType []byte var fnwi FileNameWithInfo + + if ignoreFile(file.Name(), ignoreList) { + continue + } + 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") + 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.Join(path, resolvedPath)) + if errors.Is(err, os.ErrNotExist) { + continue + } + if err != nil { + return fields, err + } + + if rFile.IsDir() { + dir, err := ioutil.ReadDir(filepath.Join(path, file.Name())) + if err != nil { + return fields, err + } + + 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(rFile.Size())) + copy(fnwi.Type[:], []byte(fileTypeFromFilename(rFile.Name()).TypeCode)[:]) + copy(fnwi.Creator[:], []byte(fileTypeFromFilename(rFile.Name()).CreatorCode)[:]) + } - dir, err := ioutil.ReadDir(filePath + "/" + file.Name()) + } else if file.IsDir() { + dir, err := ioutil.ReadDir(filepath.Join(path, file.Name())) if err != nil { return fields, err } - binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(len(dir))) - copy(fnwi.Type[:], fileType[:]) + + 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 { + // 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) + nameSize := make([]byte, 2) - binary.BigEndian.PutUint16(nameSize, uint16(len(file.Name()))) + binary.BigEndian.PutUint16(nameSize, uint16(len(strippedName))) copy(fnwi.NameSize[:], nameSize[:]) - fnwi.name = []byte(file.Name()) + fnwi.name = []byte(strippedName) b, err := fnwi.MarshalBinary() if err != nil { @@ -98,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 { @@ -133,3 +202,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 +}