Skip to content

Commit

Permalink
Save blab to metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
lylex committed Dec 13, 2018
1 parent e14f801 commit b24d52a
Show file tree
Hide file tree
Showing 14 changed files with 225 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,4 @@ nfpm:
recommends:
- tig
empty_folders:
- /var/lib/drm
- /var/local/lib/drm
5 changes: 2 additions & 3 deletions cmd/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ const (
)

// lsCmd represents the ls command.
// TODO fill the TBDs
var lsCmd = &cobra.Command{
Use: lsCmdUse,
Short: "short TBD",
Long: `long TBD`,
Short: "list all the deleted objects",
Long: `list all the deleted objects, and all can be resumed`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("in the ls cmd")
},
Expand Down
12 changes: 9 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"
"os"
"path/filepath"
"time"

"github.com/lylex/drm/pkg/blobs"
"github.com/lylex/drm/pkg/files"
"github.com/lylex/drm/pkg/utils"
"github.com/spf13/cobra"
Expand All @@ -21,6 +21,8 @@ const (

// TempFileStorePath TODO replate it
TempFileStorePath = "/Users/xuq3/workspace/drm/tem"

CfgBlobPathKey = "blobPath"
)

var (
Expand Down Expand Up @@ -80,8 +82,12 @@ var RootCmd = &cobra.Command{
if files.IsDir(path) && !isRecursive {
utils.ErrExit(fmt.Sprintf("%s: %s: is a directory\n", RootCmdName, path), nil)
}
files.Move(path, filepath.Join(string(viper.GetString("dataPath")),
fmt.Sprintf("%d_%s", time.Now().UnixNano(), files.Name(path))))

blob := blobs.Create(path)
files.Move(path, filepath.Join(string(viper.GetString(CfgBlobPathKey)), blob.Name()))
if err := blob.Save(); err != nil {
utils.ErrExit(fmt.Sprintf("%s: failed to save metadata for %s\n", RootCmdName, path), err)
}
}
},
}
Expand Down
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,21 @@ module github.com/lylex/drm
require (
github.com/BurntSushi/toml v0.3.1 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/json-iterator/go v1.1.5
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/cobra v0.0.3
github.com/spf13/pflag v1.0.3 // indirect
github.com/spf13/viper v1.2.1
github.com/stretchr/testify v1.2.2 // indirect
github.com/tidwall/btree v0.0.0-20170113224114-9876f1454cf0 // indirect
github.com/tidwall/buntdb v1.0.0
github.com/tidwall/gjson v1.1.3 // indirect
github.com/tidwall/grect v0.0.0-20161006141115-ba9a043346eb // indirect
github.com/tidwall/match v1.0.1 // indirect
github.com/tidwall/rtree v0.0.0-20180113144539-6cd427091e0e // indirect
github.com/tidwall/tinyqueue v0.0.0-20180302190814-1e39f5511563 // indirect
)

replace github.com/lylex/drm/pkg/utils => ./pkg/utils
Expand Down
20 changes: 20 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@ github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/json-iterator/go v1.1.5 h1:gL2yXlmiIo4+t+y32d4WGwOjKGYcGOuyrg46vadswDE=
github.com/json-iterator/go v1.1.5/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDePerRcY=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/mitchellh/mapstructure v1.0.0 h1:vVpGvMXJPqSDh2VYHF7gsfQj8Ncx+Xw5Y1KHeTRY+7I=
github.com/mitchellh/mapstructure v1.0.0/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand All @@ -31,6 +37,20 @@ github.com/spf13/viper v1.2.1 h1:bIcUwXqLseLF3BDAZduuNfekWG87ibtFxi59Bq+oI9M=
github.com/spf13/viper v1.2.1/go.mod h1:P4AexN0a+C9tGAnUFNwDMYYZv3pjFuvmeiMyKRaNVlI=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/tidwall/btree v0.0.0-20170113224114-9876f1454cf0 h1:QnyrPZZvPmR0AtJCxxfCtI1qN+fYpKTKJ/5opWmZ34k=
github.com/tidwall/btree v0.0.0-20170113224114-9876f1454cf0/go.mod h1:huei1BkDWJ3/sLXmO+bsCNELL+Bp2Kks9OLyQFkzvA8=
github.com/tidwall/buntdb v1.0.0 h1:urIJqQ8OR9fibXXtFSu8sR5arMqZK8ZnNq22yWl+A+8=
github.com/tidwall/buntdb v1.0.0/go.mod h1:Y39xhcDW10WlyYXeLgGftXVbjtM0QP+/kpz8xl9cbzE=
github.com/tidwall/gjson v1.1.3 h1:u4mspaByxY+Qk4U1QYYVzGFI8qxN/3jtEV0ZDb2vRic=
github.com/tidwall/gjson v1.1.3/go.mod h1:c/nTNbUr0E0OrXEhq1pwa8iEgc2DOt4ZZqAt1HtCkPA=
github.com/tidwall/grect v0.0.0-20161006141115-ba9a043346eb h1:5NSYaAdrnblKByzd7XByQEJVT8+9v0W/tIY0Oo4OwrE=
github.com/tidwall/grect v0.0.0-20161006141115-ba9a043346eb/go.mod h1:lKYYLFIr9OIgdgrtgkZ9zgRxRdvPYsExnYBsEAd8W5M=
github.com/tidwall/match v1.0.1 h1:PnKP62LPNxHKTwvHHZZzdOAOCtsJTjo6dZLCwpKm5xc=
github.com/tidwall/match v1.0.1/go.mod h1:LujAq0jyVjBy028G1WhWfIzbpQfMO8bBZ6Tyb0+pL9E=
github.com/tidwall/rtree v0.0.0-20180113144539-6cd427091e0e h1:+NL1GDIUOKxVfbp2KoJQD9cTQ6dyP2co9q4yzmT9FZo=
github.com/tidwall/rtree v0.0.0-20180113144539-6cd427091e0e/go.mod h1:/h+UnNGt0IhNNJLkGikcdcJqm66zGD/uJGMRxK/9+Ao=
github.com/tidwall/tinyqueue v0.0.0-20180302190814-1e39f5511563 h1:Otn9S136ELckZ3KKDyCkxapfufrqDqwmGjcHfAyXRrE=
github.com/tidwall/tinyqueue v0.0.0-20180302190814-1e39f5511563/go.mod h1:mLqSmt7Dv/CNneF2wfcChfN1rvapyQr01LGKnKex0DQ=
golang.org/x/sys v0.0.0-20180906133057-8cf3aee42992 h1:BH3eQWeGbwRU2+wxxuuPOdFBmaiBH81O8BugSjHeTFg=
golang.org/x/sys v0.0.0-20180906133057-8cf3aee42992/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
Expand Down
66 changes: 66 additions & 0 deletions pkg/blobs/blobs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package blobs

import (
"fmt"
"math/rand"
"time"

"github.com/lylex/drm/pkg/files"
"github.com/lylex/drm/pkg/utils"
"github.com/spf13/viper"
"github.com/tidwall/buntdb"
)

const (
MaxRandomNumber int = 10000

MetaDataDB string = "data.db"

CfgDataPathKey = "dataPath"
)

type Blob struct {
FileName string
Dir string

// CreatedAt represents when the blob is created, i.e. when it is deleted.
CreatedAt time.Time
}

// Create make a blob metadate.
func Create(fullPath string) *Blob {
return &Blob{
FileName: files.Name(fullPath),
Dir: files.Dir(fullPath),
CreatedAt: time.Now(),
}
}

func (b *Blob) Name() string {
return fmt.Sprintf("%d_%4d_%s",
b.CreatedAt.UnixNano(),
rand.Intn(MaxRandomNumber),
b.FileName)
}

// TODO
func (b *Blob) Save() error {
db, err := buntdb.Open(viper.GetString(CfgDataPathKey) + MetaDataDB)
if err != nil {
return err
}
defer db.Close()

var json string
json, err = utils.Marshal(*b)
if err != nil {
return err
}

err = db.Update(func(tx *buntdb.Tx) error {
_, _, err := tx.Set(b.FileName, json, nil)
return err
})

return nil
}
10 changes: 8 additions & 2 deletions pkg/files/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package files

import (
"os"
"path/filepath"
"strings"

"github.com/lylex/drm/pkg/utils"
Expand Down Expand Up @@ -48,9 +49,14 @@ func Move(src, dir string) {
}
}

// Name is used to retrieve the name of the file or directory of the pointed path.
// Name is used to retrieve the basename of the file or directory of the given path.
func Name(path string) string {
return getFileInfo(path).Name()
return filepath.Base(path)
}

// Dir retrieves the Directory path of the passed in full path.
func Dir(fullPath string) string {
return filepath.Dir(fullPath)
}

func getFileInfo(path string) os.FileInfo {
Expand Down
20 changes: 20 additions & 0 deletions pkg/files/files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,23 @@ func TestIsAbsolutePath(t *testing.T) {
}
}
}

func TestDir(t *testing.T) {
var testcases = []struct {
path string
expectedResult string
}{
{"/dir", "/"},
{"dir", "."},
{"/dir1/dir2", "/dir1"},
{"dir1/dir2", "dir1"},
{"", "."},
}

for _, testcase := range testcases {
r := Dir(testcase.path)
if r != testcase.expectedResult {
t.Errorf("Get dir from \"%s\" failed, expect %v, got %v", testcase.path, testcase.expectedResult, r)
}
}
}
11 changes: 0 additions & 11 deletions pkg/object/object.go

This file was deleted.

28 changes: 28 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package utils

import (
"errors"
"fmt"
"os"

json "github.com/json-iterator/go"
)

// ErrExit is used to print error to stderr, and then exist with code 1.
Expand All @@ -14,3 +17,28 @@ func ErrExit(msg string, err error) {
}
os.Exit(1)
}

// Marshal is a fast struct serializing method.
// Compared with encoding/json, it is claimed to have a better performance.
func Marshal(i interface{}) (string, error) {
stream := json.ConfigFastest.BorrowStream(nil)
defer json.ConfigFastest.ReturnStream(stream)

stream.WriteVal(i)
if stream.Error != nil {
return "", errors.New("failed to marshal object")
}
return string(stream.Buffer()), nil
}

// Unmarshal is a fast struct deserializing method.
func Unmarshal(s string, obj interface{}) error {
iter := json.ConfigFastest.BorrowIterator([]byte(s))
defer json.ConfigFastest.ReturnIterator(iter)

iter.ReadVal(obj)
if iter.Error != nil {
return errors.New("failed to unmarshal to object")
}
return nil
}
50 changes: 50 additions & 0 deletions pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package utils

import "testing"

func TestMarshal(t *testing.T) {
type ColorGroup struct {
ID int
Name string
Colors []string
}
group := ColorGroup{
ID: 1,
Name: "Reds",
Colors: []string{"Crimson", "Red", "Ruby", "Maroon"},
}

result, err := Marshal(group)
expect := `{"ID":1,"Name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"]}`

if result != expect {
t.Errorf("failed to marshal object, expect %v, got %v", expect, result)
}
if err != nil {
t.Errorf("unexpected error occured: %v", err)
}
}

func TestUnmarshal(t *testing.T) {
type ColorGroup struct {
ID int
Name string
Colors []string
}
group := ColorGroup{}
str := `{"ID":1,"Name":"Reds","Colors":["Crimson","Red","Ruby","Maroon"]}`

err := Unmarshal(str, &group)
expect := ColorGroup{
ID: 1,
Name: "Reds",
Colors: []string{"Crimson", "Red", "Ruby", "Maroon"},
}

if group.Name != expect.Name || group.ID != expect.ID {
t.Errorf("failed to unmarshal string to object: expect %v, got %v", expect, group)
}
if err != nil {
t.Errorf("unexpected error occured: %v", err)
}
}
3 changes: 2 additions & 1 deletion scripts/drm.conf
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"dataPath": "/usr/local/lib/drm/"
"dataPath": "/usr/local/lib/drm/",
"blobPath": "/usr/local/lib/drm/blob/"
}
3 changes: 2 additions & 1 deletion scripts/install_mac.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
WORK_DIR="$(dirname "${BASH_SOURCE[0]}")"
CFG_DIR=/etc/drm
DATA_DIR=/usr/local/lib/drm
BLOB_DIR=$DATA_DIR/blob
BIN_DIR=/usr/local/bin/

[ -d $CFG_DIR ] || mkdir $CFG_DIR

if [ ! -d $DATA_DIR ]; then
mkdir $DATA_DIR
mkdir $DATA_DIR $BLOB_DIR
chmod -R 777 $DATA_DIR
fi

Expand Down
12 changes: 7 additions & 5 deletions scripts/preinstall.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#!/bin/bash -ex

echo "preinstall drm"
CFG_DIR=/etc/drm
DATA_DIR=/usr/local/lib/drm
BLOB_DIR=$DATA_DIR/blob

[ -d /etc/drm ] || mkdir /etc/drm
[ -d $CFG_DIR ] || mkdir $CFG_DIR

if [ ! -d /var/lib/drm ]; then
mkdir /var/lib/drm
chmod -R 666 /var/lib/drm
if [ ! -d $DATA_DIR ]; then
mkdir $DATA_DIR $BLOB_DIR
chmod -R 777 $DATA_DIR
fi

0 comments on commit b24d52a

Please sign in to comment.