@@ -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,26 +375,49 @@ 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 ) {
387- modifiedFile .Action = model .Add
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+ modifiedFile .Action = model .Update
388390 fileDiff [fileName ] = modifiedFile
389391
390392 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 () {
394- modifiedFile .Action = model .Update
393+ }
394+
395+ // If file doesn't exist on disk.
396+ // Treat it as adding a new file.
397+ if fileStats , statErr := os .Stat (fileName ); errors .Is (statErr , os .ErrNotExist ) {
398+ modifiedFile .Action = model .Add
395399 fileDiff [fileName ] = modifiedFile
400+
401+ continue
402+ // If file already exists on disk but is not being tracked in manifest and the file hash is different.
403+ // Treat it as a file update.
404+ } else if statErr == nil {
405+ if fileStats .IsDir () {
406+ return nil , fmt .Errorf (
407+ "unable to create file %s since a directory with the same name already exists on the data plane" ,
408+ fileName ,
409+ )
410+ }
411+
412+ metadataOfFileOnDisk , err := files .FileMeta (fileName )
413+ if err != nil {
414+ return nil , fmt .Errorf ("unable to get file metadata for %s: %w" , fileName , err )
415+ }
416+
417+ if metadataOfFileOnDisk .GetHash () != modifiedFile .File .GetFileMeta ().GetHash () {
418+ modifiedFile .Action = model .Update
419+ fileDiff [fileName ] = modifiedFile
420+ }
396421 }
397422 }
398423
0 commit comments