Skip to content

Commit

Permalink
sync sync
Browse files Browse the repository at this point in the history
  • Loading branch information
michalpristas committed Mar 11, 2021
1 parent f323b36 commit bde7d0d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}()

Expand All @@ -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()
}
}
11 changes: 11 additions & 0 deletions x-pack/elastic-agent/pkg/artifact/install/zip/zip_installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
}

0 comments on commit bde7d0d

Please sign in to comment.