Skip to content

Commit

Permalink
Issue open-horizon#3667 - Bug: check error on deferred file close
Browse files Browse the repository at this point in the history
Signed-off-by: Max McAdam <max@fredcom.com>
  • Loading branch information
MaxMcAdam committed Feb 17, 2023
1 parent c929fd2 commit 1b5dcd0
Show file tree
Hide file tree
Showing 12 changed files with 69 additions and 45 deletions.
3 changes: 2 additions & 1 deletion api/api_publickey.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/golang/glog"
"github.com/gorilla/mux"
"github.com/open-horizon/anax/cutil"
)

func (a *API) publickey(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -90,7 +91,7 @@ func returnFileBytes(filename string, w http.ResponseWriter) error {
// Open the file so that we can read any header info that might be there.
file, err := os.Open(filename)
if file != nil {
defer file.Close()
defer cutil.CloseFileLogError(file)
}

if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion cli/cliconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"strings"

"github.com/open-horizon/anax/cli/cliutils"
"github.com/open-horizon/anax/cutil"
"github.com/open-horizon/anax/i18n"
)

Expand Down Expand Up @@ -209,7 +210,7 @@ func GetConfigFromNonJsonFile(configFile string) (map[string]string, error) {
if err != nil {
return hzn_vars, err
}
defer fileHandle.Close()
defer cutil.CloseFileLogError(fileHandle)

// regex for comment line
r, _ := regexp.Compile(`^(\s)*#(.*)*`)
Expand Down
2 changes: 1 addition & 1 deletion cli/cliutils/cliutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -1030,7 +1030,7 @@ func GetEnvVarFromFile(filename string, key string) (string, error) {
return "", err
}
}
defer fHandle.Close()
defer cutil.CloseFileLogError(fHandle)

scanner := bufio.NewScanner(fHandle)
for scanner.Scan() {
Expand Down
6 changes: 3 additions & 3 deletions cli/cliutils/stdout_logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"
"time"

"github.com/open-horizon/anax/cutil"
"github.com/open-horizon/anax/i18n"
)

Expand Down Expand Up @@ -64,7 +65,7 @@ func LogLinux(instanceId string, tailing bool) {
if err != nil {
Fatal(NOT_FOUND, msgPrinter.Sprintf("%v could not be opened or does not exist: %v", sysLogPath, err))
}
defer file.Close()
defer cutil.CloseFileLogError(file)
// Check file stats and capture the current size of the file if we will be tailing it.
var file_size int64
if tailing {
Expand Down Expand Up @@ -114,12 +115,11 @@ func LogLinux(instanceId string, tailing bool) {
file_size = new_file_size
} else {
// Log rotation has occurred. Re-open the new syslog file and capture the current size.
file.Close()
cutil.CloseFileLogError(file)
file, err = os.Open(sysLogPath)
if err != nil {
Fatal(NOT_FOUND, msgPrinter.Sprintf("%v could not be opened or does not exist: %v", sysLogPath, err))
}
defer file.Close()
// Setup a new reader on the new file.
reader = bufio.NewReader(file)
fi, err := file.Stat()
Expand Down
2 changes: 1 addition & 1 deletion cli/exchange/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ func SaveOpYamlToFile(sId string, clusterDeployment string, filePath string, for
if err != nil {
cliutils.Fatal(cliutils.INTERNAL_ERROR, msgPrinter.Sprintf("Failed to create file %v. %v", fileName, err))
}
defer file.Close()
defer cutil.CloseFileLogError(file)

if _, err := file.Write(archiveData); err != nil {
cliutils.Fatal(cliutils.INTERNAL_ERROR, msgPrinter.Sprintf("Failed to save the clusterDeployment operator yaml to file %v. %v", fileName, err))
Expand Down
5 changes: 3 additions & 2 deletions cli/sdo/voucher.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/open-horizon/anax/cli/exchange"
"github.com/open-horizon/anax/cli/register"
"github.com/open-horizon/anax/config"
"github.com/open-horizon/anax/cutil"
anaxExchange "github.com/open-horizon/anax/exchange"
"github.com/open-horizon/anax/exchangecommon"
"github.com/open-horizon/anax/i18n"
Expand Down Expand Up @@ -54,7 +55,7 @@ type InspectOutput struct {

// hzn sdo voucher inspect <voucher-file>
func VoucherInspect(voucherFile *os.File) {
defer voucherFile.Close()
defer cutil.CloseFileLogError(voucherFile)
msgPrinter := i18n.GetMessagePrinter()
cliutils.Verbose(msgPrinter.Sprintf("Inspecting voucher file name: %s", voucherFile.Name()))

Expand Down Expand Up @@ -317,7 +318,7 @@ func VoucherDownload(org, userCreds, device, outputFile string, overwrite bool)

// hzn sdo voucher import <voucher-file>
func VoucherImport(org, userCreds string, voucherFile *os.File, example, policyFilePath, patternName, userInputFileName, haGroupName string) {
defer voucherFile.Close()
defer cutil.CloseFileLogError(voucherFile)
msgPrinter := i18n.GetMessagePrinter()
cliutils.Verbose(msgPrinter.Sprintf("Importing voucher file name: %s", voucherFile.Name()))

Expand Down
5 changes: 3 additions & 2 deletions cli/sync_service/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/open-horizon/anax/cli/cliconfig"
"github.com/open-horizon/anax/cli/cliutils"
"github.com/open-horizon/anax/config"
"github.com/open-horizon/anax/cutil"
"github.com/open-horizon/anax/exchange"
"github.com/open-horizon/anax/i18n"
"github.com/open-horizon/edge-sync-service/common"
Expand Down Expand Up @@ -354,7 +355,7 @@ func ObjectPublish(org string, userPw string, objType string, objId string, objP

// Create public key. Sign data. Set "hashAlgorithm", "publicKey" and "signature" field
file, err := os.Open(objFile)
defer file.Close()
defer cutil.CloseFileLogError(file)
if err != nil {
cliutils.Fatal(cliutils.CLI_INPUT_ERROR, msgPrinter.Sprintf("unable to open object file %v: %v", objFile, err))
}
Expand Down Expand Up @@ -408,7 +409,7 @@ func ObjectPublish(org string, userPw string, objType string, objId string, objP
if err != nil {
cliutils.Fatal(cliutils.CLI_INPUT_ERROR, msgPrinter.Sprintf("unable to open object file %v: %v", objFile, err))
}
defer file.Close()
defer cutil.CloseFileLogError(file)

if noChunkUpload {
cliutils.ExchangePutPost("Model Management Service", http.MethodPut, cliutils.GetMMSUrl(), urlPath, cliutils.OrgAndCreds(org, userPw), []int{204}, file, nil)
Expand Down
65 changes: 37 additions & 28 deletions cli/sync_service/sync_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,45 +413,54 @@ func loadCSS(org string, fileType string, fileObjects []string) error {
msgPrinter := i18n.GetMessagePrinter()

for _, fileName := range fileObjects {
cliutils.Verbose(msgPrinter.Sprintf("Loading %v into CSS", fileName))
if err := loadCSSFile(org, fileType, fileName); err != nil {
return err
}
}

if fileObject, err := os.Open(fileName); err != nil {
return errors.New(msgPrinter.Sprintf("unable to open file object %v, error %v", fileName, err))
} else if fileBytes, err := ioutil.ReadAll(fileObject); err != nil {
return errors.New(msgPrinter.Sprintf("unable to read file object %v, error %v", fileName, err))
} else {
if len(fileObjects) > 0 {
msgPrinter.Printf("Configuration files %v loaded into the File sync service.", fileObjects)
msgPrinter.Println()
}

defer fileObject.Close()
return nil

fileObjectName := path.Base(fileName)
}

metadata := &cssFileMeta{
ObjectID: fileObjectName,
ObjectType: fileType,
}
func loadCSSFile(org string, fileType string, fileName string) error {
// get message printer
msgPrinter := i18n.GetMessagePrinter()

// Get this host's IP address because that's where the CSS is listening.
hostIP := os.Getenv("HZN_DEV_HOST_IP")
if hostIP == "" {
hostIP = "localhost"
}
cliutils.Verbose(msgPrinter.Sprintf("Loading %v into CSS", fileName))

// Form the CSS URL and then PUT the file into the CSS.
url := fmt.Sprintf("http://%v:%v/api/v1/objects/%v/%v/%v", hostIP, getCSSPort(), org, fileType, fileObjectName)
if fileObject, err := os.Open(fileName); err != nil {
return errors.New(msgPrinter.Sprintf("unable to open file object %v, error %v", fileName, err))
} else if fileBytes, err := ioutil.ReadAll(fileObject); err != nil {
return errors.New(msgPrinter.Sprintf("unable to read file object %v, error %v", fileName, err))
} else {
defer cutil.CloseFileLogError(fileObject)

if err := putFile(url, org, metadata, fileBytes); err != nil {
return errors.New(msgPrinter.Sprintf("unable to add file %v to the CSS, error %v", fileName, err))
}
fileObjectName := path.Base(fileName)

metadata := &cssFileMeta{
ObjectID: fileObjectName,
ObjectType: fileType,
}
}

if len(fileObjects) > 0 {
msgPrinter.Printf("Configuration files %v loaded into the File sync service.", fileObjects)
msgPrinter.Println()
}
// Get this host's IP address because that's where the CSS is listening.
hostIP := os.Getenv("HZN_DEV_HOST_IP")
if hostIP == "" {
hostIP = "localhost"
}

return nil
// Form the CSS URL and then PUT the file into the CSS.
url := fmt.Sprintf("http://%v:%v/api/v1/objects/%v/%v/%v", hostIP, getCSSPort(), org, fileType, fileObjectName)

if err := putFile(url, org, metadata, fileBytes); err != nil {
return errors.New(msgPrinter.Sprintf("unable to add file %v to the CSS, error %v", fileName, err))
}
}
return nil
}

type cssFileMeta struct {
Expand Down
2 changes: 1 addition & 1 deletion cli/unregister/unregister.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ func backupEventLogs() error {
if err != nil {
return errors.New(msgPrinter.Sprintf("failed to backup eventlogs file %v. %v", fileName, err))
}
defer file.Close()
defer cutil.CloseFileLogError(file)

if _, err := file.Write(elogsJson); err != nil {
return errors.New(msgPrinter.Sprintf("failed to save the eventlogs to file %v. %v", fileName, err))
Expand Down
16 changes: 13 additions & 3 deletions cutil/cutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ func GetCPUCount(cpuinfo_file string) (int, error) {
if fh, err := os.Open(cpuinfo_file); err != nil {
return 0, err
} else {
defer fh.Close()
defer CloseFileLogError(fh)

cpu_count := 0
scanner := bufio.NewScanner(fh)
Expand Down Expand Up @@ -653,7 +653,7 @@ func GetMachineSerial(cpuInfoFile string) (string, error) {
if err != nil {
return "", err
}
defer fh.Close()
defer CloseFileLogError(fh)

scanner := bufio.NewScanner(fh)
r, _ := regexp.Compile("Serial([ \t]*):")
Expand Down Expand Up @@ -694,7 +694,7 @@ func GetMemInfo(meminfo_file string) (uint64, uint64, error) {
if fh, err := os.Open(meminfo_file); err != nil {
return 0, 0, err
} else {
defer fh.Close()
defer CloseFileLogError(fh)

total_mem := uint64(0)
avail_mem := uint64(0)
Expand Down Expand Up @@ -826,3 +826,13 @@ func GetCertificateVersion(certFilePath string) string {
}
return version
}

// use this when deferring a file close so that any error message returned by the close is logged
func CloseFileLogError(f *os.File) {
if f == nil {
return
}
if err := f.Close(); err != nil {
glog.Errorf("Error closing file: %v", err)
}
}
3 changes: 2 additions & 1 deletion helm/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/base64"
"fmt"
"github.com/golang/glog"
"github.com/open-horizon/anax/cutil"
"io/ioutil"
"os"
)
Expand Down Expand Up @@ -43,7 +44,7 @@ func ConvertB64StringToFile(b64Package string) (string, error) {
} else if f, err := ioutil.TempFile("", TEMP_PACKAGE_PREFIX); err != nil {
return "", err
} else {
defer f.Close()
defer cutil.CloseFileLogError(f)
num, err := f.Write(sDec)
if err != nil {
return "", err
Expand Down
2 changes: 1 addition & 1 deletion imagefetch/imagepull.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func dockerCredsFromConfigFile(configFilePath string) (*docker.AuthConfiguration

f, err := os.Open(configFilePath)
if f != nil {
defer f.Close()
defer cutil.CloseFileLogError(f)
}
if err != nil {
return nil, err
Expand Down

0 comments on commit 1b5dcd0

Please sign in to comment.