Skip to content

Commit

Permalink
inline ioutil.WriteFile and ioutil.ReadFile calls throughout
Browse files Browse the repository at this point in the history
  • Loading branch information
stapelberg committed Jan 13, 2025
1 parent 33f6708 commit 3a9d1ec
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 39 deletions.
9 changes: 4 additions & 5 deletions error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package rsync_test

import (
"bytes"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -91,12 +90,12 @@ func TestNoReadPermission(t *testing.T) {
t.Fatal(err)
}
dummy := filepath.Join(source, "dummy")
if err := ioutil.WriteFile(dummy, []byte("dummy"), 0644); err != nil {
if err := os.WriteFile(dummy, []byte("dummy"), 0644); err != nil {
t.Fatal(err)
}
other := filepath.Join(source, "other")
want := []byte("other file contents")
if err := ioutil.WriteFile(other, want, 0644); err != nil {
if err := os.WriteFile(other, want, 0644); err != nil {
t.Fatal(err)
}

Expand Down Expand Up @@ -126,12 +125,12 @@ func TestNoReadPermission(t *testing.T) {
if os.Getuid() > 0 {
// uid 0 can read the file despite chmod(0), so skip this check:

if _, err := ioutil.ReadFile(filepath.Join(dest, "dummy")); err == nil {
if _, err := os.ReadFile(filepath.Join(dest, "dummy")); err == nil {
t.Fatalf("dummy file unexpectedly created in the destination")
}
}

got, err := ioutil.ReadFile(filepath.Join(dest, "other"))
got, err := os.ReadFile(filepath.Join(dest, "other"))
if err != nil {
t.Fatal(err)
}
Expand Down
5 changes: 2 additions & 3 deletions internal/anonssh/anonssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"os"
"path/filepath"
Expand Down Expand Up @@ -256,7 +255,7 @@ func loadHostKey() (ssh.Signer, error) {
return nil, err
}
path := filepath.Join(dir, "gokr-rsyncd", "ssh_host_ed25519_key")
b, err := ioutil.ReadFile(path)
b, err := os.ReadFile(path)
if err != nil {
if os.IsNotExist(err) {
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
Expand All @@ -275,7 +274,7 @@ func loadHostKey() (ssh.Signer, error) {
}

func loadAuthorizedKeys(path string) (map[string]bool, error) {
b, err := ioutil.ReadFile(path)
b, err := os.ReadFile(path)
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions internal/daemonmaincmd/writetest.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ package maincmd

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
)

func canUnexpectedlyWriteTo(dir string) error {
fn := filepath.Join(dir, "gokr-rsyncd.unexpectedly_writable")
if err := ioutil.WriteFile(fn, []byte("gokr-rsyncd creates this file to prevent misconfigurations. if you see this file, it means gokr-rsyncd unexpectedly was started with too many privileges"), 0644); err == nil {
if err := os.WriteFile(fn, []byte("gokr-rsyncd creates this file to prevent misconfigurations. if you see this file, it means gokr-rsyncd unexpectedly was started with too many privileges"), 0644); err == nil {
os.Remove(fn)
return fmt.Errorf("unexpectedly able to write file to %s, exiting", dir)
}
Expand Down
3 changes: 1 addition & 2 deletions internal/rsyncchecksum/checksum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package rsyncchecksum_test

import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand All @@ -25,7 +24,7 @@ func writeLargeDataFile(t *testing.T, source string, headPattern, bodyPattern, e
if err := os.MkdirAll(filepath.Dir(large), 0755); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(large, content, 0644); err != nil {
if err := os.WriteFile(large, content, 0644); err != nil {
t.Fatal(err)
}
}
Expand Down
3 changes: 1 addition & 2 deletions internal/rsyncdconfig/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package rsyncdconfig

import (
"io/ioutil"
"os"
"path/filepath"

Expand Down Expand Up @@ -36,7 +35,7 @@ func FromString(input string) (*Config, error) {
}

func FromFile(path string) (*Config, error) {
input, err := ioutil.ReadFile(path)
input, err := os.ReadFile(path)
if err != nil {
return nil, err
}
Expand Down
5 changes: 2 additions & 3 deletions internal/rsynctest/rsynctest.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"os"
"os/exec"
Expand Down Expand Up @@ -276,14 +275,14 @@ func WriteLargeDataFile(t *testing.T, source string, headPattern, bodyPattern, e
if err := os.MkdirAll(filepath.Dir(large), 0755); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(large, content, 0644); err != nil {
if err := os.WriteFile(large, content, 0644); err != nil {
t.Fatal(err)
}
}

func DataFileMatches(fn string, headPattern, bodyPattern, endPattern []byte) error {
want := ConstructLargeDataFile(headPattern, bodyPattern, endPattern)
got, err := ioutil.ReadFile(fn)
got, err := os.ReadFile(fn)
if err != nil {
return err
}
Expand Down
19 changes: 9 additions & 10 deletions interop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
Expand Down Expand Up @@ -82,7 +81,7 @@ func TestInterop(t *testing.T) {
}
dummy := filepath.Join(source, "dummy")
want := []byte("heyo")
if err := ioutil.WriteFile(dummy, want, 0644); err != nil {
if err := os.WriteFile(dummy, want, 0644); err != nil {
t.Fatal(err)
}

Expand Down Expand Up @@ -119,7 +118,7 @@ func TestInterop(t *testing.T) {
// list = true

// `
// if err := ioutil.WriteFile(config, []byte(rsyncdConfig), 0644); err != nil {
// if err := os.WriteFile(config, []byte(rsyncdConfig), 0644); err != nil {
// t.Fatal(err)
// }
// srv := exec.Command("rsync",
Expand Down Expand Up @@ -176,7 +175,7 @@ func TestInterop(t *testing.T) {
}

{
got, err := ioutil.ReadFile(filepath.Join(dest, "dummy"))
got, err := os.ReadFile(filepath.Join(dest, "dummy"))
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -233,7 +232,7 @@ func createSourceFiles(t *testing.T) (string, string, string) {
if err := os.MkdirAll(filepath.Dir(dummy), 0755); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(dummy, []byte(subdir), 0644); err != nil {
if err := os.WriteFile(dummy, []byte(subdir), 0644); err != nil {
t.Fatal(err)
}
}
Expand All @@ -258,7 +257,7 @@ func sourcesArgs(t *testing.T) []string {
func sourceFullySyncedTo(t *testing.T, dest string) error {
{
want := []byte("expensive")
got, err := ioutil.ReadFile(filepath.Join(dest, "dummy"))
got, err := os.ReadFile(filepath.Join(dest, "dummy"))
if err != nil {
return err
}
Expand All @@ -273,7 +272,7 @@ func sourceFullySyncedTo(t *testing.T, dest string) error {

{
want := []byte("cheap")
got, err := ioutil.ReadFile(filepath.Join(dest, "cheap", "dummy"))
got, err := os.ReadFile(filepath.Join(dest, "cheap", "dummy"))
if err != nil {
return err
}
Expand Down Expand Up @@ -561,7 +560,7 @@ func TestInteropRemoteDaemonAuthorizedSSHFail(t *testing.T) {
}

authorizedKeysPath := filepath.Join(tmp, "authorized_keys")
if err := ioutil.WriteFile(authorizedKeysPath, []byte("# no keys authorized"), 0644); err != nil {
if err := os.WriteFile(authorizedKeysPath, []byte("# no keys authorized"), 0644); err != nil {
t.Fatal(err)
}

Expand Down Expand Up @@ -611,11 +610,11 @@ func TestInteropRemoteDaemonAuthorizedSSHPass(t *testing.T) {
}

authorizedKeysPath := filepath.Join(tmp, "authorized_keys")
pubKey, err := ioutil.ReadFile(privKeyPath + ".pub")
pubKey, err := os.ReadFile(privKeyPath + ".pub")
if err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(authorizedKeysPath, pubKey, 0644); err != nil {
if err := os.WriteFile(authorizedKeysPath, pubKey, 0644); err != nil {
t.Fatal(err)
}

Expand Down
4 changes: 2 additions & 2 deletions receiver_listing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package rsync_test

import (
"bytes"
"io/ioutil"

"os"
"path/filepath"
"testing"
Expand All @@ -27,7 +27,7 @@ func TestReceiverListing(t *testing.T) {
t.Fatal(err)
}
hello := filepath.Join(source, "hello")
if err := ioutil.WriteFile(hello, []byte("world"), 0644); err != nil {
if err := os.WriteFile(hello, []byte("world"), 0644); err != nil {
t.Fatal(err)
}
mtime, err := time.Parse(time.RFC3339, "2009-11-10T23:00:00Z")
Expand Down
19 changes: 9 additions & 10 deletions receiver_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package rsync_test

import (
"io/ioutil"
"os"
"os/exec"
"os/user"
Expand Down Expand Up @@ -56,7 +55,7 @@ func TestReceiver(t *testing.T) {
t.Fatal(err)
}
hello := filepath.Join(source, "hello")
if err := ioutil.WriteFile(hello, []byte("world"), 0644); err != nil {
if err := os.WriteFile(hello, []byte("world"), 0644); err != nil {
t.Fatal(err)
}
mtime, err := time.Parse(time.RFC3339, "2009-11-10T23:00:00Z")
Expand All @@ -75,7 +74,7 @@ func TestReceiver(t *testing.T) {
}

no := filepath.Join(source, "no")
if err := ioutil.WriteFile(no, []byte("no"), 0666); err != nil {
if err := os.WriteFile(no, []byte("no"), 0666); err != nil {
t.Fatal(err)
}
uid, gid, verifyUid := setUid(t, no)
Expand All @@ -101,7 +100,7 @@ func TestReceiver(t *testing.T) {

{
want := []byte("world")
got, err := ioutil.ReadFile(filepath.Join(dest, "hello"))
got, err := os.ReadFile(filepath.Join(dest, "hello"))
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -146,7 +145,7 @@ func TestReceiver(t *testing.T) {

// Make a change that is invisible with our current settings:
// change the file contents without changing size and mtime.
if err := ioutil.WriteFile(hello, []byte("moon!"), 0644); err != nil {
if err := os.WriteFile(hello, []byte("moon!"), 0644); err != nil {
t.Fatal(err)
}
if err := os.Chtimes(hello, mtime, mtime); err != nil {
Expand All @@ -166,7 +165,7 @@ func TestReceiver(t *testing.T) {

{
want := []byte("world")
got, err := ioutil.ReadFile(filepath.Join(dest, "hello"))
got, err := os.ReadFile(filepath.Join(dest, "hello"))
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -240,7 +239,7 @@ func TestReceiverSSH(t *testing.T) {
if err := os.MkdirAll(source, 0755); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(filepath.Join(source, "hello"), []byte("world"), 0644); err != nil {
if err := os.WriteFile(filepath.Join(source, "hello"), []byte("world"), 0644); err != nil {
t.Fatal(err)
}

Expand Down Expand Up @@ -278,7 +277,7 @@ func TestReceiverSSH(t *testing.T) {

{
want := []byte("world")
got, err := ioutil.ReadFile(filepath.Join(dest, "hello"))
got, err := os.ReadFile(filepath.Join(dest, "hello"))
if err != nil {
t.Fatal(err)
}
Expand All @@ -296,7 +295,7 @@ func TestReceiverCommand(t *testing.T) {
if err := os.MkdirAll(source, 0755); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(filepath.Join(source, "hello"), []byte("world"), 0644); err != nil {
if err := os.WriteFile(filepath.Join(source, "hello"), []byte("world"), 0644); err != nil {
t.Fatal(err)
}

Expand Down Expand Up @@ -331,7 +330,7 @@ func TestReceiverCommand(t *testing.T) {

{
want := []byte("world")
got, err := ioutil.ReadFile(filepath.Join(dest, "hello"))
got, err := os.ReadFile(filepath.Join(dest, "hello"))
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 3a9d1ec

Please sign in to comment.