Calculate offset for ReadAt() instead of using Seek().
Fixed going out of bounds.
This commit is contained in:
parent
52e5f1be55
commit
7e83be82cb
@ -119,7 +119,8 @@ func IcecastLastSong(playlistPath string) (Song, error) {
|
|||||||
|
|
||||||
func icecastLastPlayedSongs(playlistPath string, n int) ([]Song, error) {
|
func icecastLastPlayedSongs(playlistPath string, n int) ([]Song, error) {
|
||||||
songs := make([]Song, n)
|
songs := make([]Song, n)
|
||||||
buf := make([]byte, bufferSize)
|
var buf []byte
|
||||||
|
var offset int64 = 0
|
||||||
|
|
||||||
playlist, err := os.Open(playlistPath)
|
playlist, err := os.Open(playlistPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -133,24 +134,29 @@ func icecastLastPlayedSongs(playlistPath string, n int) ([]Song, error) {
|
|||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
playlist.Seek(-bufferSize, os.SEEK_END)
|
if playlist_stat.Size() < bufferSize {
|
||||||
|
buf = make([]byte, playlist_stat.Size())
|
||||||
|
} else {
|
||||||
|
buf = make([]byte, bufferSize)
|
||||||
|
offset = playlist_stat.Size() - bufferSize
|
||||||
|
}
|
||||||
|
|
||||||
_, err = playlist.Read(buf)
|
_, err = playlist.ReadAt(buf, offset)
|
||||||
if err != nil && err != io.EOF {
|
if err != nil && err != io.EOF {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
lines := bytes.Split(buf, []byte("\n"))
|
lines := bytes.Split(buf, []byte("\n"))
|
||||||
|
|
||||||
if len(lines) < 2 {
|
if len(lines) < 3 {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(lines) < n {
|
lines = lines[:len(lines)-2]
|
||||||
n = len(lines)
|
|
||||||
}
|
|
||||||
|
|
||||||
lines = lines[len(lines)-n-2 : len(lines)-2]
|
if len(lines) > n {
|
||||||
|
lines = lines[len(lines)-n:]
|
||||||
|
}
|
||||||
|
|
||||||
for _, line := range lines {
|
for _, line := range lines {
|
||||||
fields := bytes.Split(line, []byte("|"))
|
fields := bytes.Split(line, []byte("|"))
|
||||||
|
Loading…
Reference in New Issue
Block a user