diskusage

package
v0.0.0-...-052fa94 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 14, 2026 License: Apache-2.0 Imports: 3 Imported by: 2

README

Package cloudeng.io/file/diskusage

import cloudeng.io/file/diskusage

Constants

Byte, KB, MB, GB, TB, PB, EB, KiB, MiB, GiB, TiB, PiB, EiB
Byte SizeUnit = 1
// base 10
KB = Byte * 1000
MB = KB * 1000
GB = MB * 1000
TB = GB * 1000
PB = TB * 1000
EB = PB * 1000
// base 2 quantities
KiB = Byte << 10
MiB = KiB << 10
GiB = MiB << 10
TiB = GiB << 10
PiB = TiB << 10
EiB = PiB << 10

Functions

Func BinarySize
func BinarySize(width, precision int, val int64) string
Func DecimalSize
func DecimalSize(width, precision int, val int64) string
Func ParseToBytes
func ParseToBytes(val string) (float64, error)

Types

Type Binary
type Binary SizeUnit

Binary represents a number of bytes in base 2.

Methods
func (b Binary) Format(f fmt.State, verb rune)
func (b Binary) Standardize() (float64, string)
func (b Binary) Value(value int64) float64
Type Block
type Block struct {
	// contains filtered or unexported fields
}
Methods
func (s Block) Calculate(_, blocks int64) int64
func (s Block) String() string
Type Calculator
type Calculator interface {
	Calculate(bytes, blocks int64) int64
	String() string
}

Calculator is used to calculate the size of a file or directory based on either its size in bytes (often referred to as its apparent size) and/or the number of storage blocks it occupies. Some file systems support sparse files (most unix filesystems) where the number of blocks occupied by a file is less than the number of bytes it represents, hence, the term 'apparent size'.

Functions
func NewBlock(blocksize int64) Calculator

Block uses the number of blocks occupied by a file to calculate its size.

func NewIdentity() Calculator
func NewRAID0(stripeSize int64, numStripes int) Calculator
func NewRoundup(blocksize int64) Calculator
Type Decimal
type Decimal SizeUnit

Decimal represents a number of bytes in base 10.

Methods
func (b Decimal) Format(f fmt.State, verb rune)
func (b Decimal) Standardize() (float64, string)
func (b Decimal) Value(value int64) float64
Type Identity
type Identity struct{}

Identity returns the apparent size of a file.

Methods
func (i Identity) Calculate(bytes, _ int64) int64
func (i Identity) String() string
Type RAID0
type RAID0 struct {
	// contains filtered or unexported fields
}

RAID0 is a calculator for RAID0 volumes based on the apparent size of the file and the RAID0 stripe size and number of stripes.

Methods
func (r0 RAID0) Calculate(size, _ int64) int64
func (r0 RAID0) String() string
Type Roundup
type Roundup struct {
	// contains filtered or unexported fields
}

Roundup rounds up the apparent size of a file to the nearest block size multiple.

Methods
func (s Roundup) Calculate(bytes, _ int64) int64
func (s Roundup) String() string
Type SizeUnit
type SizeUnit int64

SizeUnit represents a unit of size in bytes. It can be used to represent both decimal and binary sizes.

Functions
func BinaryUnitForSize(size int64) SizeUnit
func DecimalUnitForSize(size int64) SizeUnit
Methods
func (s SizeUnit) String() string
func (s SizeUnit) Value(v int64) float64

Examples

ExampleBinary
ExampleDecimal

Documentation

Index

Examples

Constants

View Source
const (
	Byte SizeUnit = 1

	// base 10
	KB = Byte * 1000
	MB = KB * 1000
	GB = MB * 1000
	TB = GB * 1000
	PB = TB * 1000
	EB = PB * 1000

	// base 2 quantities
	KiB = Byte << 10
	MiB = KiB << 10
	GiB = MiB << 10
	TiB = GiB << 10
	PiB = TiB << 10
	EiB = PiB << 10
)

Variables

This section is empty.

Functions

func BinarySize

func BinarySize(width, precision int, val int64) string

func DecimalSize

func DecimalSize(width, precision int, val int64) string

func ParseToBytes

func ParseToBytes(val string) (float64, error)

Types

type Binary

type Binary SizeUnit

Binary represents a number of bytes in base 2.

Example
package main

import (
	"fmt"

	"cloudeng.io/file/diskusage"
)

func main() {
	fmt.Println(diskusage.KiB.Value(512))
	fmt.Println(diskusage.KiB.Value(2048))
	fmt.Println(diskusage.GiB.Value(1073741824))
	fmt.Println(diskusage.Binary(1024).Standardize())
	fmt.Println(diskusage.Binary(1536).Standardize())
	fmt.Println(diskusage.Binary(1610612736).Standardize())
	fmt.Printf("%.0f\n", diskusage.Binary(512)) // Default precision
}
Output:

0.5
2
1
1 KiB
1.5 KiB
1.5 GiB
512 B

func (Binary) Format

func (b Binary) Format(f fmt.State, verb rune)

func (Binary) Standardize

func (b Binary) Standardize() (float64, string)

func (Binary) Value

func (b Binary) Value(value int64) float64

type Block

type Block struct {
	// contains filtered or unexported fields
}

func (Block) Calculate

func (s Block) Calculate(_, blocks int64) int64

func (Block) String

func (s Block) String() string

type Calculator

type Calculator interface {
	Calculate(bytes, blocks int64) int64
	String() string
}

Calculator is used to calculate the size of a file or directory based on either its size in bytes (often referred to as its apparent size) and/or the number of storage blocks it occupies. Some file systems support sparse files (most unix filesystems) where the number of blocks occupied by a file is less than the number of bytes it represents, hence, the term 'apparent size'.

func NewBlock

func NewBlock(blocksize int64) Calculator

Block uses the number of blocks occupied by a file to calculate its size.

func NewIdentity

func NewIdentity() Calculator

func NewRAID0

func NewRAID0(stripeSize int64, numStripes int) Calculator

func NewRoundup

func NewRoundup(blocksize int64) Calculator

type Decimal

type Decimal SizeUnit

Decimal represents a number of bytes in base 10.

Example
package main

import (
	"fmt"

	"cloudeng.io/file/diskusage"
)

func main() {
	fmt.Println(diskusage.KB.Value(500))
	fmt.Println(diskusage.KB.Value(2000))
	fmt.Println(diskusage.GB.Value(1000000000))
	fmt.Println(diskusage.Decimal(1000).Standardize())
	fmt.Println(diskusage.Decimal(1500).Standardize())
	fmt.Println(diskusage.Decimal(1500000000).Standardize())
	fmt.Printf("%.0f\n", diskusage.Decimal(500)) // Default precision
}
Output:

0.5
2
1
1 KB
1.5 KB
1.5 GB
500 B

func (Decimal) Format

func (b Decimal) Format(f fmt.State, verb rune)

func (Decimal) Standardize

func (b Decimal) Standardize() (float64, string)

func (Decimal) Value

func (b Decimal) Value(value int64) float64

type Identity

type Identity struct{}

Identity returns the apparent size of a file.

func (Identity) Calculate

func (i Identity) Calculate(bytes, _ int64) int64

func (Identity) String

func (i Identity) String() string

type RAID0

type RAID0 struct {
	// contains filtered or unexported fields
}

RAID0 is a calculator for RAID0 volumes based on the apparent size of the file and the RAID0 stripe size and number of stripes.

func (RAID0) Calculate

func (r0 RAID0) Calculate(size, _ int64) int64

func (RAID0) String

func (r0 RAID0) String() string

type Roundup

type Roundup struct {
	// contains filtered or unexported fields
}

Roundup rounds up the apparent size of a file to the nearest block size multiple.

func (Roundup) Calculate

func (s Roundup) Calculate(bytes, _ int64) int64

func (Roundup) String

func (s Roundup) String() string

type SizeUnit

type SizeUnit int64

SizeUnit represents a unit of size in bytes. It can be used to represent both decimal and binary sizes.

func BinaryUnitForSize

func BinaryUnitForSize(size int64) SizeUnit

func DecimalUnitForSize

func DecimalUnitForSize(size int64) SizeUnit

func (SizeUnit) String

func (s SizeUnit) String() string

func (SizeUnit) Value

func (s SizeUnit) Value(v int64) float64

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL