aboutsummaryrefslogtreecommitdiff
path: root/storage
diff options
context:
space:
mode:
Diffstat (limited to 'storage')
-rw-r--r--storage/storage.go37
1 files changed, 37 insertions, 0 deletions
diff --git a/storage/storage.go b/storage/storage.go
index 7cffe9c..7d09a70 100644
--- a/storage/storage.go
+++ b/storage/storage.go
@@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"io"
+ "time"
)
var (
@@ -89,3 +90,39 @@ func GetObjectOfType(s Storage, id objects.ObjectId, t objects.ObjectType) (obje
return rawobj.Object()
}
+
+// FindLatestSnapshot finds the latest snapshot, optionally filtered by archive
+func FindLatestSnapshot(store Storage, archive string) (latestSnapshot *objects.Snapshot, err error) {
+ ids, err := store.List(objects.OTSnapshot)
+ if err != nil {
+ return nil, err
+ }
+
+ var earliestTime time.Time
+ found := false
+
+ for _, id := range ids {
+ _snapshot, err := GetObjectOfType(store, id, objects.OTSnapshot)
+ if err != nil {
+ return nil, err
+ }
+
+ snapshot := _snapshot.(*objects.Snapshot)
+
+ if archive != "" && snapshot.Archive != archive {
+ continue
+ }
+
+ if snapshot.Date.After(earliestTime) {
+ earliestTime = snapshot.Date
+ latestSnapshot = snapshot
+ found = true
+ }
+ }
+
+ if !found {
+ return latestSnapshot, ObjectNotFound
+ }
+
+ return latestSnapshot, nil
+}