From 3ea5d7e2843730da038c0ce42ac097a10690fc8a Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Thu, 13 Feb 2020 07:30:41 -0700 Subject: [PATCH] implement file tree --- db.go | 2 ++ db_test.go | 6 +++++ files.go | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ type.go | 3 +++ 4 files changed, 81 insertions(+) create mode 100755 files.go diff --git a/db.go b/db.go index 60bc906..44927cf 100755 --- a/db.go +++ b/db.go @@ -32,6 +32,8 @@ func New(key Type, params ...string) (db DB, err error) { err = nil case RCLONE: db, err = NewRClone(params[0], params[1]) + case FILES: + db, err = NewFiles(params[0]) case BOLT: db, err = NewBolt(params[0]) case MINIO: diff --git a/db_test.go b/db_test.go index 964d3b1..70aaa96 100755 --- a/db_test.go +++ b/db_test.go @@ -96,6 +96,12 @@ func TestImplementations(t *testing.T) { cases = append(cases, bolt) } + if files, err := NewFiles(path.Join(dir, "files")); err != nil { + t.Errorf("cannot make files: %v", err) + } else { + cases = append(cases, files) + } + if leveldb, err := NewLevelDB(path.Join(dir, "leveldb")); err != nil { t.Errorf("cannot make leveldb: %v", err) } else { diff --git a/files.go b/files.go new file mode 100755 index 0000000..8abcf4f --- /dev/null +++ b/files.go @@ -0,0 +1,70 @@ +package storage + +import ( + "io/ioutil" + "os" + "path" + "path/filepath" + "strings" +) + +type Files struct { + root string +} + +func NewFiles(root string) (*Files, error) { + _, err := os.Stat(path.Dir(root)) + if err != nil { + return nil, err + } + root, err = filepath.Abs(root) + if err != nil { + return nil, err + } + return &Files{ + root: root, + }, os.MkdirAll(root, os.ModePerm) +} + +func (b *Files) List(ns []string, limits ...string) ([]string, error) { + namespace := resolveNamespace(ns) + files := make([]string, 0) + err := filepath.Walk(b.root, func(p string, info os.FileInfo, err error) error { + if err != nil { + return err + } + if info.IsDir() { + return nil + } + if !strings.HasPrefix(strings.TrimPrefix(p, b.root+"/"), namespace) { + return nil + } + files = append(files, path.Join(b.root, p)) + return nil + }) + return files, err + /* + namespace := resolveNamespace(ns) + limits = resolveLimits(limits) + */ +} + +func (b *Files) Get(key string, ns ...string) ([]byte, error) { + namespace := resolveNamespace(ns) + path := path.Join(b.root, namespace, key) + return ioutil.ReadFile(path) +} + +func (b *Files) Set(key string, value []byte, ns ...string) error { + namespace := resolveNamespace(ns) + dir := path.Join(b.root, namespace) + if err := os.MkdirAll(dir, os.ModePerm); err != nil { + return err + } + path := path.Join(dir, key) + return ioutil.WriteFile(path, value, os.ModePerm) +} + +func (b *Files) Close() error { + return nil +} diff --git a/type.go b/type.go index 7ac115d..edbd6d7 100755 --- a/type.go +++ b/type.go @@ -11,6 +11,7 @@ const ( REDIS = Type(iota) DYNOMITE = Type(iota) BOLT = Type(iota) + FILES = Type(iota) COCKROACH = Type(iota) CACHE = Type(iota) LEVELDB = Type(iota) @@ -33,6 +34,8 @@ func (t Type) String() string { return "rclone" case COCKROACH: return "cockroach" + case FILES: + return "files" case BOLT: return "bolt" case MINIO: