|
|
|
package zones
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
fs "github.com/hack-pad/hackpadfs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// OpenFile opens a file on the machine's config directory with the specified flags
|
|
|
|
func (m *Machine) OpenFile(name string, flags int, args ...any) (fs.File, error) {
|
|
|
|
base := m.zone.zones.dir
|
|
|
|
fullName := m.getFilename(name, args...)
|
|
|
|
|
|
|
|
return fs.OpenFile(base, fullName, flags, 0644)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveFile deletes a file from the machine's config directory
|
|
|
|
func (m *Machine) RemoveFile(name string, args ...any) error {
|
|
|
|
base := m.zone.zones.dir
|
|
|
|
fullName := m.getFilename(name, args...)
|
|
|
|
err := fs.Remove(base, fullName)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case os.IsNotExist(err):
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadFile reads a file from the machine's config directory
|
|
|
|
func (m *Machine) ReadFile(name string, args ...any) ([]byte, error) {
|
|
|
|
base := m.zone.zones.dir
|
|
|
|
fullName := m.getFilename(name, args...)
|
|
|
|
|
|
|
|
return fs.ReadFile(base, fullName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Machine) getFilename(name string, args ...any) string {
|
|
|
|
if len(args) > 0 {
|
|
|
|
name = fmt.Sprintf(name, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
s := []string{
|
|
|
|
m.zone.Name,
|
|
|
|
m.Name,
|
|
|
|
name,
|
|
|
|
}
|
|
|
|
|
|
|
|
return filepath.Join(s...)
|
|
|
|
}
|