Repository: opencontainers/runc
File: libcontainer/intelrdt/intelrdt.go (Go)

Code before:
	// the actual bandwidth using MBM counters and adjust the memory
	// bandwidth percentages to ensure:
	// "actual memory bandwidth < user specified memory bandwidth".
	//
	// For example, on a two-socket machine, the schema line could be
	// "MB:0=5000;1=7000" which means 5000 MBps memory bandwidth limit on
	// socket 0 and 7000 MBps memory bandwidth limit on socket 1.
	if r := container.IntelRdt; r != nil {
		// TODO: verify that l3CacheSchema and/or memBwSchema match the
		// existing schemata if ClosID has been specified. This is a more
		// involved than reading the file and doing plain string comparison as
		// the value written in does not necessarily match what gets read out
		// (leading zeros, cache id ordering etc).
		path := filepath.Join(m.GetPath(), "schemata")
		for _, line := range append([]string{r.L3CacheSchema, r.MemBwSchema}, r.Schemata...) {
			if line != "" {
				f, err := os.OpenFile(path, os.O_WRONLY|os.O_APPEND, 0o600)
				if err != nil {
					return err
				}
				if _, err := f.Write([]byte(line + "\n")); err != nil {
					_ = f.Close() // Ignore error, we can't do much about it.
					return newLastCmdError(fmt.Errorf("intelrdt: unable to write %v: %w", line, err))
				}
				if err := f.Close(); err != nil {
					return newLastCmdError(fmt.Errorf("intelrdt: failed to close file after writing %v: %w", line, err))
				}
			}
		}
	}
	return nil
}

func newLastCmdError(err error) error {
	status, err1 := getLastCmdStatus()
	if err1 == nil {
		return fmt.Errorf("%w, last_cmd_status: %s", err, status)
	}
	return err
}


Review comment (bug):
```suggestion
					return newLastCmdError(fmt.Errorf("intelrdt: failed to close file after writing %q: %w", line, err))
```

Code after:
	// the actual bandwidth using MBM counters and adjust the memory
	// bandwidth percentages to ensure:
	// "actual memory bandwidth < user specified memory bandwidth".
	//
	// For example, on a two-socket machine, the schema line could be
	// "MB:0=5000;1=7000" which means 5000 MBps memory bandwidth limit on
	// socket 0 and 7000 MBps memory bandwidth limit on socket 1.
	if r := container.IntelRdt; r != nil {
		// TODO: verify that l3CacheSchema and/or memBwSchema match the
		// existing schemata if ClosID has been specified. This is a more
		// involved than reading the file and doing plain string comparison as
		// the value written in does not necessarily match what gets read out
		// (leading zeros, cache id ordering etc).
		var schemata strings.Builder
		for _, s := range append([]string{r.L3CacheSchema, r.MemBwSchema}, r.Schemata...) {
			if s != "" {
				schemata.WriteString(s)
				schemata.WriteString("\n")
			}
		}

		if schemata.Len() > 0 {
			path := filepath.Join(m.GetPath(), "schemata")
			if err := os.WriteFile(path, []byte(schemata.String()), 0o600); err != nil {
				return newLastCmdError(fmt.Errorf("intelrdt: unable to write %q: %w", schemata.String(), err))
			}
		}
	}

	return nil
}

func newLastCmdError(err error) error {
	status, err1 := getLastCmdStatus()
	if err1 == nil {
		return fmt.Errorf("%w, last_cmd_status: %s", err, status)
	}
	return err
}