mirror of
https://git.numenor-labs.us/dsfx.git
synced 2025-04-29 08:10:34 +00:00
77 lines
1.9 KiB
Go
77 lines
1.9 KiB
Go
package disk
|
|
|
|
import (
|
|
"io/fs"
|
|
"log/slog"
|
|
"os"
|
|
"time"
|
|
)
|
|
|
|
type osDisk struct {
|
|
logger *slog.Logger
|
|
}
|
|
|
|
// Default returns a new instance of the defaultDisk type, which implements
|
|
// the Disk interface. This is the default implementation for file operations
|
|
// using the standard os package.
|
|
func Default() Disk {
|
|
return &osDisk{logger: slog.Default()}
|
|
}
|
|
|
|
// Create implements Disk.
|
|
func (d *osDisk) Create(name string) (File, error) {
|
|
ts := time.Now()
|
|
file, err := os.Create(name)
|
|
el := time.Now().Sub(ts).String()
|
|
d.logger.Debug("(io) disk.Create", "duration", el)
|
|
return file, err
|
|
}
|
|
|
|
// Mkdir implements Disk.
|
|
func (d *osDisk) Mkdir(name string, perm fs.FileMode) error {
|
|
ts := time.Now()
|
|
err := os.Mkdir(name, perm)
|
|
el := time.Now().Sub(ts).String()
|
|
d.logger.Debug("(io) disk.Mkdir", "duration", el)
|
|
return err
|
|
}
|
|
|
|
// MkdirAll implements Disk.
|
|
// This creates a directory and any necessary parent directories.
|
|
// If the directory already exists, it does nothing.
|
|
// It returns an error if the directory cannot be created.
|
|
func (d *osDisk) MkdirAll(name string, perm fs.FileMode) error {
|
|
ts := time.Now()
|
|
err := os.MkdirAll(name, perm)
|
|
el := time.Now().Sub(ts).String()
|
|
d.logger.Debug("(io) disk.MkdirAll", "duration", el)
|
|
return err
|
|
}
|
|
|
|
// Open implements Disk.
|
|
func (d *osDisk) Open(name string) (File, error) {
|
|
ts := time.Now()
|
|
file, err := os.Open(name)
|
|
el := time.Now().Sub(ts).String()
|
|
d.logger.Debug("(io) disk.Open", "duration", el)
|
|
return file, err
|
|
}
|
|
|
|
// Remove implements Disk.
|
|
func (d *osDisk) Remove(name string) error {
|
|
ts := time.Now()
|
|
err := os.Remove(name)
|
|
el := time.Now().Sub(ts).String()
|
|
d.logger.Debug("(io) disk.Remove", "duration", el)
|
|
return err
|
|
}
|
|
|
|
// Stat implements Disk.
|
|
func (d *osDisk) Stat(name string) (fs.FileInfo, error) {
|
|
ts := time.Now()
|
|
info, err := os.Stat(name)
|
|
el := time.Now().Sub(ts).String()
|
|
d.logger.Debug("(io) disk.Stat", "duration", el)
|
|
return info, err
|
|
}
|