From 4e3447cdb8bd6bcb1957538808c1896abbf0d233 Mon Sep 17 00:00:00 2001 From: Michal Pristas Date: Tue, 13 Oct 2020 11:40:49 +0200 Subject: [PATCH 1/2] change order --- .../pkg/artifact/install/tar/tar_installer.go | 14 +++++++++----- .../pkg/artifact/install/zip/zip_installer.go | 13 ++++++------- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/x-pack/elastic-agent/pkg/artifact/install/tar/tar_installer.go b/x-pack/elastic-agent/pkg/artifact/install/tar/tar_installer.go index b9e621c71ac..74a74e4c6bc 100644 --- a/x-pack/elastic-agent/pkg/artifact/install/tar/tar_installer.go +++ b/x-pack/elastic-agent/pkg/artifact/install/tar/tar_installer.go @@ -99,15 +99,19 @@ func unpack(r io.Reader, dir string) error { } _, err = io.Copy(wf, tr) + + if err == nil { + // sometimes we try executing binary too fast and run into text file busy after unpacking + // syncing prevents this + if syncErr := wf.Sync(); syncErr != nil { + err = syncErr + } + } + if closeErr := wf.Close(); closeErr != nil && err == nil { err = closeErr } - // sometimes we try executing binary too fast and run into text file busy after unpacking - // syncing prevents this - if syncErr := wf.Sync(); syncErr != nil && err == nil { - err = syncErr - } if err != nil { return fmt.Errorf("TarInstaller: error writing to %s: %v", abs, err) } 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 29cdb66f852..dd9912d7b39 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 @@ -105,18 +105,17 @@ func (i *Installer) unzip(artifactPath string) error { if closeErr := f.Close(); closeErr != nil { err = multierror.Append(err, closeErr) } - - // sometimes we try executing binary too fast and run into text file busy after unpacking - // syncing prevents this - if syncErr := f.Sync(); syncErr != nil { - err = multierror.Append(err, syncErr) - } - }() if _, err = io.Copy(f, rc); err != nil { return err } + + // sometimes we try executing binary too fast and run into text file busy after unpacking + // syncing prevents this + if syncErr := f.Sync(); syncErr != nil { + err = multierror.Append(err, syncErr) + } } return nil } From 2097142eb5ece016e93151ad4c3e3367704ade7b Mon Sep 17 00:00:00 2001 From: Michal Pristas Date: Tue, 13 Oct 2020 11:43:26 +0200 Subject: [PATCH 2/2] error --- .../elastic-agent/pkg/artifact/install/zip/zip_installer.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) 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 dd9912d7b39..b565f630a73 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 @@ -113,9 +113,7 @@ func (i *Installer) unzip(artifactPath string) error { // sometimes we try executing binary too fast and run into text file busy after unpacking // syncing prevents this - if syncErr := f.Sync(); syncErr != nil { - err = multierror.Append(err, syncErr) - } + f.Sync() } return nil }