2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-21 20:35:12 +00:00

Remove old cmd/ experiments

This commit is contained in:
Alexander Neumann 2014-09-21 16:02:57 +02:00
parent 0a4c9aab36
commit f1f96c4206
3 changed files with 0 additions and 328 deletions

View File

@ -1,37 +0,0 @@
package main
import (
"encoding/json"
"fmt"
"os"
"github.com/fd0/khepri"
)
func main() {
if len(os.Args) == 1 {
fmt.Printf("usage: %s [file] [file] [...]\n", os.Args[0])
os.Exit(1)
}
for _, path := range os.Args[1:] {
fmt.Printf("lstat %s\n", path)
fi, err := os.Lstat(path)
if err != nil {
fmt.Fprintf(os.Stderr, "%v", err)
continue
}
node, err := khepri.NodeFromFileInfo(path, fi)
if err != nil {
fmt.Printf("err: %v\n", err)
}
buf, err := json.MarshalIndent(node, "", " ")
if err != nil {
panic(err)
}
fmt.Printf("%s\n", string(buf))
}
}

View File

@ -1,222 +0,0 @@
package main
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"strings"
)
func check(err error) {
if err != nil {
panic(err)
}
}
// References content within a repository.
type ID []byte
func (id ID) String() string {
return hex.EncodeToString(id)
}
func (id ID) MarshalJSON() ([]byte, error) {
return json.Marshal(id.String())
}
func (id *ID) UnmarshalJSON(b []byte) error {
var s string
err := json.Unmarshal(b, &s)
if err != nil {
return err
}
*id = make([]byte, len(s)/2)
_, err = hex.Decode(*id, []byte(s))
if err != nil {
return err
}
return nil
}
// ParseID converts the given string to an ID.
func ParseID(s string) ID {
b, err := hex.DecodeString(s)
if err != nil {
panic(err)
}
return ID(b)
}
type Repository interface {
Store([]byte) ID
Get(ID) []byte
}
type Repo map[string][]byte
func (r Repo) Store(buf []byte) ID {
hash := sha256.New()
_, err := hash.Write(buf)
check(err)
id := ID(hash.Sum([]byte{}))
r[id.String()] = buf
return id
}
func (r Repo) Get(id ID) []byte {
buf, ok := r[id.String()]
if !ok {
panic("no such id")
}
return buf
}
func (r Repo) Dump(wr io.Writer) {
for k, v := range r {
_, err := wr.Write([]byte(k))
check(err)
_, err = wr.Write([]byte(":"))
check(err)
_, err = wr.Write(v)
check(err)
_, err = wr.Write([]byte("\n"))
check(err)
}
}
type Tree struct {
Nodes []*Node `json:"nodes,omitempty"`
}
type Node struct {
Name string `json:"name"`
Tree *Tree `json:"tree,omitempty"`
Subtree ID `json:"subtree,omitempty"`
Content ID `json:"content,omitempty"`
}
func (tree Tree) Save(repo Repository) ID {
// fmt.Printf("nodes: %#v\n", tree.Nodes)
for _, node := range tree.Nodes {
if node.Tree != nil {
node.Subtree = node.Tree.Save(repo)
node.Tree = nil
}
}
buf, err := json.Marshal(tree)
check(err)
return repo.Store(buf)
}
func (tree Tree) PP(wr io.Writer) {
tree.pp(0, wr)
}
func (tree Tree) pp(indent int, wr io.Writer) {
for _, node := range tree.Nodes {
if node.Tree != nil {
fmt.Printf("%s%s/\n", strings.Repeat(" ", indent), node.Name)
node.Tree.pp(indent+1, wr)
} else {
fmt.Printf("%s%s [%s]\n", strings.Repeat(" ", indent), node.Name, node.Content)
}
}
}
func create_tree(path string) *Tree {
dir, err := os.Open(path)
check(err)
entries, err := dir.Readdir(-1)
check(err)
tree := &Tree{
Nodes: make([]*Node, 0, len(entries)),
}
for _, entry := range entries {
node := &Node{}
node.Name = entry.Name()
if !entry.Mode().IsDir() && entry.Mode()&os.ModeType != 0 {
fmt.Fprintf(os.Stderr, "skipping %q\n", filepath.Join(path, entry.Name()))
continue
}
tree.Nodes = append(tree.Nodes, node)
if entry.IsDir() {
node.Tree = create_tree(filepath.Join(path, entry.Name()))
continue
}
file, err := os.Open(filepath.Join(path, entry.Name()))
defer file.Close()
check(err)
hash := sha256.New()
io.Copy(hash, file)
node.Content = hash.Sum([]byte{})
}
return tree
}
func load_tree(repo Repository, id ID) *Tree {
tree := &Tree{}
buf := repo.Get(id)
json.Unmarshal(buf, tree)
for _, node := range tree.Nodes {
if node.Subtree != nil {
node.Tree = load_tree(repo, node.Subtree)
node.Subtree = nil
}
}
return tree
}
func main() {
repo := make(Repo)
tree := create_tree(os.Args[1])
// encoder := json.NewEncoder(os.Stdout)
// fmt.Println("---------------------------")
// encoder.Encode(tree)
// fmt.Println("---------------------------")
id := tree.Save(repo)
// for k, v := range repo {
// fmt.Printf("%s: %s\n", k, v)
// }
// fmt.Println("---------------------------")
tree2 := load_tree(repo, id)
tree2.PP(os.Stdout)
// encoder.Encode(tree2)
// dumpfile, err := os.Create("dump")
// defer dumpfile.Close()
// check(err)
// repo.Dump(dumpfile)
}

View File

@ -1,69 +0,0 @@
package main
import (
"fmt"
"os"
"strings"
"github.com/fd0/khepri"
)
func check(err error) {
if err == nil {
return
}
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
func save(repo *khepri.Repository, path string) {
tree, err := khepri.NewTreeFromPath(repo, path)
check(err)
id, err := tree.Save(repo)
fmt.Printf("saved tree as %s\n", id)
}
func restore(repo *khepri.Repository, idstr string) {
id, err := khepri.ParseID(idstr)
check(err)
tree, err := khepri.NewTreeFromRepo(repo, id)
check(err)
walk(0, tree)
}
func walk(indent int, tree *khepri.Tree) {
for _, node := range tree.Nodes {
if node.Type == "dir" {
fmt.Printf("%s%s:%s/\n", strings.Repeat(" ", indent), node.Type, node.Name)
walk(indent+1, node.Tree)
} else {
fmt.Printf("%s%s:%s\n", strings.Repeat(" ", indent), node.Type, node.Name)
}
}
}
func main() {
if len(os.Args) != 3 {
fmt.Fprintf(os.Stderr, "usage: %s [save|restore] DIR\n", os.Args[0])
os.Exit(1)
}
command := os.Args[1]
arg := os.Args[2]
repo, err := khepri.NewRepository("khepri-repo")
check(err)
switch command {
case "save":
save(repo, arg)
case "restore":
restore(repo, arg)
}
}