...
1 package rifs
2
3 import (
4 "io"
5 "os"
6
7 "github.com/dsoprea/go-logging"
8 )
9
10
11
12 type SeekType int
13
14
15 func (n SeekType) String() string {
16 if n == io.SeekCurrent {
17 return "SEEK-CURRENT"
18 } else if n == io.SeekEnd {
19 return "SEEK-END"
20 } else if n == io.SeekStart {
21 return "SEEK-START"
22 }
23
24 log.Panicf("unknown seek-type: (%d)", n)
25 return ""
26 }
27
28
29 func CalculateSeek(currentOffset int64, delta int64, whence int, fileSize int64) (finalOffset int64, err error) {
30 defer func() {
31 if state := recover(); state != nil {
32 err = log.Wrap(state.(error))
33 finalOffset = 0
34 }
35 }()
36
37 if whence == os.SEEK_SET {
38 finalOffset = delta
39 } else if whence == os.SEEK_CUR {
40 finalOffset = currentOffset + delta
41 } else if whence == os.SEEK_END {
42 finalOffset = fileSize + delta
43 } else {
44 log.Panicf("whence not valid: (%d)", whence)
45 }
46
47 if finalOffset < 0 {
48 finalOffset = 0
49 }
50
51 return finalOffset, nil
52 }
53
View as plain text