aboutsummaryrefslogtreecommitdiff
path: root/snap/fs.go
blob: b1cf513eaed38159d64d1e7fc3ac9062f1f2f50c (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package snap

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

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 (snap * Snapshot) tryReference(reference string) {
	if _, err := os.Stat(reference); err != nil {

		if !os.IsNotExist(err) {
			die.Fatalf("Error encountered while attempting to read reference file in snapshot:\n%v", err)
		}

		snap.Reference = ""
	} else {
		snap.Reference = reference
	}
}

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.Fatalf("Could not find relative path to reference file: %v", err)
	}

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

	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)

		snaps[i].tryReference(pathInSnap)
	}

	
	snaps[n_snaps - 1] = &Snapshot {
		ModTime: time.Now(),
		Name: "@",
	}

	snaps[n_snaps - 1].tryReference(reference)

	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 referenceMap(snapTs []*Snapshot) []int {
	var trimmed []int

	for i, snap := range snapTs {
		if snap.Reference != "" {
			trimmed = append(trimmed, i)
		}
	}

	return trimmed
}

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) PathTo(ref *Relative) string {
	n_ts := len(oracle.timeseries)
	nthRef := referenceMap(oracle.timeseries)

	if len(nthRef) == 0 {
		die.Fatalf("The reference file is not contained in any snapshot.")
	}

	var wanted int
	switch (ref.snapshot) {
	case "@":
		wanted = nthRef[len(nthRef) - 1]
	case "%":
		wanted = nthRef[0]
	default:
		id, ok := oracle.namemap[ref.snapshot]

		if !ok {
			die.Fatalf("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
}

func (oracle *SnapshotOracle) ReadAll(ref *Relative) string {
	path := oracle.PathTo(ref)

	file, err := os.Open(path)

	data, err := io.ReadAll(file)

	if err != nil {
		die.Fatalf("Can't read snapshot.\n%v", err)
	}

	return string(data)
}