Initial commit.

This commit is contained in:
2025-10-11 17:06:14 +00:00
commit ec521c3c91
45 changed files with 9798 additions and 0 deletions

110
internal/storage/storage.go Normal file
View File

@@ -0,0 +1,110 @@
package storage
import (
"fmt"
"os"
"path/filepath"
"syscall"
)
// EnsureDir creates a directory with specified permissions if it doesn't exist
func EnsureDir(path string, perm os.FileMode) error {
if err := os.MkdirAll(path, perm); err != nil {
return fmt.Errorf("creating directory %s: %w", path, err)
}
return nil
}
// FileExists checks if a file exists
func FileExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}
// WriteFile writes content to a file with specified permissions
func WriteFile(path string, content []byte, perm os.FileMode) error {
if err := os.WriteFile(path, content, perm); err != nil {
return fmt.Errorf("writing file %s: %w", path, err)
}
return nil
}
// ReadFile reads content from a file
func ReadFile(path string) ([]byte, error) {
content, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("reading file %s: %w", path, err)
}
return content, nil
}
// Lock represents a file lock
type Lock struct {
file *os.File
path string
}
// AcquireLock acquires an exclusive lock on a file
func AcquireLock(lockPath string) (*Lock, error) {
// Ensure lock directory exists
if err := EnsureDir(filepath.Dir(lockPath), 0755); err != nil {
return nil, err
}
// Open or create lock file
file, err := os.OpenFile(lockPath, os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
return nil, fmt.Errorf("opening lock file %s: %w", lockPath, err)
}
// Acquire exclusive lock with flock
if err := syscall.Flock(int(file.Fd()), syscall.LOCK_EX); err != nil {
file.Close()
return nil, fmt.Errorf("acquiring lock on %s: %w", lockPath, err)
}
return &Lock{
file: file,
path: lockPath,
}, nil
}
// Release releases the file lock
func (l *Lock) Release() error {
if l.file == nil {
return nil
}
// Release flock
if err := syscall.Flock(int(l.file.Fd()), syscall.LOCK_UN); err != nil {
l.file.Close()
return fmt.Errorf("releasing lock on %s: %w", l.path, err)
}
// Close file
if err := l.file.Close(); err != nil {
return fmt.Errorf("closing lock file %s: %w", l.path, err)
}
l.file = nil
return nil
}
// WithLock executes a function while holding a lock
func WithLock(lockPath string, fn func() error) error {
lock, err := AcquireLock(lockPath)
if err != nil {
return err
}
defer lock.Release()
return fn()
}
// EnsureFilePermissions ensures a file has the correct permissions
func EnsureFilePermissions(path string, perm os.FileMode) error {
if err := os.Chmod(path, perm); err != nil {
return fmt.Errorf("setting permissions on %s: %w", path, err)
}
return nil
}

View File

@@ -0,0 +1,107 @@
package storage
import (
"os"
"path/filepath"
"testing"
)
func TestEnsureDir(t *testing.T) {
tmpDir := t.TempDir()
testDir := filepath.Join(tmpDir, "test", "nested", "dir")
err := EnsureDir(testDir, 0755)
if err != nil {
t.Fatalf("EnsureDir failed: %v", err)
}
// Verify directory exists
info, err := os.Stat(testDir)
if err != nil {
t.Fatalf("Directory not created: %v", err)
}
if !info.IsDir() {
t.Fatalf("Path is not a directory")
}
// Calling again should be idempotent
err = EnsureDir(testDir, 0755)
if err != nil {
t.Fatalf("EnsureDir not idempotent: %v", err)
}
}
func TestWriteFile(t *testing.T) {
tmpDir := t.TempDir()
testFile := filepath.Join(tmpDir, "test.txt")
testData := []byte("test content")
// Write file
err := WriteFile(testFile, testData, 0644)
if err != nil {
t.Fatalf("WriteFile failed: %v", err)
}
// Read file back
data, err := os.ReadFile(testFile)
if err != nil {
t.Fatalf("ReadFile failed: %v", err)
}
if string(data) != string(testData) {
t.Fatalf("Data mismatch: got %q, want %q", string(data), string(testData))
}
}
func TestFileExists(t *testing.T) {
tmpDir := t.TempDir()
testFile := filepath.Join(tmpDir, "test.txt")
// File should not exist initially
if FileExists(testFile) {
t.Fatalf("File should not exist")
}
// Create file
err := WriteFile(testFile, []byte("test"), 0644)
if err != nil {
t.Fatalf("WriteFile failed: %v", err)
}
// File should exist now
if !FileExists(testFile) {
t.Fatalf("File should exist")
}
}
func TestWithLock(t *testing.T) {
tmpDir := t.TempDir()
lockFile := filepath.Join(tmpDir, "test.lock")
counter := 0
// Execute with lock
err := WithLock(lockFile, func() error {
counter++
return nil
})
if err != nil {
t.Fatalf("WithLock failed: %v", err)
}
if counter != 1 {
t.Fatalf("Function not executed: counter=%d", counter)
}
// Should be idempotent - can acquire lock multiple times sequentially
err = WithLock(lockFile, func() error {
counter++
return nil
})
if err != nil {
t.Fatalf("WithLock failed on second call: %v", err)
}
if counter != 2 {
t.Fatalf("Function not executed on second call: counter=%d", counter)
}
}