Skip to content
This repository has been archived by the owner on Oct 15, 2020. It is now read-only.

Commit

Permalink
Rename file.Copy file.Sync
Browse files Browse the repository at this point in the history
  • Loading branch information
errm committed Jun 9, 2018
1 parent 7c286f1 commit 9d34bd6
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,12 @@ func writeConfig(path string, templ *template.Template, data interface{}) error
if err != nil {
return err
}
return file.Copy(&buff, path, 0640)
return file.Sync(&buff, path, 0640)
}

func writeCertificate(path string, data string) error {
decoder := base64.NewDecoder(base64.StdEncoding, strings.NewReader(data))
return file.Copy(decoder, path, 0640)
return file.Sync(decoder, path, 0640)
}

func runCommand(name string, args ...string) error {
Expand All @@ -142,7 +142,7 @@ func setHostname(hostname string) error {
}
h := []byte(hostname)
log.Printf("setting hostname to %s", h)
if err := file.Copy(bytes.NewReader(h), "/etc/hostname", 0644); err != nil {
if err := file.Sync(bytes.NewReader(h), "/etc/hostname", 0644); err != nil {
return err
}
return syscall.Sethostname(h)
Expand Down
8 changes: 4 additions & 4 deletions pkg/file/copy.go → pkg/file/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (
"syscall"
)

// Copy atomicly copies data to a file at the given path with the given permissions
// If a directory does not exit it is created
// If the file allready exists and diff returns 0 then this command is a noopp
// Sync atomicly writes data to a file at the given path with the given permissions
//
// If the parent directory does not exit it is created
// If the file allready exists and diff returns 0 then this command is a noopp
// Requires the diff utility to be present on the system, since it is specified in POSIX we assume it is
func Copy(data io.Reader, path string, perm os.FileMode) error {
func Sync(data io.Reader, path string, perm os.FileMode) error {
dir := filepath.Dir(path)
if err := os.MkdirAll(dir, 0710); err != nil {
return err
Expand Down
8 changes: 4 additions & 4 deletions pkg/file/copy_test.go → pkg/file/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestWritingToNonExistantFile(t *testing.T) {

filename := filepath.Join(dir, "filename")

err = file.Copy(strings.NewReader("Hello World"), filename, 0640)
err = file.Sync(strings.NewReader("Hello World"), filename, 0640)
check(t, err)

contents, err := ioutil.ReadFile(filename)
Expand All @@ -40,7 +40,7 @@ func TestPermissions(t *testing.T) {

for _, perm := range perms {
filename := filepath.Join(dir, fmt.Sprintf("filename-%s", perm))
err = file.Copy(strings.NewReader("string"), filename, perm)
err = file.Sync(strings.NewReader("string"), filename, perm)
check(t, err)
info, err := os.Stat(filename)
check(t, err)
Expand All @@ -61,7 +61,7 @@ func TestOverwrite(t *testing.T) {
err = ioutil.WriteFile(filename, []byte("Old contents"), 0644)
check(t, err)

err = file.Copy(strings.NewReader("New contents"), filename, 0644)
err = file.Sync(strings.NewReader("New contents"), filename, 0644)
check(t, err)

contents, err := ioutil.ReadFile(filename)
Expand All @@ -85,7 +85,7 @@ func TestNoOppOverwrite(t *testing.T) {
mtime := info.ModTime()
time.Sleep(10 * time.Millisecond)

err = file.Copy(strings.NewReader("contents"), filename, 0644)
err = file.Sync(strings.NewReader("contents"), filename, 0644)
check(t, err)

info, err = os.Stat(filename)
Expand Down

0 comments on commit 9d34bd6

Please sign in to comment.