/
vfs_storage_snap.go
69 lines (53 loc) · 1.33 KB
/
vfs_storage_snap.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
package storage
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"sync/atomic"
"github.com/akutz/gotil"
"github.com/rexray/rexray/libstorage/api/types"
"github.com/rexray/rexray/libstorage/api/utils"
)
func (d *driver) getSnapPath(snapshotID string) string {
return fmt.Sprintf("%s/%s.json", d.snapPath, snapshotID)
}
func (d *driver) getSnapshotByID(snapshotID string) (*types.Snapshot, error) {
snapJSONPath := d.getSnapPath(snapshotID)
if !gotil.FileExists(snapJSONPath) {
return nil, utils.NewNotFoundError(snapshotID)
}
return readSnapshot(snapJSONPath)
}
func readSnapshot(path string) (*types.Snapshot, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
v := &types.Snapshot{}
if err := json.NewDecoder(f).Decode(v); err != nil {
return nil, err
}
return v, nil
}
func (d *driver) writeSnapshot(s *types.Snapshot) error {
snapJSONPath := d.getSnapPath(s.ID)
f, err := os.Create(snapJSONPath)
if err != nil {
return err
}
defer f.Close()
enc := json.NewEncoder(f)
if err := enc.Encode(s); err != nil {
return err
}
return nil
}
func (d *driver) getSnapJSONs() ([]string, error) {
return filepath.Glob(d.snapJSONGlobPatt)
}
func (d *driver) newSnapshotID(volumeID string) string {
return fmt.Sprintf(
"%s-%03d", volumeID, atomic.AddInt64(&d.snapCount, 1))
}