]> git.r.bdr.sh - rbdr/mobius/blobdiff - hotline/files.go
Clean up various linter warnings
[rbdr/mobius] / hotline / files.go
index 0746b1f6ef51d1f5f26bdf8fad34cd386ce11ea6..6753cceb7ec5995d7f65ed75e67658c06b598f0c 100644 (file)
@@ -3,6 +3,8 @@ package hotline
 import (
        "encoding/binary"
        "errors"
+       "fmt"
+       "io"
        "io/fs"
        "os"
        "path/filepath"
@@ -30,10 +32,12 @@ func fileTypeFromInfo(info fs.FileInfo) (ft fileType, err error) {
        return ft, nil
 }
 
+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 {
@@ -47,16 +51,17 @@ func getFileNameList(path string, ignoreList []string) (fields []Field, err erro
 
                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
                        }
@@ -78,17 +83,17 @@ func getFileNameList(path string, ignoreList []string) (fields []Field, err erro
                                }
 
                                binary.BigEndian.PutUint32(fnwi.FileSize[:], c)
-                               copy(fnwi.Type[:], []byte("fldr"))
+                               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 := os.ReadDir(filepath.Join(path, file.Name()))
                        if err != nil {
-                               return fields, err
+                               return fields, fmt.Errorf("readDir: %w", err)
                        }
 
                        var c uint32
@@ -99,17 +104,17 @@ func getFileNameList(path string, ignoreList []string) (fields []Field, err erro
                        }
 
                        binary.BigEndian.PutUint32(fnwi.FileSize[:], c)
-                       copy(fnwi.Type[:], []byte("fldr"))
+                       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)
                        if err != nil {
-                               return nil, err
+                               return nil, fmt.Errorf("newFileWrapper: %w", err)
                        }
 
                        copy(fnwi.FileSize[:], hlFile.totalSize())
@@ -120,18 +125,18 @@ func getFileNameList(path string, ignoreList []string) (fields []Field, err erro
                strippedName := strings.ReplaceAll(file.Name(), ".incomplete", "")
                strippedName, err = txtEncoder.String(strippedName)
                if err != nil {
-                       return nil, err
+                       continue
                }
 
                nameSize := make([]byte, 2)
                binary.BigEndian.PutUint16(nameSize, uint16(len(strippedName)))
                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))
        }