@@ -176,6 +176,8 @@ func (fms *FileManagerService) ConfigApply(ctx context.Context,
176176
177177 fms .fileActions = diffFiles
178178
179+ slog .DebugContext (ctx , "Executing config apply file actions" , "actions" , diffFiles )
180+
179181 rollbackTempFilesErr := fms .backupFiles (ctx )
180182 if rollbackTempFilesErr != nil {
181183 return model .Error , rollbackTempFilesErr
@@ -373,24 +375,58 @@ func (fms *FileManagerService) DetermineFileActions(
373375 for _ , modifiedFile := range modifiedFiles {
374376 fileName := modifiedFile .File .GetFileMeta ().GetName ()
375377 currentFile , ok := filesMap [fileName ]
376- // default to unchanged action
377378 modifiedFile .Action = model .Unchanged
378379
379- // if file is unmanaged, action is set to unchanged so file is skipped when performing actions
380+ // If file is unmanaged, action is set to unchanged so file is skipped when performing actions.
380381 if modifiedFile .File .GetUnmanaged () {
381382 slog .DebugContext (ctx , "Skipping unmanaged file updates" , "file_name" , fileName )
382383 continue
383384 }
384- // if file doesn't exist in the current files, file has been added
385- // set file action
386- if _ , statErr := os .Stat (fileName ); errors .Is (statErr , os .ErrNotExist ) {
385+
386+ // If file currently exists on disk, is being tracked in manifest and file hash is different.
387+ // Treat it as a file update.
388+ if ok && modifiedFile .File .GetFileMeta ().GetHash () != currentFile .GetFileMeta ().GetHash () {
389+ slog .DebugContext (ctx , "Tracked file requires updating" , "file_name" , fileName )
390+ modifiedFile .Action = model .Update
391+ fileDiff [fileName ] = modifiedFile
392+
393+ continue
394+ }
395+
396+ fileStats , statErr := os .Stat (fileName )
397+
398+ // If file doesn't exist on disk.
399+ // Treat it as adding a new file.
400+ if errors .Is (statErr , os .ErrNotExist ) {
401+ slog .DebugContext (ctx , "New untracked file needs to be created" , "file_name" , fileName )
387402 modifiedFile .Action = model .Add
388403 fileDiff [fileName ] = modifiedFile
389404
390405 continue
391- // if file currently exists and file hash is different, file has been updated
392- // copy contents, set file action
393- } else if ok && modifiedFile .File .GetFileMeta ().GetHash () != currentFile .GetFileMeta ().GetHash () {
406+ }
407+
408+ // If there is an error other than not existing, return that error.
409+ if statErr != nil {
410+ return nil , fmt .Errorf ("unable to stat file %s: %w" , fileName , statErr )
411+ }
412+
413+ // If there is a directory with the same name, return an error.
414+ if fileStats .IsDir () {
415+ return nil , fmt .Errorf (
416+ "unable to create file %s since a directory with the same name already exists" ,
417+ fileName ,
418+ )
419+ }
420+
421+ // If file already exists on disk but is not being tracked in manifest and the file hash is different.
422+ // Treat it as a file update.
423+ metadataOfFileOnDisk , err := files .FileMeta (fileName )
424+ if err != nil {
425+ return nil , fmt .Errorf ("unable to get file metadata for %s: %w" , fileName , err )
426+ }
427+
428+ if metadataOfFileOnDisk .GetHash () != modifiedFile .File .GetFileMeta ().GetHash () {
429+ slog .DebugContext (ctx , "Untracked file requires updating" , "file_name" , fileName )
394430 modifiedFile .Action = model .Update
395431 fileDiff [fileName ] = modifiedFile
396432 }
0 commit comments