2022-06-22 22:24:35 +00:00
|
|
|
package disk
|
|
|
|
|
|
|
|
import (
|
2023-12-28 00:13:09 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2022-06-22 22:24:35 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ Disk = (*localDisk)(nil)
|
|
|
|
|
|
|
|
type localDisk struct {
|
|
|
|
path string
|
|
|
|
}
|
|
|
|
|
|
|
|
type localEntry struct {
|
|
|
|
os.DirEntry
|
|
|
|
}
|
|
|
|
|
|
|
|
func newLocal(path string) (Disk, error) {
|
|
|
|
return localDisk{path: path}, nil
|
|
|
|
}
|
|
|
|
|
2023-12-28 00:13:09 +00:00
|
|
|
func (l localDisk) Exists(path string) (bool, error) {
|
2022-06-22 22:24:35 +00:00
|
|
|
_, err := os.Stat(path)
|
2023-12-28 00:13:09 +00:00
|
|
|
|
|
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("failed checking file existence: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
2022-06-22 22:24:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l localDisk) Read(path string) ([]byte, error) {
|
|
|
|
return os.ReadFile(path) //nolint
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l localDisk) Write(path string, data []byte) error {
|
|
|
|
return os.WriteFile(path, data, 0777) //nolint
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l localDisk) Remove(path string) error {
|
|
|
|
return os.RemoveAll(path) //nolint
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l localDisk) MkDir(path string) error {
|
|
|
|
return os.MkdirAll(path, 0777) //nolint
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l localDisk) ReadDir(path string) ([]Entry, error) {
|
|
|
|
dir, err := os.ReadDir(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err //nolint
|
|
|
|
}
|
|
|
|
|
|
|
|
entries := make([]Entry, len(dir))
|
|
|
|
for i, entry := range dir {
|
|
|
|
entries[i] = localEntry{
|
|
|
|
DirEntry: entry,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return entries, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l localDisk) Open(path string, flag int) (io.WriteCloser, error) {
|
|
|
|
return os.OpenFile(path, flag, 0777) //nolint
|
|
|
|
}
|