X-Git-Url: https://git.r.bdr.sh/rbdr/mobius/blobdiff_plain/43ecc0f42eaeface5f640479df7372bfb8021f23..d34160c512a52cf1f5caf59fa00372d3a627e24c:/hotline/files.go diff --git a/hotline/files.go b/hotline/files.go index 1db698b..a1db329 100644 --- a/hotline/files.go +++ b/hotline/files.go @@ -2,84 +2,143 @@ package hotline import ( "encoding/binary" - "io/ioutil" + "errors" + "fmt" + "io" + "io/fs" "os" "path/filepath" + "regexp" "strings" ) -const defaultCreator = "TTXT" -const defaultType = "TEXT" - -var fileCreatorCodes = map[string]string{ - "sit": "SIT!", - "pdf": "CARO", -} - -var fileTypeCodes = map[string]string{ - "sit": "SIT!", - "jpg": "JPEG", - "pdf": "PDF ", -} - -func fileTypeFromFilename(fn string) string { - ext := strings.Split(fn, ".") - code := fileTypeCodes[ext[len(ext)-1]] - - if code == "" { - code = defaultType +func fileTypeFromFilename(filename string) fileType { + fileExt := strings.ToLower(filepath.Ext(filename)) + ft, ok := fileTypes[fileExt] + if ok { + return ft } - - return code + return defaultFileType } -func fileCreatorFromFilename(fn string) string { - ext := strings.Split(fn, ".") - code := fileCreatorCodes[ext[len(ext)-1]] - if code == "" { - code = defaultCreator +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 code + return ft, nil } -func getFileNameList(filePath string) ([]Field, error) { - var fields []Field +const maxFileSize = 4294967296 - 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 + return fields, fmt.Errorf("error reading path: %s: %w", path, err) } for _, file := range files { - var fileType []byte + var fnwi FileNameWithInfo + + if ignoreFile(file.Name(), ignoreList) { + continue + } + fileCreator := make([]byte, 4) - fileSize := make([]byte, 4) - if !file.IsDir() { - fileType = []byte(fileTypeFromFilename(file.Name())) - fileCreator = []byte(fileCreatorFromFilename(file.Name())) - binary.BigEndian.PutUint32(fileSize, uint32(file.Size())) - } else { - fileType = []byte("fldr") + fileInfo, err := file.Info() + if err != nil { + 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, fmt.Errorf("error following symlink: %s: %w", resolvedPath, err) + } - dir, err := ioutil.ReadDir(filePath + "/" + file.Name()) + rFile, err := os.Stat(resolvedPath) + if errors.Is(err, os.ErrNotExist) { + continue + } if err != nil { return fields, err } - binary.BigEndian.PutUint32(fileSize, uint32(len(dir))) + + if rFile.IsDir() { + dir, err := os.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[:], "fldr") + copy(fnwi.Creator[:], fileCreator) + } else { + binary.BigEndian.PutUint32(fnwi.FileSize[:], uint32(rFile.Size())) + copy(fnwi.Type[:], fileTypeFromFilename(rFile.Name()).TypeCode) + copy(fnwi.Creator[:], fileTypeFromFilename(rFile.Name()).CreatorCode) + } + } else if file.IsDir() { + dir, err := os.ReadDir(filepath.Join(path, file.Name())) + if err != nil { + return fields, fmt.Errorf("readDir: %w", err) + } + + var c uint32 + for _, f := range dir { + if !ignoreFile(f.Name(), ignoreList) { + c += 1 + } + } + + binary.BigEndian.PutUint32(fnwi.FileSize[:], c) + copy(fnwi.Type[:], "fldr") + copy(fnwi.Creator[:], fileCreator) + } else { + // 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) + if err != nil { + 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[:]) + } + + strippedName := strings.ReplaceAll(file.Name(), ".incomplete", "") + strippedName, err = txtEncoder.String(strippedName) + if err != nil { + continue } - fields = append(fields, NewField( - fieldFileNameWithInfo, - FileNameWithInfo{ - Type: fileType, - Creator: fileCreator, - FileSize: fileSize, - NameScript: []byte{0, 0}, - Name: []byte(file.Name()), - }.Payload(), - )) + nameSize := make([]byte, 2) + binary.BigEndian.PutUint16(nameSize, uint16(len(strippedName))) + copy(fnwi.NameSize[:], nameSize) + + fnwi.Name = []byte(strippedName) + + b, err := io.ReadAll(&fnwi) + if err != nil { + return nil, fmt.Errorf("error io.ReadAll: %w", err) + } + fields = append(fields, NewField(FieldFileNameWithInfo, b)) } return fields, nil @@ -110,15 +169,21 @@ 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 - err := filepath.Walk(filePath, func(path string, info os.FileInfo, err error) error { - itemcount += 1 + 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++ + } + return nil }) if err != nil { @@ -126,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 } @@ -149,7 +214,13 @@ func EncodeFilePath(filePath string) []byte { return bytes } -func ReadFilePath(filePathFieldData []byte) string { - fp := NewFilePath(filePathFieldData) - return fp.String() +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 }