GetPrecedingSnapshot fetches the snapshot manifest for the snapshot immediately preceding the given snapshotID if it exists.
(ctx context.Context, rep repo.Repository, snapshotID string)
| 412 | |
| 413 | // GetPrecedingSnapshot fetches the snapshot manifest for the snapshot immediately preceding the given snapshotID if it exists. |
| 414 | func GetPrecedingSnapshot(ctx context.Context, rep repo.Repository, snapshotID string) (*snapshot.Manifest, error) { |
| 415 | snapshotManifest, err := snapshotfs.FindSnapshotByRootObjectIDOrManifestID(ctx, rep, snapshotID, true) |
| 416 | if err != nil { |
| 417 | return nil, errors.Wrap(err, "failed to get snapshot manifest for the given snapshotID") |
| 418 | } |
| 419 | |
| 420 | snapshotList, err := snapshot.ListSnapshots(ctx, rep, snapshotManifest.Source) |
| 421 | if err != nil { |
| 422 | return nil, errors.Wrap(err, "failed to list snapshots") |
| 423 | } |
| 424 | |
| 425 | // sort snapshots in ascending order based on start time |
| 426 | sort.Slice(snapshotList, func(i, j int) bool { |
| 427 | return snapshotList[i].StartTime.Before(snapshotList[j].StartTime) |
| 428 | }) |
| 429 | |
| 430 | for i, snap := range snapshotList { |
| 431 | if snap.ID == snapshotManifest.ID { |
| 432 | if i == 0 { |
| 433 | return nil, errors.New("there is no immediately preceding snapshot") |
| 434 | } |
| 435 | |
| 436 | return snapshotList[i-1], nil |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | return nil, errors.New("couldn't find immediately preceding snapshot") |
| 441 | } |
| 442 | |
| 443 | // GetTwoLatestSnapshotsForASource fetches two latest snapshot manifests for a given source if they exist. |
| 444 | func GetTwoLatestSnapshotsForASource(ctx context.Context, rep repo.Repository, source snapshot.SourceInfo) (secondLast, last *snapshot.Manifest, err error) { |