@@ -39,11 +39,14 @@ import (
3939//counterfeiter:generate . fileManagerServiceInterface
4040
4141const (
42- maxAttempts = 5
42+ maxAttempts = 5
43+ dirPerm = 0o755
44+ filePerm = 0o600
45+ )
46+
47+ var (
4348 manifestDirPath = "/var/lib/nginx-agent"
4449 manifestFilePath = manifestDirPath + "/manifest.json"
45- dirPerm = 0o755
46- filePerm = 0o600
4750)
4851
4952type (
@@ -312,7 +315,7 @@ func (fms *FileManagerService) ConfigApply(ctx context.Context,
312315 }
313316 // Update map of current files on disk
314317 fms .UpdateCurrentFilesOnDisk (files .ConvertToMapOfFiles (fileOverview .GetFiles ()))
315- manifestFileErr := fms .UpdateManifestFile (files .ConvertToMapOfFiles (fileOverview .GetFiles ()))
318+ manifestFileErr := fms .updateManifestFile (files .ConvertToMapOfFiles (fileOverview .GetFiles ()))
316319 if manifestFileErr != nil {
317320 return model .Error , manifestFileErr
318321 }
@@ -341,10 +344,6 @@ func (fms *FileManagerService) Rollback(ctx context.Context, instanceID string)
341344 // currentFilesOnDisk needs to be updated after rollback action is performed
342345 delete (fms .currentFilesOnDisk , file .GetFileMeta ().GetName ())
343346 areFilesUpdated = true
344- manifestFileErr := fms .UpdateManifestFile (fms .currentFilesOnDisk )
345- if manifestFileErr != nil {
346- return manifestFileErr
347- }
348347
349348 continue
350349 case mpi .File_FILE_ACTION_DELETE , mpi .File_FILE_ACTION_UPDATE :
@@ -358,7 +357,7 @@ func (fms *FileManagerService) Rollback(ctx context.Context, instanceID string)
358357 file .GetFileMeta ().Hash = files .GenerateHash (content )
359358 fms .currentFilesOnDisk [file .GetFileMeta ().GetName ()] = file
360359 areFilesUpdated = true
361- manifestFileErr := fms .UpdateManifestFile (fms .currentFilesOnDisk )
360+ manifestFileErr := fms .updateManifestFile (fms .currentFilesOnDisk )
362361 if manifestFileErr != nil {
363362 return manifestFileErr
364363 }
@@ -370,7 +369,7 @@ func (fms *FileManagerService) Rollback(ctx context.Context, instanceID string)
370369 }
371370
372371 if areFilesUpdated {
373- manifestFileErr := fms .UpdateManifestFile (fms .currentFilesOnDisk )
372+ manifestFileErr := fms .updateManifestFile (fms .currentFilesOnDisk )
374373 if manifestFileErr != nil {
375374 return manifestFileErr
376375 }
@@ -479,7 +478,11 @@ func (fms *FileManagerService) DetermineFileActions(currentFiles, modifiedFiles
479478 fileDiff := make (map [string ]* mpi.File ) // Files that have changed, key is file name
480479 fileContents := make (map [string ][]byte ) // contents of the file, key is file name
481480
482- manifestFiles := fms .getManifestFile (currentFiles )
481+ manifestFiles , manifestFileErr := fms .getManifestFile (currentFiles )
482+
483+ if manifestFileErr != nil {
484+ return nil , nil , manifestFileErr
485+ }
483486 // if file is in manifestFiles but not in modified files, file has been deleted
484487 // copy contents, set file action
485488 for fileName , currentFile := range manifestFiles {
@@ -541,7 +544,7 @@ func (fms *FileManagerService) UpdateCurrentFilesOnDisk(currentFiles map[string]
541544 }
542545}
543546
544- func (fms * FileManagerService ) UpdateManifestFile (currentFiles map [string ]* mpi.File ) (err error ) {
547+ func (fms * FileManagerService ) updateManifestFile (currentFiles map [string ]* mpi.File ) (err error ) {
545548 manifestFiles := fms .convertToManifestFileMap (currentFiles )
546549 manifestJSON , err := json .MarshalIndent (manifestFiles , "" , " " )
547550 if err != nil {
@@ -572,28 +575,28 @@ func (fms *FileManagerService) UpdateManifestFile(currentFiles map[string]*mpi.F
572575 return nil
573576}
574577
575- func (fms * FileManagerService ) getManifestFile (currentFiles map [string ]* mpi.File ) map [string ]* mpi.File {
578+ func (fms * FileManagerService ) getManifestFile (currentFiles map [string ]* mpi.File ) ( map [string ]* mpi.File , error ) {
576579 if _ , err := os .Stat (manifestFilePath ); err != nil {
577- return currentFiles // Return current files if manifest file doesn't exist
580+ return currentFiles , nil // Return current files if manifest directory still doesn't exist
578581 }
579582
580583 file , err := os .ReadFile (manifestFilePath )
581584 if err != nil {
582585 slog .Error ("Failed to read manifest file" , "error" , err )
583- return currentFiles
586+ return nil , err
584587 }
585588
586589 var manifestFiles map [string ]* model.ManifestFile
587590
588591 err = json .Unmarshal (file , & manifestFiles )
589592 if err != nil {
590593 slog .Error ("Failed to parse manifest file" , "error" , err )
591- return currentFiles
594+ return nil , err
592595 }
593596
594597 fileMap := fms .convertToFileMap (manifestFiles )
595598
596- return fileMap
599+ return fileMap , nil
597600}
598601
599602func (fms * FileManagerService ) convertToManifestFileMap (
0 commit comments