Skip to content

Commit

Permalink
Merge pull request #411 from runcom/buffer-file-encoding
Browse files Browse the repository at this point in the history
pkg/daemon: write bytes, use buffered writer and compare bytes
  • Loading branch information
openshift-merge-robot committed Feb 12, 2019
2 parents 691bf5f + 610f216 commit f6d7208
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
13 changes: 7 additions & 6 deletions pkg/daemon/daemon.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package daemon

import (
"bytes"
"context"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -806,7 +807,7 @@ func checkUnits(units []ignv2_2types.Unit) bool {
for _, u := range units {
for j := range u.Dropins {
path := filepath.Join(pathSystemd, u.Name+".d", u.Dropins[j].Name)
if status := checkFileContentsAndMode(path, u.Dropins[j].Contents, DefaultFilePermissions); !status {
if status := checkFileContentsAndMode(path, []byte(u.Dropins[j].Contents), DefaultFilePermissions); !status {
return false
}
}
Expand All @@ -827,7 +828,7 @@ func checkUnits(units []ignv2_2types.Unit) bool {
return false
}
}
if status := checkFileContentsAndMode(path, u.Contents, DefaultFilePermissions); !status {
if status := checkFileContentsAndMode(path, []byte(u.Contents), DefaultFilePermissions); !status {
return false
}

Expand All @@ -854,7 +855,7 @@ func checkFiles(files []ignv2_2types.File) bool {
glog.Errorf("couldn't parse file: %v", err)
return false
}
if status := checkFileContentsAndMode(f.Path, string(contents.Data), mode); !status {
if status := checkFileContentsAndMode(f.Path, contents.Data, mode); !status {
return false
}
checkedFiles[f.Path] = true
Expand All @@ -866,7 +867,7 @@ func checkFiles(files []ignv2_2types.File) bool {
// contents and mode with the expectedContent and mode parameters. It logs an
// error in case of an error or mismatch and returns the status of the
// evaluation.
func checkFileContentsAndMode(filePath, expectedContent string, mode os.FileMode) bool {
func checkFileContentsAndMode(filePath string, expectedContent []byte, mode os.FileMode) bool {
fi, err := os.Lstat(filePath)
if err != nil {
glog.Errorf("could not stat file: %q, error: %v", filePath, err)
Expand All @@ -881,8 +882,8 @@ func checkFileContentsAndMode(filePath, expectedContent string, mode os.FileMode
glog.Errorf("could not read file: %q, error: %v", filePath, err)
return false
}
if strings.Compare(string(contents), expectedContent) != 0 {
glog.Errorf("content mismatch for file: %q; expected: %v; received: %v", filePath, expectedContent, string(contents))
if !bytes.Equal(contents, expectedContent) {
glog.Errorf("content mismatch for file: %q", filePath)
return false
}
return true
Expand Down
4 changes: 2 additions & 2 deletions pkg/daemon/daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestOverwrittenFile(t *testing.T) {
},
}

if status := checkFiles(files); status != true {
if status := checkFiles(files); !status {
t.Errorf("Invalid files")
}

Expand Down Expand Up @@ -82,7 +82,7 @@ func TestOverwrittenFile(t *testing.T) {
},
}

if status := checkFiles(files); status != true {
if status := checkFiles(files); !status {
t.Errorf("Validating an overwritten file failed")
}
}
5 changes: 4 additions & 1 deletion pkg/daemon/update.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package daemon

import (
"bufio"
"encoding/json"
"fmt"
"io"
Expand Down Expand Up @@ -538,9 +539,11 @@ func (dn *Daemon) writeFiles(files []ignv2_2types.File) error {
if err != nil {
return err
}
if _, err = file.WriteString(string(contents.Data)); err != nil {
w := bufio.NewWriter(file)
if _, err := w.Write(contents.Data); err != nil {
return fmt.Errorf("Failed to write inline contents to file %q: %v", f.Path, err)
}
w.Flush()

// chmod and chown
mode := DefaultFilePermissions
Expand Down

0 comments on commit f6d7208

Please sign in to comment.