Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'internal/backup/locator.go')
-rw-r--r--internal/backup/locator.go59
1 files changed, 45 insertions, 14 deletions
diff --git a/internal/backup/locator.go b/internal/backup/locator.go
index 1e1c42550..c0ccca3b3 100644
--- a/internal/backup/locator.go
+++ b/internal/backup/locator.go
@@ -267,6 +267,8 @@ func (l PointerLocator) writeLatest(ctx context.Context, path, target string) (r
return nil
}
+const latestManifestName = "+latest"
+
// ManifestLocator locates backup paths based on manifest files that are
// written to a predetermined path:
//
@@ -294,44 +296,55 @@ func (l ManifestLocator) Commit(ctx context.Context, backup *Backup) (returnErr
return err
}
- f, err := l.Sink.GetWriter(ctx, manifestPath(backup.Repository, backup.ID))
- if err != nil {
+ if err := l.writeManifest(ctx, backup, backup.ID); err != nil {
return fmt.Errorf("manifest: commit: %w", err)
}
- defer func() {
- if err := f.Close(); err != nil && returnErr == nil {
- returnErr = fmt.Errorf("manifest: commit: %w", err)
- }
- }()
-
- if err := toml.NewEncoder(f).Encode(backup); err != nil {
- return fmt.Errorf("manifest: commit: %w", err)
+ if err := l.writeManifest(ctx, backup, latestManifestName); err != nil {
+ return fmt.Errorf("manifest: commit latest: %w", err)
}
return nil
}
-// FindLatest passes through to Fallback
+// FindLatest loads the manifest called +latest. If this manifest does not
+// exist, the Fallback is used.
func (l ManifestLocator) FindLatest(ctx context.Context, repo storage.Repository) (*Backup, error) {
- return l.Fallback.FindLatest(ctx, repo)
+ backup, err := l.readManifest(ctx, repo, latestManifestName)
+ switch {
+ case errors.Is(err, ErrDoesntExist):
+ return l.Fallback.FindLatest(ctx, repo)
+ case err != nil:
+ return nil, fmt.Errorf("manifest: find latest: %w", err)
+ }
+
+ return backup, nil
}
// Find loads the manifest for the provided repo and backupID. If this manifest
// does not exist, the fallback is used.
func (l ManifestLocator) Find(ctx context.Context, repo storage.Repository, backupID string) (*Backup, error) {
- f, err := l.Sink.GetReader(ctx, manifestPath(repo, backupID))
+ backup, err := l.readManifest(ctx, repo, backupID)
switch {
case errors.Is(err, ErrDoesntExist):
return l.Fallback.Find(ctx, repo, backupID)
case err != nil:
return nil, fmt.Errorf("manifest: find: %w", err)
}
+
+ return backup, nil
+}
+
+func (l ManifestLocator) readManifest(ctx context.Context, repo storage.Repository, backupID string) (*Backup, error) {
+ f, err := l.Sink.GetReader(ctx, manifestPath(repo, backupID))
+ if err != nil {
+ return nil, fmt.Errorf("read manifest: %w", err)
+ }
defer f.Close()
var backup Backup
if err := toml.NewDecoder(f).Decode(&backup); err != nil {
- return nil, fmt.Errorf("manifest: find: %w", err)
+ return nil, fmt.Errorf("read manifest: %w", err)
}
backup.ID = backupID
@@ -340,6 +353,24 @@ func (l ManifestLocator) Find(ctx context.Context, repo storage.Repository, back
return &backup, nil
}
+func (l ManifestLocator) writeManifest(ctx context.Context, backup *Backup, backupID string) (returnErr error) {
+ f, err := l.Sink.GetWriter(ctx, manifestPath(backup.Repository, backupID))
+ if err != nil {
+ return fmt.Errorf("write manifest: %w", err)
+ }
+ defer func() {
+ if err := f.Close(); err != nil && returnErr == nil {
+ returnErr = fmt.Errorf("write manifest: %w", err)
+ }
+ }()
+
+ if err := toml.NewEncoder(f).Encode(backup); err != nil {
+ return fmt.Errorf("write manifest: %w", err)
+ }
+
+ return nil
+}
+
func manifestPath(repo storage.Repository, backupID string) string {
storageName := repo.GetStorageName()
// Other locators strip the .git suffix off of relative paths. This suffix