driveutil

package
v0.0.0-...-d2a29b3 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2025 License: MIT Imports: 5 Imported by: 0

README

driveutil

This package provides drive enumeration, metadata extraction, and utility functions for Windows drives.

Types

DriveStore
type DriveStore map[string]bool

Tracks detected drives by unique ID.

DriveInfo
type DriveInfo struct {
    Letter string
    Label  string
    Serial uint32
    Type   uint32 // DRIVE_FIXED, DRIVE_REMOVABLE, etc.
}

Functions

  • (store DriveStore) DetectDrives(onNewDrive func(drive string, serial uint32)) - Detects new drives and calls callback
  • (store DriveStore) MonitorDrives(onNewDrive func(drive string, serial uint32), interval time.Duration) - Continuously monitors for new drives
  • GetVolumeSerialNumber(root string) (uint32, error) - Gets volume serial number for drive
  • DriveExists(drive string) bool - Checks if drive path exists
  • ListDrives() []DriveInfo - Returns slice of all available drives

Example

import "github.com/Merith-TK/utils/pkg/driveutil"

drives := driveutil.ListDrives()
for _, drive := range drives {
    fmt.Printf("Drive: %s, Label: %s, Serial: %08X\n", drive.Letter, drive.Label, drive.Serial)
}

store := driveutil.DriveStore{}
store.MonitorDrives(func(drive string, serial uint32) {
    fmt.Printf("New drive detected: %s (Serial: %08X)\n", drive, serial)
}, 5*time.Second)

Documentation

Overview

Package driveutil provides comprehensive Windows drive management utilities including drive enumeration, metadata extraction, monitoring, and utility functions.

This package is Windows-specific and uses the Windows API to interact with drives. It supports both fixed and removable drives, providing detailed information about each drive including serial numbers, labels, and types.

Key features:

  • Drive detection and enumeration
  • Volume serial number extraction
  • Drive monitoring with callback support
  • Drive existence checking
  • Comprehensive drive metadata (label, type, serial)

The DriveStore type provides stateful drive monitoring, tracking drives by their unique combination of drive letter and serial number to detect insertions and removals.

Example usage:

// List all drives
drives := driveutil.ListDrives()
for _, drive := range drives {
	fmt.Printf("Drive: %s, Label: %s, Serial: %08X\n",
		drive.Letter, drive.Label, drive.Serial)
}

// Monitor for new drives
store := make(driveutil.DriveStore)
store.DetectDrives(func(drive string, serial uint32) {
	fmt.Printf("New drive detected: %s (Serial: %08X)\n", drive, serial)
})

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DriveExists

func DriveExists(drive string) bool

DriveExists checks if a drive path exists.

func GetVolumeSerialNumber

func GetVolumeSerialNumber(root string) (uint32, error)

GetVolumeSerialNumber returns the serial number for a given drive root.

Types

type DriveInfo

type DriveInfo struct {
	Letter string
	Label  string
	Serial uint32
	Type   uint32 // windows.DRIVE_FIXED, DRIVE_REMOVABLE, etc.
}

DriveInfo represents information about a drive.

func ListDrives

func ListDrives() []DriveInfo

ListDrives returns a slice of DriveInfo for all present fixed/removable drives.

type DriveStore

type DriveStore map[string]bool

DriveStore keeps track of currently detected drives by their unique ID.

func (DriveStore) DetectDrives

func (store DriveStore) DetectDrives(onNewDrive func(drive string, serial uint32))

DetectDrives enumerates all logical drives, checks their type, and gets their serial number. Calls the provided callback for each new drive detected.

func (DriveStore) MonitorDrives

func (store DriveStore) MonitorDrives(onNewDrive func(drive string, serial uint32), interval time.Duration)

MonitorDrives calls DetectDrives in a loop with a sleep interval.

Jump to

Keyboard shortcuts

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