disk

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2026 License: AGPL-3.0 Imports: 13 Imported by: 0

README

disk-go

Cross-platform disk and filesystem utilities for Go.

Go Reference

Overview

disk-go provides low-level disk I/O operations, statistics gathering, and filesystem information for Go applications. It offers comprehensive cross-platform support with optimized implementations for Linux, Windows, macOS, BSD variants, and Solaris.

This package originated from the MinIO Object Storage project and is maintained by the Kypello community as a public fork.

Features

  • Disk Information: Get disk space, inode counts, filesystem type
  • I/O Statistics: Read/write operations, sectors, ticks, and more
  • Direct I/O: Open files with O_DIRECT for cache bypass
  • Cross-Platform: Extensive support across operating systems
  • Optimized: Platform-specific implementations for best performance

Installation

go get github.com/kypello-io/disk-go@v0.1.0

Usage

package main

import (
    "fmt"
    "log"

    "github.com/kypello-io/disk-go"
)

func main() {
    // Get disk information
    info, err := disk.GetInfo("/", true)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Printf("Total: %d bytes\n", info.Total)
    fmt.Printf("Free: %d bytes\n", info.Free)
    fmt.Printf("Filesystem: %s\n", info.FSType)

    // Get I/O statistics (Linux)
    stats, err := disk.GetDriveStats(info.Major, info.Minor)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Printf("Read IOs: %d\n", stats.ReadIOs)
    fmt.Printf("Write IOs: %d\n", stats.WriteIOs)
}

API Reference

Data Types
  • Info: Disk information (Total, Free, Used, FSType, etc.)
  • IOStats: I/O statistics (ReadIOs, WriteIOs, etc.)
Functions
  • GetInfo(path string, firstTime bool) (Info, error) - Get disk space info
  • GetDriveStats(major, minor uint32) (IOStats, error) - Get I/O stats
  • SameDisk(disk1, disk2 string) (bool, error) - Compare disk paths
  • IsRootDisk(diskPath, rootDisk string) (bool, error) - Check if root disk
  • OpenFileDirectIO(filePath string, flag int, perm os.FileMode) (*os.File, error) - Open with O_DIRECT
  • DisableDirectIO(f *os.File) error - Disable direct I/O
  • AlignedBlock(blockSize int) []byte - Allocate aligned buffer
  • Fdatasync(f *os.File) error - Sync file data (Linux/Unix)

See pkg.go.dev for complete documentation.

Platform Support

  • Linux (x64, ARM, ARM64, s390x, 32-bit)
  • Windows
  • macOS / Darwin
  • FreeBSD
  • NetBSD
  • OpenBSD
  • Solaris

Attribution

This package originated from MinIO (Copyright 2015-2021 MinIO, Inc.) and is maintained as part of the Kypello public fork.

Kypello is a community-maintained fork of MinIO designed to preserve critical enterprise functionality within the open-source ecosystem. This package has been extracted into a standalone module to enable broader usage.

Note: Kypello is not affiliated with, endorsed by, or sponsored by MinIO, Inc.

License

GNU Affero General Public License v3.0 (AGPL-3.0)

See LICENSE for the full license text.

Contributing

Contributions are welcome! Please ensure:

  • Code follows existing patterns
  • Platform-specific code uses appropriate build tags
  • Tests pass on relevant platforms
  • Changes maintain AGPL-3.0 compliance

Documentation

Index

Constants

View Source
const ODirectPlatform = true

ODirectPlatform indicates if the platform supports O_DIRECT

Variables

This section is empty.

Functions

func AlignedBlock

func AlignedBlock(blockSize int) []byte

AlignedBlock - pass through to directio implementation.

func DisableDirectIO

func DisableDirectIO(f *os.File) error

DisableDirectIO - disables directio mode.

func FadviseDontNeed

func FadviseDontNeed(f *os.File) error

FadviseDontNeed invalidates page-cache

func Fdatasync

func Fdatasync(f *os.File) error

Fdatasync - fdatasync() is similar to fsync(), but does not flush modified metadata unless that metadata is needed in order to allow a subsequent data retrieval to be correctly handled. For example, changes to st_atime or st_mtime (respectively, time of last access and time of last modification; see inode(7)) do not require flushing because they are not necessary for a subsequent data read to be handled correctly. On the other hand, a change to the file size (st_size, as made by say ftruncate(2)), would require a metadata flush.

The aim of fdatasync() is to reduce disk activity for applications that do not require all metadata to be synchronized with the disk.

func IsRootDisk

func IsRootDisk(diskPath string, rootDisk string) (bool, error)

IsRootDisk returns if diskPath belongs to root-disk, i.e the disk mounted at "/"

func OpenFileDirectIO

func OpenFileDirectIO(filePath string, flag int, perm os.FileMode) (*os.File, error)

OpenFileDirectIO - bypass kernel cache.

func SameDisk

func SameDisk(disk1, disk2 string) (bool, error)

SameDisk reports whether di1 and di2 describe the same disk.

Types

type IOStats

type IOStats struct {
	ReadIOs        uint64
	ReadMerges     uint64
	ReadSectors    uint64
	ReadTicks      uint64
	WriteIOs       uint64
	WriteMerges    uint64
	WriteSectors   uint64
	WriteTicks     uint64
	CurrentIOs     uint64
	TotalTicks     uint64
	ReqTicks       uint64
	DiscardIOs     uint64
	DiscardMerges  uint64
	DiscardSectors uint64
	DiscardTicks   uint64
	FlushIOs       uint64
	FlushTicks     uint64
}

IOStats contains stats of a single drive

func GetDriveStats

func GetDriveStats(major, minor uint32) (iostats IOStats, err error)

GetDriveStats returns IO stats of the drive by its major:minor

type Info

type Info struct {
	Total      uint64
	Free       uint64
	Used       uint64
	Files      uint64
	Ffree      uint64
	FSType     string
	Major      uint32
	Minor      uint32
	Name       string
	Rotational *bool
	NRRequests uint64
}

Info stat fs struct is container which holds following values Total - total size of the volume / disk Free - free size of the volume / disk Files - total inodes available Ffree - free inodes available FSType - file system type Major - major dev id Minor - minor dev id Devname - device name

func GetInfo

func GetInfo(path string, firstTime bool) (info Info, err error)

GetInfo returns total and free bytes available in a directory, e.g. `/`.

Jump to

Keyboard shortcuts

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