Add active directory management for snapshot operations
Introduced `GetActiveDir` and `CleanActiveDir` methods in the blob store to manage a dedicated directory for active snapshot operations. This ensures a clean working state before starting new operations and prevents conflicts. Updated related logic in snapshot creation and restoration to utilize the active directory.
This commit is contained in:
27
manager.go
27
manager.go
@ -59,12 +59,20 @@ func (data *SnapshotManagerData) CreateSnapshot(ctx context.Context, sourceDir s
|
||||
// Generate a unique ID for the snapshot
|
||||
snapshotID := uuid.New().String()
|
||||
|
||||
// Create a temporary file for the archive
|
||||
tempFile, err := os.CreateTemp("", "agate-snapshot-*.zip")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create temporary file: %w", err)
|
||||
// Clean the active directory to avoid conflicts
|
||||
if err := data.blobStore.CleanActiveDir(ctx); err != nil {
|
||||
return nil, fmt.Errorf("failed to clean active directory: %w", err)
|
||||
}
|
||||
|
||||
// Get the active directory for operations
|
||||
activeDir := data.blobStore.GetActiveDir()
|
||||
|
||||
// Create a temporary file for the archive in the active directory
|
||||
tempFilePath := filepath.Join(activeDir, "temp-"+snapshotID+".zip")
|
||||
tempFile, err := os.Create(tempFilePath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create temporary file in active directory: %w", err)
|
||||
}
|
||||
tempFilePath := tempFile.Name()
|
||||
tempFile.Close() // Close it as CreateArchive will reopen it
|
||||
defer os.Remove(tempFilePath) // Clean up temp file after we're done
|
||||
|
||||
@ -253,8 +261,15 @@ func (data *SnapshotManagerData) ExtractSnapshot(ctx context.Context, snapshotID
|
||||
if snapshotID == "" {
|
||||
return errors.New("snapshot ID cannot be empty")
|
||||
}
|
||||
|
||||
// If no specific path is provided, use the active directory
|
||||
if path == "" {
|
||||
return errors.New("target path cannot be empty")
|
||||
// Clean the active directory to avoid conflicts
|
||||
if err := data.blobStore.CleanActiveDir(ctx); err != nil {
|
||||
return fmt.Errorf("failed to clean active directory: %w", err)
|
||||
}
|
||||
|
||||
path = filepath.Join(data.blobStore.GetActiveDir(), snapshotID)
|
||||
}
|
||||
|
||||
// First check if the snapshot exists
|
||||
|
Reference in New Issue
Block a user