Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkg/daemon: write bytes, use buffered writer and compare bytes #411

Merged
merged 1 commit into from
Feb 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should now be much faster compared to string methods as it's directly implemented in assembly as well

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 @@ -537,9 +538,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