1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
package backup
import (
"context"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"gitlab.com/gitlab-org/gitaly/v16/internal/helper/perm"
)
// FilesystemSink is a sink for creating and restoring backups from the local filesystem.
type FilesystemSink struct {
path string
}
// NewFilesystemSink returns a sink that uses a local filesystem to work with data.
func NewFilesystemSink(path string) *FilesystemSink {
return &FilesystemSink{
path: path,
}
}
// Write creates required file structure and stored data from r into relativePath location.
// If created file is empty it will be removed.
func (fs *FilesystemSink) Write(ctx context.Context, relativePath string, r io.Reader) (returnErr error) {
path := filepath.Join(fs.path, relativePath)
dir := filepath.Dir(path)
if err := os.MkdirAll(dir, perm.PrivateDir); err != nil {
return fmt.Errorf("create directory structure %q: %w", dir, err)
}
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, perm.PrivateFile)
if err != nil {
return fmt.Errorf("write file %q: %w", path, err)
}
defer func() {
if err := f.Close(); err != nil && returnErr == nil {
returnErr = fmt.Errorf("write file %q: %w", path, err)
}
}()
if _, err := io.Copy(f, r); err != nil {
return fmt.Errorf("write file %q: %w", path, err)
}
return nil
}
// GetReader returns a reader of the requested file path.
// It's the caller's responsibility to Close returned reader once it is not needed anymore.
// If relativePath doesn't exist the ErrDoesntExist is returned.
func (fs *FilesystemSink) GetReader(ctx context.Context, relativePath string) (io.ReadCloser, error) {
path := filepath.Join(fs.path, relativePath)
f, err := os.Open(path)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
err = ErrDoesntExist
}
return nil, fmt.Errorf("filesystem sink: get reader for %q: %w", relativePath, err)
}
return f, nil
}
|