]> git.r.bdr.sh - rbdr/mobius/blobdiff - hotline/files.go
Account for the root
[rbdr/mobius] / hotline / files.go
index d4bea538258dc2ebc362b455bb48e27bd02952fa..a1db329b6ebfad16c7a88df914ea006298263835 100644 (file)
@@ -3,6 +3,7 @@ package hotline
 import (
        "encoding/binary"
        "errors"
+       "fmt"
        "io"
        "io/fs"
        "os"
@@ -33,10 +34,10 @@ func fileTypeFromInfo(info fs.FileInfo) (ft fileType, err error) {
 
 const maxFileSize = 4294967296
 
-func getFileNameList(path string, ignoreList []string) (fields []Field, err error) {
+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 {
@@ -50,14 +51,14 @@ 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(resolvedPath)
@@ -92,7 +93,7 @@ func getFileNameList(path string, ignoreList []string) (fields []Field, err erro
                } 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
@@ -111,14 +112,14 @@ func getFileNameList(path string, ignoreList []string) (fields []Field, err erro
                                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.ReplaceAll(file.Name(), ".incomplete", "")
@@ -131,11 +132,11 @@ func getFileNameList(path string, ignoreList []string) (fields []Field, err erro
                binary.BigEndian.PutUint16(nameSize, uint16(len(strippedName)))
                copy(fnwi.NameSize[:], nameSize)
 
-               fnwi.name = []byte(strippedName)
+               fnwi.Name = []byte(strippedName)
 
                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))
        }
@@ -168,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
@@ -186,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
 }