From 41f96494948d1f9f1e0c3066099e0bd9ad0ee8dd Mon Sep 17 00:00:00 2001 From: Thomas Habets Date: Sun, 8 Jun 2014 03:16:33 +0200 Subject: [PATCH] Cleaned up file handling. --- daemon.go | 42 +++++++++++++++++------------------------- daemon_test.go | 8 ++++---- events.go | 13 +++++-------- goircd.go | 26 +++++++++++--------------- 4 files changed, 37 insertions(+), 52 deletions(-) diff --git a/daemon.go b/daemon.go index 1f142c2..1f979ce 100644 --- a/daemon.go +++ b/daemon.go @@ -18,11 +18,9 @@ along with this program. If not, see . package main import ( - "bytes" "fmt" - "io" + "io/ioutil" "log" - "os" "regexp" "sort" "strings" @@ -72,29 +70,23 @@ func (daemon *Daemon) SendLusers(client *Client) { } func (daemon *Daemon) SendMotd(client *Client) { - if daemon.motd != "" { - fd, err := os.Open(daemon.motd) - if err == nil { - defer fd.Close() - motd := []byte{} - var err error - for err != io.EOF { - buf := make([]byte, 1024) - _, err = fd.Read(buf) - motd = append(motd, bytes.TrimRight(buf, "\x00")...) - } - - client.ReplyNicknamed("375", "- "+daemon.hostname+" Message of the day -") - for _, s := range bytes.Split(bytes.TrimRight(motd, "\n"), []byte("\n")) { - client.ReplyNicknamed("372", "- "+string(s)) - } - client.ReplyNicknamed("376", "End of /MOTD command") - return - } else { - log.Println("Can not open motd file", daemon.motd, err) - } + if len(daemon.motd) == 0 { + client.ReplyNicknamed("422", "MOTD File is missing") + return } - client.ReplyNicknamed("422", "MOTD File is missing") + + motd, err := ioutil.ReadFile(daemon.motd) + if err != nil { + log.Printf("Can not read motd file %s: %v", daemon.motd, err) + client.ReplyNicknamed("422", "Error reading MOTD File") + return + } + + client.ReplyNicknamed("375", "- "+daemon.hostname+" Message of the day -") + for _, s := range strings.Split(strings.Trim(string(motd), "\n"), "\n") { + client.ReplyNicknamed("372", "- "+string(s)) + } + client.ReplyNicknamed("376", "End of /MOTD command") } func (daemon *Daemon) SendWhois(client *Client, nicknames []string) { diff --git a/daemon_test.go b/daemon_test.go index 1c86f35..9c9db3d 100644 --- a/daemon_test.go +++ b/daemon_test.go @@ -113,10 +113,10 @@ func TestRegistrationWorkflow(t *testing.T) { func TestMotd(t *testing.T) { fd, err := ioutil.TempFile("", "motd") if err != nil { - t.Fatal("can not create temporary file") + t.Fatalf("can not create temporary file: %v", err) } defer os.Remove(fd.Name()) - fd.Write([]byte("catched\n")) + fd.WriteString("catched\n") conn := NewTestingConn() client := NewClient("foohost", conn) @@ -129,7 +129,7 @@ func TestMotd(t *testing.T) { if r := <-conn.outbound; !strings.Contains(r, "372 * :- catched\r\n") { t.Fatal("MOTD contents", r) } - if r := <-conn.outbound; !strings.HasPrefix(r, ":foohost 376") { - t.Fatal("MOTD end", r) + if got, want := <-conn.outbound, ":foohost 376"; !strings.HasPrefix(got, want) { + t.Fatalf("MOTD end: got %q, want prefix %q", got, want) } } diff --git a/events.go b/events.go index 95760e8..0921069 100644 --- a/events.go +++ b/events.go @@ -19,6 +19,7 @@ package main import ( "fmt" + "io/ioutil" "log" "os" "path" @@ -94,16 +95,12 @@ type StateEvent struct { // Room states shows that either topic or key has been changed // Each room's state is written to separate file in statedir func StateKeeper(statedir string, events <-chan StateEvent) { - mode := os.O_CREATE | os.O_TRUNC | os.O_WRONLY - perm := os.FileMode(0660) for event := range events { - state_path := path.Join(statedir, event.where) - fd, err := os.OpenFile(state_path, mode, perm) + fn := path.Join(statedir, event.where) + data := event.topic + "\n" + event.key + "\n" + err := ioutil.WriteFile(fn, []byte(data), os.FileMode(0660)) if err != nil { - log.Println("Can not open statefile", state_path, err) - continue + log.Printf("Can not write statefile %s: %v", fn, err) } - fd.WriteString(event.topic + "\n" + event.key + "\n") - fd.Close() } } diff --git a/goircd.go b/goircd.go index e176804..9fdabc0 100644 --- a/goircd.go +++ b/goircd.go @@ -18,12 +18,11 @@ along with this program. If not, see . package main import ( - "bytes" "crypto/tls" "flag" + "io/ioutil" "log" "net" - "os" "path" "path/filepath" "strings" @@ -77,27 +76,24 @@ func Run() { if !path.IsAbs(*statedir) { log.Fatalln("Need absolute path for statedir") } - states, err := filepath.Glob(*statedir + "/#*") + states, err := filepath.Glob(path.Join(*statedir, "#*")) if err != nil { log.Fatalln("Can not read statedir", err) } for _, state := range states { - fd, err := os.Open(state) + buf, err := ioutil.ReadFile(state) if err != nil { - log.Fatalln("Can not open state", state, err) - } - buf := make([]byte, 1024) - _, err = fd.Read(buf) - fd.Close() - if err != nil { - log.Fatalln("Can not read state", state, err) + log.Fatalf("Can not read state %s: %v", state, err) } room, _ := daemon.RoomRegister(path.Base(state)) - buf = bytes.TrimRight(buf, "\x00") contents := strings.Split(string(buf), "\n") - room.topic = contents[0] - room.key = contents[1] - log.Println("Loaded state for room", room.name) + if len(contents) < 2 { + log.Printf("State corrupted for %s: %q", room.name, contents) + } else { + room.topic = contents[0] + room.key = contents[1] + log.Println("Loaded state for room", room.name) + } } go StateKeeper(*statedir, state_sink) log.Println(*statedir, "statekeeper initialized")