2019-11-07 20:05:39 +01:00
|
|
|
package fsremote
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/backup/common"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/backup/fscommon"
|
2023-05-03 10:48:53 +02:00
|
|
|
libfs "github.com/VictoriaMetrics/VictoriaMetrics/lib/fs"
|
2019-11-07 20:05:39 +01:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
|
|
|
)
|
|
|
|
|
|
|
|
// FS represents remote filesystem.
|
|
|
|
//
|
|
|
|
// Backups are uploaded there.
|
|
|
|
// Data is downloaded from there during restore.
|
|
|
|
type FS struct {
|
|
|
|
// Dir is a path to remote directory with backup data.
|
|
|
|
Dir string
|
|
|
|
}
|
|
|
|
|
2020-10-09 14:31:39 +02:00
|
|
|
// MustStop stops fs.
|
|
|
|
func (fs *FS) MustStop() {
|
|
|
|
// Nothing to do
|
|
|
|
}
|
|
|
|
|
2019-11-07 20:05:39 +01:00
|
|
|
// String returns human-readable string representation for fs.
|
|
|
|
func (fs *FS) String() string {
|
|
|
|
return fmt.Sprintf("fsremote %q", fs.Dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListParts returns all the parts from fs.
|
|
|
|
func (fs *FS) ListParts() ([]common.Part, error) {
|
|
|
|
dir := fs.Dir
|
|
|
|
if _, err := os.Stat(dir); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
// Return empty part list for non-existing directory.
|
|
|
|
// The directory will be created later.
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
files, err := fscommon.AppendFiles(nil, dir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var parts []common.Part
|
2023-05-03 10:48:53 +02:00
|
|
|
dir += string(filepath.Separator)
|
2019-11-07 20:05:39 +01:00
|
|
|
for _, file := range files {
|
|
|
|
if !strings.HasPrefix(file, dir) {
|
|
|
|
logger.Panicf("BUG: unexpected prefix for file %q; want %q", file, dir)
|
|
|
|
}
|
2020-01-09 14:24:26 +01:00
|
|
|
if fscommon.IgnorePath(file) {
|
|
|
|
continue
|
|
|
|
}
|
2019-11-07 20:05:39 +01:00
|
|
|
var p common.Part
|
2023-09-18 16:10:16 +02:00
|
|
|
remotePath := common.ToCanonicalPath(file[len(dir):])
|
|
|
|
if !p.ParseFromRemotePath(remotePath) {
|
2019-11-07 20:05:39 +01:00
|
|
|
logger.Infof("skipping unknown file %s", file)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Check for correct part size.
|
|
|
|
fi, err := os.Stat(file)
|
|
|
|
if err != nil {
|
2020-06-30 21:58:18 +02:00
|
|
|
return nil, fmt.Errorf("cannot stat file %q for part %q: %w", file, p.Path, err)
|
2019-11-07 20:05:39 +01:00
|
|
|
}
|
|
|
|
p.ActualSize = uint64(fi.Size())
|
|
|
|
parts = append(parts, p)
|
|
|
|
}
|
|
|
|
return parts, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeletePart deletes the given part p from fs.
|
|
|
|
func (fs *FS) DeletePart(p common.Part) error {
|
|
|
|
path := fs.path(p)
|
|
|
|
if err := os.Remove(path); err != nil {
|
2020-06-30 21:58:18 +02:00
|
|
|
return fmt.Errorf("cannot remove %q: %w", path, err)
|
2019-11-07 20:05:39 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveEmptyDirs recursively removes all the empty directories in fs.
|
|
|
|
func (fs *FS) RemoveEmptyDirs() error {
|
|
|
|
return fscommon.RemoveEmptyDirs(fs.Dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CopyPart copies the part p from srcFS to fs.
|
|
|
|
//
|
|
|
|
// srcFS must have *FS type.
|
|
|
|
func (fs *FS) CopyPart(srcFS common.OriginFS, p common.Part) error {
|
|
|
|
src, ok := srcFS.(*FS)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("cannot perform server-side copying from %s to %s: both of them must be fsremote", srcFS, fs)
|
|
|
|
}
|
|
|
|
srcPath := src.path(p)
|
|
|
|
dstPath := fs.path(p)
|
|
|
|
if err := fs.mkdirAll(dstPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Attempt to create hardlink from srcPath to dstPath.
|
|
|
|
if err := os.Link(srcPath, dstPath); err == nil {
|
2023-05-03 10:48:53 +02:00
|
|
|
libfs.MustSyncPath(dstPath)
|
|
|
|
return nil
|
2019-11-07 20:05:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cannot create hardlink. Just copy file contents
|
|
|
|
srcFile, err := os.Open(srcPath)
|
|
|
|
if err != nil {
|
2022-12-04 06:55:06 +01:00
|
|
|
return fmt.Errorf("cannot open source file: %w", err)
|
2019-11-07 20:05:39 +01:00
|
|
|
}
|
|
|
|
dstFile, err := os.Create(dstPath)
|
|
|
|
if err != nil {
|
|
|
|
_ = srcFile.Close()
|
2022-12-04 06:55:06 +01:00
|
|
|
return fmt.Errorf("cannot create destination file: %w", err)
|
2019-11-07 20:05:39 +01:00
|
|
|
}
|
|
|
|
n, err := io.Copy(dstFile, srcFile)
|
2023-05-03 10:48:53 +02:00
|
|
|
if err := dstFile.Sync(); err != nil {
|
|
|
|
return fmt.Errorf("cannot fsync dstFile: %q: %w", dstFile.Name(), err)
|
|
|
|
}
|
2019-11-07 20:05:39 +01:00
|
|
|
if err1 := dstFile.Close(); err1 != nil {
|
|
|
|
err = err1
|
|
|
|
}
|
|
|
|
if err1 := srcFile.Close(); err1 != nil {
|
|
|
|
err = err1
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
_ = os.RemoveAll(dstPath)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if uint64(n) != p.Size {
|
|
|
|
_ = os.RemoveAll(dstPath)
|
|
|
|
return fmt.Errorf("unexpected number of bytes copied from %q to %q; got %d bytes; want %d bytes", srcPath, dstPath, n, p.Size)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DownloadPart download part p from fs to w.
|
|
|
|
func (fs *FS) DownloadPart(p common.Part, w io.Writer) error {
|
|
|
|
path := fs.path(p)
|
|
|
|
r, err := os.Open(path)
|
|
|
|
if err != nil {
|
2022-12-04 06:55:06 +01:00
|
|
|
return err
|
2019-11-07 20:05:39 +01:00
|
|
|
}
|
|
|
|
n, err := io.Copy(w, r)
|
|
|
|
if err1 := r.Close(); err1 != nil && err == nil {
|
|
|
|
err = err1
|
|
|
|
}
|
|
|
|
if err != nil {
|
2020-06-30 21:58:18 +02:00
|
|
|
return fmt.Errorf("cannot download data from %q: %w", path, err)
|
2019-11-07 20:05:39 +01:00
|
|
|
}
|
|
|
|
if uint64(n) != p.Size {
|
|
|
|
return fmt.Errorf("wrong data size downloaded from %q; got %d bytes; want %d bytes", path, n, p.Size)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UploadPart uploads p from r to fs.
|
|
|
|
func (fs *FS) UploadPart(p common.Part, r io.Reader) error {
|
|
|
|
path := fs.path(p)
|
|
|
|
if err := fs.mkdirAll(path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
w, err := os.Create(path)
|
|
|
|
if err != nil {
|
2020-06-30 21:58:18 +02:00
|
|
|
return fmt.Errorf("cannot create file %q: %w", path, err)
|
2019-11-07 20:05:39 +01:00
|
|
|
}
|
|
|
|
n, err := io.Copy(w, r)
|
2023-05-03 10:48:53 +02:00
|
|
|
if err := w.Sync(); err != nil {
|
|
|
|
return fmt.Errorf("cannot fsync file: %q: %w", w.Name(), err)
|
|
|
|
}
|
2019-11-07 20:05:39 +01:00
|
|
|
if err1 := w.Close(); err1 != nil && err == nil {
|
|
|
|
err = err1
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
_ = os.RemoveAll(path)
|
2020-06-30 21:58:18 +02:00
|
|
|
return fmt.Errorf("cannot upload data to %q: %w", path, err)
|
2019-11-07 20:05:39 +01:00
|
|
|
}
|
|
|
|
if uint64(n) != p.Size {
|
|
|
|
_ = os.RemoveAll(path)
|
|
|
|
return fmt.Errorf("wrong data size uploaded to %q; got %d bytes; want %d bytes", path, n, p.Size)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *FS) mkdirAll(filePath string) error {
|
|
|
|
dir := filepath.Dir(filePath)
|
|
|
|
if err := os.MkdirAll(dir, 0700); err != nil {
|
2020-06-30 21:58:18 +02:00
|
|
|
return fmt.Errorf("cannot create directory %q: %w", dir, err)
|
2019-11-07 20:05:39 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *FS) path(p common.Part) string {
|
2023-09-18 16:10:16 +02:00
|
|
|
return filepath.Join(p.LocalPath(fs.Dir), fmt.Sprintf("%016X_%016X_%016X", p.FileSize, p.Offset, p.Size))
|
2019-11-07 20:05:39 +01:00
|
|
|
}
|
2020-01-09 14:24:26 +01:00
|
|
|
|
|
|
|
// DeleteFile deletes filePath at fs.
|
|
|
|
//
|
|
|
|
// The function does nothing if the filePath doesn't exist.
|
|
|
|
func (fs *FS) DeleteFile(filePath string) error {
|
|
|
|
path := filepath.Join(fs.Dir, filePath)
|
|
|
|
err := os.Remove(path)
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
2020-06-30 21:58:18 +02:00
|
|
|
return fmt.Errorf("cannot remove %q: %w", path, err)
|
2020-01-09 14:24:26 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateFile creates filePath at fs and puts data into it.
|
|
|
|
//
|
|
|
|
// The file is overwritten if it exists.
|
|
|
|
func (fs *FS) CreateFile(filePath string, data []byte) error {
|
2020-06-05 18:28:14 +02:00
|
|
|
path := filepath.Join(fs.Dir, filePath)
|
|
|
|
if err := fs.mkdirAll(path); err != nil {
|
2020-06-05 09:24:51 +02:00
|
|
|
return err
|
|
|
|
}
|
2022-08-21 22:51:13 +02:00
|
|
|
if err := os.WriteFile(path, data, 0600); err != nil {
|
2020-06-30 21:58:18 +02:00
|
|
|
return fmt.Errorf("cannot write %d bytes to %q: %w", len(data), path, err)
|
2020-01-09 14:24:26 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasFile returns true if filePath exists at fs.
|
|
|
|
func (fs *FS) HasFile(filePath string) (bool, error) {
|
|
|
|
path := filepath.Join(fs.Dir, filePath)
|
|
|
|
fi, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false, nil
|
|
|
|
}
|
2020-06-30 21:58:18 +02:00
|
|
|
return false, fmt.Errorf("cannot stat %q: %w", path, err)
|
2020-01-09 14:24:26 +01:00
|
|
|
}
|
|
|
|
if fi.IsDir() {
|
|
|
|
return false, fmt.Errorf("%q is directory, while file is needed", path)
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
2023-08-14 02:17:12 +02:00
|
|
|
|
|
|
|
// ReadFile returns the content of filePath at fs.
|
|
|
|
func (fs *FS) ReadFile(filePath string) ([]byte, error) {
|
|
|
|
path := filepath.Join(fs.Dir, filePath)
|
|
|
|
return os.ReadFile(path)
|
|
|
|
}
|