Skip to content

Commit

Permalink
Add missing error handlings
Browse files Browse the repository at this point in the history
  • Loading branch information
hnakamur committed Aug 4, 2019
1 parent 98b4899 commit b0678c6
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions tail.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ func Open(file string, posfile string) (*Tail, error) {
return &t, err
}
posStat := Stat{}
yaml.Unmarshal(posdata, &posStat)
err = yaml.Unmarshal(posdata, &posStat)
if err != nil {
return &t, err
}

// open tail file.
t.fileFd, err = os.Open(t.file)
Expand Down Expand Up @@ -92,7 +95,10 @@ func Open(file string, posfile string) (*Tail, error) {
}

// tail seek posititon.
t.fileFd.Seek(t.Stat.Offset, os.SEEK_SET)
_, err = t.fileFd.Seek(t.Stat.Offset, os.SEEK_SET)
if err != nil {
return &t, err
}

return &t, nil
}
Expand Down Expand Up @@ -125,12 +131,15 @@ func posUpdate(t *Tail) error {
return err
}

t.posFd.Write(yml)
_, err = t.posFd.Write(yml)
if err != nil {
return err
}

t.posFd.Sync()
err = t.posFd.Sync()
if err != nil {
return err
}
return nil
}

Expand Down Expand Up @@ -199,15 +208,19 @@ func (t *Tail) Scan() bool {
t.Stat.Inode = stat.Ino
t.Stat.Offset = 0
t.Stat.Size = stat.Size
t.fileFd.Close()
_ = t.fileFd.Close()
t.fileFd = fd
} else {
if stat.Size < t.Stat.Size {
t.fileFd.Seek(0, os.SEEK_SET)
_, err = t.fileFd.Seek(0, os.SEEK_SET)
if err != nil {
t.err = err
return false
}
}
t.Stat.Size = stat.Size
time.Sleep(time.Millisecond * 10)
fd.Close()
_ = fd.Close()
}
t.scanner = bufio.NewScanner(t.fileFd)

Expand Down

0 comments on commit b0678c6

Please sign in to comment.