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

Add optimizations to encoder and decoder #387

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
19 changes: 19 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,25 @@

linters:
enable:
- dupl
- exportloopref
- gci
- goconst
- gocritic
- gofmt
- gofumpt
- perfsprint
- prealloc
- unconvert
- unparam
- usestdlibvars
- wastedassign

linters-settings:
gocritic:
disabled-checks:
- appendAssign
- unlambda
settings:
ifElseChain:
minThreshold: 4
10 changes: 6 additions & 4 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ const (
waitingForReject
)

const rejectedString = "REJECTED"

// Auth defines the behaviour of an authentication mechanism.
type Auth interface {
// Return the name of the mechanism, the argument to the first AUTH command
Expand Down Expand Up @@ -69,7 +71,7 @@ func (conn *Conn) Auth(methods []Auth) error {
if err != nil {
return err
}
if len(s) < 2 || !bytes.Equal(s[0], []byte("REJECTED")) {
if len(s) < 2 || !bytes.Equal(s[0], []byte(rejectedString)) {
return errors.New("dbus: authentication protocol error")
}
s = s[1:]
Expand Down Expand Up @@ -161,7 +163,7 @@ func (conn *Conn) tryAuth(m Auth, state authState, in *bufio.Reader) (bool, erro
return false, err
}
}
case state == waitingForData && string(s[0]) == "REJECTED":
case state == waitingForData && string(s[0]) == rejectedString:
return false, nil
case state == waitingForData && string(s[0]) == "ERROR":
err = authWriteLine(conn.transport, []byte("CANCEL"))
Expand Down Expand Up @@ -201,7 +203,7 @@ func (conn *Conn) tryAuth(m Auth, state authState, in *bufio.Reader) (bool, erro
if err != nil {
return false, nil
}
case state == waitingForOk && string(s[0]) == "REJECTED":
case state == waitingForOk && string(s[0]) == rejectedString:
return false, nil
case state == waitingForOk && string(s[0]) == "ERROR":
err = authWriteLine(conn.transport, []byte("CANCEL"))
Expand All @@ -214,7 +216,7 @@ func (conn *Conn) tryAuth(m Auth, state authState, in *bufio.Reader) (bool, erro
if err != nil {
return false, err
}
case state == waitingForReject && string(s[0]) == "REJECTED":
case state == waitingForReject && string(s[0]) == rejectedString:
return false, nil
case state == waitingForReject:
return false, errors.New("dbus: authentication protocol error")
Expand Down
8 changes: 5 additions & 3 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,15 +400,17 @@ func (conn *Conn) inWorker() {
continue
}
conn.eavesdroppedLck.Lock()
eavesdropped := conn.eavesdropped
conn.eavesdroppedLck.Unlock()

if conn.eavesdropped != nil {
select {
case conn.eavesdropped <- msg:
case eavesdropped <- msg:
default:
}
conn.eavesdroppedLck.Unlock()

continue
}
conn.eavesdroppedLck.Unlock()
dest, _ := msg.Headers[FieldDestination].value.(string)
found := dest == "" ||
!conn.names.uniqueNameIsKnown() ||
Expand Down
3 changes: 1 addition & 2 deletions conn_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package dbus

import (
"bufio"
"io/ioutil"
"os"
"os/exec"
"syscall"
Expand Down Expand Up @@ -46,7 +45,7 @@ func startDaemonInDifferentUserNamespace(t *testing.T) (string, *os.Process) {
</policy>
</busconfig>
`
cfg, err := ioutil.TempFile("", "")
cfg, err := os.CreateTemp("", "")
if err != nil {
t.Fatal(err)
}
Expand Down
8 changes: 3 additions & 5 deletions conn_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ package dbus
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"os/user"
Expand Down Expand Up @@ -54,14 +52,14 @@ func tryDiscoverDbusSessionBusAddress() string {
if runUserBusFile := path.Join(runtimeDirectory, "bus"); fileExists(runUserBusFile) {
// if /run/user/<uid>/bus exists, that file itself
// *is* the unix socket, so return its path
return fmt.Sprintf("unix:path=%s", EscapeBusAddressValue(runUserBusFile))
return "unix:path=" + EscapeBusAddressValue(runUserBusFile)
}
if runUserSessionDbusFile := path.Join(runtimeDirectory, "dbus-session"); fileExists(runUserSessionDbusFile) {
// if /run/user/<uid>/dbus-session exists, it's a
// text file // containing the address of the socket, e.g.:
// DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-E1c73yNqrG

if f, err := ioutil.ReadFile(runUserSessionDbusFile); err == nil {
if f, err := os.ReadFile(runUserSessionDbusFile); err == nil {
fileContent := string(f)

prefix := "DBUS_SESSION_BUS_ADDRESS="
Expand All @@ -80,7 +78,7 @@ func getRuntimeDirectory() (string, error) {
if currentUser, err := user.Current(); err != nil {
return "", err
} else {
return fmt.Sprintf("/run/user/%s", currentUser.Uid), nil
return "/run/user/" + currentUser.Uid, nil
}
}

Expand Down
11 changes: 7 additions & 4 deletions conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/binary"
"fmt"
"io"
"io/ioutil"
"log"
"sync"
"testing"
Expand Down Expand Up @@ -170,20 +169,23 @@ func TestCloseBeforeSignal(t *testing.T) {
reader, pipewriter := io.Pipe()
defer pipewriter.Close()
defer reader.Close()
wg := sync.WaitGroup{}

bus, err := NewConn(rwc{Reader: reader, Writer: ioutil.Discard})
bus, err := NewConn(rwc{Reader: reader, Writer: io.Discard})
if err != nil {
t.Fatal(err)
}
// give ch a buffer so sends won't block
ch := make(chan *Signal, 1)
bus.Signal(ch)

wg.Add(1)
go func() {
_, err := pipewriter.Write([]byte("REJECTED name\r\nOK myuuid\r\n"))
if err != nil {
t.Errorf("error writing to pipe: %v", err)
}
wg.Done()
}()

err = bus.Auth([]Auth{fakeAuth{}})
Expand All @@ -208,6 +210,7 @@ func TestCloseBeforeSignal(t *testing.T) {
if err != nil {
t.Fatal(err)
}
wg.Wait()
}

func TestCloseChannelAfterRemoveSignal(t *testing.T) {
Expand Down Expand Up @@ -743,7 +746,7 @@ func TestDisconnectCancelsConnectionContext(t *testing.T) {
defer pipewriter.Close()
defer reader.Close()

bus, err := NewConn(rwc{Reader: reader, Writer: ioutil.Discard})
bus, err := NewConn(rwc{Reader: reader, Writer: io.Discard})
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -778,7 +781,7 @@ func TestCancellingContextClosesConnection(t *testing.T) {
defer pipewriter.Close()
defer reader.Close()

bus, err := NewConn(rwc{Reader: reader, Writer: ioutil.Discard}, WithContext(ctx))
bus, err := NewConn(rwc{Reader: reader, Writer: io.Discard}, WithContext(ctx))
if err != nil {
t.Fatal(err)
}
Expand Down
Loading
Loading