aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main.go9
-rw-r--r--restore_dir.go48
-rw-r--r--write_dir.go37
3 files changed, 77 insertions, 17 deletions
diff --git a/main.go b/main.go
index 9057231..b0f3f6b 100644
--- a/main.go
+++ b/main.go
@@ -12,7 +12,7 @@ type subcmd func(args []string) int
var subcmds = map[string]subcmd{
"write-dir": WriteDir,
- "restore-dir": notImplementedYet,
+ "restore-dir": RestoreDir,
"take-snapshot": notImplementedYet,
"create-snapshot": notImplementedYet,
"list-snapshots": notImplementedYet,
@@ -56,7 +56,12 @@ func Main() int {
}
defer objectstore.Close()
- remaining := flag.Args()
+ remaining := make([]string, 0)
+ for _, arg := range flag.Args() {
+ if arg != "" {
+ remaining = append(remaining, arg)
+ }
+ }
var cmd subcmd
if len(remaining) > 0 {
diff --git a/restore_dir.go b/restore_dir.go
new file mode 100644
index 0000000..65f2785
--- /dev/null
+++ b/restore_dir.go
@@ -0,0 +1,48 @@
+package main
+
+import (
+ "code.laria.me/petrific/backup"
+ "code.laria.me/petrific/fs"
+ "code.laria.me/petrific/objects"
+ "fmt"
+ "os"
+)
+
+func RestoreDir(args []string) int {
+ usage := subcmdUsage("restore-dir", "directory object-id", nil)
+
+ if len(args) != 2 {
+ usage()
+ return 2
+ }
+
+ dir_path, err := abspath(args[0])
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "restore-dir: %s\n", err)
+ return 1
+ }
+
+ d, err := fs.OpenOSFile(dir_path)
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "restore-dir: %s\n", err)
+ return 1
+ }
+
+ if d.Type() != fs.FDir {
+ fmt.Fprintf(os.Stderr, "restore-dir: %s is not a directory\n", dir_path)
+ return 1
+ }
+
+ id, err := objects.ParseObjectId(args[1])
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "restore-dir: %s\n", err)
+ return 1
+ }
+
+ if err := backup.RestoreDir(objectstore, id, d); err != nil {
+ fmt.Fprintf(os.Stderr, "restore-dir: %s\n", err)
+ return 1
+ }
+
+ return 0
+}
diff --git a/write_dir.go b/write_dir.go
index cc148c2..e701a56 100644
--- a/write_dir.go
+++ b/write_dir.go
@@ -9,38 +9,45 @@ import (
"path"
)
+func abspath(p string) (string, error) {
+ if p[0] != '/' {
+ pwd, err := os.Getwd()
+ if err != nil {
+ return "", fmt.Errorf("abspath(%s): %s", p, err)
+ }
+ p = pwd + "/" + p
+ }
+ return path.Clean(p), nil
+}
+
func WriteDir(args []string) int {
- if len(args) != 1 || len(args[0]) == 0 {
- subcmdUsage("write-dir", "directory", nil)()
+ usage := subcmdUsage("write-dir", "directory", nil)
+
+ if len(args) != 1 {
+ usage()
return 2
}
- dir_path := args[0]
- // Make path absolute
- if dir_path[0] != '/' {
- pwd, err := os.Getwd()
- if err != nil {
- fmt.Fprintf(os.Stderr, "write-dir: %s", err)
- return 1
- }
- dir_path = pwd + "/" + dir_path
+ dir_path, err := abspath(args[0])
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "write-dir: %s\n", err)
+ return 1
}
- dir_path = path.Clean(dir_path)
d, err := fs.OpenOSFile(dir_path)
if err != nil {
- fmt.Fprintf(os.Stderr, "write-dir: %s", err)
+ fmt.Fprintf(os.Stderr, "write-dir: %s\n", err)
return 1
}
if d.Type() != fs.FDir {
- fmt.Fprintf(os.Stderr, "write-dir: %s is not a directory", dir_path)
+ fmt.Fprintf(os.Stderr, "write-dir: %s is not a directory\n", dir_path)
return 1
}
id, err := backup.WriteDir(objectstore, dir_path, d, cache.NopCache{})
if err != nil {
- fmt.Fprintf(os.Stderr, "write-dir: %s", err)
+ fmt.Fprintf(os.Stderr, "write-dir: %s\n", err)
return 1
}