Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FAT32: add functions to get File cluster chain and disk areas #271

Merged
merged 3 commits into from
Dec 9, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions filesystem/fat32/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,59 @@
filesystem *FileSystem
}

// Get the full cluster chain of the File
func (fl *File) GetClusterChain() ([]uint32, error) {
if fl == nil || fl.filesystem == nil {
return nil, os.ErrClosed
}

fs := fl.filesystem
clusters, err := fs.getClusterList(fl.clusterLocation)
if err != nil {
return nil, fmt.Errorf("unable to get list of clusters for file: %v", err)
}

return clusters, nil
}

type DiskRange struct {
Offset uint64
Length uint64
}

// Get the disk ranges occupied by the File.
// Returns an array of disk ranges, where each entry is a contiguous area on disk.
func (fl *File) GetDiskRanges() ([]DiskRange, error) {
clusters, err := fl.GetClusterChain()
if err != nil {
return nil, err
}

fs := fl.filesystem
bytesPerCluster := uint64(fs.bytesPerCluster)
dataStart := uint64(fs.dataStart)

var ranges []DiskRange
var lastCluster uint32 = 0

Check failure on line 54 in filesystem/fat32/file.go

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

var-declaration: should drop = 0 from declaration of var lastCluster; it is the zero value (revive)

Check failure on line 54 in filesystem/fat32/file.go

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

var-declaration: should drop = 0 from declaration of var lastCluster; it is the zero value (revive)

for _, cluster := range clusters {
if lastCluster != 0 && cluster == lastCluster+1 {
// Extend the current range
ranges[len(ranges)-1].Length += bytesPerCluster
} else {
// Add a new range
offset := dataStart + uint64(cluster-2)*bytesPerCluster
ranges = append(ranges, DiskRange{
Offset: offset,
Length: bytesPerCluster,
})
}
lastCluster = cluster
}

return ranges, nil
}

// Read reads up to len(b) bytes from the File.
// It returns the number of bytes read and any error encountered.
// At end of file, Read returns 0, io.EOF
Expand Down
Loading