Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 9 additions & 0 deletions superblock.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,15 @@ func (sb *Superblock) BlockCount() uint64 {
}
}

// FreeBlockCount returns the number of free data blocks in the fs.
func (sb *Superblock) FreeBlockCount() uint64 {
if sb.is64Bit == true {
return (uint64(sb.data.SFreeBlocksCountHi) << 32) | uint64(sb.data.SFreeBlocksCountLo)
}

return uint64(sb.data.SFreeBlocksCountLo)
}

func (sb *Superblock) BlockGroupCount() (blockGroups uint64) {
blockGroups = sb.BlockCount() / uint64(sb.data.SBlocksPerGroup)

Expand Down
19 changes: 19 additions & 0 deletions superblock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,22 @@ func ExampleSuperblock_ReadPhysicalBlock() {

// Output:
}

func TestSuperblock_FreeBlockCount(t *testing.T) {
filepath := path.Join(assetsPath, "tiny.ext4")

f, err := os.Open(filepath)
log.PanicIf(err)

defer f.Close()

_, err = f.Seek(Superblock0Offset, io.SeekStart)
log.PanicIf(err)

sb, err := NewSuperblockWithReader(f)
log.PanicIf(err)

if sb.FreeBlockCount() != 155 {
t.Fatalf("Supoerblock free blocks count was incorrect.")
}
}