aboutsummaryrefslogtreecommitdiff
path: root/snap/fs.go
blob: d0dc83d1548d5bbf9e15a8ce374cf4f88b8341ef (plain)
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package snap

import (
	"time"
	"path/filepath"
	"os"
	"sort"
)

type Snapshot struct {
	Name      string
	ModTime   time.Time
	Reference string
}

func getZFSRoot(reference string) string {
	cursor := filepath.Dir(reference)
	zfsNotFound := false

	for !zfsNotFound {
		zfsDir := filepath.Join(cursor, ".zfs")
		
		if _, err := os.Stat(zfsDir); !os.IsNotExist(err) {
			break
		} 

		cursor = filepath.Join(cursor, "..")

		zfsNotFound = filepath.ToSlash(cursor) == "/"
	}

	if(zfsNotFound) {
		die.Fatal("Could not find a .zfs directory. Are you within a zfs dataset?")
	}

	return cursor
}

func getSnapshots(reference string) []*Snapshot {
	zfsRoot := getZFSRoot(reference)

	snapshotDir := filepath.Join(zfsRoot, ".zfs", "snapshot");

	files, err := os.ReadDir(snapshotDir)
	if err != nil {
		die.Fatalf("Could not find the snapshots directory inside %s", snapshotDir);
	}

	relPath, err := filepath.Rel(zfsRoot, reference)
	if err != nil {
		die.Fatal("Could not find relative path to reference file: %v", err)
	}

	snaps := make([]*Snapshot, len(files))

	for i, file := range files {

		info, err := file.Info()
		if(err != nil) {
			die.Fatalf("Could not read file %v", err)
		}

		name := file.Name()
		snaps[i] = &Snapshot{
			Name: name,
			ModTime: info.ModTime(),
		}

		pathInSnap := filepath.Join(snapshotDir, name, relPath)

		if _, err := os.Stat(pathInSnap); err != nil {
			if !os.IsNotExist(err) {
				die.Fatalf("Error encountered while attempting to read reference file in snapshot:\n%v", err)
			}
		} else {
			snaps[i].Reference = pathInSnap
		}

	}

	return snaps
}

func GetTimeseries(reference string) []*Snapshot {
	reference, err := filepath.Abs(reference)

	if err != nil {
	  die.Fatal(err)
	}
	
	snaps := getSnapshots(reference)

	sort.Slice(snaps, func(i, j int) bool {
		return snaps[i].ModTime.Before(snaps[j].ModTime)
	})

	return snaps
}

func buildNameMap(snapTs []*Snapshot) map[string] int {
	namemap := make(map[string] int)

	for i, snap := range snapTs {
		namemap[snap.Name] = i
	}

	return namemap
}

type SnapshotOracle struct {
	timeseries []*Snapshot
	namemap    map[string] int
};

func GetOracle(reference string) *SnapshotOracle {
	timeseries := GetTimeseries(reference)
	namemap := buildNameMap(timeseries)


	return &SnapshotOracle {
		timeseries,
		namemap,
	}
}

func (oracle *SnapshotOracle) ResolveRelative(ref *Relative) string {
	n_ts := len(oracle.timeseries)
	wanted := n_ts - 1

	if ref.snapshot != "" {
		id, ok := oracle.namemap[ref.snapshot]

		if !ok {
			die.Fatal("Could not find the snapshot %s", ref.snapshot)
		}

		wanted = id
	}

	wanted += ref.offset

	if wanted < 0 || wanted >= n_ts {
		die.Fatal("The snapshot you requested is out of range.")
	}

	return oracle.timeseries[wanted].Reference
}