From 9c3cc59d8512d60b8e0e702d2a61d8c0c3184ab3 Mon Sep 17 00:00:00 2001 From: Jeffrey Frey Date: Tue, 29 Nov 2022 14:03:58 -0500 Subject: [PATCH 1/6] Missing --keep option, older versions of gzip Older versions of the `gzip` utility do not understand the `--keep` flag. If `gzip` is used and initially exits in error with the text "unrecognized option" produced on stderr, use the write-to-stdout behavior for `gzip` and open and attach the desired output file to that command's stdout. --- internal/pkg/util/util.go | 50 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/internal/pkg/util/util.go b/internal/pkg/util/util.go index ae2b7351..5cc33d2a 100644 --- a/internal/pkg/util/util.go +++ b/internal/pkg/util/util.go @@ -596,10 +596,14 @@ func FileGz( compressor, err := exec.LookPath("pigz") if err != nil { wwlog.Verbose("Could not locate PIGZ") - compressor = "gzip" + compressor, err := exec.LookPath("gzip") + if err != nil { + wwlog.Verbose("Could not locate GZIP") + return errors.Wrapf(err, "No compressor program for image file: %s", file_gz) + } } - wwlog.Verbose("Using gz compressor: %s", compressor) + wwlog.Verbose("Using compressor program: %s", compressor) proc := exec.Command( compressor, @@ -608,6 +612,48 @@ func FileGz( out, err := proc.CombinedOutput() if len(out) > 0 { + if err != nil && strings.HasSuffix(compressor, "gzip") && strings.Contains(out, "unrecognized option") { + /* Older version of gzip, try it another way: */ + wwlog.Verbose("%s does not recognize the --keep flag, trying piped stdout", compressor) + + /* Open the output file for writing: */ + gzippedFile, err := os.Create(file_gz) + if err != nil { + return errors.Wrapf(err, "Unable to open compressed image file for writing: %s", file_gz) + } + + /* We'll execute gzip with output to stdout and attach stdout to the compressed file we just + created: + */ + proc = exec.Command( + compressor, + "--stdout", + file ) + proc.Stdout = gzippedFile + gzipStderr, err := proc.StderrPipe() + if err != nil { + return errors.Wrapf(err, "Unable to open stderr pipe for compression program: %s", compressor) + } + + /* Execute the command: */ + err = proc.Start() + if err != nil { + proc.Wait() + gzippedFile.Close() + os.Remove(file_gz) + err = errors.Wrapf(err, "Unable to successfully execute compression program: %s", compressor) + } else { + err = proc.Wait() + gzippedFile.Close() + if err != nil { + os.Remove(file_gz) + err = errors.Wrapf(err, "Unable to successfully create compressed image file: %s", file_gz) + } else { + wwlog.Verbose("Successfully compressed image file: %s", file_gz) + } + } + out, _ = io.ReadAll(gzipStderr) + } wwlog.Debug(string(out)) } From e9d7432e79be08ca2835f41e0c6f6341d0a7698d Mon Sep 17 00:00:00 2001 From: Jeffrey Frey Date: Tue, 29 Nov 2022 14:10:16 -0500 Subject: [PATCH 2/6] Fixup spacing Tabs, not spaces. --- internal/pkg/util/util.go | 94 +++++++++++++++++++-------------------- 1 file changed, 47 insertions(+), 47 deletions(-) diff --git a/internal/pkg/util/util.go b/internal/pkg/util/util.go index 5cc33d2a..62ac0c89 100644 --- a/internal/pkg/util/util.go +++ b/internal/pkg/util/util.go @@ -596,11 +596,11 @@ func FileGz( compressor, err := exec.LookPath("pigz") if err != nil { wwlog.Verbose("Could not locate PIGZ") - compressor, err := exec.LookPath("gzip") - if err != nil { - wwlog.Verbose("Could not locate GZIP") - return errors.Wrapf(err, "No compressor program for image file: %s", file_gz) - } + compressor, err := exec.LookPath("gzip") + if err != nil { + wwlog.Verbose("Could not locate GZIP") + return errors.Wrapf(err, "No compressor program for image file: %s", file_gz) + } } wwlog.Verbose("Using compressor program: %s", compressor) @@ -612,48 +612,48 @@ func FileGz( out, err := proc.CombinedOutput() if len(out) > 0 { - if err != nil && strings.HasSuffix(compressor, "gzip") && strings.Contains(out, "unrecognized option") { - /* Older version of gzip, try it another way: */ - wwlog.Verbose("%s does not recognize the --keep flag, trying piped stdout", compressor) - - /* Open the output file for writing: */ - gzippedFile, err := os.Create(file_gz) - if err != nil { - return errors.Wrapf(err, "Unable to open compressed image file for writing: %s", file_gz) - } - - /* We'll execute gzip with output to stdout and attach stdout to the compressed file we just - created: - */ - proc = exec.Command( - compressor, - "--stdout", - file ) - proc.Stdout = gzippedFile - gzipStderr, err := proc.StderrPipe() - if err != nil { - return errors.Wrapf(err, "Unable to open stderr pipe for compression program: %s", compressor) - } - - /* Execute the command: */ - err = proc.Start() - if err != nil { - proc.Wait() - gzippedFile.Close() - os.Remove(file_gz) - err = errors.Wrapf(err, "Unable to successfully execute compression program: %s", compressor) - } else { - err = proc.Wait() - gzippedFile.Close() - if err != nil { - os.Remove(file_gz) - err = errors.Wrapf(err, "Unable to successfully create compressed image file: %s", file_gz) - } else { - wwlog.Verbose("Successfully compressed image file: %s", file_gz) - } - } - out, _ = io.ReadAll(gzipStderr) - } + if err != nil && strings.HasSuffix(compressor, "gzip") && strings.Contains(out, "unrecognized option") { + /* Older version of gzip, try it another way: */ + wwlog.Verbose("%s does not recognize the --keep flag, trying piped stdout", compressor) + + /* Open the output file for writing: */ + gzippedFile, err := os.Create(file_gz) + if err != nil { + return errors.Wrapf(err, "Unable to open compressed image file for writing: %s", file_gz) + } + + /* We'll execute gzip with output to stdout and attach stdout to the compressed file we just + created: + */ + proc = exec.Command( + compressor, + "--stdout", + file ) + proc.Stdout = gzippedFile + gzipStderr, err := proc.StderrPipe() + if err != nil { + return errors.Wrapf(err, "Unable to open stderr pipe for compression program: %s", compressor) + } + + /* Execute the command: */ + err = proc.Start() + if err != nil { + proc.Wait() + gzippedFile.Close() + os.Remove(file_gz) + err = errors.Wrapf(err, "Unable to successfully execute compression program: %s", compressor) + } else { + err = proc.Wait() + gzippedFile.Close() + if err != nil { + os.Remove(file_gz) + err = errors.Wrapf(err, "Unable to successfully create compressed image file: %s", file_gz) + } else { + wwlog.Verbose("Successfully compressed image file: %s", file_gz) + } + } + out, _ = io.ReadAll(gzipStderr) + } wwlog.Debug(string(out)) } From 52b932081ce7c6e2d76d260f4702be50f0bb5956 Mon Sep 17 00:00:00 2001 From: Jeffrey Frey Date: Tue, 29 Nov 2022 14:40:06 -0500 Subject: [PATCH 3/6] Scoping issue with `err` Use of := inside conditional body overrode scope of return value `err` --- internal/pkg/util/util.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/internal/pkg/util/util.go b/internal/pkg/util/util.go index 62ac0c89..ab2d67cc 100644 --- a/internal/pkg/util/util.go +++ b/internal/pkg/util/util.go @@ -596,7 +596,7 @@ func FileGz( compressor, err := exec.LookPath("pigz") if err != nil { wwlog.Verbose("Could not locate PIGZ") - compressor, err := exec.LookPath("gzip") + compressor, err = exec.LookPath("gzip") if err != nil { wwlog.Verbose("Could not locate GZIP") return errors.Wrapf(err, "No compressor program for image file: %s", file_gz) @@ -612,12 +612,15 @@ func FileGz( out, err := proc.CombinedOutput() if len(out) > 0 { - if err != nil && strings.HasSuffix(compressor, "gzip") && strings.Contains(out, "unrecognized option") { + outStr := string(out[:]) + if err != nil && strings.HasSuffix(compressor, "gzip") && strings.Contains(outStr, "unrecognized option") { + var gzippedFile *os.File + /* Older version of gzip, try it another way: */ wwlog.Verbose("%s does not recognize the --keep flag, trying piped stdout", compressor) /* Open the output file for writing: */ - gzippedFile, err := os.Create(file_gz) + gzippedFile, err = os.Create(file_gz) if err != nil { return errors.Wrapf(err, "Unable to open compressed image file for writing: %s", file_gz) } From fd1f197468cc1dc3beac554f7a2e8e2dda779d78 Mon Sep 17 00:00:00 2001 From: Jeffrey Frey Date: Tue, 29 Nov 2022 15:09:14 -0500 Subject: [PATCH 4/6] Update util.go --- internal/pkg/util/util.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/internal/pkg/util/util.go b/internal/pkg/util/util.go index ab2d67cc..ce4dc49c 100644 --- a/internal/pkg/util/util.go +++ b/internal/pkg/util/util.go @@ -615,7 +615,8 @@ func FileGz( outStr := string(out[:]) if err != nil && strings.HasSuffix(compressor, "gzip") && strings.Contains(outStr, "unrecognized option") { var gzippedFile *os.File - + var gzipStderr io.ReadCloser + /* Older version of gzip, try it another way: */ wwlog.Verbose("%s does not recognize the --keep flag, trying piped stdout", compressor) @@ -633,7 +634,7 @@ func FileGz( "--stdout", file ) proc.Stdout = gzippedFile - gzipStderr, err := proc.StderrPipe() + gzipStderr, err = proc.StderrPipe() if err != nil { return errors.Wrapf(err, "Unable to open stderr pipe for compression program: %s", compressor) } From 8c6f85a0a9a59599ba6f005d1a093d075cb0202a Mon Sep 17 00:00:00 2001 From: Jeffrey Frey Date: Tue, 29 Nov 2022 15:13:00 -0500 Subject: [PATCH 5/6] Update util.go --- internal/pkg/util/util.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/pkg/util/util.go b/internal/pkg/util/util.go index ce4dc49c..8cdad4a1 100644 --- a/internal/pkg/util/util.go +++ b/internal/pkg/util/util.go @@ -618,7 +618,7 @@ func FileGz( var gzipStderr io.ReadCloser /* Older version of gzip, try it another way: */ - wwlog.Verbose("%s does not recognize the --keep flag, trying piped stdout", compressor) + wwlog.Verbose("%s does not recognize the --keep flag, trying redirected stdout", compressor) /* Open the output file for writing: */ gzippedFile, err = os.Create(file_gz) From 17d6448180fc32843aa8470371bf144b02ba89ae Mon Sep 17 00:00:00 2001 From: Jeffrey Frey Date: Wed, 30 Nov 2022 16:08:58 -0500 Subject: [PATCH 6/6] Fix unhandled return value from Wait() Discard return error from proc.Wait() to pass lint tests --- internal/pkg/util/util.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/pkg/util/util.go b/internal/pkg/util/util.go index 8cdad4a1..3f97fbb5 100644 --- a/internal/pkg/util/util.go +++ b/internal/pkg/util/util.go @@ -642,7 +642,7 @@ func FileGz( /* Execute the command: */ err = proc.Start() if err != nil { - proc.Wait() + _ = proc.Wait() gzippedFile.Close() os.Remove(file_gz) err = errors.Wrapf(err, "Unable to successfully execute compression program: %s", compressor)