mirror of
https://github.com/yusing/godoxy.git
synced 2025-05-19 20:32:35 +02:00
fix(access_log): file size calculation
This commit is contained in:
parent
978d82060e
commit
1d22bcfed9
3 changed files with 19 additions and 14 deletions
|
@ -26,12 +26,8 @@ type BackScanner struct {
|
||||||
|
|
||||||
// NewBackScanner creates a new Scanner to read the file backward.
|
// NewBackScanner creates a new Scanner to read the file backward.
|
||||||
// chunkSize determines the size of each read chunk from the end of the file.
|
// chunkSize determines the size of each read chunk from the end of the file.
|
||||||
func NewBackScanner(file ReaderAtSeeker, chunkSize int) *BackScanner {
|
func NewBackScanner(file ReaderAtSeeker, fileSize int64, chunkSize int) *BackScanner {
|
||||||
size, err := file.Seek(0, io.SeekEnd)
|
return newBackScanner(file, fileSize, make([]byte, chunkSize))
|
||||||
if err != nil {
|
|
||||||
return &BackScanner{err: err}
|
|
||||||
}
|
|
||||||
return newBackScanner(file, size, make([]byte, chunkSize))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func newBackScanner(file ReaderAtSeeker, fileSize int64, buf []byte) *BackScanner {
|
func newBackScanner(file ReaderAtSeeker, fileSize int64, buf []byte) *BackScanner {
|
||||||
|
@ -111,11 +107,6 @@ func (s *BackScanner) Bytes() []byte {
|
||||||
return s.line
|
return s.line
|
||||||
}
|
}
|
||||||
|
|
||||||
// FileSize returns the size of the file.
|
|
||||||
func (s *BackScanner) FileSize() int64 {
|
|
||||||
return s.size
|
|
||||||
}
|
|
||||||
|
|
||||||
// Err returns the first non-EOF error encountered by the scanner.
|
// Err returns the first non-EOF error encountered by the scanner.
|
||||||
func (s *BackScanner) Err() error {
|
func (s *BackScanner) Err() error {
|
||||||
return s.err
|
return s.err
|
||||||
|
|
|
@ -72,6 +72,14 @@ func (f *File) Seek(offset int64, whence int) (int64, error) {
|
||||||
return f.f.Seek(offset, whence)
|
return f.f.Seek(offset, whence)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (f *File) Size() (int64, error) {
|
||||||
|
stat, err := f.f.Stat()
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return stat.Size(), nil
|
||||||
|
}
|
||||||
|
|
||||||
func (f *File) Truncate(size int64) error {
|
func (f *File) Truncate(size int64) error {
|
||||||
return f.f.Truncate(size)
|
return f.f.Truncate(size)
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,6 +16,7 @@ type supportRotate interface {
|
||||||
io.ReaderAt
|
io.ReaderAt
|
||||||
io.WriterAt
|
io.WriterAt
|
||||||
Truncate(size int64) error
|
Truncate(size int64) error
|
||||||
|
Size() (int64, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type RotateResult struct {
|
type RotateResult struct {
|
||||||
|
@ -96,9 +97,14 @@ func rotateLogFileByPolicy(file supportRotate, config *Retention) (result *Rotat
|
||||||
return nil, nil // should not happen
|
return nil, nil // should not happen
|
||||||
}
|
}
|
||||||
|
|
||||||
s := NewBackScanner(file, defaultChunkSize)
|
fileSize, err := file.Size()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
s := NewBackScanner(file, fileSize, defaultChunkSize)
|
||||||
result = &RotateResult{
|
result = &RotateResult{
|
||||||
OriginalSize: s.FileSize(),
|
OriginalSize: fileSize,
|
||||||
}
|
}
|
||||||
|
|
||||||
// nothing to rotate, return the nothing
|
// nothing to rotate, return the nothing
|
||||||
|
@ -201,7 +207,7 @@ func rotateLogFileByPolicy(file supportRotate, config *Retention) (result *Rotat
|
||||||
//
|
//
|
||||||
// Invalid lines will not be detected and included in the result.
|
// Invalid lines will not be detected and included in the result.
|
||||||
func rotateLogFileBySize(file supportRotate, config *Retention) (result *RotateResult, err error) {
|
func rotateLogFileBySize(file supportRotate, config *Retention) (result *RotateResult, err error) {
|
||||||
filesize, err := file.Seek(0, io.SeekEnd)
|
filesize, err := file.Size()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue