maddy

Fork https://github.com/foxcpp/maddy

git clone git://git.lin.moe/go/maddy.git

 1package fs
 2
 3import (
 4	"context"
 5	"fmt"
 6	"io"
 7	"os"
 8	"path/filepath"
 9
10	"github.com/foxcpp/maddy/framework/config"
11	"github.com/foxcpp/maddy/framework/module"
12)
13
14// FSStore struct represents directory on FS used to store blobs.
15type FSStore struct {
16	instName string
17	root     string
18}
19
20func New(_, instName string, _, inlineArgs []string) (module.Module, error) {
21	switch len(inlineArgs) {
22	case 0:
23		return &FSStore{instName: instName}, nil
24	case 1:
25		return &FSStore{instName: instName, root: inlineArgs[0]}, nil
26	default:
27		return nil, fmt.Errorf("storage.blob.fs: 1 or 0 arguments expected")
28	}
29}
30
31func (s FSStore) Name() string {
32	return "storage.blob.fs"
33}
34
35func (s FSStore) InstanceName() string {
36	return s.instName
37}
38
39func (s *FSStore) Init(cfg *config.Map) error {
40	cfg.String("root", false, false, s.root, &s.root)
41	if _, err := cfg.Process(); err != nil {
42		return err
43	}
44
45	if s.root == "" {
46		return config.NodeErr(cfg.Block, "storage.blob.fs: directory not set")
47	}
48
49	if err := os.MkdirAll(s.root, os.ModeDir|os.ModePerm); err != nil {
50		return err
51	}
52
53	return nil
54}
55
56func (s *FSStore) Open(_ context.Context, key string) (io.ReadCloser, error) {
57	f, err := os.Open(filepath.Join(s.root, key))
58	if err != nil {
59		if os.IsNotExist(err) {
60			return nil, module.ErrNoSuchBlob
61		}
62		return nil, err
63	}
64	return f, nil
65}
66
67func (s *FSStore) Create(_ context.Context, key string, blobSize int64) (module.Blob, error) {
68	f, err := os.Create(filepath.Join(s.root, key))
69	if err != nil {
70		return nil, err
71	}
72	if blobSize >= 0 {
73		if err := f.Truncate(blobSize); err != nil {
74			return nil, err
75		}
76	}
77	return f, nil
78}
79
80func (s *FSStore) Delete(_ context.Context, keys []string) error {
81	for _, key := range keys {
82		if err := os.Remove(filepath.Join(s.root, key)); err != nil {
83			if os.IsNotExist(err) {
84				continue
85			}
86			return err
87		}
88	}
89	return nil
90}
91
92func init() {
93	var _ module.BlobStore = &FSStore{}
94	module.Register(FSStore{}.Name(), New)
95}