This repository has been archived by the owner on Aug 15, 2020. It is now read-only.
/
restore.go
131 lines (120 loc) · 3.73 KB
/
restore.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* Copyright 2012 Marc-Antoine Ruel. Licensed under the Apache License, Version
2.0 (the "License"); you may not use this file except in compliance with the
License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0. Unless required by applicable law or
agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the License for the specific language governing permissions and
limitations under the License. */
package main
import (
"fmt"
"io"
"log"
"os"
"path/filepath"
"github.com/maruel/dumbcas/dumbcaslib"
"github.com/maruel/interrupt"
"github.com/maruel/subcommands"
)
var cmdRestore = &subcommands.Command{
UsageLine: "restore <node> -out <out>",
ShortDesc: "restores a tree from a dumbcas archive",
LongDesc: "Restores files listed in <node> archive to a directory from a DumbCas(tm) archive.",
CommandRun: func() subcommands.CommandRun {
c := &restoreRun{}
c.Init()
c.Flags.StringVar(&c.Out, "out", "", "Directory to restore data to; required.")
return c
},
}
type restoreRun struct {
CommonFlags
Out string
}
// Restores entries and keep going on in case of error. Returns the first seen
// error.
// Do not overwrite files. A file already present is considered an error.
func restoreEntry(l *log.Logger, cas dumbcaslib.CasTable, entry *dumbcaslib.Entry, root string) (count int, out error) {
if entry.Sha1 != "" {
f, err := cas.Open(entry.Sha1)
if err != nil {
out = fmt.Errorf("Failed to fetch %s for %s: %s", entry.Sha1, root, err)
} else {
defer func() {
_ = f.Close()
}()
baseDir := filepath.Dir(root)
if err = os.MkdirAll(baseDir, 0755); err != nil && !os.IsExist(err) {
out = fmt.Errorf("Failed to create %s: %s", baseDir, err)
} else {
dst, err := os.OpenFile(root, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0644)
if err != nil {
out = fmt.Errorf("Failed to create %s in %s: %s", root, baseDir, err)
} else {
size, err := io.Copy(dst, f)
if err != nil {
out = fmt.Errorf("Failed to copy %s: %s", root, err)
} else if size != entry.Size {
out = fmt.Errorf("Failed to write %s, expected %d, wrote %d", root, entry.Size, size)
} else {
count++
}
}
}
}
if out != nil {
l.Printf("%s(%d): %s", root, entry.Size, out)
} else {
l.Printf("%s(%d)", root, entry.Size)
}
}
for name, child := range entry.Files {
c, err := restoreEntry(l, cas, child, filepath.Join(root, name))
if err != nil && out == nil {
out = err
}
count += c
}
return
}
func (c *restoreRun) main(a DumbcasApplication, nodeArg string) error {
if err := c.Parse(a, true); err != nil {
return err
}
// Load the Node and process it.
// Do it serially for now, assuming that it is I/O bound on magnetic disks.
// For a network CAS, it would be good to implement concurrent fetches.
f, err := c.nodes.Open(nodeArg)
if err != nil {
return err
}
defer func() {
_ = f.Close()
}()
node := &dumbcaslib.Node{}
if err := dumbcaslib.LoadReaderAsJSON(f, node); err != nil {
return err
}
entry, err := dumbcaslib.LoadEntry(c.cas, node.Entry)
if err != nil {
return err
}
// TODO(maruel): Progress bar.
count, err := restoreEntry(a.GetLog(), c.cas, entry, c.Out)
fmt.Fprintf(a.GetOut(), "Restored %d files in %s\n", count, c.Out)
return err
}
func (c *restoreRun) Run(a subcommands.Application, args []string, _ subcommands.Env) int {
if len(args) != 1 {
fmt.Fprintf(a.GetErr(), "%s: Must only provide a <node>.\n", a.GetName())
return 1
}
interrupt.HandleCtrlC()
d := a.(DumbcasApplication)
if err := c.main(d, args[0]); err != nil {
fmt.Fprintf(a.GetErr(), "%s: %s\n", a.GetName(), err)
return 1
}
return 0
}