@@ -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
@@ -193,7 +195,7 @@ func (fms *FileManagerService) UpdateOverview(
193195 delta := files .ConvertToMapOfFiles (response .GetOverview ().GetFiles ())
194196
195197 if len (delta ) != 0 {
196- return fms .updateFiles (ctx , delta , request . GetOverview (). GetFiles (), instanceID , iteration )
198+ return fms .updateFiles (ctx , delta , instanceID , iteration )
197199 }
198200
199201 return err
@@ -217,7 +219,6 @@ func (fms *FileManagerService) setupIdentifiers(ctx context.Context, iteration i
217219func (fms * FileManagerService ) updateFiles (
218220 ctx context.Context ,
219221 delta map [string ]* mpi.File ,
220- fileOverview []* mpi.File ,
221222 instanceID string ,
222223 iteration int ,
223224) error {
@@ -233,7 +234,7 @@ func (fms *FileManagerService) updateFiles(
233234 iteration ++
234235 slog .Info ("Updating file overview after file updates" , "attempt_number" , iteration )
235236
236- return fms .UpdateOverview (ctx , instanceID , fileOverview , iteration )
237+ return fms .UpdateOverview (ctx , instanceID , diffFiles , iteration )
237238}
238239
239240func (fms * FileManagerService ) UpdateFile (
@@ -538,12 +539,13 @@ func (fms *FileManagerService) ConfigApply(ctx context.Context,
538539func (fms * FileManagerService ) ClearCache () {
539540 clear (fms .rollbackFileContents )
540541 clear (fms .fileActions )
542+ clear (fms .previousManifestFiles )
541543}
542544
543545// nolint:revive,cyclop
544546func (fms * FileManagerService ) Rollback (ctx context.Context , instanceID string ) error {
545547 slog .InfoContext (ctx , "Rolling back config for instance" , "instanceid" , instanceID )
546- areFilesUpdated := false
548+
547549 fms .filesMutex .Lock ()
548550 defer fms .filesMutex .Unlock ()
549551 for _ , fileAction := range fms .fileActions {
@@ -555,7 +557,6 @@ func (fms *FileManagerService) Rollback(ctx context.Context, instanceID string)
555557
556558 // currentFilesOnDisk needs to be updated after rollback action is performed
557559 delete (fms .currentFilesOnDisk , fileAction .File .GetFileMeta ().GetName ())
558- areFilesUpdated = true
559560
560561 continue
561562 case model .Delete , model .Update :
@@ -568,19 +569,16 @@ func (fms *FileManagerService) Rollback(ctx context.Context, instanceID string)
568569 // currentFilesOnDisk needs to be updated after rollback action is performed
569570 fileAction .File .GetFileMeta ().Hash = files .GenerateHash (content )
570571 fms .currentFilesOnDisk [fileAction .File .GetFileMeta ().GetName ()] = fileAction .File
571- areFilesUpdated = true
572572 case model .Unchanged :
573573 fallthrough
574574 default :
575575 slog .DebugContext (ctx , "File Action not implemented" )
576576 }
577577 }
578578
579- if areFilesUpdated {
580- manifestFileErr := fms .UpdateManifestFile (fms .currentFilesOnDisk , true )
581- if manifestFileErr != nil {
582- return manifestFileErr
583- }
579+ manifestFileErr := fms .writeManifestFile (fms .previousManifestFiles )
580+ if manifestFileErr != nil {
581+ return manifestFileErr
584582 }
585583
586584 return nil
@@ -818,6 +816,7 @@ func (fms *FileManagerService) UpdateCurrentFilesOnDisk(
818816// nolint: revive
819817func (fms * FileManagerService ) UpdateManifestFile (currentFiles map [string ]* mpi.File , referenced bool ) (err error ) {
820818 currentManifestFiles , _ , readError := fms .manifestFile ()
819+ fms .previousManifestFiles = currentManifestFiles
821820 if readError != nil && ! errors .Is (readError , os .ErrNotExist ) {
822821 return fmt .Errorf ("unable to read manifest file: %w" , readError )
823822 }
0 commit comments