Skip to content

Commit fa85591

Browse files
committed
chore: updated manifest file
1 parent ec69dd8 commit fa85591

File tree

1 file changed

+38
-19
lines changed

1 file changed

+38
-19
lines changed

internal/file/file_manager_service.go

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ import (
3838
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6@v6.8.1 -generate
3939
//counterfeiter:generate . fileManagerServiceInterface
4040

41-
const maxAttempts = 5
41+
const (
42+
maxAttempts = 5
43+
manifestDirPath = "/var/lib/nginx-agent"
44+
manifestFilePath = manifestDirPath + "/manifest.json"
45+
)
4246

4347
type (
4448
fileOperator interface {
@@ -71,7 +75,6 @@ type FileManagerService struct {
7175
// map of the files currently on disk, used to determine the file action during config apply
7276
currentFilesOnDisk map[string]*mpi.File // key is file path
7377
filesMutex sync.RWMutex
74-
manifestFilePath string
7578
}
7679

7780
func NewFileManagerService(fileServiceClient mpi.FileServiceClient, agentConfig *config.Config) *FileManagerService {
@@ -453,20 +456,32 @@ func (fms *FileManagerService) DetermineFileActions(currentFiles, modifiedFiles
453456
fileDiff := make(map[string]*mpi.File) // Files that have changed, key is file name
454457
fileContents := make(map[string][]byte) // contents of the file, key is file name
455458

456-
// if file is in currentFiles but not in modified files, file has been deleted
459+
file, err := os.ReadFile(manifestFilePath)
460+
if err != nil {
461+
fmt.Printf("Failed to read manifest file: %v\n", err)
462+
}
463+
464+
// Parse JSON into a map
465+
var manifestFiles map[string]*mpi.File
466+
err = json.Unmarshal(file, &manifestFiles)
467+
if err != nil {
468+
fmt.Printf("Failed to parse manifest JSON: %v\n", err)
469+
}
470+
471+
// if file is in manifestFiles but not in modified files, file has been deleted
457472
// copy contents, set file action
458-
for _, currentFile := range currentFiles {
459-
fileName := currentFile.GetFileMeta().GetName()
460-
_, ok := modifiedFiles[fileName]
473+
for fileName, currentFile := range manifestFiles {
474+
_, exists := modifiedFiles[fileName]
461475

462-
if !ok {
476+
if !exists {
477+
// Read file contents before marking it deleted
463478
fileContent, readErr := os.ReadFile(fileName)
464479
if readErr != nil {
465480
return nil, nil, fmt.Errorf("error reading file %s, error: %w", fileName, readErr)
466481
}
467482
fileContents[fileName] = fileContent
468483
currentFile.Action = &deleteAction
469-
fileDiff[currentFile.GetFileMeta().GetName()] = currentFile
484+
fileDiff[fileName] = currentFile
470485
}
471486
}
472487

@@ -513,22 +528,26 @@ func (fms *FileManagerService) UpdateCurrentFilesOnDisk(currentFiles map[string]
513528
fms.currentFilesOnDisk[file.GetFileMeta().GetName()] = file
514529
}
515530

516-
if err := fms.writeManifest(); err != nil {
517-
// Handle error (e.g., logging)
531+
jsonData, err := json.MarshalIndent(currentFiles, "", " ")
532+
if err != nil {
533+
fmt.Printf("Failed to read manifest file: %v\n", err)
518534
}
519-
}
520535

521-
// writeManifest writes the currentFilesOnDisk to a JSON manifest file
522-
func (fms *FileManagerService) writeManifest() error {
523-
fileList := make([]string, 0, len(fms.currentFilesOnDisk))
524-
for name := range fms.currentFilesOnDisk {
525-
fileList = append(fileList, name)
536+
err = os.MkdirAll(manifestDirPath, 0755) // 0755 allows read/execute for all, write for owner
537+
if err != nil {
538+
fmt.Printf("Failed to read manifest file: %v\n", err)
526539
}
527540

528-
data, err := json.MarshalIndent(fileList, "", " ")
541+
newFile, err := os.OpenFile(manifestFilePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) // 0600 ensures only root can read/write
529542
if err != nil {
530-
return err
543+
fmt.Printf("Failed to read manifest file: %v\n", err)
544+
}
545+
defer newFile.Close()
546+
547+
_, err = newFile.Write(jsonData)
548+
if err != nil {
549+
fmt.Printf("Failed to read manifest file: %v\n", err)
531550
}
532551

533-
return os.WriteFile(fms.manifestFilePath, data, 0640)
552+
slog.Error("Manifest File updated successfully")
534553
}

0 commit comments

Comments
 (0)