Overview
TheUsageStat struct contains filesystem usage statistics for a specific path, including total space, used space, free space, and inode information. It works across all supported platforms.
Struct Definition
Fields
Path
- Type:
string - Description: The filesystem path that was queried
- Example:
/home,C:\,/mnt/data - Note: This is the path passed to
disk.Usage(), not necessarily the actual mount point
Fstype
- Type:
string - Description: Filesystem type
- Examples:
- Linux:
ext4,xfs,btrfs,tmpfs,nfs - Windows:
NTFS,FAT32,ReFS - macOS:
apfs,hfs
- Linux:
- Note: Empty string if the filesystem type cannot be determined
Total
- Type:
uint64 - Description: Total size of the filesystem in bytes
- Example:
1000000000000(1 TB) - Note: This is the raw capacity, not accounting for filesystem overhead or reserved space
Free
- Type:
uint64 - Description: Free space available in bytes
- Note: On some filesystems (like ext4), this may include space reserved for root that regular users cannot use
- Use case: Use this for administrative tools that need to know all available space
Used
- Type:
uint64 - Description: Used space in bytes
- Calculation: Typically
Total - Free, but may vary by platform and filesystem - Note: Includes space used by files, directories, and filesystem metadata
UsedPercent
- Type:
float64 - Description: Percentage of space used (0.0 to 100.0)
- Calculation:
(Used / Total) * 100 - Example:
67.5means 67.5% of the filesystem is used - Use case: Primary metric for monitoring disk space alerts
InodesTotal
- Type:
uint64 - Description: Total number of inodes (file/directory entries) available on the filesystem
- Platform: Unix-like systems (Linux, macOS, BSD)
- Note: Zero on Windows and filesystems that don’t use inodes
- Example:
62545920(typical for a large ext4 filesystem)
InodesUsed
- Type:
uint64 - Description: Number of inodes currently in use
- Platform: Unix-like systems
- Use case: Track how many files/directories exist, independent of their size
InodesFree
- Type:
uint64 - Description: Number of unused inodes available
- Platform: Unix-like systems
- Calculation:
InodesTotal - InodesUsed - Note: A filesystem can run out of inodes even if there’s free space (many small files)
InodesUsedPercent
- Type:
float64 - Description: Percentage of inodes used (0.0 to 100.0)
- Platform: Unix-like systems
- Calculation:
(InodesUsed / InodesTotal) * 100 - Use case: Alert when approaching inode exhaustion (typically at 90%)
Methods
String
Usage Function
path- A filesystem path (e.g.,/,/home,C:\) - NOT a device path (e.g.,/dev/sda1)
disk.Partitions(), use the Mountpoint field, not the Device field.
Returns:
*UsageStat- Pointer to usage statistics structerror- Error if path doesn’t exist or cannot be accessed
Usage Examples
Basic Usage
Monitoring Multiple Filesystems
Disk Space Alerts
With Context for Timeout
Human-Readable Formatting
Platform-Specific Behavior
Linux
- All fields are populated from the
statvfssystem call Fstypeis obtained from/proc/mountsor mount system call- Reserved space (typically 5% on ext4) is included in
Freebut not available to non-root users - Inode information is always available (except for pseudo filesystems)
Windows
- Uses
GetDiskFreeSpaceExAPI Fstypeobtained fromGetVolumeInformation- Inode fields are always 0 (Windows doesn’t use inodes)
- Path should be drive letter or UNC path (e.g.,
C:\,\\server\share)
macOS
- Uses
statvfssystem call Fstypedetermined from mount information- Inode information available for most filesystems
- APFS filesystems have dynamic inode allocation
BSD (FreeBSD, OpenBSD)
- Uses
statvfssystem call - All fields typically available
- Some pseudo filesystems may have limited information
Common Pitfalls
Inode Exhaustion: A filesystem can run out of inodes even with free space remaining. This typically happens with millions of small files. Always monitor both
UsedPercent and InodesUsedPercent on Unix-like systems.Reserved Space: On Linux ext2/ext3/ext4 filesystems, typically 5% is reserved for root. The
Free field includes this space, but regular users cannot use it. This means a filesystem may show free space but return “disk full” errors to non-root processes.See Also
- Partitions - List and query disk partitions
- I/O Counters - Disk I/O statistics
- Disk Package Overview - Package introduction and main functions