@@ -37,7 +37,13 @@ import (
3737//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6@v6.8.1 -generate
3838//counterfeiter:generate . fileManagerServiceInterface
3939
40- const maxAttempts = 5
40+ const (
41+ maxAttempts = 5
42+ addAction = "add"
43+ updateAction = "update"
44+ deleteAction = "delete"
45+ unchangedAction = "unchanged"
46+ )
4147
4248type (
4349 fileOperator interface {
5157 Rollback (ctx context.Context , instanceID string ) error
5258 UpdateFile (ctx context.Context , instanceID string , fileToUpdate * mpi.File ) error
5359 ClearCache ()
54- UpdateCurrentFilesOnDisk (updateFiles map [string ]* mpi. File )
55- DetermineFileActions (currentFiles , modifiedFiles map [string ]* mpi. File ) (map [string ]* mpi. File ,
60+ UpdateCurrentFilesOnDisk (updateFiles map [string ]model. FileCache )
61+ DetermineFileActions (currentFiles , modifiedFiles map [string ]model. FileCache ) (map [string ]model. FileCache ,
5662 map [string ][]byte , error )
5763 SetIsConnected (isConnected bool )
5864 }
@@ -64,11 +70,11 @@ type FileManagerService struct {
6470 isConnected * atomic.Bool
6571 fileOperator fileOperator
6672 // map of files and the actions performed on them during config apply
67- fileActions map [string ]* mpi. File // key is file path
73+ fileActions map [string ]model. FileCache // key is File path
6874 // map of the contents of files which have been updated or deleted during config apply, used during rollback
69- rollbackFileContents map [string ][]byte // key is file path
70- // map of the files currently on disk, used to determine the file action during config apply
71- currentFilesOnDisk map [string ]* mpi. File // key is file path
75+ rollbackFileContents map [string ][]byte // key is File path
76+ // map of the files currently on disk, used to determine the File Action during config apply
77+ currentFilesOnDisk map [string ]model. FileCache // key is File path
7278 filesMutex sync.RWMutex
7379}
7480
@@ -80,9 +86,9 @@ func NewFileManagerService(fileServiceClient mpi.FileServiceClient, agentConfig
8086 fileServiceClient : fileServiceClient ,
8187 agentConfig : agentConfig ,
8288 fileOperator : NewFileOperator (),
83- fileActions : make (map [string ]* mpi. File ),
89+ fileActions : make (map [string ]model. FileCache ),
8490 rollbackFileContents : make (map [string ][]byte ),
85- currentFilesOnDisk : make (map [string ]* mpi. File ),
91+ currentFilesOnDisk : make (map [string ]model. FileCache ),
8692 isConnected : isConnected ,
8793 }
8894}
@@ -286,7 +292,7 @@ func (fms *FileManagerService) ConfigApply(ctx context.Context,
286292 }
287293
288294 diffFiles , fileContent , compareErr := fms .DetermineFileActions (fms .currentFilesOnDisk ,
289- files .ConvertToMapOfFiles (fileOverview .GetFiles ()))
295+ files .ConvertToMapOfFileCache (fileOverview .GetFiles ()))
290296
291297 if compareErr != nil {
292298 return model .Error , compareErr
@@ -305,7 +311,7 @@ func (fms *FileManagerService) ConfigApply(ctx context.Context,
305311 }
306312
307313 // Update map of current files on disk
308- fms .UpdateCurrentFilesOnDisk (files .ConvertToMapOfFiles (fileOverview .GetFiles ()))
314+ fms .UpdateCurrentFilesOnDisk (files .ConvertToMapOfFileCache (fileOverview .GetFiles ()))
309315
310316 return model .OK , nil
311317}
@@ -319,29 +325,29 @@ func (fms *FileManagerService) Rollback(ctx context.Context, instanceID string)
319325 slog .InfoContext (ctx , "Rolling back config for instance" , "instanceid" , instanceID )
320326 fms .filesMutex .Lock ()
321327 defer fms .filesMutex .Unlock ()
322- for _ , file := range fms .fileActions {
323- switch file . GetAction () {
324- case mpi . File_FILE_ACTION_ADD :
325- if err := os .Remove (file .GetFileMeta ().GetName ()); err != nil && ! os .IsNotExist (err ) {
326- return fmt .Errorf ("error deleting file: %s error: %w" , file .GetFileMeta ().GetName (), err )
328+ for _ , fileAction := range fms .fileActions {
329+ switch fileAction . Action {
330+ case addAction :
331+ if err := os .Remove (fileAction . File .GetFileMeta ().GetName ()); err != nil && ! os .IsNotExist (err ) {
332+ return fmt .Errorf ("error deleting file: %s error: %w" , fileAction . File .GetFileMeta ().GetName (), err )
327333 }
328334
329335 // currentFilesOnDisk needs to be updated after rollback action is performed
330- delete (fms .currentFilesOnDisk , file .GetFileMeta ().GetName ())
336+ delete (fms .currentFilesOnDisk , fileAction . File .GetFileMeta ().GetName ())
331337
332338 continue
333- case mpi . File_FILE_ACTION_DELETE , mpi . File_FILE_ACTION_UPDATE :
334- content := fms .rollbackFileContents [file .GetFileMeta ().GetName ()]
339+ case deleteAction , updateAction :
340+ content := fms .rollbackFileContents [fileAction . File .GetFileMeta ().GetName ()]
335341
336- err := fms .fileOperator .Write (ctx , content , file .GetFileMeta ())
342+ err := fms .fileOperator .Write (ctx , content , fileAction . File .GetFileMeta ())
337343 if err != nil {
338344 return err
339345 }
340346
341347 // currentFilesOnDisk needs to be updated after rollback action is performed
342- file .GetFileMeta ().Hash = files .GenerateHash (content )
343- fms .currentFilesOnDisk [file . GetFileMeta ().GetName ()] = file
344- case mpi . File_FILE_ACTION_UNSPECIFIED , mpi . File_FILE_ACTION_UNCHANGED :
348+ fileAction . File .GetFileMeta ().Hash = files .GenerateHash (content )
349+ fms .currentFilesOnDisk [fileAction . File . GetFileMeta ().GetName ()] = fileAction
350+ case "" , unchangedAction :
345351 fallthrough
346352 default :
347353 slog .DebugContext (ctx , "File Action not implemented" )
@@ -352,23 +358,23 @@ func (fms *FileManagerService) Rollback(ctx context.Context, instanceID string)
352358}
353359
354360func (fms * FileManagerService ) executeFileActions (ctx context.Context ) error {
355- for _ , file := range fms .fileActions {
356- switch file . GetAction () {
357- case mpi . File_FILE_ACTION_DELETE :
358- if err := os .Remove (file .GetFileMeta ().GetName ()); err != nil && ! os .IsNotExist (err ) {
359- return fmt .Errorf ("error deleting file: %s error: %w" , file .GetFileMeta ().GetName (), err )
361+ for _ , fileAction := range fms .fileActions {
362+ switch fileAction . Action {
363+ case deleteAction :
364+ if err := os .Remove (fileAction . File .GetFileMeta ().GetName ()); err != nil && ! os .IsNotExist (err ) {
365+ return fmt .Errorf ("error deleting file: %s error: %w" , fileAction . File .GetFileMeta ().GetName (), err )
360366 }
361367
362368 continue
363- case mpi . File_FILE_ACTION_ADD , mpi . File_FILE_ACTION_UPDATE :
364- updateErr := fms .fileUpdate (ctx , file )
369+ case addAction , updateAction :
370+ updateErr := fms .fileUpdate (ctx , fileAction . File )
365371 if updateErr != nil {
366372 return updateErr
367373 }
368- case mpi . File_FILE_ACTION_UNSPECIFIED , mpi . File_FILE_ACTION_UNCHANGED :
374+ case "" , unchangedAction :
369375 fallthrough
370376 default :
371- slog .DebugContext (ctx , "File Action not implemented" , "action" , file . GetAction () )
377+ slog .DebugContext (ctx , "File Action not implemented" , "action" , fileAction . Action )
372378 }
373379 }
374380
@@ -416,7 +422,7 @@ func (fms *FileManagerService) validateFileHash(filePath string) error {
416422 }
417423 fileHash := files .GenerateHash (content )
418424
419- if fileHash != fms .fileActions [filePath ].GetFileMeta ().GetHash () {
425+ if fileHash != fms .fileActions [filePath ].File . GetFileMeta ().GetHash () {
420426 return fmt .Errorf ("error writing file, file hash does not match for file %s" , filePath )
421427 }
422428
@@ -437,24 +443,19 @@ func (fms *FileManagerService) checkAllowedDirectory(checkFiles []*mpi.File) err
437443// DetermineFileActions compares two sets of files to determine the file action for each file. Returns a map of files
438444// that have changed and a map of the contents for each updated and deleted file. Key to both maps is file path
439445// nolint: revive
440- func (fms * FileManagerService ) DetermineFileActions (currentFiles , modifiedFiles map [string ]* mpi. File ) (
441- map [string ]* mpi. File , map [string ][]byte , error ,
446+ func (fms * FileManagerService ) DetermineFileActions (currentFiles , modifiedFiles map [string ]model. FileCache ) (
447+ map [string ]model. FileCache , map [string ][]byte , error ,
442448) {
443449 fms .filesMutex .Lock ()
444450 defer fms .filesMutex .Unlock ()
445- // Go doesn't allow address of numeric constant
446- addAction := mpi .File_FILE_ACTION_ADD
447- updateAction := mpi .File_FILE_ACTION_UPDATE
448- deleteAction := mpi .File_FILE_ACTION_DELETE
449- unchangedAction := mpi .File_FILE_ACTION_UNCHANGED
450451
451- fileDiff := make (map [string ]* mpi. File ) // Files that have changed, key is file name
452- fileContents := make (map [string ][]byte ) // contents of the file, key is file name
452+ fileDiff := make (map [string ]model. FileCache ) // Files that have changed, key is file name
453+ fileContents := make (map [string ][]byte ) // contents of the file, key is file name
453454
454455 // if file is in currentFiles but not in modified files, file has been deleted
455456 // copy contents, set file action
456457 for _ , currentFile := range currentFiles {
457- fileName := currentFile .GetFileMeta ().GetName ()
458+ fileName := currentFile .File . GetFileMeta ().GetName ()
458459 _ , ok := modifiedFiles [fileName ]
459460
460461 if ! ok {
@@ -463,36 +464,36 @@ func (fms *FileManagerService) DetermineFileActions(currentFiles, modifiedFiles
463464 return nil , nil , fmt .Errorf ("error reading file %s, error: %w" , fileName , readErr )
464465 }
465466 fileContents [fileName ] = fileContent
466- currentFile .Action = & deleteAction
467- fileDiff [currentFile .GetFileMeta ().GetName ()] = currentFile
467+ currentFile .Action = deleteAction
468+ fileDiff [currentFile .File . GetFileMeta ().GetName ()] = currentFile
468469 }
469470 }
470471
471- for _ , file := range modifiedFiles {
472- fileName := file .GetFileMeta ().GetName ()
473- currentFile , ok := currentFiles [file .GetFileMeta ().GetName ()]
472+ for _ , modifiedFile := range modifiedFiles {
473+ fileName := modifiedFile . File .GetFileMeta ().GetName ()
474+ currentFile , ok := currentFiles [modifiedFile . File .GetFileMeta ().GetName ()]
474475 // default to unchanged action
475- file .Action = & unchangedAction
476+ modifiedFile .Action = unchangedAction
476477
477478 // if file is unmanaged, action is set to unchanged so file is skipped when performing actions
478- if file .GetUnmanaged () {
479+ if modifiedFile . File .GetUnmanaged () {
479480 continue
480481 }
481482 // if file doesn't exist in the current files, file has been added
482483 // set file action
483484 if ! ok {
484- file .Action = & addAction
485- fileDiff [file . GetFileMeta ().GetName ()] = file
485+ modifiedFile .Action = addAction
486+ fileDiff [modifiedFile . File . GetFileMeta ().GetName ()] = modifiedFile
486487 // if file currently exists and file hash is different, file has been updated
487488 // copy contents, set file action
488- } else if file . GetFileMeta ().GetHash () != currentFile .GetFileMeta ().GetHash () {
489+ } else if modifiedFile . File . GetFileMeta ().GetHash () != currentFile . File .GetFileMeta ().GetHash () {
489490 fileContent , readErr := os .ReadFile (fileName )
490491 if readErr != nil {
491492 return nil , nil , fmt .Errorf ("error reading file %s, error: %w" , fileName , readErr )
492493 }
493- file .Action = & updateAction
494+ modifiedFile .Action = updateAction
494495 fileContents [fileName ] = fileContent
495- fileDiff [file . GetFileMeta ().GetName ()] = file
496+ fileDiff [modifiedFile . File . GetFileMeta ().GetName ()] = modifiedFile
496497 }
497498 }
498499
@@ -501,13 +502,13 @@ func (fms *FileManagerService) DetermineFileActions(currentFiles, modifiedFiles
501502
502503// UpdateCurrentFilesOnDisk updates the FileManagerService currentFilesOnDisk slice which contains the files
503504// currently on disk
504- func (fms * FileManagerService ) UpdateCurrentFilesOnDisk (currentFiles map [string ]* mpi. File ) {
505+ func (fms * FileManagerService ) UpdateCurrentFilesOnDisk (currentFiles map [string ]model. FileCache ) {
505506 fms .filesMutex .Lock ()
506507 defer fms .filesMutex .Unlock ()
507508
508509 clear (fms .currentFilesOnDisk )
509510
510- for _ , file := range currentFiles {
511- fms .currentFilesOnDisk [file . GetFileMeta ().GetName ()] = file
511+ for _ , currentFile := range currentFiles {
512+ fms .currentFilesOnDisk [currentFile . File . GetFileMeta ().GetName ()] = currentFile
512513 }
513514}
0 commit comments