summaryrefslogtreecommitdiff
path: root/libpod/container_internal.go
diff options
context:
space:
mode:
authorOpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com>2021-01-13 09:58:09 -0500
committerGitHub <noreply@github.com>2021-01-13 09:58:09 -0500
commit99c5746150799d6cfe3ea612d3c88c6b904f9312 (patch)
treefe856f549b38f0ca55f7c2dd8b8640e4b0a22214 /libpod/container_internal.go
parent183f443a585a3659d807ee413e5b708d37a72924 (diff)
parent0cff5ad0a30cf7de1d4c23ca56a5670c7e2f1192 (diff)
downloadpodman-99c5746150799d6cfe3ea612d3c88c6b904f9312.tar.gz
podman-99c5746150799d6cfe3ea612d3c88c6b904f9312.tar.bz2
podman-99c5746150799d6cfe3ea612d3c88c6b904f9312.zip
Merge pull request #8958 from zhangguanzhang/duplicated-hosts
Fixes /etc/hosts duplicated every time after container restarted in a pod
Diffstat (limited to 'libpod/container_internal.go')
-rw-r--r--libpod/container_internal.go17
1 files changed, 14 insertions, 3 deletions
diff --git a/libpod/container_internal.go b/libpod/container_internal.go
index c7548e0e5..f4cdb749f 100644
--- a/libpod/container_internal.go
+++ b/libpod/container_internal.go
@@ -1,6 +1,7 @@
package libpod
import (
+ "bufio"
"bytes"
"context"
"fmt"
@@ -1865,16 +1866,26 @@ func (c *Container) writeStringToStaticDir(filename, contents string) (string, e
return destFileName, nil
}
-// appendStringToRundir appends the provided string to the runtimedir file
-func (c *Container) appendStringToRundir(destFile, output string) (string, error) {
+// appendStringToRunDir appends the provided string to the runtimedir file
+func (c *Container) appendStringToRunDir(destFile, output string) (string, error) {
destFileName := filepath.Join(c.state.RunDir, destFile)
- f, err := os.OpenFile(destFileName, os.O_APPEND|os.O_WRONLY, 0600)
+ f, err := os.OpenFile(destFileName, os.O_APPEND|os.O_RDWR, 0600)
if err != nil {
return "", err
}
defer f.Close()
+ compareStr := strings.TrimRight(output, "\n")
+ scanner := bufio.NewScanner(f)
+ scanner.Split(bufio.ScanLines)
+
+ for scanner.Scan() {
+ if strings.Compare(scanner.Text(), compareStr) == 0 {
+ return filepath.Join(c.state.RunDir, destFile), nil
+ }
+ }
+
if _, err := f.WriteString(output); err != nil {
return "", errors.Wrapf(err, "unable to write %s", destFileName)
}