@@ -96,22 +96,24 @@ type FileManagerService struct {
9696 // map of the contents of files which have been updated or deleted during config apply, used during rollback
9797 rollbackFileContents map [string ][]byte // key is file path
9898 // map of the files currently on disk, used to determine the file action during config apply
99- currentFilesOnDisk map [string ]* mpi.File // key is file path
100- filesMutex sync.RWMutex
99+ currentFilesOnDisk map [string ]* mpi.File // key is file path
100+ previousManifestFiles map [string ]* model.ManifestFile
101+ filesMutex sync.RWMutex
101102}
102103
103104func NewFileManagerService (fileServiceClient mpi.FileServiceClient , agentConfig * config.Config ) * FileManagerService {
104105 isConnected := & atomic.Bool {}
105106 isConnected .Store (false )
106107
107108 return & FileManagerService {
108- fileServiceClient : fileServiceClient ,
109- agentConfig : agentConfig ,
110- fileOperator : NewFileOperator (),
111- fileActions : make (map [string ]* model.FileCache ),
112- rollbackFileContents : make (map [string ][]byte ),
113- currentFilesOnDisk : make (map [string ]* mpi.File ),
114- isConnected : isConnected ,
109+ fileServiceClient : fileServiceClient ,
110+ agentConfig : agentConfig ,
111+ fileOperator : NewFileOperator (),
112+ fileActions : make (map [string ]* model.FileCache ),
113+ rollbackFileContents : make (map [string ][]byte ),
114+ currentFilesOnDisk : make (map [string ]* mpi.File ),
115+ previousManifestFiles : make (map [string ]* model.ManifestFile ),
116+ isConnected : isConnected ,
115117 }
116118}
117119
@@ -540,12 +542,13 @@ func (fms *FileManagerService) ConfigApply(ctx context.Context,
540542func (fms * FileManagerService ) ClearCache () {
541543 clear (fms .rollbackFileContents )
542544 clear (fms .fileActions )
545+ clear (fms .previousManifestFiles )
543546}
544547
545548// nolint:revive,cyclop
546549func (fms * FileManagerService ) Rollback (ctx context.Context , instanceID string ) error {
547550 slog .InfoContext (ctx , "Rolling back config for instance" , "instanceid" , instanceID )
548- areFilesUpdated := false
551+
549552 fms .filesMutex .Lock ()
550553 defer fms .filesMutex .Unlock ()
551554 for _ , fileAction := range fms .fileActions {
@@ -557,7 +560,6 @@ func (fms *FileManagerService) Rollback(ctx context.Context, instanceID string)
557560
558561 // currentFilesOnDisk needs to be updated after rollback action is performed
559562 delete (fms .currentFilesOnDisk , fileAction .File .GetFileMeta ().GetName ())
560- areFilesUpdated = true
561563
562564 continue
563565 case model .Delete , model .Update :
@@ -570,19 +572,16 @@ func (fms *FileManagerService) Rollback(ctx context.Context, instanceID string)
570572 // currentFilesOnDisk needs to be updated after rollback action is performed
571573 fileAction .File .GetFileMeta ().Hash = files .GenerateHash (content )
572574 fms .currentFilesOnDisk [fileAction .File .GetFileMeta ().GetName ()] = fileAction .File
573- areFilesUpdated = true
574575 case model .Unchanged :
575576 fallthrough
576577 default :
577578 slog .DebugContext (ctx , "File Action not implemented" )
578579 }
579580 }
580581
581- if areFilesUpdated {
582- manifestFileErr := fms .UpdateManifestFile (fms .currentFilesOnDisk , true )
583- if manifestFileErr != nil {
584- return manifestFileErr
585- }
582+ manifestFileErr := fms .writeManifestFile (fms .previousManifestFiles )
583+ if manifestFileErr != nil {
584+ return manifestFileErr
586585 }
587586
588587 return nil
@@ -819,6 +818,7 @@ func (fms *FileManagerService) UpdateCurrentFilesOnDisk(
819818// nolint: revive
820819func (fms * FileManagerService ) UpdateManifestFile (currentFiles map [string ]* mpi.File , referenced bool ) (err error ) {
821820 currentManifestFiles , _ , readError := fms .manifestFile ()
821+ fms .previousManifestFiles = currentManifestFiles
822822 if readError != nil && ! errors .Is (readError , os .ErrNotExist ) {
823823 return fmt .Errorf ("unable to read manifest file: %w" , readError )
824824 }
0 commit comments