2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
/*
|
|
|
|
Package file provides functions that combine file handling, JSON marshaling
|
|
|
|
and file system abstraction.
|
|
|
|
*/
|
|
|
|
package file
|
|
|
|
|
|
|
|
import (
|
2022-05-18 12:10:57 -04:00
|
|
|
"bytes"
|
2022-03-22 11:03:15 -04:00
|
|
|
"encoding/json"
|
2022-05-11 07:53:02 -04:00
|
|
|
"errors"
|
2023-05-22 07:31:20 -04:00
|
|
|
"fmt"
|
2022-03-22 11:03:15 -04:00
|
|
|
"io"
|
|
|
|
"io/fs"
|
|
|
|
"os"
|
2022-04-13 03:15:27 -04:00
|
|
|
"path"
|
2023-05-22 07:31:20 -04:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2022-03-22 11:03:15 -04:00
|
|
|
|
2023-01-02 07:33:56 -05:00
|
|
|
"github.com/siderolabs/talos/pkg/machinery/config/encoder"
|
2022-03-22 11:03:15 -04:00
|
|
|
"github.com/spf13/afero"
|
2022-05-11 07:53:02 -04:00
|
|
|
"gopkg.in/yaml.v3"
|
2022-03-22 11:03:15 -04:00
|
|
|
)
|
|
|
|
|
2022-04-13 03:15:27 -04:00
|
|
|
// Option is a bitmask of options for file operations.
|
2022-08-08 05:04:17 -04:00
|
|
|
type Option struct {
|
|
|
|
uint
|
|
|
|
}
|
2022-04-13 03:15:27 -04:00
|
|
|
|
2022-08-08 05:04:17 -04:00
|
|
|
// hasOption determines if a set of options contains the given option.
|
|
|
|
func hasOption(options []Option, op Option) bool {
|
|
|
|
for _, ops := range options {
|
|
|
|
if ops == op {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
2022-04-13 03:15:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
// OptNone is a no-op.
|
2022-08-08 05:04:17 -04:00
|
|
|
optNone uint = iota
|
2022-04-13 03:15:27 -04:00
|
|
|
// OptOverwrite overwrites an existing file.
|
2022-08-08 05:04:17 -04:00
|
|
|
optOverwrite
|
2022-04-13 03:15:27 -04:00
|
|
|
// OptMkdirAll creates the path to the file.
|
2022-08-08 05:04:17 -04:00
|
|
|
optMkdirAll
|
2023-05-30 07:47:36 -04:00
|
|
|
// OptAppend appends to the file.
|
|
|
|
optAppend
|
2022-08-08 05:04:17 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-11-09 09:57:54 -05:00
|
|
|
// OptNone is a no-op.
|
|
|
|
OptNone = Option{optNone}
|
|
|
|
// OptOverwrite overwrites an existing file.
|
2022-08-08 05:04:17 -04:00
|
|
|
OptOverwrite = Option{optOverwrite}
|
2022-11-09 09:57:54 -05:00
|
|
|
// OptMkdirAll creates the path to the file.
|
|
|
|
OptMkdirAll = Option{optMkdirAll}
|
2023-05-30 07:47:36 -04:00
|
|
|
// OptAppend appends to the file.
|
|
|
|
OptAppend = Option{optAppend}
|
2022-04-13 03:15:27 -04:00
|
|
|
)
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
// Handler handles file interaction.
|
|
|
|
type Handler struct {
|
|
|
|
fs *afero.Afero
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewHandler returns a new file handler.
|
|
|
|
func NewHandler(fs afero.Fs) Handler {
|
|
|
|
afs := &afero.Afero{Fs: fs}
|
|
|
|
return Handler{fs: afs}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read reads the file given name and returns the bytes read.
|
|
|
|
func (h *Handler) Read(name string) ([]byte, error) {
|
2022-05-17 07:10:39 -04:00
|
|
|
file, err := h.fs.OpenFile(name, os.O_RDONLY, 0o600)
|
2022-03-22 11:03:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
return io.ReadAll(file)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write writes the data bytes into the file with the given name.
|
2022-08-08 05:04:17 -04:00
|
|
|
func (h *Handler) Write(name string, data []byte, options ...Option) error {
|
|
|
|
if hasOption(options, OptMkdirAll) {
|
2022-04-13 03:15:27 -04:00
|
|
|
if err := h.fs.MkdirAll(path.Dir(name), os.ModePerm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2022-03-22 11:03:15 -04:00
|
|
|
flags := os.O_WRONLY | os.O_CREATE | os.O_EXCL
|
2022-08-08 05:04:17 -04:00
|
|
|
if hasOption(options, OptOverwrite) {
|
2022-03-22 11:03:15 -04:00
|
|
|
flags = os.O_WRONLY | os.O_CREATE | os.O_TRUNC
|
|
|
|
}
|
2023-05-30 07:47:36 -04:00
|
|
|
if hasOption(options, OptAppend) {
|
|
|
|
flags = os.O_WRONLY | os.O_CREATE | os.O_APPEND
|
|
|
|
}
|
2022-05-17 07:10:39 -04:00
|
|
|
file, err := h.fs.OpenFile(name, flags, 0o600)
|
2022-03-22 11:03:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = file.Write(data)
|
|
|
|
if errTmp := file.Close(); errTmp != nil && err == nil {
|
|
|
|
err = errTmp
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadJSON reads a JSON file from name and unmarshals it into the content interface.
|
|
|
|
// The interface content must be a pointer to a JSON marchalable object.
|
2022-04-28 04:28:28 -04:00
|
|
|
func (h *Handler) ReadJSON(name string, content any) error {
|
2022-03-22 11:03:15 -04:00
|
|
|
data, err := h.Read(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return json.Unmarshal(data, content)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteJSON marshals the content interface to JSON and writes it to the path with the given name.
|
2022-08-08 05:04:17 -04:00
|
|
|
func (h *Handler) WriteJSON(name string, content any, options ...Option) error {
|
2022-03-22 11:03:15 -04:00
|
|
|
jsonData, err := json.MarshalIndent(content, "", "\t")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-08-08 05:04:17 -04:00
|
|
|
return h.Write(name, jsonData, options...)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-05-11 07:53:02 -04:00
|
|
|
// ReadYAML reads a YAML file from name and unmarshals it into the content interface.
|
2023-05-03 05:11:53 -04:00
|
|
|
// The interface content must be a pointer to a YAML marshalable object.
|
2022-05-11 07:53:02 -04:00
|
|
|
func (h *Handler) ReadYAML(name string, content any) error {
|
2022-05-18 12:10:57 -04:00
|
|
|
return h.readYAML(name, content, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadYAMLStrict does the same as ReadYAML, but fails if YAML contains fields
|
|
|
|
// that are not specified in content.
|
|
|
|
func (h *Handler) ReadYAMLStrict(name string, content any) error {
|
|
|
|
return h.readYAML(name, content, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) readYAML(name string, content any, strict bool) error {
|
2022-05-11 07:53:02 -04:00
|
|
|
data, err := h.Read(name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-05-18 12:10:57 -04:00
|
|
|
decoder := yaml.NewDecoder(bytes.NewBuffer(data))
|
|
|
|
decoder.KnownFields(strict)
|
|
|
|
return decoder.Decode(content)
|
2022-05-11 07:53:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// WriteYAML marshals the content interface to YAML and writes it to the path with the given name.
|
2022-08-08 05:04:17 -04:00
|
|
|
func (h *Handler) WriteYAML(name string, content any, options ...Option) (err error) {
|
2022-05-11 07:53:02 -04:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
err = errors.New("recovered from panic")
|
|
|
|
}
|
|
|
|
}()
|
2022-05-16 12:54:25 -04:00
|
|
|
data, err := encoder.NewEncoder(content).Encode()
|
2022-05-11 07:53:02 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-08-08 05:04:17 -04:00
|
|
|
return h.Write(name, data, options...)
|
2022-05-11 07:53:02 -04:00
|
|
|
}
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
// Remove deletes the file with the given name.
|
|
|
|
func (h *Handler) Remove(name string) error {
|
|
|
|
return h.fs.Remove(name)
|
|
|
|
}
|
|
|
|
|
2022-09-26 09:52:31 -04:00
|
|
|
// RemoveAll deletes the file or directory with the given name.
|
|
|
|
func (h *Handler) RemoveAll(name string) error {
|
|
|
|
return h.fs.RemoveAll(name)
|
|
|
|
}
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
// Stat returns a FileInfo describing the named file, or an error, if any
|
|
|
|
// happens.
|
|
|
|
func (h *Handler) Stat(name string) (fs.FileInfo, error) {
|
|
|
|
return h.fs.Stat(name)
|
|
|
|
}
|
2022-11-14 12:18:58 -05:00
|
|
|
|
|
|
|
// MkdirAll creates a directory path and all parents that does not exist yet.
|
|
|
|
func (h *Handler) MkdirAll(name string) error {
|
|
|
|
return h.fs.MkdirAll(name, 0o700)
|
|
|
|
}
|
2023-05-22 07:31:20 -04:00
|
|
|
|
|
|
|
// CopyDir copies the src directory recursively into dst with the given options. OptMkdirAll
|
|
|
|
// is always set. CopyDir does not follow symlinks.
|
|
|
|
func (h *Handler) CopyDir(src, dst string, opts ...Option) error {
|
|
|
|
opts = append(opts, OptMkdirAll)
|
|
|
|
root := filepath.Join(src, string(filepath.Separator))
|
|
|
|
|
|
|
|
walkFunc := func(path string, info fs.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if info.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
pathWithoutRoot := strings.TrimPrefix(path, root)
|
|
|
|
return h.CopyFile(path, filepath.Join(dst, pathWithoutRoot), opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return h.fs.Walk(src, walkFunc)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CopyFile copies the file from src to dst with the given options, respecting file permissions.
|
|
|
|
func (h *Handler) CopyFile(src, dst string, opts ...Option) error {
|
|
|
|
srcInfo, err := h.fs.Stat(src)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("stat source file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
content, err := h.fs.ReadFile(src)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("read source file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = h.Write(dst, content, opts...)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("write destination file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = h.fs.Chmod(dst, srcInfo.Mode())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("chmod destination file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-10-09 07:04:29 -04:00
|
|
|
|
|
|
|
// RenameFile renames a file, overwriting any existing file at the destination.
|
|
|
|
func (h *Handler) RenameFile(old, new string) error {
|
|
|
|
return h.fs.Rename(old, new)
|
|
|
|
}
|
2023-10-31 07:46:40 -04:00
|
|
|
|
|
|
|
// IsEmpty returns true if the given directory is empty.
|
|
|
|
func (h *Handler) IsEmpty(dirName string) (bool, error) {
|
|
|
|
f, err := h.fs.Open(dirName)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
_, err = f.Readdirnames(1)
|
|
|
|
if err == io.EOF {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
return false, err
|
|
|
|
}
|