diff --git a/x-pack/elastic-agent/pkg/artifact/install/atomic/atomic_installer.go b/x-pack/elastic-agent/pkg/artifact/install/atomic/atomic_installer.go index 60c5f1d28cfd..a9406cf98153 100644 --- a/x-pack/elastic-agent/pkg/artifact/install/atomic/atomic_installer.go +++ b/x-pack/elastic-agent/pkg/artifact/install/atomic/atomic_installer.go @@ -55,15 +55,8 @@ func (i *Installer) Install(ctx context.Context, spec program.Spec, version, ins // on windows rename is not atomic, let's force it to flush the cache defer func() { if runtime.GOOS == "windows" { - if f, err := os.OpenFile(installDir, os.O_RDWR, 0777); err == nil { - f.Sync() - f.Close() - } - - if f, err := os.OpenFile(tempInstallDir, os.O_RDWR, 0777); err == nil { - f.Sync() - f.Close() - } + syncDir(installDir) + syncDir(tempInstallDir) } }() @@ -87,3 +80,10 @@ func (i *Installer) Install(ctx context.Context, spec program.Spec, version, ins return nil } + +func syncDir(dir string) { + if f, err := os.OpenFile(dir, os.O_RDWR, 0777); err == nil { + f.Sync() + f.Close() + } +} diff --git a/x-pack/elastic-agent/pkg/artifact/install/zip/zip_installer.go b/x-pack/elastic-agent/pkg/artifact/install/zip/zip_installer.go index 034e4be7b416..eba432feefb7 100644 --- a/x-pack/elastic-agent/pkg/artifact/install/zip/zip_installer.go +++ b/x-pack/elastic-agent/pkg/artifact/install/zip/zip_installer.go @@ -61,9 +61,13 @@ func (i *Installer) Install(ctx context.Context, spec program.Spec, version, ins // if root directory is not the same as desired directory rename // e.g contains `-windows-` or `-SNAPSHOT-` if rootDir != installDir { + defer syncDir(rootDir) + defer syncDir(installDir) + if err := os.Rename(rootDir, installDir); err != nil { return errors.New(err, errors.TypeFilesystem, errors.M(errors.MetaKeyPath, installDir)) } + } return nil @@ -155,3 +159,10 @@ func (i *Installer) getRootDir(zipPath string) (dir string, err error) { return rootDir, nil } + +func syncDir(dir string) { + if f, err := os.OpenFile(dir, os.O_RDWR, 0777); err == nil { + f.Sync() + f.Close() + } +}