Commit f5b78322 authored by nitikag's avatar nitikag Committed by Ivan Aguilar
Browse files

Improve handling of file closure

cr https://code.amazon.com/reviews/CR-52099704
parent 999f36f1
......@@ -390,7 +390,7 @@ func DeleteSessionOrchestrationDirectories(log log.T, instanceID, orchestrationR
// This append only mode results into error while deletion of the file.
// Below logic is to attempt to delete ipcTempFile in case of such errors.
u := &utility.SessionUtil{}
success, err := u.DeleteIpcTempFile(sessionOrchestrationPath)
success, err := u.DeleteIpcTempFile(log, sessionOrchestrationPath)
if err != nil || !success {
log.Debugf("Retry attempt to delete session orchestration directory %s failed, %v", sessionOrchestrationPath, err)
continue
......
......@@ -50,7 +50,11 @@ func main() {
log.Errorf("Failed to open log file %s", args[1])
return
}
defer file.Close()
defer func() {
if closeErr := file.Close(); closeErr != nil {
log.Warnf("error occurred while closing file, %v", closeErr)
}
}()
enableVirtualTerminalProcessingForWindows, err := strconv.ParseBool(args[2])
if err != nil {
......
......@@ -222,7 +222,11 @@ func (p *ShellPlugin) execute(config agentContracts.Configuration,
output.MarkAsFailed(errorString)
return
}
defer ipcFile.Close()
defer func() {
if closeErr := ipcFile.Close(); closeErr != nil {
log.Warnf("error occurred while closing ipcFile, %v", closeErr)
}
}()
go func() {
cancelState := cancelFlag.Wait()
......
......@@ -323,9 +323,9 @@ func (p *ShellPlugin) generateLogData(log log.T, config agentContracts.Configura
if err = p.generateTranscriptFile(log, transcriptFile, p.logger.ipcFilePath, false, config); err != nil {
return err
}
cleanControlCharacters(transcriptFile, p.logger.logFilePath)
cleanControlCharacters(log, transcriptFile, p.logger.logFilePath)
} else {
cleanControlCharacters(p.logger.ipcFilePath, p.logger.logFilePath)
cleanControlCharacters(log, p.logger.ipcFilePath, p.logger.logFilePath)
}
return nil
......@@ -383,18 +383,26 @@ func (p *ShellPlugin) generateTranscriptFile(
}
// cleanControlCharacters cleans up control characters from the log file
func cleanControlCharacters(sourceFileName, destinationFileName string) error {
func cleanControlCharacters(log log.T, sourceFileName, destinationFileName string) error {
sourceFile, err := os.Open(sourceFileName)
if err != nil {
return err
}
defer sourceFile.Close()
defer func() {
if closeErr := sourceFile.Close(); closeErr != nil {
log.Warnf("error occurred while closing sourceFile, %v", closeErr)
}
}()
destinationFile, err := os.Create(destinationFileName)
if err != nil {
return err
}
defer destinationFile.Close()
defer func() {
if closeErr := destinationFile.Close(); closeErr != nil {
log.Warnf("error occurred while closing destinationFile, %v", closeErr)
}
}()
escapeCharRegEx := regexp.MustCompile(`←`)
specialChar1RegEx := regexp.MustCompile(`\[\?[0-9]+[a-zA-Z]`)
......
......@@ -111,7 +111,11 @@ func (u *SessionUtil) createSudoersFileIfNotPresent(log log.T) error {
log.Errorf("Failed to add %s to sudoers file: %v", appconfig.DefaultRunAsUserName, err)
return err
}
defer file.Close()
defer func() {
if closeErr := file.Close(); closeErr != nil {
log.Warnf("error occurred while closing file, %v", closeErr)
}
}()
if _, err := file.WriteString(fmt.Sprintf("# User rules for %s\n", appconfig.DefaultRunAsUserName)); err != nil {
return err
......@@ -170,7 +174,7 @@ func (u *SessionUtil) GetAttr(f *os.File) (int32, error) {
}
// DeleteIpcTempFile resets file properties of ipcTempFile and tries deletion
func (u *SessionUtil) DeleteIpcTempFile(sessionOrchestrationPath string) (bool, error) {
func (u *SessionUtil) DeleteIpcTempFile(log log.T, sessionOrchestrationPath string) (bool, error) {
ipcTempFilePath := filepath.Join(sessionOrchestrationPath, appconfig.PluginNameStandardStream, "ipcTempFile.log")
// check if ipcTempFile exists
......@@ -183,7 +187,11 @@ func (u *SessionUtil) DeleteIpcTempFile(sessionOrchestrationPath string) (bool,
if err != nil {
return false, fmt.Errorf("failed to open ipcTempFile %s, %v", ipcTempFilePath, err)
}
defer ipcFile.Close()
defer func() {
if closeErr := ipcFile.Close(); closeErr != nil {
log.Warnf("error occurred while closing ipcFile, %v", closeErr)
}
}()
// reset file attributes
if err := u.SetAttr(ipcFile, FS_RESET_FL); err != nil {
......
......@@ -638,7 +638,7 @@ func NewListener(log log.T, address string) (listener net.Listener, err error) {
}
// DeleteIpcTempFile resets file properties of ipcTempFile and tries deletion
func (u *SessionUtil) DeleteIpcTempFile(sessionOrchestrationPath string) (bool, error) {
func (u *SessionUtil) DeleteIpcTempFile(log log.T, sessionOrchestrationPath string) (bool, error) {
// no deletion needed for windows since ipcTempFile properties are default and no issues during deletion
return false, nil
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment